Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : */
9 :
10 : #include <com/sun/star/beans/PropertyAttribute.hpp>
11 : #include <com/sun/star/beans/PropertyValue.hpp>
12 : #include <com/sun/star/beans/XPropertySetInfo.hpp>
13 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
14 : #include <com/sun/star/task/XInteractionHandler.hpp>
15 : #include <com/sun/star/ucb/XCommandInfo.hpp>
16 : #include <com/sun/star/ucb/XDynamicResultSet.hpp>
17 : #include <com/sun/star/ucb/XProgressHandler.hpp>
18 : #ifndef SYSTEM_CURL
19 : #include <com/sun/star/xml/crypto/XDigestContext.hpp>
20 : #include <com/sun/star/xml/crypto/XDigestContextSupplier.hpp>
21 : #include <com/sun/star/xml/crypto/DigestID.hpp>
22 : #include <com/sun/star/xml/crypto/NSSInitializer.hpp>
23 : #endif
24 :
25 : #include <comphelper/processfactory.hxx>
26 : #include <config_oauth2.h>
27 : #include <rtl/uri.hxx>
28 : #include <ucbhelper/cancelcommandexecution.hxx>
29 : #include <ucbhelper/commandenvironment.hxx>
30 : #include <ucbhelper/contentidentifier.hxx>
31 : #include <ucbhelper/propertyvalueset.hxx>
32 : #include <ucbhelper/proxydecider.hxx>
33 :
34 : #include "auth_provider.hxx"
35 : #include "certvalidation_handler.hxx"
36 : #include "cmis_content.hxx"
37 : #include "cmis_provider.hxx"
38 : #include "cmis_repo_content.hxx"
39 : #include "cmis_resultset.hxx"
40 : #include <boost/scoped_ptr.hpp>
41 :
42 : #define OUSTR_TO_STDSTR(s) string( OUStringToOString( s, RTL_TEXTENCODING_UTF8 ).getStr() )
43 : #define STD_TO_OUSTR( str ) OUString( str.c_str(), str.length( ), RTL_TEXTENCODING_UTF8 )
44 :
45 : using namespace com::sun::star;
46 : using namespace std;
47 :
48 : namespace cmis
49 : {
50 0 : RepoContent::RepoContent( const uno::Reference< uno::XComponentContext >& rxContext,
51 : ContentProvider *pProvider, const uno::Reference< ucb::XContentIdentifier >& Identifier,
52 : vector< libcmis::RepositoryPtr > aRepos )
53 : throw ( ucb::ContentCreationException )
54 : : ContentImplHelper( rxContext, pProvider, Identifier ),
55 : m_pProvider( pProvider ),
56 0 : m_aURL( Identifier->getContentIdentifier( ) ),
57 : m_sRepositoryId( ),
58 0 : m_aRepositories( aRepos )
59 : {
60 : // Split the URL into bits
61 0 : OUString sURL = m_xIdentifier->getContentIdentifier( );
62 : SAL_INFO( "ucb.ucp.cmis", "RepoContent::RepoContent() " << sURL );
63 :
64 0 : m_sRepositoryId = m_aURL.getObjectPath();
65 0 : if (!m_sRepositoryId.isEmpty() && m_sRepositoryId[0] == '/')
66 0 : m_sRepositoryId = m_sRepositoryId.copy(1);
67 0 : }
68 :
69 0 : RepoContent::~RepoContent()
70 : {
71 0 : }
72 :
73 0 : uno::Any RepoContent::getBadArgExcept()
74 : {
75 : return uno::makeAny( lang::IllegalArgumentException(
76 : OUString("Wrong argument type!"),
77 0 : static_cast< cppu::OWeakObject * >( this ), -1) );
78 : }
79 :
80 0 : uno::Reference< sdbc::XRow > RepoContent::getPropertyValues(
81 : const uno::Sequence< beans::Property >& rProperties,
82 : const uno::Reference< ucb::XCommandEnvironment >& xEnv )
83 : {
84 0 : rtl::Reference< ::ucbhelper::PropertyValueSet > xRow = new ::ucbhelper::PropertyValueSet( m_xContext );
85 :
86 : sal_Int32 nProps;
87 : const beans::Property* pProps;
88 :
89 0 : nProps = rProperties.getLength();
90 0 : pProps = rProperties.getConstArray();
91 :
92 0 : for( sal_Int32 n = 0; n < nProps; ++n )
93 : {
94 0 : const beans::Property& rProp = pProps[ n ];
95 :
96 : try
97 : {
98 0 : if ( rProp.Name == "IsDocument" )
99 : {
100 0 : xRow->appendBoolean( rProp, false );
101 : }
102 0 : else if ( rProp.Name == "IsFolder" )
103 : {
104 0 : xRow->appendBoolean( rProp, true );
105 : }
106 0 : else if ( rProp.Name == "Title" )
107 : {
108 0 : xRow->appendString( rProp, STD_TO_OUSTR( getRepository( xEnv )->getName( ) ) );
109 : }
110 0 : else if ( rProp.Name == "IsReadOnly" )
111 : {
112 0 : xRow->appendBoolean( rProp, true );
113 : }
114 : else
115 : {
116 0 : xRow->appendVoid( rProp );
117 : SAL_INFO( "ucb.ucp.cmis", "Looking for unsupported property " << rProp.Name );
118 : }
119 : }
120 0 : catch (const libcmis::Exception&)
121 : {
122 0 : xRow->appendVoid( rProp );
123 : }
124 : }
125 :
126 0 : return uno::Reference< sdbc::XRow >( xRow.get() );
127 : }
128 :
129 0 : void RepoContent::getRepositories( const uno::Reference< ucb::XCommandEnvironment > & xEnv )
130 : {
131 : #ifndef SYSTEM_CURL
132 : // Initialize NSS library to make sure libcmis (and curl) can access CACERTs using NSS
133 : // when using internal libcurl.
134 : uno::Reference< com::sun::star::xml::crypto::XNSSInitializer >
135 : xNSSInitializer = com::sun::star::xml::crypto::NSSInitializer::create( m_xContext );
136 :
137 : uno::Reference< com::sun::star::xml::crypto::XDigestContext > xDigestContext(
138 : xNSSInitializer->getDigestContext( com::sun::star::xml::crypto::DigestID::SHA256,
139 : uno::Sequence< beans::NamedValue >() ),
140 : uno::UNO_SET_THROW );
141 : #endif
142 :
143 : // Set the proxy if needed. We are doing that all times as the proxy data shouldn't be cached.
144 0 : ucbhelper::InternetProxyDecider aProxyDecider( m_xContext );
145 0 : INetURLObject aBindingUrl( m_aURL.getBindingUrl( ) );
146 : const ucbhelper::InternetProxyServer& rProxy = aProxyDecider.getProxy(
147 0 : INetURLObject::GetScheme( aBindingUrl.GetProtocol( ) ), aBindingUrl.GetHost(), aBindingUrl.GetPort() );
148 0 : OUString sProxy = rProxy.aName;
149 0 : if ( rProxy.nPort > 0 )
150 0 : sProxy += ":" + OUString::number( rProxy.nPort );
151 0 : libcmis::SessionFactory::setProxySettings( OUSTR_TO_STDSTR( sProxy ), string(), string(), string() );
152 :
153 0 : if ( m_aRepositories.empty() )
154 : {
155 : // Set the SSL Validation handler
156 : libcmis::CertValidationHandlerPtr certHandler(
157 0 : new CertValidationHandler( xEnv, m_xContext, aBindingUrl.GetHost( ) ) );
158 0 : libcmis::SessionFactory::setCertificateValidationHandler( certHandler );
159 :
160 : // Get the auth credentials
161 0 : AuthProvider authProvider( xEnv, m_xIdentifier->getContentIdentifier( ), m_aURL.getBindingUrl( ) );
162 :
163 0 : string rUsername = OUSTR_TO_STDSTR( m_aURL.getUsername( ) );
164 0 : string rPassword = OUSTR_TO_STDSTR( m_aURL.getPassword( ) );
165 0 : if ( authProvider.authenticationQuery( rUsername, rPassword ) )
166 : {
167 : try
168 : {
169 : // Create a session to get repositories
170 0 : libcmis::OAuth2DataPtr oauth2Data;
171 0 : if ( m_aURL.getBindingUrl( ) == GDRIVE_BASE_URL )
172 : oauth2Data.reset( new libcmis::OAuth2Data(
173 : GDRIVE_AUTH_URL, GDRIVE_TOKEN_URL,
174 : GDRIVE_SCOPE, GDRIVE_REDIRECT_URI,
175 0 : GDRIVE_CLIENT_ID, GDRIVE_CLIENT_SECRET ) );
176 0 : if ( m_aURL.getBindingUrl().startsWith( ALFRESCO_CLOUD_BASE_URL ) )
177 : oauth2Data.reset( new libcmis::OAuth2Data(
178 : ALFRESCO_CLOUD_AUTH_URL, ALFRESCO_CLOUD_TOKEN_URL,
179 : ALFRESCO_CLOUD_SCOPE, ALFRESCO_CLOUD_REDIRECT_URI,
180 0 : ALFRESCO_CLOUD_CLIENT_ID, ALFRESCO_CLOUD_CLIENT_SECRET ) );
181 :
182 : boost::scoped_ptr<libcmis::Session> session(libcmis::SessionFactory::createSession(
183 0 : OUSTR_TO_STDSTR( m_aURL.getBindingUrl( ) ),
184 0 : rUsername, rPassword, "", false, oauth2Data ));
185 0 : if (!session)
186 : ucbhelper::cancelCommandExecution(
187 : ucb::IOErrorCode_INVALID_DEVICE,
188 : uno::Sequence< uno::Any >( 0 ),
189 : xEnv,
190 0 : OUString( ) );
191 0 : m_aRepositories = session->getRepositories( );
192 : }
193 0 : catch (const libcmis::Exception& e)
194 : {
195 : SAL_INFO( "ucb.ucp.cmis", "Error getting repositories: " << e.what() );
196 : }
197 : }
198 : else
199 : {
200 : // Throw user cancelled exception
201 : ucbhelper::cancelCommandExecution(
202 : ucb::IOErrorCode_ABORT,
203 : uno::Sequence< uno::Any >( 0 ),
204 : xEnv,
205 0 : OUString( "Authentication cancelled" ) );
206 0 : }
207 0 : }
208 0 : }
209 :
210 0 : libcmis::RepositoryPtr RepoContent::getRepository( const uno::Reference< ucb::XCommandEnvironment > & xEnv )
211 : {
212 : // Ensure we have the repositories extracted
213 0 : getRepositories( xEnv );
214 :
215 0 : libcmis::RepositoryPtr repo;
216 :
217 0 : if ( !m_sRepositoryId.isEmpty() )
218 : {
219 0 : for ( vector< libcmis::RepositoryPtr >::iterator it = m_aRepositories.begin( );
220 0 : it != m_aRepositories.end( ) && NULL == repo.get( ); ++it )
221 : {
222 0 : if ( STD_TO_OUSTR( ( *it )->getId( ) ) == m_sRepositoryId )
223 0 : repo = *it;
224 : }
225 : }
226 : else
227 0 : repo = m_aRepositories.front( );
228 0 : return repo;
229 : }
230 :
231 0 : uno::Sequence< beans::Property > RepoContent::getProperties(
232 : const uno::Reference< ucb::XCommandEnvironment > & /*xEnv*/ )
233 : {
234 : static const beans::Property aGenericProperties[] =
235 : {
236 : beans::Property( OUString( "IsDocument" ),
237 0 : -1, getCppuBooleanType(),
238 : beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
239 : beans::Property( OUString( "IsFolder" ),
240 0 : -1, getCppuBooleanType(),
241 : beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
242 : beans::Property( OUString( "Title" ),
243 0 : -1, cppu::UnoType<OUString>::get(),
244 : beans::PropertyAttribute::BOUND ),
245 : beans::Property( OUString( "IsReadOnly" ),
246 0 : -1, getCppuBooleanType(),
247 : beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
248 0 : };
249 :
250 0 : const int nProps = SAL_N_ELEMENTS(aGenericProperties);
251 0 : return uno::Sequence< beans::Property > ( aGenericProperties, nProps );
252 : }
253 :
254 0 : uno::Sequence< ucb::CommandInfo > RepoContent::getCommands(
255 : const uno::Reference< ucb::XCommandEnvironment > & /*xEnv*/ )
256 : {
257 : static const ucb::CommandInfo aCommandInfoTable[] =
258 : {
259 : // Required commands
260 : ucb::CommandInfo
261 : ( OUString( "getCommandInfo" ),
262 0 : -1, getCppuVoidType() ),
263 : ucb::CommandInfo
264 : ( OUString( "getPropertySetInfo" ),
265 0 : -1, getCppuVoidType() ),
266 : ucb::CommandInfo
267 : ( OUString( "getPropertyValues" ),
268 0 : -1, getCppuType( static_cast<uno::Sequence< beans::Property > * >( 0 ) ) ),
269 : ucb::CommandInfo
270 : ( OUString( "setPropertyValues" ),
271 0 : -1, getCppuType( static_cast<uno::Sequence< beans::PropertyValue > * >( 0 ) ) ),
272 :
273 : // Optional standard commands
274 : ucb::CommandInfo
275 : ( OUString( "open" ),
276 0 : -1, cppu::UnoType<ucb::OpenCommandArgument2>::get() ),
277 0 : };
278 :
279 0 : const int nProps = SAL_N_ELEMENTS(aCommandInfoTable);
280 0 : return uno::Sequence< ucb::CommandInfo >(aCommandInfoTable, nProps );
281 : }
282 :
283 0 : OUString RepoContent::getParentURL( )
284 : {
285 0 : OUString sRet;
286 :
287 : SAL_INFO( "ucb.ucp.cmis", "RepoContent::getParentURL()" );
288 :
289 : // TODO Implement me
290 :
291 0 : return sRet;
292 : }
293 :
294 0 : XTYPEPROVIDER_COMMON_IMPL( RepoContent );
295 :
296 0 : void SAL_CALL RepoContent::acquire() throw()
297 : {
298 0 : ContentImplHelper::acquire();
299 0 : }
300 :
301 0 : void SAL_CALL RepoContent::release() throw()
302 : {
303 0 : ContentImplHelper::release();
304 0 : }
305 :
306 0 : uno::Any SAL_CALL RepoContent::queryInterface( const uno::Type & rType ) throw ( uno::RuntimeException, std::exception )
307 : {
308 0 : return ContentImplHelper::queryInterface(rType);
309 : }
310 :
311 0 : OUString SAL_CALL RepoContent::getImplementationName() throw( uno::RuntimeException, std::exception )
312 : {
313 0 : return OUString("com.sun.star.comp.CmisRepoContent");
314 : }
315 :
316 0 : uno::Sequence< OUString > SAL_CALL RepoContent::getSupportedServiceNames()
317 : throw( uno::RuntimeException, std::exception )
318 : {
319 0 : uno::Sequence< OUString > aSNS( 1 );
320 0 : aSNS.getArray()[ 0 ] = "com.sun.star.ucb.Content";
321 0 : return aSNS;
322 : }
323 :
324 0 : OUString SAL_CALL RepoContent::getContentType() throw( uno::RuntimeException, std::exception )
325 : {
326 0 : return OUString( CMIS_REPO_TYPE );
327 : }
328 :
329 0 : uno::Any SAL_CALL RepoContent::execute(
330 : const ucb::Command& aCommand,
331 : sal_Int32 /*CommandId*/,
332 : const uno::Reference< ucb::XCommandEnvironment >& xEnv )
333 : throw( uno::Exception, ucb::CommandAbortedException, uno::RuntimeException, std::exception )
334 : {
335 : SAL_INFO( "ucb.ucp.cmis", "RepoContent::execute( ) - " << aCommand.Name );
336 :
337 0 : uno::Any aRet;
338 :
339 0 : if ( aCommand.Name == "getPropertyValues" )
340 : {
341 0 : uno::Sequence< beans::Property > Properties;
342 0 : if ( !( aCommand.Argument >>= Properties ) )
343 0 : ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
344 0 : aRet <<= getPropertyValues( Properties, xEnv );
345 : }
346 0 : else if ( aCommand.Name == "getPropertySetInfo" )
347 0 : aRet <<= getPropertySetInfo( xEnv, false );
348 0 : else if ( aCommand.Name == "getCommandInfo" )
349 0 : aRet <<= getCommandInfo( xEnv, false );
350 0 : else if ( aCommand.Name == "open" )
351 : {
352 0 : ucb::OpenCommandArgument2 aOpenCommand;
353 0 : if ( !( aCommand.Argument >>= aOpenCommand ) )
354 0 : ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
355 0 : const ucb::OpenCommandArgument2& rOpenCommand = aOpenCommand;
356 :
357 0 : getRepositories( xEnv );
358 : uno::Reference< ucb::XDynamicResultSet > xSet
359 0 : = new DynamicResultSet(m_xContext, this, rOpenCommand, xEnv );
360 0 : aRet <<= xSet;
361 : }
362 : else
363 : {
364 : SAL_INFO( "ucb.ucp.cmis", "Command not allowed" );
365 : }
366 :
367 0 : return aRet;
368 : }
369 :
370 0 : void SAL_CALL RepoContent::abort( sal_Int32 /*CommandId*/ ) throw( uno::RuntimeException, std::exception )
371 : {
372 : SAL_INFO( "ucb.ucp.cmis", "TODO - RepoContent::abort()" );
373 : // TODO Implement me
374 0 : }
375 :
376 0 : uno::Sequence< uno::Type > SAL_CALL RepoContent::getTypes() throw( uno::RuntimeException, std::exception )
377 : {
378 : static cppu::OTypeCollection aFolderCollection
379 0 : (CPPU_TYPE_REF( lang::XTypeProvider ),
380 0 : CPPU_TYPE_REF( lang::XServiceInfo ),
381 0 : CPPU_TYPE_REF( lang::XComponent ),
382 0 : CPPU_TYPE_REF( ucb::XContent ),
383 0 : CPPU_TYPE_REF( ucb::XCommandProcessor ),
384 0 : CPPU_TYPE_REF( beans::XPropertiesChangeNotifier ),
385 0 : CPPU_TYPE_REF( ucb::XCommandInfoChangeNotifier ),
386 0 : CPPU_TYPE_REF( beans::XPropertyContainer ),
387 0 : CPPU_TYPE_REF( beans::XPropertySetInfoChangeNotifier ),
388 0 : CPPU_TYPE_REF( container::XChild ) );
389 0 : return aFolderCollection.getTypes();
390 : }
391 :
392 0 : list< uno::Reference< ucb::XContent > > RepoContent::getChildren( )
393 : {
394 0 : list< uno::Reference< ucb::XContent > > result;
395 :
396 : // TODO Cache the results somehow
397 : SAL_INFO( "ucb.ucp.cmis", "RepoContent::getChildren" );
398 :
399 0 : if ( m_sRepositoryId.isEmpty( ) )
400 : {
401 0 : for ( vector< libcmis::RepositoryPtr >::iterator it = m_aRepositories.begin( );
402 0 : it != m_aRepositories.end(); ++it )
403 : {
404 0 : URL aUrl( m_aURL );
405 0 : aUrl.setObjectPath( STD_TO_OUSTR( ( *it )->getId( ) ) );
406 :
407 0 : uno::Reference< ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aUrl.asString( ) );
408 0 : uno::Reference< ucb::XContent > xContent = new RepoContent( m_xContext, m_pProvider, xId, m_aRepositories );
409 :
410 0 : result.push_back( xContent );
411 0 : }
412 : }
413 : else
414 : {
415 : // Return the repository root as child
416 0 : OUString sUrl;
417 : OUString sEncodedBinding = rtl::Uri::encode(
418 0 : m_aURL.getBindingUrl( ) + "#" + m_sRepositoryId,
419 : rtl_UriCharClassRelSegment,
420 : rtl_UriEncodeKeepEscapes,
421 0 : RTL_TEXTENCODING_UTF8 );
422 0 : sUrl = "vnd.libreoffice.cmis://" + sEncodedBinding;
423 :
424 0 : uno::Reference< ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( sUrl );
425 0 : uno::Reference< ucb::XContent > xContent = new Content( m_xContext, m_pProvider, xId );
426 :
427 0 : result.push_back( xContent );
428 : }
429 0 : return result;
430 : }
431 0 : }
432 :
433 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|