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
102f86921cSAndrew Rist  *
112f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122f86921cSAndrew Rist  *
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.
192f86921cSAndrew Rist  *
202f86921cSAndrew Rist  *************************************************************/
212f86921cSAndrew Rist 
222f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25*421ed02eSdamjan #include "precompiled_webdav.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>
42*421ed02eSdamjan #include <apr_version.h>
43*421ed02eSdamjan #include <apu_version.h>
44*421ed02eSdamjan #include <serf.h>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace com::sun::star;
4759ddfc10SAndre Fischer using namespace http_dav_ucp;
48cdf0e10cSrcweir 
496601ee31SAriel Constenla-Haile 
506601ee31SAriel Constenla-Haile rtl::OUString &WebDAVUserAgent::operator()() const
516601ee31SAriel Constenla-Haile {
526601ee31SAriel Constenla-Haile     rtl::OUStringBuffer aBuffer;
539c0c1533SAndrea Pescetti     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "Apache " ));
546601ee31SAriel Constenla-Haile     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "$ooName/$ooSetupVersion" ));
556601ee31SAriel Constenla-Haile #if OSL_DEBUG_LEVEL > 0
56*421ed02eSdamjan     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " apr/" ) );
57*421ed02eSdamjan     aBuffer.appendAscii(apr_version_string());
586601ee31SAriel Constenla-Haile 
59*421ed02eSdamjan     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " apr-util/" ) );
60*421ed02eSdamjan     aBuffer.appendAscii(apu_version_string());
616601ee31SAriel Constenla-Haile 
62*421ed02eSdamjan     int major, minor, patch;
63*421ed02eSdamjan     serf_lib_version(&major, &minor, &patch);
64*421ed02eSdamjan     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " serf/" ) );
65*421ed02eSdamjan     aBuffer.append(major);
66*421ed02eSdamjan     aBuffer.append( L'.' );
67*421ed02eSdamjan     aBuffer.append(minor);
68*421ed02eSdamjan     aBuffer.append( L'.' );
69*421ed02eSdamjan     aBuffer.append(patch);
706601ee31SAriel Constenla-Haile #endif
716601ee31SAriel Constenla-Haile     static rtl::OUString aUserAgent( aBuffer.makeStringAndClear() );
726601ee31SAriel Constenla-Haile     return aUserAgent;
736601ee31SAriel Constenla-Haile }
746601ee31SAriel Constenla-Haile 
75cdf0e10cSrcweir //=========================================================================
76cdf0e10cSrcweir //=========================================================================
77cdf0e10cSrcweir //
78cdf0e10cSrcweir // ContentProvider Implementation.
79cdf0e10cSrcweir //
80cdf0e10cSrcweir //=========================================================================
81cdf0e10cSrcweir //=========================================================================
82cdf0e10cSrcweir 
83cdf0e10cSrcweir ContentProvider::ContentProvider(
84cdf0e10cSrcweir                 const uno::Reference< lang::XMultiServiceFactory >& rSMgr )
85cdf0e10cSrcweir : ::ucbhelper::ContentProviderImplHelper( rSMgr ),
86cdf0e10cSrcweir   m_xDAVSessionFactory( new DAVSessionFactory() ),
87cdf0e10cSrcweir   m_pProps( 0 )
88cdf0e10cSrcweir {
896601ee31SAriel Constenla-Haile     static bool bInit = false;
906601ee31SAriel Constenla-Haile     if ( bInit )
916601ee31SAriel Constenla-Haile         return;
926601ee31SAriel Constenla-Haile     bInit = true;
936601ee31SAriel Constenla-Haile     try
946601ee31SAriel Constenla-Haile     {
956601ee31SAriel Constenla-Haile         uno::Reference< uno::XComponentContext > xContext(
966601ee31SAriel Constenla-Haile             ::comphelper::getProcessComponentContext() );
976601ee31SAriel Constenla-Haile         uno::Reference< lang::XMultiServiceFactory > xConfigProvider(
986601ee31SAriel Constenla-Haile             xContext->getServiceManager()->createInstanceWithContext(
996601ee31SAriel Constenla-Haile                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
1006601ee31SAriel Constenla-Haile                                   "com.sun.star.configuration.ConfigurationProvider")), xContext),
1016601ee31SAriel Constenla-Haile             uno::UNO_QUERY_THROW );
1026601ee31SAriel Constenla-Haile 
1036601ee31SAriel Constenla-Haile         beans::NamedValue aNodePath;
1046601ee31SAriel Constenla-Haile         aNodePath.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
1056601ee31SAriel Constenla-Haile         aNodePath.Value <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Product"));
1066601ee31SAriel Constenla-Haile 
1076601ee31SAriel Constenla-Haile         uno::Sequence< uno::Any > aArgs( 1 );
1086601ee31SAriel Constenla-Haile         aArgs[0] <<= aNodePath;
1096601ee31SAriel Constenla-Haile 
1106601ee31SAriel Constenla-Haile         uno::Reference< container::XNameAccess > xConfigAccess(
1116601ee31SAriel Constenla-Haile             xConfigProvider->createInstanceWithArguments(
1126601ee31SAriel Constenla-Haile                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
1136601ee31SAriel Constenla-Haile                     "com.sun.star.configuration.ConfigurationAccess")), aArgs),
1146601ee31SAriel Constenla-Haile             uno::UNO_QUERY_THROW );
1156601ee31SAriel Constenla-Haile 
1166601ee31SAriel Constenla-Haile         rtl::OUString aVal;
1176601ee31SAriel Constenla-Haile         xConfigAccess->getByName(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ooName"))) >>= aVal;
1186601ee31SAriel Constenla-Haile 
1196601ee31SAriel Constenla-Haile         rtl::OUString &aUserAgent = WebDAVUserAgent::get();
1206601ee31SAriel Constenla-Haile         sal_Int32 nIndex = aUserAgent.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "$ooName" ) );
1216601ee31SAriel Constenla-Haile         if ( !aVal.getLength() || nIndex == -1 )
1226601ee31SAriel Constenla-Haile             return;
1236601ee31SAriel Constenla-Haile         aUserAgent = aUserAgent.replaceAt( nIndex, RTL_CONSTASCII_LENGTH( "$ooName" ), aVal );
1246601ee31SAriel Constenla-Haile 
1256601ee31SAriel Constenla-Haile         xConfigAccess->getByName(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ooSetupVersion"))) >>= aVal;
1266601ee31SAriel Constenla-Haile         nIndex = aUserAgent.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "$ooSetupVersion" ) );
1276601ee31SAriel Constenla-Haile         if ( !aVal.getLength() || nIndex == -1 )
1286601ee31SAriel Constenla-Haile             return;
1296601ee31SAriel Constenla-Haile         aUserAgent = aUserAgent.replaceAt( nIndex, RTL_CONSTASCII_LENGTH( "$ooSetupVersion" ), aVal );
1306601ee31SAriel Constenla-Haile 
1316601ee31SAriel Constenla-Haile     }
1326601ee31SAriel Constenla-Haile     catch ( const uno::Exception &e )
1336601ee31SAriel Constenla-Haile     {
1346601ee31SAriel Constenla-Haile         OSL_TRACE( "ContentProvider -caught exception! %s",
1356601ee31SAriel Constenla-Haile                    rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
1366601ee31SAriel Constenla-Haile         (void) e;
1376601ee31SAriel Constenla-Haile     }
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir //=========================================================================
141cdf0e10cSrcweir // virtual
142cdf0e10cSrcweir ContentProvider::~ContentProvider()
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	delete m_pProps;
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir //=========================================================================
148cdf0e10cSrcweir //
149cdf0e10cSrcweir // XInterface methods.
150cdf0e10cSrcweir //
151cdf0e10cSrcweir //=========================================================================
152cdf0e10cSrcweir 
153cdf0e10cSrcweir XINTERFACE_IMPL_3( ContentProvider,
154cdf0e10cSrcweir                    lang::XTypeProvider,
155cdf0e10cSrcweir                    lang::XServiceInfo,
156cdf0e10cSrcweir                    ucb::XContentProvider );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir //=========================================================================
159cdf0e10cSrcweir //
160cdf0e10cSrcweir // XTypeProvider methods.
161cdf0e10cSrcweir //
162cdf0e10cSrcweir //=========================================================================
163cdf0e10cSrcweir 
164cdf0e10cSrcweir XTYPEPROVIDER_IMPL_3( ContentProvider,
165cdf0e10cSrcweir                       lang::XTypeProvider,
166cdf0e10cSrcweir                       lang::XServiceInfo,
167cdf0e10cSrcweir                       ucb::XContentProvider );
168cdf0e10cSrcweir 
169cdf0e10cSrcweir //=========================================================================
170cdf0e10cSrcweir //
171cdf0e10cSrcweir // XServiceInfo methods.
172cdf0e10cSrcweir //
173cdf0e10cSrcweir //=========================================================================
174cdf0e10cSrcweir 
175cdf0e10cSrcweir XSERVICEINFO_IMPL_1( ContentProvider,
176cdf0e10cSrcweir                      rtl::OUString::createFromAscii(
177cdf0e10cSrcweir 			 			"com.sun.star.comp.WebDAVContentProvider" ),
178cdf0e10cSrcweir                      rtl::OUString::createFromAscii(
179cdf0e10cSrcweir 				 		WEBDAV_CONTENT_PROVIDER_SERVICE_NAME ) );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir //=========================================================================
182cdf0e10cSrcweir //
183cdf0e10cSrcweir // Service factory implementation.
184cdf0e10cSrcweir //
185cdf0e10cSrcweir //=========================================================================
186cdf0e10cSrcweir 
187cdf0e10cSrcweir ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir //=========================================================================
190cdf0e10cSrcweir //
191cdf0e10cSrcweir // XContentProvider methods.
192cdf0e10cSrcweir //
193cdf0e10cSrcweir //=========================================================================
194cdf0e10cSrcweir 
195cdf0e10cSrcweir // virtual
196cdf0e10cSrcweir uno::Reference< ucb::XContent > SAL_CALL
197cdf0e10cSrcweir ContentProvider::queryContent(
198cdf0e10cSrcweir             const uno::Reference<
199cdf0e10cSrcweir                     ucb::XContentIdentifier >& Identifier )
200cdf0e10cSrcweir     throw( ucb::IllegalIdentifierException,
201cdf0e10cSrcweir            uno::RuntimeException )
202cdf0e10cSrcweir {
203cdf0e10cSrcweir 	// Check URL scheme...
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     const rtl::OUString aScheme
206cdf0e10cSrcweir         = Identifier->getContentProviderScheme().toAsciiLowerCase();
207cdf0e10cSrcweir     if ( !aScheme.equalsAsciiL(
208cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( HTTP_URL_SCHEME ) ) &&
209cdf0e10cSrcweir          !aScheme.equalsAsciiL(
210cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( HTTPS_URL_SCHEME ) ) &&
211cdf0e10cSrcweir          !aScheme.equalsAsciiL(
212cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( WEBDAV_URL_SCHEME ) ) &&
213cdf0e10cSrcweir          !aScheme.equalsAsciiL(
214cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( DAV_URL_SCHEME ) ) &&
215cdf0e10cSrcweir          !aScheme.equalsAsciiL(
21659ddfc10SAndre Fischer             RTL_CONSTASCII_STRINGPARAM( DAVS_URL_SCHEME ) ) )
217cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 	// Normalize URL and create new Id, if nessacary.
220cdf0e10cSrcweir     rtl::OUString aURL = Identifier->getContentIdentifier();
221cdf0e10cSrcweir 
222cdf0e10cSrcweir     // At least: <scheme> + "://"
223cdf0e10cSrcweir     if ( aURL.getLength() < ( aScheme.getLength() + 3 ) )
224cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     if ( ( aURL.getStr()[ aScheme.getLength() ]     != sal_Unicode( ':' ) ) ||
227cdf0e10cSrcweir          ( aURL.getStr()[ aScheme.getLength() + 1 ] != sal_Unicode( '/' ) ) ||
228cdf0e10cSrcweir          ( aURL.getStr()[ aScheme.getLength() + 2 ] != sal_Unicode( '/' ) ) )
229cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
230cdf0e10cSrcweir 
231cdf0e10cSrcweir     uno::Reference< ucb::XContentIdentifier > xCanonicId;
232cdf0e10cSrcweir 
233cdf0e10cSrcweir     bool bNewId = false;
234cdf0e10cSrcweir     if ( aScheme.equalsAsciiL(
235cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( WEBDAV_URL_SCHEME ) ) )
236cdf0e10cSrcweir     {
237cdf0e10cSrcweir         aURL = aURL.replaceAt( 0,
238cdf0e10cSrcweir                                WEBDAV_URL_SCHEME_LENGTH,
239cdf0e10cSrcweir                                rtl::OUString::createFromAscii(
240cdf0e10cSrcweir                                                     HTTP_URL_SCHEME ) );
241cdf0e10cSrcweir         bNewId = true;
242cdf0e10cSrcweir     }
243cdf0e10cSrcweir     else if ( aScheme.equalsAsciiL(
244cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( DAV_URL_SCHEME ) ) )
245cdf0e10cSrcweir     {
246cdf0e10cSrcweir         aURL = aURL.replaceAt( 0,
247cdf0e10cSrcweir                                DAV_URL_SCHEME_LENGTH,
248cdf0e10cSrcweir                                rtl::OUString::createFromAscii(
249cdf0e10cSrcweir                                                     HTTP_URL_SCHEME ) );
250cdf0e10cSrcweir         bNewId = true;
251cdf0e10cSrcweir     }
252cdf0e10cSrcweir     else if ( aScheme.equalsAsciiL(
253cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( DAVS_URL_SCHEME ) ) )
254cdf0e10cSrcweir     {
255cdf0e10cSrcweir         aURL = aURL.replaceAt( 0,
256cdf0e10cSrcweir                                DAVS_URL_SCHEME_LENGTH,
257cdf0e10cSrcweir                                rtl::OUString::createFromAscii(
258cdf0e10cSrcweir                                                     HTTPS_URL_SCHEME ) );
259cdf0e10cSrcweir         bNewId = true;
260cdf0e10cSrcweir     }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir     sal_Int32 nPos = aURL.lastIndexOf( '/' );
263cdf0e10cSrcweir 	if ( nPos != aURL.getLength() - 1 )
264cdf0e10cSrcweir 	{
265cdf0e10cSrcweir 		// Find second slash in URL.
266cdf0e10cSrcweir 		nPos = aURL.indexOf( '/', aURL.indexOf( '/' ) + 1 );
267cdf0e10cSrcweir 		if ( nPos == -1 )
268cdf0e10cSrcweir             throw ucb::IllegalIdentifierException();
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 		nPos = aURL.indexOf( '/', nPos + 1 );
271cdf0e10cSrcweir 		if ( nPos == -1 )
272cdf0e10cSrcweir 		{
273cdf0e10cSrcweir             aURL += rtl::OUString::createFromAscii( "/" );
274cdf0e10cSrcweir             bNewId = true;
275cdf0e10cSrcweir 		}
276cdf0e10cSrcweir 	}
277cdf0e10cSrcweir 
278cdf0e10cSrcweir     if ( bNewId )
279cdf0e10cSrcweir         xCanonicId = new ::ucbhelper::ContentIdentifier( m_xSMgr, aURL );
280cdf0e10cSrcweir     else
281cdf0e10cSrcweir         xCanonicId = Identifier;
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 	// Check, if a content with given id already exists...
286cdf0e10cSrcweir     uno::Reference< ucb::XContent > xContent
287cdf0e10cSrcweir 		= queryExistingContent( xCanonicId ).get();
288cdf0e10cSrcweir 	if ( xContent.is() )
289cdf0e10cSrcweir 		return xContent;
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 	// Create a new content.
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 	try
294cdf0e10cSrcweir 	{
29559ddfc10SAndre Fischer         xContent = new ::http_dav_ucp::Content(
296cdf0e10cSrcweir                         m_xSMgr, this, xCanonicId, m_xDAVSessionFactory );
297cdf0e10cSrcweir         registerNewContent( xContent );
298cdf0e10cSrcweir 	}
299cdf0e10cSrcweir     catch ( ucb::ContentCreationException const & )
300cdf0e10cSrcweir 	{
301cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
302cdf0e10cSrcweir 	}
303cdf0e10cSrcweir 
304cdf0e10cSrcweir 	if ( !xContent->getIdentifier().is() )
305cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 	return xContent;
308cdf0e10cSrcweir }
309cdf0e10cSrcweir 
310