Debian Codegraph @

ucb/source/ucp/cmis/cmis_content.cxx

raw
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 <string_view>
11
12 #include <boost/make_shared.hpp>
13
14 #include <com/sun/star/beans/IllegalTypeException.hpp>
15 #include <com/sun/star/beans/PropertyAttribute.hpp>
16 #include <com/sun/star/beans/PropertyValue.hpp>
17 #include <com/sun/star/beans/XPropertySetInfo.hpp>
18 #include <com/sun/star/document/CmisProperty.hpp>
19 #include <com/sun/star/io/XActiveDataSink.hpp>
20 #include <com/sun/star/io/XActiveDataStreamer.hpp>
21 #include <com/sun/star/lang/IllegalAccessException.hpp>
22 #include <com/sun/star/lang/IllegalArgumentException.hpp>
23 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
24 #include <com/sun/star/task/InteractionClassification.hpp>
25 #include <com/sun/star/ucb/ContentInfo.hpp>
26 #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
27 #include <com/sun/star/ucb/InsertCommandArgument2.hpp>
28 #include <com/sun/star/ucb/InteractiveBadTransferURLException.hpp>
29 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
30 #include <com/sun/star/ucb/InteractiveNetworkResolveNameException.hpp>
31 #include <com/sun/star/ucb/InteractiveNetworkConnectException.hpp>
32 #include <com/sun/star/ucb/InteractiveNetworkReadException.hpp>
33 #include <com/sun/star/ucb/MissingInputStreamException.hpp>
34 #include <com/sun/star/ucb/OpenMode.hpp>
35 #include <com/sun/star/ucb/UnsupportedCommandException.hpp>
36 #include <com/sun/star/ucb/UnsupportedDataSinkException.hpp>
37 #include <com/sun/star/ucb/UnsupportedOpenModeException.hpp>
38 #include <com/sun/star/ucb/XCommandInfo.hpp>
39 #include <com/sun/star/ucb/XDynamicResultSet.hpp>
40
41 #include <comphelper/processfactory.hxx>
42 #include <comphelper/sequence.hxx>
43 #include <cppuhelper/exc_hlp.hxx>
44 #include <cppuhelper/queryinterface.hxx>
45 #include <config_oauth2.h>
46 #include <o3tl/runtimetooustring.hxx>
47 #include <sal/log.hxx>
48 #include <tools/urlobj.hxx>
49 #include <tools/long.hxx>
50 #include <ucbhelper/cancelcommandexecution.hxx>
51 #include <ucbhelper/content.hxx>
52 #include <ucbhelper/contentidentifier.hxx>
53 #include <ucbhelper/propertyvalueset.hxx>
54 #include <ucbhelper/proxydecider.hxx>
55 #include <ucbhelper/macros.hxx>
56 #include <sax/tools/converter.hxx>
57 #include <systools/curlinit.hxx>
58
59 #include <utility>
60
61 #include "auth_provider.hxx"
62 #include "cmis_content.hxx"
63 #include "cmis_provider.hxx"
64 #include "cmis_resultset.hxx"
65 #include "cmis_strings.hxx"
66 #include "std_inputstream.hxx"
67 #include "std_outputstream.hxx"
68
69 #define OUSTR_TO_STDSTR(s) std::string( OUStringToOString( s, RTL_TEXTENCODING_UTF8 ) )
70 #define STD_TO_OUSTR( str ) OStringToOUString( str, RTL_TEXTENCODING_UTF8 )
71
72 using namespace com::sun::star;
73
74 namespace
75 {
76 util::DateTime lcl_boostToUnoTime(const boost::posix_time::ptime& boostTime)
77 {
78 util::DateTime unoTime;
79 unoTime.Year = boostTime.date().year();
80 unoTime.Month = boostTime.date().month();
81 unoTime.Day = boostTime.date().day();
82 unoTime.Hours = boostTime.time_of_day().hours();
83 unoTime.Minutes = boostTime.time_of_day().minutes();
84 unoTime.Seconds = boostTime.time_of_day().seconds();
85
86 // TODO FIXME maybe we should compile with BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
87 // to actually get nanosecond precision in boostTime?
88 // use this way rather than total_nanos to avoid overflows with 32-bit long
89 const tools::Long ticks = boostTime.time_of_day().fractional_seconds();
90 tools::Long nanoSeconds = ticks * ( 1000000000 / boost::posix_time::time_duration::ticks_per_second());
91
92 unoTime.NanoSeconds = nanoSeconds;
93
94 return unoTime;
95 }
96
97 uno::Any lcl_cmisPropertyToUno( const libcmis::PropertyPtr& pProperty )
98 {
99 uno::Any aValue;
100 switch ( pProperty->getPropertyType( )->getType( ) )
101 {
102 default:
103 case libcmis::PropertyType::String:
104 {
105 auto aCmisStrings = pProperty->getStrings( );
106 uno::Sequence< OUString > aStrings( aCmisStrings.size( ) );
107 OUString* aStringsArr = aStrings.getArray( );
108 sal_Int32 i = 0;
109 for ( const auto& rCmisStr : aCmisStrings )
110 {
111 aStringsArr[i++] = STD_TO_OUSTR( rCmisStr );
112 }
113 aValue <<= aStrings;
114 }
115 break;
116 case libcmis::PropertyType::Integer:
117 {
118 auto aCmisLongs = pProperty->getLongs( );
119 uno::Sequence< sal_Int64 > aLongs( aCmisLongs.size( ) );
120 sal_Int64* aLongsArr = aLongs.getArray( );
121 sal_Int32 i = 0;
122 for ( const auto& rCmisLong : aCmisLongs )
123 {
124 aLongsArr[i++] = rCmisLong;
125 }
126 aValue <<= aLongs;
127 }
128 break;
129 case libcmis::PropertyType::Decimal:
130 {
131 auto aCmisDoubles = pProperty->getDoubles( );
132 uno::Sequence< double > aDoubles = comphelper::containerToSequence(aCmisDoubles);
133 aValue <<= aDoubles;
134 }
135 break;
136 case libcmis::PropertyType::Bool:
137 {
138 auto aCmisBools = pProperty->getBools( );
139 uno::Sequence< sal_Bool > aBools( aCmisBools.size( ) );
140 sal_Bool* aBoolsArr = aBools.getArray( );
141 sal_Int32 i = 0;
142 for ( bool bCmisBool : aCmisBools )
143 {
144 aBoolsArr[i++] = bCmisBool;
145 }
146 aValue <<= aBools;
147 }
148 break;
149 case libcmis::PropertyType::DateTime:
150 {
151 auto aCmisTimes = pProperty->getDateTimes( );
152 uno::Sequence< util::DateTime > aTimes( aCmisTimes.size( ) );
153 util::DateTime* aTimesArr = aTimes.getArray( );
154 sal_Int32 i = 0;
155 for ( const auto& rCmisTime : aCmisTimes )
156 {
157 aTimesArr[i++] = lcl_boostToUnoTime( rCmisTime );
158 }
159 aValue <<= aTimes;
160 }
161 break;
162 }
163 return aValue;
164 }
165
166 libcmis::PropertyPtr lcl_unoToCmisProperty(const document::CmisProperty& prop )
167 {
168 libcmis::PropertyTypePtr propertyType( new libcmis::PropertyType( ) );
169
170 OUString id = prop.Id;
171 OUString name = prop.Name;
172 bool bUpdatable = prop.Updatable;
173 bool bRequired = prop.Required;
174 bool bMultiValued = prop.MultiValued;
175 bool bOpenChoice = prop.OpenChoice;
176 uno::Any value = prop.Value;
177 std::vector< std::string > values;
178
179 libcmis::PropertyType::Type type = libcmis::PropertyType::String;
180 if ( prop.Type == CMIS_TYPE_STRING )
181 {
182 uno::Sequence< OUString > seqValue;
183 value >>= seqValue;
184 std::transform(std::cbegin(seqValue), std::cend(seqValue), std::back_inserter(values),
185 [](const OUString& rValue) -> std::string { return OUSTR_TO_STDSTR( rValue ); });
186 type = libcmis::PropertyType::String;
187 }
188 else if ( prop.Type == CMIS_TYPE_BOOL )
189 {
190 uno::Sequence< sal_Bool > seqValue;
191 value >>= seqValue;
192 std::transform(std::cbegin(seqValue), std::cend(seqValue), std::back_inserter(values),
193 [](const bool nValue) -> std::string { return std::string( OString::boolean( nValue ) ); });
194 type = libcmis::PropertyType::Bool;
195 }
196 else if ( prop.Type == CMIS_TYPE_INTEGER )
197 {
198 uno::Sequence< sal_Int64 > seqValue;
199 value >>= seqValue;
200 std::transform(std::cbegin(seqValue), std::cend(seqValue), std::back_inserter(values),
201 [](const sal_Int64 nValue) -> std::string { return std::string( OString::number( nValue ) ); });
202 type = libcmis::PropertyType::Integer;
203 }
204 else if ( prop.Type == CMIS_TYPE_DECIMAL )
205 {
206 uno::Sequence< double > seqValue;
207 value >>= seqValue;
208 std::transform(std::cbegin(seqValue), std::cend(seqValue), std::back_inserter(values),
209 [](const double fValue) -> std::string { return std::string( OString::number( fValue ) ); });
210 type = libcmis::PropertyType::Decimal;
211 }
212 else if ( prop.Type == CMIS_TYPE_DATETIME )
213 {
214 uno::Sequence< util::DateTime > seqValue;
215 value >>= seqValue;
216 std::transform(std::cbegin(seqValue), std::cend(seqValue), std::back_inserter(values),
217 [](const util::DateTime& rValue) -> std::string {
218 OUStringBuffer aBuffer;
219 ::sax::Converter::convertDateTime( aBuffer, rValue, nullptr );
220 return OUSTR_TO_STDSTR( aBuffer );
221 });
222 type = libcmis::PropertyType::DateTime;
223 }
224
225 propertyType->setId( OUSTR_TO_STDSTR( id ));
226 propertyType->setDisplayName( OUSTR_TO_STDSTR( name ) );
227 propertyType->setUpdatable( bUpdatable );
228 propertyType->setRequired( bRequired );
229 propertyType->setMultiValued( bMultiValued );
230 propertyType->setOpenChoice( bOpenChoice );
231 propertyType->setType( type );
232
233 libcmis::PropertyPtr property( new libcmis::Property( std::move(propertyType),
234 std::move(values) ) );
235
236 return property;
237 }
238
239 uno::Sequence< uno::Any > generateErrorArguments( const cmis::URL & rURL )
240 {
241 uno::Sequence< uno::Any > aArguments{ uno::Any(beans::PropertyValue(
242 u"Binding URL"_ustr,
243 - 1,
244 uno::Any( rURL.getBindingUrl() ),
245 beans::PropertyState_DIRECT_VALUE )),
246 uno::Any(beans::PropertyValue(
247 u"Username"_ustr,
248 -1,
249 uno::Any( rURL.getUsername() ),
250 beans::PropertyState_DIRECT_VALUE )),
251 uno::Any(beans::PropertyValue(
252 u"Repository Id"_ustr,
253 -1,
254 uno::Any( rURL.getRepositoryId() ),
255 beans::PropertyState_DIRECT_VALUE )) };
256
257 return aArguments;
258 }
259
260 bool isOAuth2(const cmis::URL& url)
261 {
262 return url.getBindingUrl() == ONEDRIVE_BASE_URL || url.getBindingUrl() == GDRIVE_BASE_URL;
263 }
264
265 OUString getSessionId(const cmis::URL& url)
266 {
267 return isOAuth2(url) ? url.getBindingUrl() : url.getBindingUrl() + url.getRepositoryId();
268 }
269 }
270
271 namespace cmis
272 {
273 Content::Content( const uno::Reference< uno::XComponentContext >& rxContext,
274 ContentProvider *pProvider, const uno::Reference< ucb::XContentIdentifier >& Identifier,
275 libcmis::ObjectPtr pObject )
276 : ContentImplHelper( rxContext, pProvider, Identifier ),
277 m_pProvider( pProvider ),
278 m_pSession( nullptr ),
279 m_pObject(std::move( pObject )),
280 m_sURL( Identifier->getContentIdentifier( ) ),
281 m_aURL( m_sURL ),
282 m_bTransient( false ),
283 m_bIsFolder( false )
284 {
285 SAL_INFO( "ucb.ucp.cmis", "Content::Content() " << m_sURL );
286
287 m_sObjectPath = m_aURL.getObjectPath( );
288 m_sObjectId = m_aURL.getObjectId( );
289 }
290
291 Content::Content( const uno::Reference< uno::XComponentContext >& rxContext, ContentProvider *pProvider,
292 const uno::Reference< ucb::XContentIdentifier >& Identifier,
293 bool bIsFolder )
294 : ContentImplHelper( rxContext, pProvider, Identifier ),
295 m_pProvider( pProvider ),
296 m_pSession( nullptr ),
297 m_sURL( Identifier->getContentIdentifier( ) ),
298 m_aURL( m_sURL ),
299 m_bTransient( true ),
300 m_bIsFolder( bIsFolder )
301 {
302 SAL_INFO( "ucb.ucp.cmis", "Content::Content() " << m_sURL );
303
304 m_sObjectPath = m_aURL.getObjectPath( );
305 m_sObjectId = m_aURL.getObjectId( );
306 }
307
308 Content::~Content()
309 {
310 }
311
312 libcmis::Session* Content::getSession( const uno::Reference< ucb::XCommandEnvironment >& xEnv )
313 {
314 // Set the proxy if needed. We are doing that all times as the proxy data shouldn't be cached.
315 ucbhelper::InternetProxyDecider aProxyDecider( m_xContext );
316 INetURLObject aBindingUrl( m_aURL.getBindingUrl( ) );
317 const OUString sProxy = aProxyDecider.getProxy(
318 INetURLObject::GetScheme( aBindingUrl.GetProtocol( ) ), aBindingUrl.GetHost(), aBindingUrl.GetPort() );
319 libcmis::SessionFactory::setProxySettings( OUSTR_TO_STDSTR( sProxy ), std::string(), std::string(), std::string() );
320
321 // Look for a cached session, key is binding url + repo id
322 if ( nullptr == m_pSession )
323 m_pSession = m_pProvider->getSession(getSessionId(m_aURL), m_aURL.getUsername());
324
325 if ( nullptr == m_pSession )
326 {
327 m_pSession = createSession(xEnv, *m_pProvider, m_aURL,
328 m_xIdentifier->getContentIdentifier(), this);
329 }
330
331 return m_pSession;
332 }
333
334 libcmis::Session* createSession(const css::uno::Reference<css::ucb::XCommandEnvironment>& xEnv,
335 ContentProvider& rProvider, const URL& rURL,
336 const OUString& rContentId,
337 const css::uno::Reference<css::ucb::XContent>& xContext)
338 {
339 // init libcurl callback
340 libcmis::SessionFactory::setCurlInitProtocolsFunction(&::InitCurl_easy);
341
342 // Get the auth credentials
343 AuthProvider aAuthProvider(xEnv, rContentId, rURL.getBindingUrl());
344 AuthProvider::setXEnv( xEnv );
345
346 auto rUsername = OUSTR_TO_STDSTR( rURL.getUsername( ) );
347 auto rPassword = OUSTR_TO_STDSTR( rURL.getPassword( ) );
348
349 bool bSkipInitialPWAuth = false;
350 if (isOAuth2(rURL))
351 {
352 // skip the initial username and pw-auth prompt
353 bSkipInitialPWAuth = true;
354 rPassword = aAuthProvider.getRefreshToken(rUsername);
355 }
356
357 while (true)
358 {
359 if (bSkipInitialPWAuth || aAuthProvider.authenticationQuery(rUsername, rPassword))
360 {
361 // Initiate a CMIS session and register it as we found nothing
362 libcmis::OAuth2DataPtr oauth2Data;
363 if ( rURL.getBindingUrl( ) == GDRIVE_BASE_URL )
364 {
365 // reset the skip, so user gets a chance to cancel
366 bSkipInitialPWAuth = false;
367 libcmis::SessionFactory::setOAuth2AuthCodeProvider(AuthProvider::copyWebAuthCodeFallback);
368 oauth2Data = boost::make_shared<libcmis::OAuth2Data>(
369 GDRIVE_AUTH_URL, GDRIVE_TOKEN_URL,
370 GDRIVE_SCOPE, GDRIVE_REDIRECT_URI,
371 GDRIVE_CLIENT_ID, GDRIVE_CLIENT_SECRET );
372 }
373 if ( rURL.getBindingUrl().startsWith( ALFRESCO_CLOUD_BASE_URL ) )
374 oauth2Data = boost::make_shared<libcmis::OAuth2Data>(
375 ALFRESCO_CLOUD_AUTH_URL, ALFRESCO_CLOUD_TOKEN_URL,
376 ALFRESCO_CLOUD_SCOPE, ALFRESCO_CLOUD_REDIRECT_URI,
377 ALFRESCO_CLOUD_CLIENT_ID, ALFRESCO_CLOUD_CLIENT_SECRET );
378 if ( rURL.getBindingUrl( ) == ONEDRIVE_BASE_URL )
379 {
380 // reset the skip, so user gets a chance to cancel
381 bSkipInitialPWAuth = false;
382 libcmis::SessionFactory::setOAuth2AuthCodeProvider(AuthProvider::copyWebAuthCodeFallback);
383 oauth2Data = boost::make_shared<libcmis::OAuth2Data>(
384 ONEDRIVE_AUTH_URL, ONEDRIVE_TOKEN_URL,
385 ONEDRIVE_SCOPE, ONEDRIVE_REDIRECT_URI,
386 ONEDRIVE_CLIENT_ID, ONEDRIVE_CLIENT_SECRET );
387 }
388 try
389 {
390 std::unique_ptr<libcmis::Session> pSession(libcmis::SessionFactory::createSession(
391 OUSTR_TO_STDSTR( rURL.getBindingUrl( ) ),
392 rUsername, rPassword, OUSTR_TO_STDSTR( rURL.getRepositoryId( ) ), false, std::move(oauth2Data) ));
393
394 if ( pSession == nullptr )
395 {
396 // Fail: session was not created
397 ucbhelper::cancelCommandExecution(
398 ucb::IOErrorCode_INVALID_DEVICE,
399 generateErrorArguments(rURL),
400 xEnv);
401 }
402 else if (pSession->getRepository() == nullptr
403 && xContext->getContentType() != CMIS_REPO_TYPE)
404 {
405 // Fail: no repository or repository is invalid
406 ucbhelper::cancelCommandExecution(
407 ucb::IOErrorCode_INVALID_DEVICE,
408 generateErrorArguments(rURL),
409 xEnv,
410 u"error accessing a repository"_ustr);
411 }
412 else
413 {
414 rProvider.registerSession(getSessionId(rURL), rURL.getUsername(), pSession.get());
415 if (isOAuth2(rURL))
416 {
417 aAuthProvider.storeRefreshToken(rUsername, rPassword,
418 pSession->getRefreshToken());
419 }
420 }
421
422 return pSession.release();
423 }
424 catch( const libcmis::Exception & e )
425 {
426 if (e.getType() == "dnsFailed")
427 {
428 uno::Any ex;
429 ex <<= ucb::InteractiveNetworkResolveNameException(
430 OStringToOUString(e.what(), RTL_TEXTENCODING_UTF8),
431 xContext,
432 task::InteractionClassification_ERROR,
433 rURL.getBindingUrl());
434 ucbhelper::cancelCommandExecution(ex, xEnv);
435 }
436 else if (e.getType() == "connectFailed" || e.getType() == "connectTimeout")
437 {
438 uno::Any ex;
439 ex <<= ucb::InteractiveNetworkConnectException(
440 OStringToOUString(e.what(), RTL_TEXTENCODING_UTF8),
441 xContext,
442 task::InteractionClassification_ERROR,
443 rURL.getBindingUrl());
444 ucbhelper::cancelCommandExecution(ex, xEnv);
445 }
446 else if (e.getType() == "transferFailed")
447 {
448 uno::Any ex;
449 ex <<= ucb::InteractiveNetworkReadException(
450 OStringToOUString(e.what(), RTL_TEXTENCODING_UTF8),
451 xContext,
452 task::InteractionClassification_ERROR,
453 rURL.getBindingUrl());
454 ucbhelper::cancelCommandExecution(ex, xEnv);
455 }
456 else if (e.getType() == "permissionDenied")
457 {
458 if (isOAuth2(rURL))
459 {
460 // For OAuth2, this means user cancelled - don't try again
461 ucbhelper::cancelCommandExecution(ucb::IOErrorCode_ABORT, {}, xEnv);
462 }
463 }
464 else
465 {
466 SAL_INFO("ucb.ucp.cmis", "Unexpected libcmis exception: " << e.what());
467 throw;
468 }
469 }
470 }
471 else
472 {
473 // Silently fail as the user cancelled the authentication
474 ucbhelper::cancelCommandExecution(
475 ucb::IOErrorCode_ABORT,
476 uno::Sequence< uno::Any >( 0 ),
477 xEnv );
478 throw uno::RuntimeException( );
479 }
480 }
481 }
482
483 libcmis::ObjectTypePtr const & Content::getObjectType( const uno::Reference< ucb::XCommandEnvironment >& xEnv )
484 {
485 if ( nullptr == m_pObjectType.get( ) && m_bTransient )
486 {
487 const std::string_view typeId = m_bIsFolder ? "cmis:folder" : "cmis:document";
488 // The type to create needs to be fetched from the possible children types
489 // defined in the parent folder. Then, we'll pick up the first one we find matching
490 // cmis:folder or cmis:document (depending what we need to create).
491 // The easy case will work in most cases, but not on some servers (like Lotus Live)
492 libcmis::Folder* pParent = nullptr;
493 bool bTypeRestricted = false;
494 try
495 {
496 pParent = dynamic_cast< libcmis::Folder* >( getObject( xEnv ).get( ) );
497 }
498 catch ( const libcmis::Exception& )
499 {
500 }
501
502 if ( pParent )
503 {
504 std::map< std::string, libcmis::PropertyPtr >& aProperties = pParent->getProperties( );
505 std::map< std::string, libcmis::PropertyPtr >::iterator it = aProperties.find( "cmis:allowedChildObjectTypeIds" );
506 if ( it != aProperties.end( ) )
507 {
508 libcmis::PropertyPtr pProperty = it->second;
509 if ( pProperty )
510 {
511 std::vector< std::string > typesIds = pProperty->getStrings( );
512 for ( const auto& rType : typesIds )
513 {
514 bTypeRestricted = true;
515 libcmis::ObjectTypePtr type = getSession( xEnv )->getType( rType );
516
517 // FIXME Improve performances by adding getBaseTypeId( ) method to libcmis
518 if ( type->getBaseType( )->getId( ) == typeId )
519 {
520 m_pObjectType = std::move(type);
521 break;
522 }
523 }
524 }
525 }
526 }
527
528 if ( !bTypeRestricted )
529 m_pObjectType = getSession( xEnv )->getType(std::string(typeId));
530 }
531 return m_pObjectType;
532 }
533
534
535 libcmis::ObjectPtr const & Content::getObject( const uno::Reference< ucb::XCommandEnvironment >& xEnv )
536 {
537 // can't get the session for some reason
538 // the recent file opening at start up is an example.
539 try
540 {
541 if ( !getSession( xEnv ) )
542 return m_pObject;
543 }
544 catch ( uno::RuntimeException& )
545 {
546 return m_pObject;
547 }
548 if ( !m_pObject.get() )
549 {
550 if ( !m_sObjectId.isEmpty( ) )
551 {
552 try
553 {
554 m_pObject = getSession( xEnv )->getObject( OUSTR_TO_STDSTR( m_sObjectId ) );
555 }
556 catch ( const libcmis::Exception& )
557 {
558 SAL_INFO( "ucb.ucp.cmis", "object: " << OUSTR_TO_STDSTR(m_sObjectId));
559 throw libcmis::Exception( "Object not found" );
560 }
561 }
562 else if (!(m_sObjectPath.isEmpty() || m_sObjectPath == "/"))
563 {
564 try
565 {
566 m_pObject = getSession( xEnv )->getObjectByPath( OUSTR_TO_STDSTR( m_sObjectPath ) );
567 }
568 catch ( const libcmis::Exception& )
569 {
570 // In some cases, getting the object from the path doesn't work,
571 // but getting the parent from its path and the get the child in the list is OK.
572 // It's weird, but needed to handle case where the path isn't the folders/files
573 // names separated by '/' (as in Lotus Live)
574 INetURLObject aParentUrl( m_sURL );
575 std::string sName = OUSTR_TO_STDSTR( aParentUrl.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset ) );
576 aParentUrl.removeSegment( );
577 OUString sParentUrl = aParentUrl.GetMainURL( INetURLObject::DecodeMechanism::NONE );
578 // Avoid infinite recursion if sParentUrl == m_sURL
579 if (sParentUrl != m_sURL)
580 {
581 rtl::Reference<Content> xParent(new Content(m_xContext, m_pProvider, new ucbhelper::ContentIdentifier(sParentUrl)));
582 libcmis::FolderPtr pParentFolder = boost::dynamic_pointer_cast< libcmis::Folder >(xParent->getObject(xEnv));
583 if (pParentFolder)
584 {
585 std::vector< libcmis::ObjectPtr > children = pParentFolder->getChildren();
586 auto it = std::find_if(children.begin(), children.end(),
587 [&sName](const libcmis::ObjectPtr& rChild) { return rChild->getName() == sName; });
588 if (it != children.end())
589 m_pObject = *it;
590 }
591 }
592
593 if ( !m_pObject )
594 throw libcmis::Exception( "Object not found" );
595 }
596 }
597 else
598 {
599 m_pObject = getSession( xEnv )->getRootFolder( );
600 m_sObjectPath = "/";
601 m_sObjectId = OUString( );
602 }
603 }
604
605 return m_pObject;
606 }
607
608 bool Content::isFolder(const uno::Reference< ucb::XCommandEnvironment >& xEnv )
609 {
610 bool bIsFolder = false;
611 try
612 {
613 libcmis::ObjectPtr obj = getObject( xEnv );
614 if ( obj )
615 bIsFolder = obj->getBaseType( ) == "cmis:folder";
616 }
617 catch ( const libcmis::Exception& e )
618 {
619 SAL_INFO( "ucb.ucp.cmis", "Unexpected libcmis exception: " << e.what( ) );
620
621 ucbhelper::cancelCommandExecution(
622 ucb::IOErrorCode_GENERAL,
623 uno::Sequence< uno::Any >( 0 ),
624 xEnv,
625 OUString::createFromAscii( e.what( ) ) );
626
627 }
628 return bIsFolder;
629 }
630
631 uno::Any Content::getBadArgExcept()
632 {
633 return uno::Any( lang::IllegalArgumentException(
634 u"Wrong argument type!"_ustr,
635 getXWeak(), -1) );
636 }
637
638 libcmis::ObjectPtr Content::updateProperties(
639 const uno::Any& iCmisProps,
640 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
641 {
642 // Convert iCmisProps to Cmis Properties;
643 uno::Sequence< document::CmisProperty > aPropsSeq;
644 iCmisProps >>= aPropsSeq;
645 std::map< std::string, libcmis::PropertyPtr > aProperties;
646
647 for (const auto& rProp : aPropsSeq)
648 {
649 std::string id = OUSTR_TO_STDSTR( rProp.Id );
650 libcmis::PropertyPtr prop = lcl_unoToCmisProperty( rProp );
651 aProperties.insert( std::pair<std::string, libcmis::PropertyPtr>( id, prop ) );
652 }
653 libcmis::ObjectPtr updateObj;
654 try
655 {
656 updateObj = getObject( xEnv )->updateProperties( aProperties );
657 }
658 catch ( const libcmis::Exception& e )
659 {
660 SAL_INFO( "ucb.ucp.cmis", "Unexpected libcmis exception: "<< e.what( ) );
661 }
662
663 return updateObj;
664 }
665
666 uno::Reference< sdbc::XRow > Content::getPropertyValues(
667 const uno::Sequence< beans::Property >& rProperties,
668 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
669 {
670 rtl::Reference< ::ucbhelper::PropertyValueSet > xRow = new ::ucbhelper::PropertyValueSet( m_xContext );
671
672 for( const beans::Property& rProp : rProperties )
673 {
674 try
675 {
676 if ( rProp.Name == "IsDocument" )
677 {
678 try
679 {
680 libcmis::ObjectPtr obj = getObject( xEnv );
681 if ( obj )
682 xRow->appendBoolean( rProp, obj->getBaseType( ) == "cmis:document" );
683 }
684 catch ( const libcmis::Exception& )
685 {
686 if ( m_pObjectType.get( ) )
687 xRow->appendBoolean( rProp, getObjectType( xEnv )->getBaseType()->getId( ) == "cmis:document" );
688 else
689 xRow->appendVoid( rProp );
690 }
691 }
692 else if ( rProp.Name == "IsFolder" )
693 {
694 try
695 {
696 libcmis::ObjectPtr obj = getObject( xEnv );
697 if ( obj )
698 xRow->appendBoolean( rProp, obj->getBaseType( ) == "cmis:folder" );
699 else
700 xRow->appendBoolean( rProp, false );
701 }
702 catch ( const libcmis::Exception& )
703 {
704 if ( m_pObjectType.get( ) )
705 xRow->appendBoolean( rProp, getObjectType( xEnv )->getBaseType()->getId( ) == "cmis:folder" );
706 else
707 xRow->appendVoid( rProp );
708 }
709 }
710 else if ( rProp.Name == "Title" )
711 {
712 OUString sTitle;
713 try
714 {
715 sTitle = STD_TO_OUSTR( getObject( xEnv )->getName() );
716 }
717 catch ( const libcmis::Exception& )
718 {
719 if ( !m_pObjectProps.empty() )
720 {
721 std::map< std::string, libcmis::PropertyPtr >::iterator it = m_pObjectProps.find( "cmis:name" );
722 if ( it != m_pObjectProps.end( ) )
723 {
724 std::vector< std::string > values = it->second->getStrings( );
725 if ( !values.empty() )
726 sTitle = STD_TO_OUSTR( values.front( ) );
727 }
728 }
729 }
730
731 // Nothing worked... get it from the path
732 if ( sTitle.isEmpty( ) )
733 {
734 OUString sPath = m_sObjectPath;
735
736 // Get rid of the trailing slash problem
737 if ( sPath.endsWith("/") )
738 sPath = sPath.copy( 0, sPath.getLength() - 1 );
739
740 // Get the last segment
741 sal_Int32 nPos = sPath.lastIndexOf( '/' );
742 if ( nPos >= 0 )
743 sTitle = sPath.copy( nPos + 1 );
744 }
745
746 if ( !sTitle.isEmpty( ) )
747 xRow->appendString( rProp, sTitle );
748 else
749 xRow->appendVoid( rProp );
750 }
751 else if ( rProp.Name == "ObjectId" )
752 {
753 OUString sId;
754 try
755 {
756 sId = STD_TO_OUSTR( getObject( xEnv )->getId() );
757 }
758 catch ( const libcmis::Exception& )
759 {
760 if ( !m_pObjectProps.empty() )
761 {
762 std::map< std::string, libcmis::PropertyPtr >::iterator it = m_pObjectProps.find( "cmis:objectId" );
763 if ( it != m_pObjectProps.end( ) )
764 {
765 std::vector< std::string > values = it->second->getStrings( );
766 if ( !values.empty() )
767 sId = STD_TO_OUSTR( values.front( ) );
768 }
769 }
770 }
771
772 if ( !sId.isEmpty( ) )
773 xRow->appendString( rProp, sId );
774 else
775 xRow->appendVoid( rProp );
776 }
777 else if ( rProp.Name == "TitleOnServer" )
778 {
779 xRow->appendString( rProp, m_sObjectPath);
780 }
781 else if ( rProp.Name == "IsReadOnly" )
782 {
783 boost::shared_ptr< libcmis::AllowableActions > allowableActions = getObject( xEnv )->getAllowableActions( );
784 bool bReadOnly = false;
785 if ( !allowableActions->isAllowed( libcmis::ObjectAction::SetContentStream ) &&
786 !allowableActions->isAllowed( libcmis::ObjectAction::CheckIn ) )
787 bReadOnly = true;
788
789 xRow->appendBoolean( rProp, bReadOnly );
790 }
791 else if ( rProp.Name == "DateCreated" )
792 {
793 util::DateTime aTime = lcl_boostToUnoTime( getObject( xEnv )->getCreationDate( ) );
794 xRow->appendTimestamp( rProp, aTime );
795 }
796 else if ( rProp.Name == "DateModified" )
797 {
798 util::DateTime aTime = lcl_boostToUnoTime( getObject( xEnv )->getLastModificationDate( ) );
799 xRow->appendTimestamp( rProp, aTime );
800 }
801 else if ( rProp.Name == "Size" )
802 {
803 try
804 {
805 libcmis::Document* document = dynamic_cast< libcmis::Document* >( getObject( xEnv ).get( ) );
806 if ( nullptr != document )
807 xRow->appendLong( rProp, document->getContentLength() );
808 else
809 xRow->appendVoid( rProp );
810 }
811 catch ( const libcmis::Exception& )
812 {
813 xRow->appendVoid( rProp );
814 }
815 }
816 else if ( rProp.Name == "CreatableContentsInfo" )
817 {
818 xRow->appendObject( rProp, uno::Any( queryCreatableContentsInfo( xEnv ) ) );
819 }
820 else if ( rProp.Name == "MediaType" )
821 {
822 try
823 {
824 libcmis::Document* document = dynamic_cast< libcmis::Document* >( getObject( xEnv ).get( ) );
825 if ( nullptr != document )
826 xRow->appendString( rProp, STD_TO_OUSTR( document->getContentType() ) );
827 else
828 xRow->appendVoid( rProp );
829 }
830 catch ( const libcmis::Exception& )
831 {
832 xRow->appendVoid( rProp );
833 }
834 }
835 else if ( rProp.Name == "IsVolume" )
836 {
837 xRow->appendBoolean( rProp, false );
838 }
839 else if ( rProp.Name == "IsRemote" )
840 {
841 xRow->appendBoolean( rProp, false );
842 }
843 else if ( rProp.Name == "IsRemoveable" )
844 {
845 xRow->appendBoolean( rProp, false );
846 }
847 else if ( rProp.Name == "IsFloppy" )
848 {
849 xRow->appendBoolean( rProp, false );
850 }
851 else if ( rProp.Name == "IsCompactDisc" )
852 {
853 xRow->appendBoolean( rProp, false );
854 }
855 else if ( rProp.Name == "IsHidden" )
856 {
857 xRow->appendBoolean( rProp, false );
858 }
859 else if ( rProp.Name == "TargetURL" )
860 {
861 xRow->appendString( rProp, u""_ustr );
862 }
863 else if ( rProp.Name == "BaseURI" )
864 {
865 xRow->appendString( rProp, m_aURL.getBindingUrl( ) );
866 }
867 else if ( rProp.Name == "CmisProperties" )
868 {
869 try
870 {
871 libcmis::ObjectPtr object = getObject( xEnv );
872 std::map< std::string, libcmis::PropertyPtr >& aProperties = object->getProperties( );
873 uno::Sequence< document::CmisProperty > aCmisProperties( aProperties.size( ) );
874 document::CmisProperty* pCmisProps = aCmisProperties.getArray( );
875 sal_Int32 i = 0;
876 for ( const auto& [sId, rProperty] : aProperties )
877 {
878 auto sDisplayName = rProperty->getPropertyType()->getDisplayName( );
879 bool bUpdatable = rProperty->getPropertyType()->isUpdatable( );
880 bool bRequired = rProperty->getPropertyType()->isRequired( );
881 bool bMultiValued = rProperty->getPropertyType()->isMultiValued();
882 bool bOpenChoice = rProperty->getPropertyType()->isOpenChoice();
883
884 pCmisProps[i].Id = STD_TO_OUSTR( sId );
885 pCmisProps[i].Name = STD_TO_OUSTR( sDisplayName );
886 pCmisProps[i].Updatable = bUpdatable;
887 pCmisProps[i].Required = bRequired;
888 pCmisProps[i].MultiValued = bMultiValued;
889 pCmisProps[i].OpenChoice = bOpenChoice;
890 pCmisProps[i].Value = lcl_cmisPropertyToUno( rProperty );
891 switch ( rProperty->getPropertyType( )->getType( ) )
892 {
893 default:
894 case libcmis::PropertyType::String:
895 pCmisProps[i].Type = CMIS_TYPE_STRING;
896 break;
897 case libcmis::PropertyType::Integer:
898 pCmisProps[i].Type = CMIS_TYPE_INTEGER;
899 break;
900 case libcmis::PropertyType::Decimal:
901 pCmisProps[i].Type = CMIS_TYPE_DECIMAL;
902 break;
903 case libcmis::PropertyType::Bool:
904 pCmisProps[i].Type = CMIS_TYPE_BOOL;
905 break;
906 case libcmis::PropertyType::DateTime:
907 pCmisProps[i].Type = CMIS_TYPE_DATETIME;
908 break;
909 }
910 ++i;
911 }
912 xRow->appendObject( rProp.Name, uno::Any( aCmisProperties ) );
913 }
914 catch ( const libcmis::Exception& )
915 {
916 xRow->appendVoid( rProp );
917 }
918 }
919 else if ( rProp.Name == "IsVersionable" )
920 {
921 try
922 {
923 libcmis::ObjectPtr object = getObject( xEnv );
924 bool bIsVersionable = object->getTypeDescription( )->isVersionable( );
925 xRow->appendBoolean( rProp, bIsVersionable );
926 }
927 catch ( const libcmis::Exception& )
928 {
929 xRow->appendVoid( rProp );
930 }
931 }
932 else if ( rProp.Name == "CanCheckOut" )
933 {
934 try
935 {
936 libcmis::ObjectPtr pObject = getObject( xEnv );
937 libcmis::AllowableActionsPtr aAllowables = pObject->getAllowableActions( );
938 bool bAllowed = false;
939 if ( aAllowables )
940 {
941 bAllowed = aAllowables->isAllowed( libcmis::ObjectAction::CheckOut );
942 }
943 xRow->appendBoolean( rProp, bAllowed );
944 }
945 catch ( const libcmis::Exception& )
946 {
947 xRow->appendVoid( rProp );
948 }
949 }
950 else if ( rProp.Name == "CanCancelCheckOut" )
951 {
952 try
953 {
954 libcmis::ObjectPtr pObject = getObject( xEnv );
955 libcmis::AllowableActionsPtr aAllowables = pObject->getAllowableActions( );
956 bool bAllowed = false;
957 if ( aAllowables )
958 {
959 bAllowed = aAllowables->isAllowed( libcmis::ObjectAction::CancelCheckOut );
960 }
961 xRow->appendBoolean( rProp, bAllowed );
962 }
963 catch ( const libcmis::Exception& )
964 {
965 xRow->appendVoid( rProp );
966 }
967 }
968 else if ( rProp.Name == "CanCheckIn" )
969 {
970 try
971 {
972 libcmis::ObjectPtr pObject = getObject( xEnv );
973 libcmis::AllowableActionsPtr aAllowables = pObject->getAllowableActions( );
974 bool bAllowed = false;
975 if ( aAllowables )
976 {
977 bAllowed = aAllowables->isAllowed( libcmis::ObjectAction::CheckIn );
978 }
979 xRow->appendBoolean( rProp, bAllowed );
980 }
981 catch ( const libcmis::Exception& )
982 {
983 xRow->appendVoid( rProp );
984 }
985 }
986 else
987 SAL_INFO( "ucb.ucp.cmis", "Looking for unsupported property " << rProp.Name );
988 }
989 catch (const libcmis::Exception&)
990 {
991 xRow->appendVoid( rProp );
992 }
993 }
994
995 return xRow;
996 }
997
998 uno::Any Content::open(const ucb::OpenCommandArgument2 & rOpenCommand,
999 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
1000 {
1001 bool bIsFolder = isFolder( xEnv );
1002
1003 // Handle the case of the non-existing file
1004 if ( !getObject( xEnv ) )
1005 {
1006 uno::Sequence< uno::Any > aArgs{ uno::Any(m_xIdentifier->getContentIdentifier()) };
1007 uno::Any aErr(
1008 ucb::InteractiveAugmentedIOException(OUString(), getXWeak(),
1009 task::InteractionClassification_ERROR,
1010 bIsFolder ? ucb::IOErrorCode_NOT_EXISTING_PATH : ucb::IOErrorCode_NOT_EXISTING, aArgs)
1011 );
1012
1013 ucbhelper::cancelCommandExecution(aErr, xEnv);
1014 }
1015
1016 uno::Any aRet;
1017
1018 bool bOpenFolder = (
1019 ( rOpenCommand.Mode == ucb::OpenMode::ALL ) ||
1020 ( rOpenCommand.Mode == ucb::OpenMode::FOLDERS ) ||
1021 ( rOpenCommand.Mode == ucb::OpenMode::DOCUMENTS )
1022 );
1023
1024 if ( bOpenFolder && bIsFolder )
1025 {
1026 uno::Reference< ucb::XDynamicResultSet > xSet
1027 = new DynamicResultSet(m_xContext, this, rOpenCommand, xEnv );
1028 aRet <<= xSet;
1029 }
1030 else if ( rOpenCommand.Sink.is() )
1031 {
1032 if (
1033 ( rOpenCommand.Mode == ucb::OpenMode::DOCUMENT_SHARE_DENY_NONE ) ||
1034 ( rOpenCommand.Mode == ucb::OpenMode::DOCUMENT_SHARE_DENY_WRITE )
1035 )
1036 {
1037 ucbhelper::cancelCommandExecution(
1038 uno::Any ( ucb::UnsupportedOpenModeException
1039 ( OUString(), getXWeak(),
1040 sal_Int16( rOpenCommand.Mode ) ) ),
1041 xEnv );
1042 }
1043
1044 if ( !feedSink( rOpenCommand.Sink, xEnv ) )
1045 {
1046 // Note: rOpenCommand.Sink may contain an XStream
1047 // implementation. Support for this type of
1048 // sink is optional...
1049 SAL_INFO( "ucb.ucp.cmis", "Failed to copy data to sink" );
1050
1051 ucbhelper::cancelCommandExecution(
1052 uno::Any (ucb::UnsupportedDataSinkException
1053 ( OUString(), getXWeak(),
1054 rOpenCommand.Sink ) ),
1055 xEnv );
1056 }
1057 }
1058 else
1059 SAL_INFO( "ucb.ucp.cmis", "Open falling through ..." );
1060
1061 return aRet;
1062 }
1063
1064 OUString Content::checkIn( const ucb::CheckinArgument& rArg,
1065 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
1066 {
1067 ucbhelper::Content aSourceContent( rArg.SourceURL, xEnv, comphelper::getProcessComponentContext( ) );
1068 uno::Reference< io::XInputStream > xIn = aSourceContent.openStream( );
1069
1070 libcmis::ObjectPtr object;
1071 try
1072 {
1073 object = getObject( xEnv );
1074 }
1075 catch ( const libcmis::Exception& e )
1076 {
1077 SAL_INFO( "ucb.ucp.cmis", "Unexpected libcmis exception: " << e.what( ) );
1078 ucbhelper::cancelCommandExecution(
1079 ucb::IOErrorCode_GENERAL,
1080 uno::Sequence< uno::Any >( 0 ),
1081 xEnv,
1082 OUString::createFromAscii( e.what() ) );
1083 }
1084
1085 libcmis::Document* pPwc = dynamic_cast< libcmis::Document* >( object.get( ) );
1086 if ( !pPwc )
1087 {
1088 ucbhelper::cancelCommandExecution(
1089 ucb::IOErrorCode_GENERAL,
1090 uno::Sequence< uno::Any >( 0 ),
1091 xEnv,
1092 u"Checkin only supported by documents"_ustr );
1093 }
1094
1095 boost::shared_ptr< std::ostream > pOut( new std::ostringstream ( std::ios_base::binary | std::ios_base::in | std::ios_base::out ) );
1096 uno::Reference < io::XOutputStream > xOutput = new StdOutputStream( pOut );
1097 copyData( xIn, xOutput );
1098
1099 std::map< std::string, libcmis::PropertyPtr > newProperties;
1100 libcmis::DocumentPtr pDoc;
1101
1102 try
1103 {
1104 pDoc = pPwc->checkIn( rArg.MajorVersion, OUSTR_TO_STDSTR( rArg.VersionComment ), newProperties,
1105 std::move(pOut), OUSTR_TO_STDSTR( rArg.MimeType ), OUSTR_TO_STDSTR( rArg.NewTitle ) );
1106 }
1107 catch ( const libcmis::Exception& e )
1108 {
1109 SAL_INFO( "ucb.ucp.cmis", "Unexpected libcmis exception: " << e.what( ) );
1110 ucbhelper::cancelCommandExecution(
1111 ucb::IOErrorCode_GENERAL,
1112 uno::Sequence< uno::Any >( 0 ),
1113 xEnv,
1114 OUString::createFromAscii( e.what() ) );
1115 }
1116
1117 // Get the URL and send it back as a result
1118 URL aCmisUrl( m_sURL );
1119 std::vector< std::string > aPaths = pDoc->getPaths( );
1120 if ( !aPaths.empty() )
1121 {
1122 aCmisUrl.setObjectPath(STD_TO_OUSTR(aPaths.front()));
1123 }
1124 else
1125 {
1126 // We may have unfiled document depending on the server, those
1127 // won't have any path, use their ID instead
1128 aCmisUrl.setObjectId(STD_TO_OUSTR(pDoc->getId()));
1129 }
1130 return aCmisUrl.asString( );
1131 }
1132
1133 OUString Content::checkOut( const uno::Reference< ucb::XCommandEnvironment > & xEnv )
1134 {
1135 OUString aRet;
1136 try
1137 {
1138 // Checkout the document if possible
1139 libcmis::DocumentPtr pDoc = boost::dynamic_pointer_cast< libcmis::Document >( getObject( xEnv ) );
1140 if ( pDoc.get( ) == nullptr )
1141 {
1142 ucbhelper::cancelCommandExecution(
1143 ucb::IOErrorCode_GENERAL,
1144 uno::Sequence< uno::Any >( 0 ),
1145 xEnv,
1146 u"Checkout only supported by documents"_ustr );
1147 }
1148 libcmis::DocumentPtr pPwc = pDoc->checkOut( );
1149
1150 // Compute the URL of the Private Working Copy (PWC)
1151 URL aCmisUrl( m_sURL );
1152 std::vector< std::string > aPaths = pPwc->getPaths( );
1153 if ( !aPaths.empty() )
1154 {
1155 aCmisUrl.setObjectPath(STD_TO_OUSTR(aPaths.front()));
1156 }
1157 else
1158 {
1159 // We may have unfiled PWC depending on the server, those
1160 // won't have any path, use their ID instead
1161 auto sId = pPwc->getId( );
1162 aCmisUrl.setObjectId( STD_TO_OUSTR( sId ) );
1163 }
1164 aRet = aCmisUrl.asString( );
1165 }
1166 catch ( const libcmis::Exception& e )
1167 {
1168 SAL_INFO( "ucb.ucp.cmis", "Unexpected libcmis exception: " << e.what( ) );
1169 ucbhelper::cancelCommandExecution(
1170 ucb::IOErrorCode_GENERAL,
1171 uno::Sequence< uno::Any >( 0 ),
1172 xEnv,
1173 o3tl::runtimeToOUString(e.what()));
1174 }
1175 return aRet;
1176 }
1177
1178 OUString Content::cancelCheckOut( const uno::Reference< ucb::XCommandEnvironment > & xEnv )
1179 {
1180 OUString aRet;
1181 try
1182 {
1183 libcmis::DocumentPtr pPwc = boost::dynamic_pointer_cast< libcmis::Document >( getObject( xEnv ) );
1184 if ( pPwc.get( ) == nullptr )
1185 {
1186 ucbhelper::cancelCommandExecution(
1187 ucb::IOErrorCode_GENERAL,
1188 uno::Sequence< uno::Any >( 0 ),
1189 xEnv,
1190 u"CancelCheckout only supported by documents"_ustr );
1191 }
1192 pPwc->cancelCheckout( );
1193
1194 // Get the Original document (latest version)
1195 std::vector< libcmis::DocumentPtr > aVersions = pPwc->getAllVersions( );
1196 for ( const auto& rVersion : aVersions )
1197 {
1198 libcmis::DocumentPtr pVersion = rVersion;
1199 std::map< std::string, libcmis::PropertyPtr > aProps = pVersion->getProperties( );
1200 bool bIsLatestVersion = false;
1201 std::map< std::string, libcmis::PropertyPtr >::iterator propIt = aProps.find( std::string( "cmis:isLatestVersion" ) );
1202 if ( propIt != aProps.end( ) && !propIt->second->getBools( ).empty( ) )
1203 {
1204 bIsLatestVersion = propIt->second->getBools( ).front( );
1205 }
1206
1207 if ( bIsLatestVersion )
1208 {
1209 // Compute the URL of the Document
1210 URL aCmisUrl( m_sURL );
1211 std::vector< std::string > aPaths = pVersion->getPaths( );
1212 if ( !aPaths.empty() )
1213 {
1214 aCmisUrl.setObjectPath(STD_TO_OUSTR(aPaths.front()));
1215 }
1216 else
1217 {
1218 // We may have unfiled doc depending on the server, those
1219 // won't have any path, use their ID instead
1220 auto sId = pVersion->getId( );
1221 aCmisUrl.setObjectId( STD_TO_OUSTR( sId ) );
1222 }
1223 aRet = aCmisUrl.asString( );
1224 break;
1225 }
1226 }
1227 }
1228 catch ( const libcmis::Exception& e )
1229 {
1230 SAL_INFO( "ucb.ucp.cmis", "Unexpected libcmis exception: " << e.what( ) );
1231 ucbhelper::cancelCommandExecution(
1232 ucb::IOErrorCode_GENERAL,
1233 uno::Sequence< uno::Any >( 0 ),
1234 xEnv,
1235 o3tl::runtimeToOUString(e.what()));
1236 }
1237 return aRet;
1238 }
1239
1240 uno::Sequence< document::CmisVersion> Content::getAllVersions( const uno::Reference< ucb::XCommandEnvironment > & xEnv )
1241 {
1242 try
1243 {
1244 // get the document
1245 libcmis::DocumentPtr pDoc = boost::dynamic_pointer_cast< libcmis::Document >( getObject( xEnv ) );
1246 if ( pDoc.get( ) == nullptr )
1247 {
1248 ucbhelper::cancelCommandExecution(
1249 ucb::IOErrorCode_GENERAL,
1250 uno::Sequence< uno::Any >( 0 ),
1251 xEnv,
1252 u"Can not get the document"_ustr );
1253 }
1254 std::vector< libcmis::DocumentPtr > aCmisVersions = pDoc->getAllVersions( );
1255 uno::Sequence< document::CmisVersion > aVersions( aCmisVersions.size( ) );
1256 auto aVersionsRange = asNonConstRange(aVersions);
1257 int i = 0;
1258 for ( const auto& rVersion : aCmisVersions )
1259 {
1260 libcmis::DocumentPtr pVersion = rVersion;
1261 aVersionsRange[i].Id = STD_TO_OUSTR( pVersion->getId( ) );
1262 aVersionsRange[i].Author = STD_TO_OUSTR( pVersion->getCreatedBy( ) );
1263 aVersionsRange[i].TimeStamp = lcl_boostToUnoTime( pVersion->getLastModificationDate( ) );
1264 aVersionsRange[i].Comment = STD_TO_OUSTR( pVersion->getStringProperty("cmis:checkinComment") );
1265 ++i;
1266 }
1267 return aVersions;
1268 }
1269 catch ( const libcmis::Exception& e )
1270 {
1271 SAL_INFO( "ucb.ucp.cmis", "Unexpected libcmis exception: " << e.what( ) );
1272 ucbhelper::cancelCommandExecution(
1273 ucb::IOErrorCode_GENERAL,
1274 uno::Sequence< uno::Any >( 0 ),
1275 xEnv,
1276 o3tl::runtimeToOUString(e.what()));
1277 }
1278 return uno::Sequence< document::CmisVersion > ( );
1279 }
1280
1281 void Content::transfer( const ucb::TransferInfo& rTransferInfo,
1282 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
1283 {
1284 // If the source isn't on the same CMIS repository, then simply copy
1285 INetURLObject aSourceUrl( rTransferInfo.SourceURL );
1286 if ( aSourceUrl.GetProtocol() != INetProtocol::Cmis )
1287 {
1288 OUString sSrcBindingUrl = URL( rTransferInfo.SourceURL ).getBindingUrl( );
1289 if ( sSrcBindingUrl != m_aURL.getBindingUrl( ) )
1290 {
1291 ucbhelper::cancelCommandExecution(
1292 uno::Any(
1293 ucb::InteractiveBadTransferURLException(
1294 u"Unsupported URL scheme!"_ustr,
1295 getXWeak() ) ),
1296 xEnv );
1297 }
1298 }
1299
1300 SAL_INFO( "ucb.ucp.cmis", "TODO - Content::transfer()" );
1301 }
1302
1303 void Content::insert( const uno::Reference< io::XInputStream > & xInputStream,
1304 bool bReplaceExisting, std::u16string_view rMimeType,
1305 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1306 {
1307 if ( !xInputStream.is() )
1308 {
1309 ucbhelper::cancelCommandExecution( uno::Any
1310 ( ucb::MissingInputStreamException
1311 ( OUString(), getXWeak() ) ),
1312 xEnv );
1313 }
1314
1315 // For transient content, the URL is the one of the parent
1316 if ( !m_bTransient )
1317 return;
1318
1319 OUString sNewPath;
1320
1321 // Try to get the object from the server if there is any
1322 libcmis::FolderPtr pFolder;
1323 try
1324 {
1325 pFolder = boost::dynamic_pointer_cast< libcmis::Folder >( getObject( xEnv ) );
1326 }
1327 catch ( const libcmis::Exception& )
1328 {
1329 }
1330
1331 if ( pFolder == nullptr )
1332 return;
1333
1334 libcmis::ObjectPtr object;
1335 std::map< std::string, libcmis::PropertyPtr >::iterator it = m_pObjectProps.find( "cmis:name" );
1336 if ( it == m_pObjectProps.end( ) )
1337 {
1338 ucbhelper::cancelCommandExecution( uno::Any
1339 ( uno::RuntimeException( u"Missing name property"_ustr,
1340 getXWeak() ) ),
1341 xEnv );
1342 }
1343 auto newPath = OUSTR_TO_STDSTR( m_sObjectPath );
1344 if ( !newPath.empty( ) && newPath[ newPath.size( ) - 1 ] != '/' )
1345 newPath += "/";
1346 newPath += it->second->getStrings( ).front( );
1347 try
1348 {
1349 if ( !m_sObjectId.isEmpty( ) )
1350 object = getSession( xEnv )->getObject( OUSTR_TO_STDSTR( m_sObjectId) );
1351 else
1352 object = getSession( xEnv )->getObjectByPath( newPath );
1353 sNewPath = STD_TO_OUSTR( newPath );
1354 }
1355 catch ( const libcmis::Exception& )
1356 {
1357 // Nothing matched the path
1358 }
1359
1360 if ( nullptr != object.get( ) )
1361 {
1362 // Are the base type matching?
1363 if ( object->getBaseType( ) != m_pObjectType->getBaseType( )->getId() )
1364 {
1365 ucbhelper::cancelCommandExecution( uno::Any
1366 ( uno::RuntimeException( u"Can't change a folder into a document and vice-versa."_ustr,
1367 getXWeak() ) ),
1368 xEnv );
1369 }
1370
1371 // Update the existing object if it's a document
1372 libcmis::Document* document = dynamic_cast< libcmis::Document* >( object.get( ) );
1373 if ( nullptr != document )
1374 {
1375 boost::shared_ptr< std::ostream > pOut( new std::ostringstream ( std::ios_base::binary | std::ios_base::in | std::ios_base::out ) );
1376 uno::Reference < io::XOutputStream > xOutput = new StdOutputStream( pOut );
1377 copyData( xInputStream, xOutput );
1378 try
1379 {
1380 document->setContentStream( std::move(pOut), OUSTR_TO_STDSTR( rMimeType ), std::string( ), bReplaceExisting );
1381 }
1382 catch ( const libcmis::Exception& )
1383 {
1384 ucbhelper::cancelCommandExecution( uno::Any
1385 ( uno::RuntimeException( u"Error when setting document content"_ustr,
1386 getXWeak() ) ),
1387 xEnv );
1388 }
1389 }
1390 }
1391 else
1392 {
1393 // We need to create a brand new object... either folder or document
1394 bool bIsFolder = getObjectType( xEnv )->getBaseType( )->getId( ) == "cmis:folder";
1395 setCmisProperty( "cmis:objectTypeId", getObjectType( xEnv )->getId( ), xEnv );
1396
1397 if ( bIsFolder )
1398 {
1399 try
1400 {
1401 pFolder->createFolder( m_pObjectProps );
1402 sNewPath = STD_TO_OUSTR( newPath );
1403 }
1404 catch ( const libcmis::Exception& )
1405 {
1406 ucbhelper::cancelCommandExecution( uno::Any
1407 ( uno::RuntimeException( u"Error when creating folder"_ustr,
1408 getXWeak() ) ),
1409 xEnv );
1410 }
1411 }
1412 else
1413 {
1414 boost::shared_ptr< std::ostream > pOut( new std::ostringstream ( std::ios_base::binary | std::ios_base::in | std::ios_base::out ) );
1415 uno::Reference < io::XOutputStream > xOutput = new StdOutputStream( pOut );
1416 copyData( xInputStream, xOutput );
1417 try
1418 {
1419 pFolder->createDocument( m_pObjectProps, std::move(pOut), OUSTR_TO_STDSTR( rMimeType ), std::string() );
1420 sNewPath = STD_TO_OUSTR( newPath );
1421 }
1422 catch ( const libcmis::Exception& )
1423 {
1424 ucbhelper::cancelCommandExecution( uno::Any
1425 ( uno::RuntimeException( u"Error when creating document"_ustr,
1426 getXWeak() ) ),
1427 xEnv );
1428 }
1429 }
1430 }
1431
1432 if ( sNewPath.isEmpty( ) && m_sObjectId.isEmpty( ) )
1433 return;
1434
1435 // Update the current content: it's no longer transient
1436 m_sObjectPath = sNewPath;
1437 URL aUrl( m_sURL );
1438 aUrl.setObjectPath( m_sObjectPath );
1439 aUrl.setObjectId( m_sObjectId );
1440 m_sURL = aUrl.asString( );
1441 m_pObject.reset( );
1442 m_pObjectType.reset( );
1443 m_pObjectProps.clear( );
1444 m_bTransient = false;
1445 inserted();
1446 }
1447
1448 const int TRANSFER_BUFFER_SIZE = 65536;
1449
1450 void Content::copyData(
1451 const uno::Reference< io::XInputStream >& xIn,
1452 const uno::Reference< io::XOutputStream >& xOut )
1453 {
1454 uno::Sequence< sal_Int8 > theData( TRANSFER_BUFFER_SIZE );
1455
1456 while ( xIn->readBytes( theData, TRANSFER_BUFFER_SIZE ) > 0 )
1457 xOut->writeBytes( theData );
1458
1459 xOut->closeOutput();
1460 }
1461
1462 uno::Sequence< uno::Any > Content::setPropertyValues(
1463 const uno::Sequence< beans::PropertyValue >& rValues,
1464 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1465 {
1466 try
1467 {
1468 // Get the already set properties if possible
1469 if ( !m_bTransient && getObject( xEnv ).get( ) )
1470 {
1471 m_pObjectProps.clear( );
1472 m_pObjectType = getObject( xEnv )->getTypeDescription();
1473 }
1474 }
1475 catch ( const libcmis::Exception& e )
1476 {
1477 SAL_INFO( "ucb.ucp.cmis", "Unexpected libcmis exception: " << e.what( ) );
1478 ucbhelper::cancelCommandExecution(
1479 ucb::IOErrorCode_GENERAL,
1480 uno::Sequence< uno::Any >( 0 ),
1481 xEnv,
1482 o3tl::runtimeToOUString(e.what()));
1483 }
1484
1485 sal_Int32 nCount = rValues.getLength();
1486 uno::Sequence< uno::Any > aRet( nCount );
1487 auto aRetRange = asNonConstRange(aRet);
1488 bool bChanged = false;
1489 const beans::PropertyValue* pValues = rValues.getConstArray();
1490 for ( sal_Int32 n = 0; n < nCount; ++n )
1491 {
1492 const beans::PropertyValue& rValue = pValues[ n ];
1493 if ( rValue.Name == "ContentType" ||
1494 rValue.Name == "MediaType" ||
1495 rValue.Name == "IsDocument" ||
1496 rValue.Name == "IsFolder" ||
1497 rValue.Name == "Size" ||
1498 rValue.Name == "CreatableContentsInfo" )
1499 {
1500 lang::IllegalAccessException e ( u"Property is read-only!"_ustr,
1501 getXWeak() );
1502 aRetRange[ n ] <<= e;
1503 }
1504 else if ( rValue.Name == "Title" )
1505 {
1506 OUString aNewTitle;
1507 if (!( rValue.Value >>= aNewTitle ))
1508 {
1509 aRetRange[ n ] <<= beans::IllegalTypeException
1510 ( u"Property value has wrong type!"_ustr,
1511 getXWeak() );
1512 continue;
1513 }
1514
1515 if ( aNewTitle.isEmpty() )
1516 {
1517 aRetRange[ n ] <<= lang::IllegalArgumentException
1518 ( u"Empty title not allowed!"_ustr,
1519 getXWeak(), -1 );
1520 continue;
1521
1522 }
1523
1524 setCmisProperty( "cmis:name", OUSTR_TO_STDSTR( aNewTitle ), xEnv );
1525 bChanged = true;
1526 }
1527 else
1528 {
1529 SAL_INFO( "ucb.ucp.cmis", "Couldn't set property: " << rValue.Name );
1530 lang::IllegalAccessException e ( u"Property is read-only!"_ustr,
1531 getXWeak() );
1532 aRetRange[ n ] <<= e;
1533 }
1534 }
1535
1536 try
1537 {
1538 if ( !m_bTransient && bChanged )
1539 {
1540 getObject( xEnv )->updateProperties( m_pObjectProps );
1541 }
1542 }
1543 catch ( const libcmis::Exception& e )
1544 {
1545 SAL_INFO( "ucb.ucp.cmis", "Unexpected libcmis exception: " << e.what( ) );
1546 ucbhelper::cancelCommandExecution(
1547 ucb::IOErrorCode_GENERAL,
1548 uno::Sequence< uno::Any >( 0 ),
1549 xEnv,
1550 o3tl::runtimeToOUString(e.what()));
1551 }
1552
1553 return aRet;
1554 }
1555
1556 bool Content::feedSink( const uno::Reference< uno::XInterface>& xSink,
1557 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1558 {
1559 if ( !xSink.is() )
1560 return false;
1561
1562 uno::Reference< io::XOutputStream > xOut(xSink, uno::UNO_QUERY );
1563 uno::Reference< io::XActiveDataSink > xDataSink(xSink, uno::UNO_QUERY );
1564 uno::Reference< io::XActiveDataStreamer > xDataStreamer( xSink, uno::UNO_QUERY );
1565
1566 if ( !xOut.is() && !xDataSink.is() && ( !xDataStreamer.is() || !xDataStreamer->getStream().is() ) )
1567 return false;
1568
1569 if ( xDataStreamer.is() && !xOut.is() )
1570 xOut = xDataStreamer->getStream()->getOutputStream();
1571
1572 try
1573 {
1574 libcmis::Document* document = dynamic_cast< libcmis::Document* >( getObject( xEnv ).get() );
1575
1576 if (!document)
1577 return false;
1578
1579 uno::Reference< io::XInputStream > xIn = new StdInputStream(document->getContentStream());
1580 if( !xIn.is( ) )
1581 return false;
1582
1583 if ( xDataSink.is() )
1584 xDataSink->setInputStream( xIn );
1585 else if ( xOut.is() )
1586 copyData( xIn, xOut );
1587 }
1588 catch ( const libcmis::Exception& e )
1589 {
1590 SAL_INFO( "ucb.ucp.cmis", "Unexpected libcmis exception: " << e.what( ) );
1591 ucbhelper::cancelCommandExecution(
1592 ucb::IOErrorCode_GENERAL,
1593 uno::Sequence< uno::Any >( 0 ),
1594 xEnv,
1595 o3tl::runtimeToOUString(e.what()));
1596 }
1597
1598 return true;
1599 }
1600
1601 uno::Sequence< beans::Property > Content::getProperties(
1602 const uno::Reference< ucb::XCommandEnvironment > & )
1603 {
1604 static const beans::Property aGenericProperties[] =
1605 {
1606 beans::Property( u"IsDocument"_ustr,
1607 -1, cppu::UnoType<bool>::get(),
1608 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1609 beans::Property( u"IsFolder"_ustr,
1610 -1, cppu::UnoType<bool>::get(),
1611 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1612 beans::Property( u"Title"_ustr,
1613 -1, cppu::UnoType<OUString>::get(),
1614 beans::PropertyAttribute::BOUND ),
1615 beans::Property( u"ObjectId"_ustr,
1616 -1, cppu::UnoType<OUString>::get(),
1617 beans::PropertyAttribute::BOUND ),
1618 beans::Property( u"TitleOnServer"_ustr,
1619 -1, cppu::UnoType<OUString>::get(),
1620 beans::PropertyAttribute::BOUND ),
1621 beans::Property( u"IsReadOnly"_ustr,
1622 -1, cppu::UnoType<bool>::get(),
1623 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1624 beans::Property( u"DateCreated"_ustr,
1625 -1, cppu::UnoType<util::DateTime>::get(),
1626 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1627 beans::Property( u"DateModified"_ustr,
1628 -1, cppu::UnoType<util::DateTime>::get(),
1629 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1630 beans::Property( u"Size"_ustr,
1631 -1, cppu::UnoType<sal_Int64>::get(),
1632 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1633 beans::Property( u"CreatableContentsInfo"_ustr,
1634 -1, cppu::UnoType<uno::Sequence< ucb::ContentInfo >>::get(),
1635 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1636 beans::Property( u"MediaType"_ustr,
1637 -1, cppu::UnoType<OUString>::get(),
1638 beans::PropertyAttribute::BOUND ),
1639 beans::Property( u"CmisProperties"_ustr,
1640 -1, cppu::UnoType<uno::Sequence< document::CmisProperty>>::get(),
1641 beans::PropertyAttribute::BOUND ),
1642 beans::Property( u"IsVersionable"_ustr,
1643 -1, cppu::UnoType<bool>::get(),
1644 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1645 beans::Property( u"CanCheckOut"_ustr,
1646 -1, cppu::UnoType<bool>::get(),
1647 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1648 beans::Property( u"CanCancelCheckOut"_ustr,
1649 -1, cppu::UnoType<bool>::get(),
1650 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1651 beans::Property( u"CanCheckIn"_ustr,
1652 -1, cppu::UnoType<bool>::get(),
1653 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ),
1654 };
1655
1656 const int nProps = SAL_N_ELEMENTS(aGenericProperties);
1657 return uno::Sequence< beans::Property > ( aGenericProperties, nProps );
1658 }
1659
1660 uno::Sequence< ucb::CommandInfo > Content::getCommands(
1661 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
1662 {
1663 static const ucb::CommandInfo aCommandInfoTable[] =
1664 {
1665 // Required commands
1666 ucb::CommandInfo
1667 ( u"getCommandInfo"_ustr,
1668 -1, cppu::UnoType<void>::get() ),
1669 ucb::CommandInfo
1670 ( u"getPropertySetInfo"_ustr,
1671 -1, cppu::UnoType<void>::get() ),
1672 ucb::CommandInfo
1673 ( u"getPropertyValues"_ustr,
1674 -1, cppu::UnoType<uno::Sequence< beans::Property >>::get() ),
1675 ucb::CommandInfo
1676 ( u"setPropertyValues"_ustr,
1677 -1, cppu::UnoType<uno::Sequence< beans::PropertyValue >>::get() ),
1678
1679 // Optional standard commands
1680 ucb::CommandInfo
1681 ( u"delete"_ustr,
1682 -1, cppu::UnoType<bool>::get() ),
1683 ucb::CommandInfo
1684 ( u"insert"_ustr,
1685 -1, cppu::UnoType<ucb::InsertCommandArgument2>::get() ),
1686 ucb::CommandInfo
1687 ( u"open"_ustr,
1688 -1, cppu::UnoType<ucb::OpenCommandArgument2>::get() ),
1689
1690 // Mandatory CMIS-only commands
1691 ucb::CommandInfo ( u"checkout"_ustr, -1, cppu::UnoType<void>::get() ),
1692 ucb::CommandInfo ( u"cancelCheckout"_ustr, -1, cppu::UnoType<void>::get() ),
1693 ucb::CommandInfo ( u"checkIn"_ustr, -1,
1694 cppu::UnoType<ucb::TransferInfo>::get() ),
1695 ucb::CommandInfo ( u"updateProperties"_ustr, -1, cppu::UnoType<void>::get() ),
1696 ucb::CommandInfo
1697 ( u"getAllVersions"_ustr,
1698 -1, cppu::UnoType<uno::Sequence< document::CmisVersion >>::get() ),
1699
1700
1701 // Folder Only, omitted if not a folder
1702 ucb::CommandInfo
1703 ( u"transfer"_ustr,
1704 -1, cppu::UnoType<ucb::TransferInfo>::get() ),
1705 ucb::CommandInfo
1706 ( u"createNewContent"_ustr,
1707 -1, cppu::UnoType<ucb::ContentInfo>::get() )
1708 };
1709
1710 const int nProps = SAL_N_ELEMENTS( aCommandInfoTable );
1711 return uno::Sequence< ucb::CommandInfo >(aCommandInfoTable, isFolder( xEnv ) ? nProps : nProps - 2);
1712 }
1713
1714 OUString Content::getParentURL( )
1715 {
1716 SAL_INFO( "ucb.ucp.cmis", "Content::getParentURL()" );
1717 OUString parentUrl = u"/"_ustr;
1718 if ( m_sObjectPath == "/" )
1719 return parentUrl;
1720 else
1721 {
1722 INetURLObject aUrl( m_sURL );
1723 if ( aUrl.getSegmentCount( ) > 0 )
1724 {
1725 URL aCmisUrl( m_sURL );
1726 aUrl.removeSegment( );
1727 aCmisUrl.setObjectPath( aUrl.GetURLPath( INetURLObject::DecodeMechanism::WithCharset ) );
1728 parentUrl = aCmisUrl.asString( );
1729 }
1730 }
1731 return parentUrl;
1732 }
1733
1734 XTYPEPROVIDER_COMMON_IMPL( Content );
1735
1736 void SAL_CALL Content::acquire() noexcept
1737 {
1738 ContentImplHelper::acquire();
1739 }
1740
1741 void SAL_CALL Content::release() noexcept
1742 {
1743 ContentImplHelper::release();
1744 }
1745
1746 uno::Any SAL_CALL Content::queryInterface( const uno::Type & rType )
1747 {
1748 uno::Any aRet = cppu::queryInterface( rType, static_cast< ucb::XContentCreator * >( this ) );
1749 return aRet.hasValue() ? aRet : ContentImplHelper::queryInterface(rType);
1750 }
1751
1752 OUString SAL_CALL Content::getImplementationName()
1753 {
1754 return u"com.sun.star.comp.CmisContent"_ustr;
1755 }
1756
1757 uno::Sequence< OUString > SAL_CALL Content::getSupportedServiceNames()
1758 {
1759 uno::Sequence<OUString> aSNS { u"com.sun.star.ucb.CmisContent"_ustr };
1760 return aSNS;
1761 }
1762
1763 OUString SAL_CALL Content::getContentType()
1764 {
1765 OUString sRet;
1766 try
1767 {
1768 if (isFolder( uno::Reference< ucb::XCommandEnvironment >() ))
1769 sRet = CMIS_FOLDER_TYPE;
1770 else
1771 sRet = CMIS_FILE_TYPE;
1772 }
1773 catch (const uno::RuntimeException&)
1774 {
1775 throw;
1776 }
1777 catch (const uno::Exception& e)
1778 {
1779 uno::Any a(cppu::getCaughtException());
1780 throw lang::WrappedTargetRuntimeException(
1781 "wrapped Exception " + e.Message,
1782 uno::Reference<uno::XInterface>(), a);
1783 }
1784 return sRet;
1785 }
1786
1787 uno::Any SAL_CALL Content::execute(
1788 const ucb::Command& aCommand,
1789 sal_Int32 /*CommandId*/,
1790 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1791 {
1792 SAL_INFO( "ucb.ucp.cmis", "Content::execute( ) - " << aCommand.Name );
1793 uno::Any aRet;
1794
1795 if ( aCommand.Name == "getPropertyValues" )
1796 {
1797 uno::Sequence< beans::Property > Properties;
1798 if ( !( aCommand.Argument >>= Properties ) )
1799 ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
1800 aRet <<= getPropertyValues( Properties, xEnv );
1801 }
1802 else if ( aCommand.Name == "getPropertySetInfo" )
1803 aRet <<= getPropertySetInfo( xEnv, false );
1804 else if ( aCommand.Name == "getCommandInfo" )
1805 aRet <<= getCommandInfo( xEnv, false );
1806 else if ( aCommand.Name == "open" )
1807 {
1808 ucb::OpenCommandArgument2 aOpenCommand;
1809 if ( !( aCommand.Argument >>= aOpenCommand ) )
1810 ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
1811 aRet = open( aOpenCommand, xEnv );
1812 }
1813 else if ( aCommand.Name == "transfer" )
1814 {
1815 ucb::TransferInfo transferArgs;
1816 if ( !( aCommand.Argument >>= transferArgs ) )
1817 ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
1818 transfer( transferArgs, xEnv );
1819 }
1820 else if ( aCommand.Name == "setPropertyValues" )
1821 {
1822 uno::Sequence< beans::PropertyValue > aProperties;
1823 if ( !( aCommand.Argument >>= aProperties ) || !aProperties.hasElements() )
1824 ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
1825 aRet <<= setPropertyValues( aProperties, xEnv );
1826 }
1827 else if (aCommand.Name == "createNewContent"
1828 && isFolder( xEnv ) )
1829 {
1830 ucb::ContentInfo arg;
1831 if ( !( aCommand.Argument >>= arg ) )
1832 ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
1833 aRet <<= createNewContent( arg );
1834 }
1835 else if ( aCommand.Name == "insert" )
1836 {
1837 ucb::InsertCommandArgument2 arg;
1838 if ( !( aCommand.Argument >>= arg ) )
1839 {
1840 ucb::InsertCommandArgument insertArg;
1841 if ( !( aCommand.Argument >>= insertArg ) )
1842 ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv );
1843
1844 arg.Data = insertArg.Data;
1845 arg.ReplaceExisting = insertArg.ReplaceExisting;
1846 }
1847 // store the document id
1848 m_sObjectId = arg.DocumentId;
1849 insert( arg.Data, arg.ReplaceExisting, arg.MimeType, xEnv );
1850 }
1851 else if ( aCommand.Name == "delete" )
1852 {
1853 try
1854 {
1855 if ( !isFolder( xEnv ) )
1856 {
1857 getObject( xEnv )->remove( );
1858 }
1859 else
1860 {
1861 libcmis::Folder* folder = dynamic_cast< libcmis::Folder* >( getObject( xEnv ).get() );
1862 if (folder)
1863 folder->removeTree( );
1864 }
1865 }
1866 catch ( const libcmis::Exception& e )
1867 {
1868 SAL_INFO( "ucb.ucp.cmis", "Unexpected libcmis exception: " << e.what( ) );
1869 ucbhelper::cancelCommandExecution(
1870 ucb::IOErrorCode_GENERAL,
1871 uno::Sequence< uno::Any >( 0 ),
1872 xEnv,
1873 o3tl::runtimeToOUString(e.what()));
1874 }
1875 }
1876 else if ( aCommand.Name == "checkout" )
1877 {
1878 aRet <<= checkOut( xEnv );
1879 }
1880 else if ( aCommand.Name == "cancelCheckout" )
1881 {
1882 aRet <<= cancelCheckOut( xEnv );
1883 }
1884 else if ( aCommand.Name == "checkin" )
1885 {
1886 ucb::CheckinArgument aArg;
1887 if ( !( aCommand.Argument >>= aArg ) )
1888 {
1889 ucbhelper::cancelCommandExecution ( getBadArgExcept(), xEnv );
1890 }
1891 aRet <<= checkIn( aArg, xEnv );
1892 }
1893 else if ( aCommand.Name == "getAllVersions" )
1894 {
1895 aRet <<= getAllVersions( xEnv );
1896 }
1897 else if ( aCommand.Name == "updateProperties" )
1898 {
1899 updateProperties( aCommand.Argument, xEnv );
1900 }
1901 else
1902 {
1903 SAL_INFO( "ucb.ucp.cmis", "Unknown command to execute" );
1904
1905 ucbhelper::cancelCommandExecution
1906 ( uno::Any( ucb::UnsupportedCommandException
1907 ( OUString(),
1908 getXWeak() ) ),
1909 xEnv );
1910 }
1911
1912 return aRet;
1913 }
1914
1915 void SAL_CALL Content::abort( sal_Int32 /*CommandId*/ )
1916 {
1917 SAL_INFO( "ucb.ucp.cmis", "TODO - Content::abort()" );
1918 // TODO Implement me
1919 }
1920
1921 uno::Sequence< ucb::ContentInfo > SAL_CALL Content::queryCreatableContentsInfo()
1922 {
1923 return queryCreatableContentsInfo( uno::Reference< ucb::XCommandEnvironment >() );
1924 }
1925
1926 uno::Reference< ucb::XContent > SAL_CALL Content::createNewContent(
1927 const ucb::ContentInfo& Info )
1928 {
1929 bool create_document;
1930
1931 if ( Info.Type == CMIS_FILE_TYPE )
1932 create_document = true;
1933 else if ( Info.Type == CMIS_FOLDER_TYPE )
1934 create_document = false;
1935 else
1936 {
1937 SAL_INFO( "ucb.ucp.cmis", "Unknown type of content to create" );
1938 return uno::Reference< ucb::XContent >();
1939 }
1940
1941 OUString sParentURL = m_xIdentifier->getContentIdentifier();
1942
1943 // Set the parent URL for the transient objects
1944 uno::Reference< ucb::XContentIdentifier > xId(new ::ucbhelper::ContentIdentifier(sParentURL));
1945
1946 try
1947 {
1948 return new ::cmis::Content( m_xContext, m_pProvider, xId, !create_document );
1949 }
1950 catch ( ucb::ContentCreationException & )
1951 {
1952 return uno::Reference< ucb::XContent >();
1953 }
1954 }
1955
1956 uno::Sequence< uno::Type > SAL_CALL Content::getTypes()
1957 {
1958 try
1959 {
1960 if ( isFolder( uno::Reference< ucb::XCommandEnvironment >() ) )
1961 {
1962 static cppu::OTypeCollection s_aFolderCollection
1963 (CPPU_TYPE_REF( lang::XTypeProvider ),
1964 CPPU_TYPE_REF( lang::XServiceInfo ),
1965 CPPU_TYPE_REF( lang::XComponent ),
1966 CPPU_TYPE_REF( ucb::XContent ),
1967 CPPU_TYPE_REF( ucb::XCommandProcessor ),
1968 CPPU_TYPE_REF( beans::XPropertiesChangeNotifier ),
1969 CPPU_TYPE_REF( ucb::XCommandInfoChangeNotifier ),
1970 CPPU_TYPE_REF( beans::XPropertyContainer ),
1971 CPPU_TYPE_REF( beans::XPropertySetInfoChangeNotifier ),
1972 CPPU_TYPE_REF( container::XChild ),
1973 CPPU_TYPE_REF( ucb::XContentCreator ) );
1974 return s_aFolderCollection.getTypes();
1975 }
1976 }
1977 catch (const uno::RuntimeException&)
1978 {
1979 throw;
1980 }
1981 catch (const uno::Exception& e)
1982 {
1983 uno::Any a(cppu::getCaughtException());
1984 throw lang::WrappedTargetRuntimeException(
1985 "wrapped Exception " + e.Message,
1986 uno::Reference<uno::XInterface>(), a);
1987 }
1988
1989 static cppu::OTypeCollection s_aFileCollection
1990 (CPPU_TYPE_REF( lang::XTypeProvider ),
1991 CPPU_TYPE_REF( lang::XServiceInfo ),
1992 CPPU_TYPE_REF( lang::XComponent ),
1993 CPPU_TYPE_REF( ucb::XContent ),
1994 CPPU_TYPE_REF( ucb::XCommandProcessor ),
1995 CPPU_TYPE_REF( beans::XPropertiesChangeNotifier ),
1996 CPPU_TYPE_REF( ucb::XCommandInfoChangeNotifier ),
1997 CPPU_TYPE_REF( beans::XPropertyContainer ),
1998 CPPU_TYPE_REF( beans::XPropertySetInfoChangeNotifier ),
1999 CPPU_TYPE_REF( container::XChild ) );
2000
2001 return s_aFileCollection.getTypes();
2002 }
2003
2004 uno::Sequence< ucb::ContentInfo > Content::queryCreatableContentsInfo(
2005 const uno::Reference< ucb::XCommandEnvironment >& xEnv)
2006 {
2007 try
2008 {
2009 if ( isFolder( xEnv ) )
2010 {
2011
2012 // Minimum set of props we really need
2013 uno::Sequence< beans::Property > props
2014 {
2015 {
2016 u"Title"_ustr,
2017 -1,
2018 cppu::UnoType<OUString>::get(),
2019 beans::PropertyAttribute::MAYBEVOID | beans::PropertyAttribute::BOUND
2020 }
2021 };
2022
2023 return
2024 {
2025 {
2026 CMIS_FILE_TYPE,
2027 ( ucb::ContentInfoAttribute::INSERT_WITH_INPUTSTREAM |
2028 ucb::ContentInfoAttribute::KIND_DOCUMENT ),
2029 props
2030 },
2031 {
2032 CMIS_FOLDER_TYPE,
2033 ucb::ContentInfoAttribute::KIND_FOLDER,
2034 props
2035 }
2036 };
2037 }
2038 }
2039 catch (const uno::RuntimeException&)
2040 {
2041 throw;
2042 }
2043 catch (const uno::Exception& e)
2044 {
2045 uno::Any a(cppu::getCaughtException());
2046 throw lang::WrappedTargetRuntimeException(
2047 "wrapped Exception " + e.Message,
2048 uno::Reference<uno::XInterface>(), a);
2049 }
2050 return {};
2051 }
2052
2053 std::vector< uno::Reference< ucb::XContent > > Content::getChildren( )
2054 {
2055 std::vector< uno::Reference< ucb::XContent > > results;
2056 SAL_INFO( "ucb.ucp.cmis", "Content::getChildren() " << m_sURL );
2057
2058 libcmis::FolderPtr pFolder = boost::dynamic_pointer_cast< libcmis::Folder >( getObject( uno::Reference< ucb::XCommandEnvironment >() ) );
2059 if ( nullptr != pFolder )
2060 {
2061 // Get the children from pObject
2062 try
2063 {
2064 std::vector< libcmis::ObjectPtr > children = pFolder->getChildren( );
2065
2066 // Loop over the results
2067 for ( const auto& rChild : children )
2068 {
2069 // TODO Cache the objects
2070
2071 INetURLObject aURL( m_sURL );
2072 OUString sUser = aURL.GetUser( INetURLObject::DecodeMechanism::WithCharset );
2073
2074 URL aUrl( m_sURL );
2075 OUString sPath( m_sObjectPath );
2076 if ( !sPath.endsWith("/") )
2077 sPath += "/";
2078 sPath += STD_TO_OUSTR( rChild->getName( ) );
2079 OUString sId = STD_TO_OUSTR( rChild->getId( ) );
2080
2081 aUrl.setObjectId( sId );
2082 aUrl.setObjectPath( sPath );
2083 aUrl.setUsername( sUser );
2084
2085 uno::Reference< ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aUrl.asString( ) );
2086 uno::Reference< ucb::XContent > xContent = new Content( m_xContext, m_pProvider, xId, rChild );
2087
2088 results.push_back( xContent );
2089 }
2090 }
2091 catch ( const libcmis::Exception& e )
2092 {
2093 SAL_INFO( "ucb.ucp.cmis", "Exception thrown: " << e.what() );
2094 }
2095 }
2096
2097 return results;
2098 }
2099
2100 void Content::setCmisProperty(const std::string& rName, const std::string& rValue, const uno::Reference< ucb::XCommandEnvironment >& xEnv )
2101 {
2102 if ( !getObjectType( xEnv ).get( ) )
2103 return;
2104
2105 std::map< std::string, libcmis::PropertyPtr >::iterator propIt = m_pObjectProps.find(rName);
2106
2107 if ( propIt == m_pObjectProps.end( ) && getObjectType( xEnv ).get( ) )
2108 {
2109 std::map< std::string, libcmis::PropertyTypePtr > propsTypes = getObjectType( xEnv )->getPropertiesTypes( );
2110 std::map< std::string, libcmis::PropertyTypePtr >::iterator typeIt = propsTypes.find(rName);
2111
2112 if ( typeIt != propsTypes.end( ) )
2113 {
2114 libcmis::PropertyPtr property( new libcmis::Property( typeIt->second, { rValue }) );
2115 m_pObjectProps.insert(std::pair< std::string, libcmis::PropertyPtr >(rName, property));
2116 }
2117 }
2118 else if ( propIt != m_pObjectProps.end( ) )
2119 {
2120 propIt->second->setValues( { rValue } );
2121 }
2122 }
2123 }
2124
2125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */