1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_ucb.hxx"
26 
27 /**************************************************************************
28 								TODO
29  **************************************************************************
30 
31  *************************************************************************/
32 #include <ucbhelper/contentidentifier.hxx>
33 #include "webdavprovider.hxx"
34 #include "webdavcontent.hxx"
35 #include "webdavuseragent.hxx"
36 
37 #include <osl/mutex.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <comphelper/processfactory.hxx>
40 #include <com/sun/star/beans/NamedValue.hpp>
41 #include <com/sun/star/container/XNameAccess.hpp>
42 
43 using namespace com::sun::star;
44 using namespace http_dav_ucp;
45 
46 
operator ()() const47 rtl::OUString &WebDAVUserAgent::operator()() const
48 {
49     rtl::OUStringBuffer aBuffer;
50     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "Apache " ));
51     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "$ooName/$ooSetupVersion" ));
52 #if OSL_DEBUG_LEVEL > 0
53 #ifdef APR_VERSION
54     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " apr/" APR_VERSION ));
55 #endif
56 
57 #ifdef APR_UTIL_VERSION
58     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " apr-util/" APR_UTIL_VERSION ));
59 #endif
60 
61 #ifdef SERF_VERSION
62     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " serf/" SERF_VERSION ));
63 #endif
64 #endif
65     static rtl::OUString aUserAgent( aBuffer.makeStringAndClear() );
66     return aUserAgent;
67 }
68 
69 //=========================================================================
70 //=========================================================================
71 //
72 // ContentProvider Implementation.
73 //
74 //=========================================================================
75 //=========================================================================
76 
ContentProvider(const uno::Reference<lang::XMultiServiceFactory> & rSMgr)77 ContentProvider::ContentProvider(
78                 const uno::Reference< lang::XMultiServiceFactory >& rSMgr )
79 : ::ucbhelper::ContentProviderImplHelper( rSMgr ),
80   m_xDAVSessionFactory( new DAVSessionFactory() ),
81   m_pProps( 0 )
82 {
83     static bool bInit = false;
84     if ( bInit )
85         return;
86     bInit = true;
87     try
88     {
89         uno::Reference< uno::XComponentContext > xContext(
90             ::comphelper::getProcessComponentContext() );
91         uno::Reference< lang::XMultiServiceFactory > xConfigProvider(
92             xContext->getServiceManager()->createInstanceWithContext(
93                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
94                                   "com.sun.star.configuration.ConfigurationProvider")), xContext),
95             uno::UNO_QUERY_THROW );
96 
97         beans::NamedValue aNodePath;
98         aNodePath.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
99         aNodePath.Value <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Product"));
100 
101         uno::Sequence< uno::Any > aArgs( 1 );
102         aArgs[0] <<= aNodePath;
103 
104         uno::Reference< container::XNameAccess > xConfigAccess(
105             xConfigProvider->createInstanceWithArguments(
106                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
107                     "com.sun.star.configuration.ConfigurationAccess")), aArgs),
108             uno::UNO_QUERY_THROW );
109 
110         rtl::OUString aVal;
111         xConfigAccess->getByName(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ooName"))) >>= aVal;
112 
113         rtl::OUString &aUserAgent = WebDAVUserAgent::get();
114         sal_Int32 nIndex = aUserAgent.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "$ooName" ) );
115         if ( !aVal.getLength() || nIndex == -1 )
116             return;
117         aUserAgent = aUserAgent.replaceAt( nIndex, RTL_CONSTASCII_LENGTH( "$ooName" ), aVal );
118 
119         xConfigAccess->getByName(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ooSetupVersion"))) >>= aVal;
120         nIndex = aUserAgent.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "$ooSetupVersion" ) );
121         if ( !aVal.getLength() || nIndex == -1 )
122             return;
123         aUserAgent = aUserAgent.replaceAt( nIndex, RTL_CONSTASCII_LENGTH( "$ooSetupVersion" ), aVal );
124 
125     }
126     catch ( const uno::Exception &e )
127     {
128         OSL_TRACE( "ContentProvider -caught exception! %s",
129                    rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
130         (void) e;
131     }
132 }
133 
134 //=========================================================================
135 // virtual
~ContentProvider()136 ContentProvider::~ContentProvider()
137 {
138 	delete m_pProps;
139 }
140 
141 //=========================================================================
142 //
143 // XInterface methods.
144 //
145 //=========================================================================
146 
147 XINTERFACE_IMPL_3( ContentProvider,
148                    lang::XTypeProvider,
149                    lang::XServiceInfo,
150                    ucb::XContentProvider );
151 
152 //=========================================================================
153 //
154 // XTypeProvider methods.
155 //
156 //=========================================================================
157 
158 XTYPEPROVIDER_IMPL_3( ContentProvider,
159                       lang::XTypeProvider,
160                       lang::XServiceInfo,
161                       ucb::XContentProvider );
162 
163 //=========================================================================
164 //
165 // XServiceInfo methods.
166 //
167 //=========================================================================
168 
169 XSERVICEINFO_IMPL_1( ContentProvider,
170                      rtl::OUString::createFromAscii(
171 			 			"com.sun.star.comp.WebDAVContentProvider" ),
172                      rtl::OUString::createFromAscii(
173 				 		WEBDAV_CONTENT_PROVIDER_SERVICE_NAME ) );
174 
175 //=========================================================================
176 //
177 // Service factory implementation.
178 //
179 //=========================================================================
180 
181 ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
182 
183 //=========================================================================
184 //
185 // XContentProvider methods.
186 //
187 //=========================================================================
188 
189 // virtual
190 uno::Reference< ucb::XContent > SAL_CALL
queryContent(const uno::Reference<ucb::XContentIdentifier> & Identifier)191 ContentProvider::queryContent(
192             const uno::Reference<
193                     ucb::XContentIdentifier >& Identifier )
194     throw( ucb::IllegalIdentifierException,
195            uno::RuntimeException )
196 {
197 	// Check URL scheme...
198 
199     const rtl::OUString aScheme
200         = Identifier->getContentProviderScheme().toAsciiLowerCase();
201     if ( !aScheme.equalsAsciiL(
202             RTL_CONSTASCII_STRINGPARAM( HTTP_URL_SCHEME ) ) &&
203          !aScheme.equalsAsciiL(
204             RTL_CONSTASCII_STRINGPARAM( HTTPS_URL_SCHEME ) ) &&
205          !aScheme.equalsAsciiL(
206             RTL_CONSTASCII_STRINGPARAM( WEBDAV_URL_SCHEME ) ) &&
207          !aScheme.equalsAsciiL(
208             RTL_CONSTASCII_STRINGPARAM( DAV_URL_SCHEME ) ) &&
209          !aScheme.equalsAsciiL(
210             RTL_CONSTASCII_STRINGPARAM( DAVS_URL_SCHEME ) ) )
211         throw ucb::IllegalIdentifierException();
212 
213 	// Normalize URL and create new Id, if nessacary.
214     rtl::OUString aURL = Identifier->getContentIdentifier();
215 
216     // At least: <scheme> + "://"
217     if ( aURL.getLength() < ( aScheme.getLength() + 3 ) )
218         throw ucb::IllegalIdentifierException();
219 
220     if ( ( aURL.getStr()[ aScheme.getLength() ]     != sal_Unicode( ':' ) ) ||
221          ( aURL.getStr()[ aScheme.getLength() + 1 ] != sal_Unicode( '/' ) ) ||
222          ( aURL.getStr()[ aScheme.getLength() + 2 ] != sal_Unicode( '/' ) ) )
223         throw ucb::IllegalIdentifierException();
224 
225     uno::Reference< ucb::XContentIdentifier > xCanonicId;
226 
227     bool bNewId = false;
228     if ( aScheme.equalsAsciiL(
229             RTL_CONSTASCII_STRINGPARAM( WEBDAV_URL_SCHEME ) ) )
230     {
231         aURL = aURL.replaceAt( 0,
232                                WEBDAV_URL_SCHEME_LENGTH,
233                                rtl::OUString::createFromAscii(
234                                                     HTTP_URL_SCHEME ) );
235         bNewId = true;
236     }
237     else if ( aScheme.equalsAsciiL(
238             RTL_CONSTASCII_STRINGPARAM( DAV_URL_SCHEME ) ) )
239     {
240         aURL = aURL.replaceAt( 0,
241                                DAV_URL_SCHEME_LENGTH,
242                                rtl::OUString::createFromAscii(
243                                                     HTTP_URL_SCHEME ) );
244         bNewId = true;
245     }
246     else if ( aScheme.equalsAsciiL(
247             RTL_CONSTASCII_STRINGPARAM( DAVS_URL_SCHEME ) ) )
248     {
249         aURL = aURL.replaceAt( 0,
250                                DAVS_URL_SCHEME_LENGTH,
251                                rtl::OUString::createFromAscii(
252                                                     HTTPS_URL_SCHEME ) );
253         bNewId = true;
254     }
255 
256     sal_Int32 nPos = aURL.lastIndexOf( '/' );
257 	if ( nPos != aURL.getLength() - 1 )
258 	{
259 		// Find second slash in URL.
260 		nPos = aURL.indexOf( '/', aURL.indexOf( '/' ) + 1 );
261 		if ( nPos == -1 )
262             throw ucb::IllegalIdentifierException();
263 
264 		nPos = aURL.indexOf( '/', nPos + 1 );
265 		if ( nPos == -1 )
266 		{
267             aURL += rtl::OUString::createFromAscii( "/" );
268             bNewId = true;
269 		}
270 	}
271 
272     if ( bNewId )
273         xCanonicId = new ::ucbhelper::ContentIdentifier( m_xSMgr, aURL );
274     else
275         xCanonicId = Identifier;
276 
277 	osl::MutexGuard aGuard( m_aMutex );
278 
279 	// Check, if a content with given id already exists...
280     uno::Reference< ucb::XContent > xContent
281 		= queryExistingContent( xCanonicId ).get();
282 	if ( xContent.is() )
283 		return xContent;
284 
285 	// Create a new content.
286 
287 	try
288 	{
289         xContent = new ::http_dav_ucp::Content(
290                         m_xSMgr, this, xCanonicId, m_xDAVSessionFactory );
291         registerNewContent( xContent );
292 	}
293     catch ( ucb::ContentCreationException const & )
294 	{
295         throw ucb::IllegalIdentifierException();
296 	}
297 
298 	if ( !xContent->getIdentifier().is() )
299         throw ucb::IllegalIdentifierException();
300 
301 	return xContent;
302 }
303 
304