1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <ucbhelper/contentidentifier.hxx>
21 #include "webdavprovider.hxx"
22 #include "webdavcontent.hxx"
24 #include <osl/mutex.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <com/sun/star/beans/NamedValue.hpp>
28 #include <com/sun/star/container/XNameAccess.hpp>
30 using namespace com::sun::star
;
31 using namespace http_dav_ucp
;
34 // ContentProvider Implementation.
39 ContentProvider::ContentProvider(
40 const uno::Reference
< uno::XComponentContext
>& rContext
)
41 : ::ucbhelper::ContentProviderImplHelper( rContext
),
42 m_xDAVSessionFactory( new DAVSessionFactory() ),
49 ContentProvider::~ContentProvider()
56 // XInterface methods.
57 void SAL_CALL
ContentProvider::acquire()
60 OWeakObject::acquire();
63 void SAL_CALL
ContentProvider::release()
66 OWeakObject::release();
69 css::uno::Any SAL_CALL
ContentProvider::queryInterface( const css::uno::Type
& rType
)
70 throw( css::uno::RuntimeException
, std::exception
)
72 css::uno::Any aRet
= cppu::queryInterface( rType
,
73 (static_cast< lang::XTypeProvider
* >(this)),
74 (static_cast< lang::XServiceInfo
* >(this)),
75 (static_cast< ucb::XContentProvider
* >(this))
77 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
80 // XTypeProvider methods.
84 XTYPEPROVIDER_IMPL_3( ContentProvider
,
87 ucb::XContentProvider
);
91 // XServiceInfo methods.
95 XSERVICEINFO_IMPL_1_CTX( ContentProvider
,
96 OUString( "com.sun.star.comp.WebDAVContentProvider" ),
97 OUString( WEBDAV_CONTENT_PROVIDER_SERVICE_NAME
) );
101 // Service factory implementation.
105 ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider
);
109 // XContentProvider methods.
114 uno::Reference
< ucb::XContent
> SAL_CALL
115 ContentProvider::queryContent(
116 const uno::Reference
<
117 ucb::XContentIdentifier
>& Identifier
)
118 throw( ucb::IllegalIdentifierException
,
119 uno::RuntimeException
)
121 // Check URL scheme...
123 const OUString aScheme
124 = Identifier
->getContentProviderScheme().toAsciiLowerCase();
125 if ( aScheme
!= HTTP_URL_SCHEME
&& aScheme
!= HTTPS_URL_SCHEME
&&
126 aScheme
!= WEBDAV_URL_SCHEME
&& aScheme
!= DAV_URL_SCHEME
&&
127 aScheme
!= DAVS_URL_SCHEME
)
128 throw ucb::IllegalIdentifierException();
130 // Normalize URL and create new Id, if nessacary.
131 OUString aURL
= Identifier
->getContentIdentifier();
133 // At least: <scheme> + "://"
134 if ( aURL
.getLength() < ( aScheme
.getLength() + 3 ) )
135 throw ucb::IllegalIdentifierException();
137 if ( aURL
.copy( aScheme
.getLength(), 3 ) != "://" )
138 throw ucb::IllegalIdentifierException();
140 uno::Reference
< ucb::XContentIdentifier
> xCanonicId
;
143 if ( aScheme
== WEBDAV_URL_SCHEME
)
145 aURL
= aURL
.replaceAt( 0,
146 WEBDAV_URL_SCHEME_LENGTH
,
147 OUString( HTTP_URL_SCHEME
) );
150 else if ( aScheme
== DAV_URL_SCHEME
)
152 aURL
= aURL
.replaceAt( 0,
153 DAV_URL_SCHEME_LENGTH
,
154 OUString( HTTP_URL_SCHEME
) );
157 else if ( aScheme
== DAVS_URL_SCHEME
)
159 aURL
= aURL
.replaceAt( 0,
160 DAVS_URL_SCHEME_LENGTH
,
161 OUString( HTTPS_URL_SCHEME
) );
165 sal_Int32 nPos
= aURL
.lastIndexOf( '/' );
166 if ( nPos
!= aURL
.getLength() - 1 )
168 // Find second slash in URL.
169 nPos
= aURL
.indexOf( '/', aURL
.indexOf( '/' ) + 1 );
171 throw ucb::IllegalIdentifierException();
173 nPos
= aURL
.indexOf( '/', nPos
+ 1 );
182 xCanonicId
= new ::ucbhelper::ContentIdentifier( aURL
);
184 xCanonicId
= Identifier
;
186 osl::MutexGuard
aGuard( m_aMutex
);
188 // Check, if a content with given id already exists...
189 uno::Reference
< ucb::XContent
> xContent
190 = queryExistingContent( xCanonicId
).get();
194 // Create a new content.
198 xContent
= new ::http_dav_ucp::Content(
199 m_xContext
, this, xCanonicId
, m_xDAVSessionFactory
);
200 registerNewContent( xContent
);
202 catch ( ucb::ContentCreationException
const & )
204 throw ucb::IllegalIdentifierException();
207 if ( !xContent
->getIdentifier().is() )
208 throw ucb::IllegalIdentifierException();
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */