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
25421ed02eSdamjan #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*9646dec5SDamjan Jovanovic #include <curl/curl.h>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace com::sun::star;
4559ddfc10SAndre Fischer using namespace http_dav_ucp;
46cdf0e10cSrcweir 
476601ee31SAriel Constenla-Haile 
operator ()() const486601ee31SAriel Constenla-Haile rtl::OUString &WebDAVUserAgent::operator()() const
496601ee31SAriel Constenla-Haile {
506601ee31SAriel Constenla-Haile     rtl::OUStringBuffer aBuffer;
519c0c1533SAndrea Pescetti     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "Apache " ));
526601ee31SAriel Constenla-Haile     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "$ooName/$ooSetupVersion" ));
536601ee31SAriel Constenla-Haile #if OSL_DEBUG_LEVEL > 0
54*9646dec5SDamjan Jovanovic     curl_version_info_data *curl_ver = curl_version_info(CURLVERSION_NOW);
55*9646dec5SDamjan Jovanovic     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " curl/" ) );
56*9646dec5SDamjan Jovanovic     aBuffer.appendAscii( curl_ver->version );
576601ee31SAriel Constenla-Haile #endif
586601ee31SAriel Constenla-Haile     static rtl::OUString aUserAgent( aBuffer.makeStringAndClear() );
596601ee31SAriel Constenla-Haile     return aUserAgent;
606601ee31SAriel Constenla-Haile }
616601ee31SAriel Constenla-Haile 
62cdf0e10cSrcweir //=========================================================================
63cdf0e10cSrcweir //=========================================================================
64cdf0e10cSrcweir //
65cdf0e10cSrcweir // ContentProvider Implementation.
66cdf0e10cSrcweir //
67cdf0e10cSrcweir //=========================================================================
68cdf0e10cSrcweir //=========================================================================
69cdf0e10cSrcweir 
ContentProvider(const uno::Reference<lang::XMultiServiceFactory> & rSMgr)70cdf0e10cSrcweir ContentProvider::ContentProvider(
71cdf0e10cSrcweir                 const uno::Reference< lang::XMultiServiceFactory >& rSMgr )
72cdf0e10cSrcweir : ::ucbhelper::ContentProviderImplHelper( rSMgr ),
73cdf0e10cSrcweir   m_xDAVSessionFactory( new DAVSessionFactory() ),
74cdf0e10cSrcweir   m_pProps( 0 )
75cdf0e10cSrcweir {
766601ee31SAriel Constenla-Haile     static bool bInit = false;
776601ee31SAriel Constenla-Haile     if ( bInit )
786601ee31SAriel Constenla-Haile         return;
796601ee31SAriel Constenla-Haile     bInit = true;
806601ee31SAriel Constenla-Haile     try
816601ee31SAriel Constenla-Haile     {
826601ee31SAriel Constenla-Haile         uno::Reference< uno::XComponentContext > xContext(
836601ee31SAriel Constenla-Haile             ::comphelper::getProcessComponentContext() );
846601ee31SAriel Constenla-Haile         uno::Reference< lang::XMultiServiceFactory > xConfigProvider(
856601ee31SAriel Constenla-Haile             xContext->getServiceManager()->createInstanceWithContext(
866601ee31SAriel Constenla-Haile                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
876601ee31SAriel Constenla-Haile                                   "com.sun.star.configuration.ConfigurationProvider")), xContext),
886601ee31SAriel Constenla-Haile             uno::UNO_QUERY_THROW );
896601ee31SAriel Constenla-Haile 
906601ee31SAriel Constenla-Haile         beans::NamedValue aNodePath;
916601ee31SAriel Constenla-Haile         aNodePath.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
926601ee31SAriel Constenla-Haile         aNodePath.Value <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Product"));
936601ee31SAriel Constenla-Haile 
946601ee31SAriel Constenla-Haile         uno::Sequence< uno::Any > aArgs( 1 );
956601ee31SAriel Constenla-Haile         aArgs[0] <<= aNodePath;
966601ee31SAriel Constenla-Haile 
976601ee31SAriel Constenla-Haile         uno::Reference< container::XNameAccess > xConfigAccess(
986601ee31SAriel Constenla-Haile             xConfigProvider->createInstanceWithArguments(
996601ee31SAriel Constenla-Haile                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
1006601ee31SAriel Constenla-Haile                     "com.sun.star.configuration.ConfigurationAccess")), aArgs),
1016601ee31SAriel Constenla-Haile             uno::UNO_QUERY_THROW );
1026601ee31SAriel Constenla-Haile 
1036601ee31SAriel Constenla-Haile         rtl::OUString aVal;
1046601ee31SAriel Constenla-Haile         xConfigAccess->getByName(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ooName"))) >>= aVal;
1056601ee31SAriel Constenla-Haile 
1066601ee31SAriel Constenla-Haile         rtl::OUString &aUserAgent = WebDAVUserAgent::get();
1076601ee31SAriel Constenla-Haile         sal_Int32 nIndex = aUserAgent.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "$ooName" ) );
1086601ee31SAriel Constenla-Haile         if ( !aVal.getLength() || nIndex == -1 )
1096601ee31SAriel Constenla-Haile             return;
1106601ee31SAriel Constenla-Haile         aUserAgent = aUserAgent.replaceAt( nIndex, RTL_CONSTASCII_LENGTH( "$ooName" ), aVal );
1116601ee31SAriel Constenla-Haile 
1126601ee31SAriel Constenla-Haile         xConfigAccess->getByName(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ooSetupVersion"))) >>= aVal;
1136601ee31SAriel Constenla-Haile         nIndex = aUserAgent.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "$ooSetupVersion" ) );
1146601ee31SAriel Constenla-Haile         if ( !aVal.getLength() || nIndex == -1 )
1156601ee31SAriel Constenla-Haile             return;
1166601ee31SAriel Constenla-Haile         aUserAgent = aUserAgent.replaceAt( nIndex, RTL_CONSTASCII_LENGTH( "$ooSetupVersion" ), aVal );
1176601ee31SAriel Constenla-Haile 
1186601ee31SAriel Constenla-Haile     }
1196601ee31SAriel Constenla-Haile     catch ( const uno::Exception &e )
1206601ee31SAriel Constenla-Haile     {
1216601ee31SAriel Constenla-Haile         OSL_TRACE( "ContentProvider -caught exception! %s",
1226601ee31SAriel Constenla-Haile                    rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
1236601ee31SAriel Constenla-Haile         (void) e;
1246601ee31SAriel Constenla-Haile     }
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir //=========================================================================
128cdf0e10cSrcweir // virtual
~ContentProvider()129cdf0e10cSrcweir ContentProvider::~ContentProvider()
130cdf0e10cSrcweir {
131cdf0e10cSrcweir 	delete m_pProps;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //=========================================================================
135cdf0e10cSrcweir //
136cdf0e10cSrcweir // XInterface methods.
137cdf0e10cSrcweir //
138cdf0e10cSrcweir //=========================================================================
139cdf0e10cSrcweir 
140cdf0e10cSrcweir XINTERFACE_IMPL_3( ContentProvider,
141cdf0e10cSrcweir                    lang::XTypeProvider,
142cdf0e10cSrcweir                    lang::XServiceInfo,
143cdf0e10cSrcweir                    ucb::XContentProvider );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir //=========================================================================
146cdf0e10cSrcweir //
147cdf0e10cSrcweir // XTypeProvider methods.
148cdf0e10cSrcweir //
149cdf0e10cSrcweir //=========================================================================
150cdf0e10cSrcweir 
151cdf0e10cSrcweir XTYPEPROVIDER_IMPL_3( ContentProvider,
152cdf0e10cSrcweir                       lang::XTypeProvider,
153cdf0e10cSrcweir                       lang::XServiceInfo,
154cdf0e10cSrcweir                       ucb::XContentProvider );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir //=========================================================================
157cdf0e10cSrcweir //
158cdf0e10cSrcweir // XServiceInfo methods.
159cdf0e10cSrcweir //
160cdf0e10cSrcweir //=========================================================================
161cdf0e10cSrcweir 
162cdf0e10cSrcweir XSERVICEINFO_IMPL_1( ContentProvider,
163cdf0e10cSrcweir                      rtl::OUString::createFromAscii(
164cdf0e10cSrcweir 			 			"com.sun.star.comp.WebDAVContentProvider" ),
165cdf0e10cSrcweir                      rtl::OUString::createFromAscii(
166cdf0e10cSrcweir 				 		WEBDAV_CONTENT_PROVIDER_SERVICE_NAME ) );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir //=========================================================================
169cdf0e10cSrcweir //
170cdf0e10cSrcweir // Service factory implementation.
171cdf0e10cSrcweir //
172cdf0e10cSrcweir //=========================================================================
173cdf0e10cSrcweir 
174cdf0e10cSrcweir ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
175cdf0e10cSrcweir 
176cdf0e10cSrcweir //=========================================================================
177cdf0e10cSrcweir //
178cdf0e10cSrcweir // XContentProvider methods.
179cdf0e10cSrcweir //
180cdf0e10cSrcweir //=========================================================================
181cdf0e10cSrcweir 
182cdf0e10cSrcweir // virtual
183cdf0e10cSrcweir uno::Reference< ucb::XContent > SAL_CALL
queryContent(const uno::Reference<ucb::XContentIdentifier> & Identifier)184cdf0e10cSrcweir ContentProvider::queryContent(
185cdf0e10cSrcweir             const uno::Reference<
186cdf0e10cSrcweir                     ucb::XContentIdentifier >& Identifier )
187cdf0e10cSrcweir     throw( ucb::IllegalIdentifierException,
188cdf0e10cSrcweir            uno::RuntimeException )
189cdf0e10cSrcweir {
190cdf0e10cSrcweir 	// Check URL scheme...
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     const rtl::OUString aScheme
193cdf0e10cSrcweir         = Identifier->getContentProviderScheme().toAsciiLowerCase();
194cdf0e10cSrcweir     if ( !aScheme.equalsAsciiL(
195cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( HTTP_URL_SCHEME ) ) &&
196cdf0e10cSrcweir          !aScheme.equalsAsciiL(
197cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( HTTPS_URL_SCHEME ) ) &&
198cdf0e10cSrcweir          !aScheme.equalsAsciiL(
199cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( WEBDAV_URL_SCHEME ) ) &&
200cdf0e10cSrcweir          !aScheme.equalsAsciiL(
201cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( DAV_URL_SCHEME ) ) &&
202cdf0e10cSrcweir          !aScheme.equalsAsciiL(
20359ddfc10SAndre Fischer             RTL_CONSTASCII_STRINGPARAM( DAVS_URL_SCHEME ) ) )
204cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 	// Normalize URL and create new Id, if nessacary.
207cdf0e10cSrcweir     rtl::OUString aURL = Identifier->getContentIdentifier();
208cdf0e10cSrcweir 
209cdf0e10cSrcweir     // At least: <scheme> + "://"
210cdf0e10cSrcweir     if ( aURL.getLength() < ( aScheme.getLength() + 3 ) )
211cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     if ( ( aURL.getStr()[ aScheme.getLength() ]     != sal_Unicode( ':' ) ) ||
214cdf0e10cSrcweir          ( aURL.getStr()[ aScheme.getLength() + 1 ] != sal_Unicode( '/' ) ) ||
215cdf0e10cSrcweir          ( aURL.getStr()[ aScheme.getLength() + 2 ] != sal_Unicode( '/' ) ) )
216cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     uno::Reference< ucb::XContentIdentifier > xCanonicId;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     bool bNewId = false;
221cdf0e10cSrcweir     if ( aScheme.equalsAsciiL(
222cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( WEBDAV_URL_SCHEME ) ) )
223cdf0e10cSrcweir     {
224cdf0e10cSrcweir         aURL = aURL.replaceAt( 0,
225cdf0e10cSrcweir                                WEBDAV_URL_SCHEME_LENGTH,
226cdf0e10cSrcweir                                rtl::OUString::createFromAscii(
227cdf0e10cSrcweir                                                     HTTP_URL_SCHEME ) );
228cdf0e10cSrcweir         bNewId = true;
229cdf0e10cSrcweir     }
230cdf0e10cSrcweir     else if ( aScheme.equalsAsciiL(
231cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( DAV_URL_SCHEME ) ) )
232cdf0e10cSrcweir     {
233cdf0e10cSrcweir         aURL = aURL.replaceAt( 0,
234cdf0e10cSrcweir                                DAV_URL_SCHEME_LENGTH,
235cdf0e10cSrcweir                                rtl::OUString::createFromAscii(
236cdf0e10cSrcweir                                                     HTTP_URL_SCHEME ) );
237cdf0e10cSrcweir         bNewId = true;
238cdf0e10cSrcweir     }
239cdf0e10cSrcweir     else if ( aScheme.equalsAsciiL(
240cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM( DAVS_URL_SCHEME ) ) )
241cdf0e10cSrcweir     {
242cdf0e10cSrcweir         aURL = aURL.replaceAt( 0,
243cdf0e10cSrcweir                                DAVS_URL_SCHEME_LENGTH,
244cdf0e10cSrcweir                                rtl::OUString::createFromAscii(
245cdf0e10cSrcweir                                                     HTTPS_URL_SCHEME ) );
246cdf0e10cSrcweir         bNewId = true;
247cdf0e10cSrcweir     }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir     sal_Int32 nPos = aURL.lastIndexOf( '/' );
250cdf0e10cSrcweir 	if ( nPos != aURL.getLength() - 1 )
251cdf0e10cSrcweir 	{
252cdf0e10cSrcweir 		// Find second slash in URL.
253cdf0e10cSrcweir 		nPos = aURL.indexOf( '/', aURL.indexOf( '/' ) + 1 );
254cdf0e10cSrcweir 		if ( nPos == -1 )
255cdf0e10cSrcweir             throw ucb::IllegalIdentifierException();
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 		nPos = aURL.indexOf( '/', nPos + 1 );
258cdf0e10cSrcweir 		if ( nPos == -1 )
259cdf0e10cSrcweir 		{
260cdf0e10cSrcweir             aURL += rtl::OUString::createFromAscii( "/" );
261cdf0e10cSrcweir             bNewId = true;
262cdf0e10cSrcweir 		}
263cdf0e10cSrcweir 	}
264cdf0e10cSrcweir 
265cdf0e10cSrcweir     if ( bNewId )
266cdf0e10cSrcweir         xCanonicId = new ::ucbhelper::ContentIdentifier( m_xSMgr, aURL );
267cdf0e10cSrcweir     else
268cdf0e10cSrcweir         xCanonicId = Identifier;
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 	// Check, if a content with given id already exists...
273cdf0e10cSrcweir     uno::Reference< ucb::XContent > xContent
274cdf0e10cSrcweir 		= queryExistingContent( xCanonicId ).get();
275cdf0e10cSrcweir 	if ( xContent.is() )
276cdf0e10cSrcweir 		return xContent;
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 	// Create a new content.
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 	try
281cdf0e10cSrcweir 	{
28259ddfc10SAndre Fischer         xContent = new ::http_dav_ucp::Content(
283cdf0e10cSrcweir                         m_xSMgr, this, xCanonicId, m_xDAVSessionFactory );
284cdf0e10cSrcweir         registerNewContent( xContent );
285cdf0e10cSrcweir 	}
286cdf0e10cSrcweir     catch ( ucb::ContentCreationException const & )
287cdf0e10cSrcweir 	{
288cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
289cdf0e10cSrcweir 	}
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 	if ( !xContent->getIdentifier().is() )
292cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
293cdf0e10cSrcweir 
294cdf0e10cSrcweir 	return xContent;
295cdf0e10cSrcweir }
296cdf0e10cSrcweir 
297