xref: /AOO41X/main/ucb/source/ucp/webdav/webdavprovider.cxx (revision 3edf699282957a8d2e0e344ed73ba96dfb5a45a4)
12f86921cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32f86921cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42f86921cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52f86921cSAndrew Rist  * distributed with this work for additional information
62f86921cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72f86921cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
82f86921cSAndrew Rist  * "License"); you may not use this file except in compliance
92f86921cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
112f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
132f86921cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
142f86921cSAndrew Rist  * software distributed under the License is distributed on an
152f86921cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162f86921cSAndrew Rist  * KIND, either express or implied.  See the License for the
172f86921cSAndrew Rist  * specific language governing permissions and limitations
182f86921cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
202f86921cSAndrew Rist  *************************************************************/
212f86921cSAndrew Rist 
222f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucb.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /**************************************************************************
28cdf0e10cSrcweir 								TODO
29cdf0e10cSrcweir  **************************************************************************
30cdf0e10cSrcweir 
31cdf0e10cSrcweir  *************************************************************************/
32cdf0e10cSrcweir #include <ucbhelper/contentidentifier.hxx>
33cdf0e10cSrcweir #include "webdavprovider.hxx"
34cdf0e10cSrcweir #include "webdavcontent.hxx"
356601ee31SAriel Constenla-Haile #include "webdavuseragent.hxx"
36cdf0e10cSrcweir 
37c1c10f68SAriel Constenla-Haile #include <osl/mutex.hxx>
386601ee31SAriel Constenla-Haile #include <rtl/ustrbuf.hxx>
396601ee31SAriel Constenla-Haile #include <comphelper/processfactory.hxx>
406601ee31SAriel Constenla-Haile #include <com/sun/star/beans/NamedValue.hpp>
416601ee31SAriel Constenla-Haile #include <com/sun/star/container/XNameAccess.hpp>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using namespace com::sun::star;
4459ddfc10SAndre Fischer using namespace http_dav_ucp;
45cdf0e10cSrcweir 
466601ee31SAriel Constenla-Haile 
operator ()() const476601ee31SAriel Constenla-Haile rtl::OUString &WebDAVUserAgent::operator()() const
486601ee31SAriel Constenla-Haile {
496601ee31SAriel Constenla-Haile     rtl::OUStringBuffer aBuffer;
50*3edf6992SAndrea Pescetti     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "Apache " ));
516601ee31SAriel Constenla-Haile     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "$ooName/$ooSetupVersion" ));
526601ee31SAriel Constenla-Haile #if OSL_DEBUG_LEVEL > 0
536601ee31SAriel Constenla-Haile #ifdef APR_VERSION
546601ee31SAriel Constenla-Haile     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " apr/" APR_VERSION ));
556601ee31SAriel Constenla-Haile #endif
566601ee31SAriel Constenla-Haile 
576601ee31SAriel Constenla-Haile #ifdef APR_UTIL_VERSION
586601ee31SAriel Constenla-Haile     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " apr-util/" APR_UTIL_VERSION ));
596601ee31SAriel Constenla-Haile #endif
606601ee31SAriel Constenla-Haile 
616601ee31SAriel Constenla-Haile #ifdef SERF_VERSION
626601ee31SAriel Constenla-Haile     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " serf/" SERF_VERSION ));
636601ee31SAriel Constenla-Haile #endif
646601ee31SAriel Constenla-Haile #endif
656601ee31SAriel Constenla-Haile     static rtl::OUString aUserAgent( aBuffer.makeStringAndClear() );
666601ee31SAriel Constenla-Haile     return aUserAgent;
676601ee31SAriel Constenla-Haile }
686601ee31SAriel Constenla-Haile 
69cdf0e10cSrcweir //=========================================================================
70cdf0e10cSrcweir //=========================================================================
71cdf0e10cSrcweir //
72cdf0e10cSrcweir // ContentProvider Implementation.
73cdf0e10cSrcweir //
74cdf0e10cSrcweir //=========================================================================
75cdf0e10cSrcweir //=========================================================================
76cdf0e10cSrcweir 
ContentProvider(const uno::Reference<lang::XMultiServiceFactory> & rSMgr)77cdf0e10cSrcweir ContentProvider::ContentProvider(
78cdf0e10cSrcweir                 const uno::Reference< lang::XMultiServiceFactory >& rSMgr )
79cdf0e10cSrcweir : ::ucbhelper::ContentProviderImplHelper( rSMgr ),
80cdf0e10cSrcweir   m_xDAVSessionFactory( new DAVSessionFactory() ),
81cdf0e10cSrcweir   m_pProps( 0 )
82cdf0e10cSrcweir {
836601ee31SAriel Constenla-Haile     static bool bInit = false;
846601ee31SAriel Constenla-Haile     if ( bInit )
856601ee31SAriel Constenla-Haile         return;
866601ee31SAriel Constenla-Haile     bInit = true;
876601ee31SAriel Constenla-Haile     try
886601ee31SAriel Constenla-Haile     {
896601ee31SAriel Constenla-Haile         uno::Reference< uno::XComponentContext > xContext(
906601ee31SAriel Constenla-Haile             ::comphelper::getProcessComponentContext() );
916601ee31SAriel Constenla-Haile         uno::Reference< lang::XMultiServiceFactory > xConfigProvider(
926601ee31SAriel Constenla-Haile             xContext->getServiceManager()->createInstanceWithContext(
936601ee31SAriel Constenla-Haile                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
946601ee31SAriel Constenla-Haile                                   "com.sun.star.configuration.ConfigurationProvider")), xContext),
956601ee31SAriel Constenla-Haile             uno::UNO_QUERY_THROW );
966601ee31SAriel Constenla-Haile 
976601ee31SAriel Constenla-Haile         beans::NamedValue aNodePath;
986601ee31SAriel Constenla-Haile         aNodePath.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
996601ee31SAriel Constenla-Haile         aNodePath.Value <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Product"));
1006601ee31SAriel Constenla-Haile 
1016601ee31SAriel Constenla-Haile         uno::Sequence< uno::Any > aArgs( 1 );
1026601ee31SAriel Constenla-Haile         aArgs[0] <<= aNodePath;
1036601ee31SAriel Constenla-Haile 
1046601ee31SAriel Constenla-Haile         uno::Reference< container::XNameAccess > xConfigAccess(
1056601ee31SAriel Constenla-Haile             xConfigProvider->createInstanceWithArguments(
1066601ee31SAriel Constenla-Haile                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
1076601ee31SAriel Constenla-Haile                     "com.sun.star.configuration.ConfigurationAccess")), aArgs),
1086601ee31SAriel Constenla-Haile             uno::UNO_QUERY_THROW );
1096601ee31SAriel Constenla-Haile 
1106601ee31SAriel Constenla-Haile         rtl::OUString aVal;
1116601ee31SAriel Constenla-Haile         xConfigAccess->getByName(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ooName"))) >>= aVal;
1126601ee31SAriel Constenla-Haile 
1136601ee31SAriel Constenla-Haile         rtl::OUString &aUserAgent = WebDAVUserAgent::get();
1146601ee31SAriel Constenla-Haile         sal_Int32 nIndex = aUserAgent.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "$ooName" ) );
1156601ee31SAriel Constenla-Haile         if ( !aVal.getLength() || nIndex == -1 )
1166601ee31SAriel Constenla-Haile             return;
1176601ee31SAriel Constenla-Haile         aUserAgent = aUserAgent.replaceAt( nIndex, RTL_CONSTASCII_LENGTH( "$ooName" ), aVal );
1186601ee31SAriel Constenla-Haile 
1196601ee31SAriel Constenla-Haile         xConfigAccess->getByName(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ooSetupVersion"))) >>= aVal;
1206601ee31SAriel Constenla-Haile         nIndex = aUserAgent.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "$ooSetupVersion" ) );
1216601ee31SAriel Constenla-Haile         if ( !aVal.getLength() || nIndex == -1 )
1226601ee31SAriel Constenla-Haile             return;
1236601ee31SAriel Constenla-Haile         aUserAgent = aUserAgent.replaceAt( nIndex, RTL_CONSTASCII_LENGTH( "$ooSetupVersion" ), aVal );
1246601ee31SAriel Constenla-Haile 
1256601ee31SAriel Constenla-Haile     }
1266601ee31SAriel Constenla-Haile     catch ( const uno::Exception &e )
1276601ee31SAriel Constenla-Haile     {
1286601ee31SAriel Constenla-Haile         OSL_TRACE( "ContentProvider -caught exception! %s",
1296601ee31SAriel Constenla-Haile                    rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
1306601ee31SAriel Constenla-Haile         (void) e;
1316601ee31SAriel Constenla-Haile     }
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //=========================================================================
135cdf0e10cSrcweir // virtual
~ContentProvider()136cdf0e10cSrcweir ContentProvider::~ContentProvider()
137cdf0e10cSrcweir {
138cdf0e10cSrcweir 	delete m_pProps;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir //=========================================================================
142cdf0e10cSrcweir //
143cdf0e10cSrcweir // XInterface methods.
144cdf0e10cSrcweir //
145cdf0e10cSrcweir //=========================================================================
146cdf0e10cSrcweir 
147cdf0e10cSrcweir XINTERFACE_IMPL_3( ContentProvider,
148cdf0e10cSrcweir                    lang::XTypeProvider,
149cdf0e10cSrcweir                    lang::XServiceInfo,
150cdf0e10cSrcweir                    ucb::XContentProvider );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir //=========================================================================
153cdf0e10cSrcweir //
154cdf0e10cSrcweir // XTypeProvider methods.
155cdf0e10cSrcweir //
156cdf0e10cSrcweir //=========================================================================
157cdf0e10cSrcweir 
158cdf0e10cSrcweir XTYPEPROVIDER_IMPL_3( ContentProvider,
159cdf0e10cSrcweir                       lang::XTypeProvider,
160cdf0e10cSrcweir                       lang::XServiceInfo,
161cdf0e10cSrcweir                       ucb::XContentProvider );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir //=========================================================================
164cdf0e10cSrcweir //
165cdf0e10cSrcweir // XServiceInfo methods.
166cdf0e10cSrcweir //
167cdf0e10cSrcweir //=========================================================================
168cdf0e10cSrcweir 
169cdf0e10cSrcweir XSERVICEINFO_IMPL_1( ContentProvider,
170cdf0e10cSrcweir                      rtl::OUString::createFromAscii(
171cdf0e10cSrcweir 			 			"com.sun.star.comp.WebDAVContentProvider" ),
172cdf0e10cSrcweir                      rtl::OUString::createFromAscii(
173cdf0e10cSrcweir 				 		WEBDAV_CONTENT_PROVIDER_SERVICE_NAME ) );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir //=========================================================================
176cdf0e10cSrcweir //
177cdf0e10cSrcweir // Service factory implementation.
178cdf0e10cSrcweir //
179cdf0e10cSrcweir //=========================================================================
180cdf0e10cSrcweir 
181cdf0e10cSrcweir ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir //=========================================================================
184cdf0e10cSrcweir //
185cdf0e10cSrcweir // XContentProvider methods.
186cdf0e10cSrcweir //
187cdf0e10cSrcweir //=========================================================================
188cdf0e10cSrcweir 
189cdf0e10cSrcweir // virtual
190cdf0e10cSrcweir uno::Reference< ucb::XContent > SAL_CALL
queryContent(const uno::Reference<ucb::XContentIdentifier> & Identifier)191cdf0e10cSrcweir ContentProvider::queryContent(
192cdf0e10cSrcweir             const uno::Reference<
193cdf0e10cSrcweir                     ucb::XContentIdentifier >& Identifier )
194cdf0e10cSrcweir     throw( ucb::IllegalIdentifierException,
195cdf0e10cSrcweir            uno::RuntimeException )
196cdf0e10cSrcweir {
197cdf0e10cSrcweir 	// Check URL scheme...
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     const rtl::OUString aScheme
200cdf0e10cSrcweir         = Identifier->getContentProviderScheme().toAsciiLowerCase();
201cdf0e10cSrcweir     if ( !aScheme.equalsAsciiL(
202cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( HTTP_URL_SCHEME ) ) &&
203cdf0e10cSrcweir          !aScheme.equalsAsciiL(
204cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( HTTPS_URL_SCHEME ) ) &&
205cdf0e10cSrcweir          !aScheme.equalsAsciiL(
206cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( WEBDAV_URL_SCHEME ) ) &&
207cdf0e10cSrcweir          !aScheme.equalsAsciiL(
208cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( DAV_URL_SCHEME ) ) &&
209cdf0e10cSrcweir          !aScheme.equalsAsciiL(
21059ddfc10SAndre Fischer             RTL_CONSTASCII_STRINGPARAM( DAVS_URL_SCHEME ) ) )
211cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 	// Normalize URL and create new Id, if nessacary.
214cdf0e10cSrcweir     rtl::OUString aURL = Identifier->getContentIdentifier();
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     // At least: <scheme> + "://"
217cdf0e10cSrcweir     if ( aURL.getLength() < ( aScheme.getLength() + 3 ) )
218cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     if ( ( aURL.getStr()[ aScheme.getLength() ]     != sal_Unicode( ':' ) ) ||
221cdf0e10cSrcweir          ( aURL.getStr()[ aScheme.getLength() + 1 ] != sal_Unicode( '/' ) ) ||
222cdf0e10cSrcweir          ( aURL.getStr()[ aScheme.getLength() + 2 ] != sal_Unicode( '/' ) ) )
223cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     uno::Reference< ucb::XContentIdentifier > xCanonicId;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     bool bNewId = false;
228cdf0e10cSrcweir     if ( aScheme.equalsAsciiL(
229cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( WEBDAV_URL_SCHEME ) ) )
230cdf0e10cSrcweir     {
231cdf0e10cSrcweir         aURL = aURL.replaceAt( 0,
232cdf0e10cSrcweir                                WEBDAV_URL_SCHEME_LENGTH,
233cdf0e10cSrcweir                                rtl::OUString::createFromAscii(
234cdf0e10cSrcweir                                                     HTTP_URL_SCHEME ) );
235cdf0e10cSrcweir         bNewId = true;
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir     else if ( aScheme.equalsAsciiL(
238cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( DAV_URL_SCHEME ) ) )
239cdf0e10cSrcweir     {
240cdf0e10cSrcweir         aURL = aURL.replaceAt( 0,
241cdf0e10cSrcweir                                DAV_URL_SCHEME_LENGTH,
242cdf0e10cSrcweir                                rtl::OUString::createFromAscii(
243cdf0e10cSrcweir                                                     HTTP_URL_SCHEME ) );
244cdf0e10cSrcweir         bNewId = true;
245cdf0e10cSrcweir     }
246cdf0e10cSrcweir     else if ( aScheme.equalsAsciiL(
247cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( DAVS_URL_SCHEME ) ) )
248cdf0e10cSrcweir     {
249cdf0e10cSrcweir         aURL = aURL.replaceAt( 0,
250cdf0e10cSrcweir                                DAVS_URL_SCHEME_LENGTH,
251cdf0e10cSrcweir                                rtl::OUString::createFromAscii(
252cdf0e10cSrcweir                                                     HTTPS_URL_SCHEME ) );
253cdf0e10cSrcweir         bNewId = true;
254cdf0e10cSrcweir     }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     sal_Int32 nPos = aURL.lastIndexOf( '/' );
257cdf0e10cSrcweir 	if ( nPos != aURL.getLength() - 1 )
258cdf0e10cSrcweir 	{
259cdf0e10cSrcweir 		// Find second slash in URL.
260cdf0e10cSrcweir 		nPos = aURL.indexOf( '/', aURL.indexOf( '/' ) + 1 );
261cdf0e10cSrcweir 		if ( nPos == -1 )
262cdf0e10cSrcweir             throw ucb::IllegalIdentifierException();
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 		nPos = aURL.indexOf( '/', nPos + 1 );
265cdf0e10cSrcweir 		if ( nPos == -1 )
266cdf0e10cSrcweir 		{
267cdf0e10cSrcweir             aURL += rtl::OUString::createFromAscii( "/" );
268cdf0e10cSrcweir             bNewId = true;
269cdf0e10cSrcweir 		}
270cdf0e10cSrcweir 	}
271cdf0e10cSrcweir 
272cdf0e10cSrcweir     if ( bNewId )
273cdf0e10cSrcweir         xCanonicId = new ::ucbhelper::ContentIdentifier( m_xSMgr, aURL );
274cdf0e10cSrcweir     else
275cdf0e10cSrcweir         xCanonicId = Identifier;
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 	// Check, if a content with given id already exists...
280cdf0e10cSrcweir     uno::Reference< ucb::XContent > xContent
281cdf0e10cSrcweir 		= queryExistingContent( xCanonicId ).get();
282cdf0e10cSrcweir 	if ( xContent.is() )
283cdf0e10cSrcweir 		return xContent;
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 	// Create a new content.
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 	try
288cdf0e10cSrcweir 	{
28959ddfc10SAndre Fischer         xContent = new ::http_dav_ucp::Content(
290cdf0e10cSrcweir                         m_xSMgr, this, xCanonicId, m_xDAVSessionFactory );
291cdf0e10cSrcweir         registerNewContent( xContent );
292cdf0e10cSrcweir 	}
293cdf0e10cSrcweir     catch ( ucb::ContentCreationException const & )
294cdf0e10cSrcweir 	{
295cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
296cdf0e10cSrcweir 	}
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 	if ( !xContent->getIdentifier().is() )
299cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 	return xContent;
302cdf0e10cSrcweir }
303cdf0e10cSrcweir 
304