LibreOffice Module ucb (master)  1
neon/webdavcontent.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 
30 /**************************************************************************
31  TODO
32  **************************************************************************
33 
34  *************************************************************************/
35 
36 #include <memory>
37 #include <rtl/uri.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <sal/log.hxx>
41 #include <officecfg/Inet.hxx>
46 #include <com/sun/star/beans/IllegalTypeException.hpp>
47 #include <com/sun/star/beans/NotRemoveableException.hpp>
48 #include <com/sun/star/beans/PropertyAttribute.hpp>
49 #include <com/sun/star/beans/PropertyExistException.hpp>
50 #include <com/sun/star/beans/PropertySetInfoChange.hpp>
51 #include <com/sun/star/beans/PropertySetInfoChangeEvent.hpp>
52 #include <com/sun/star/beans/PropertyValue.hpp>
53 #include <com/sun/star/io/XActiveDataSink.hpp>
54 #include <com/sun/star/io/XOutputStream.hpp>
55 #include <com/sun/star/lang/IllegalAccessException.hpp>
56 #include <com/sun/star/sdbc/SQLException.hpp>
57 #include <com/sun/star/task/PasswordContainerInteractionHandler.hpp>
58 #include <com/sun/star/ucb/CommandEnvironment.hpp>
59 #include <com/sun/star/ucb/CommandFailedException.hpp>
60 #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
61 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
62 #include <com/sun/star/ucb/InsertCommandArgument.hpp>
63 #include <com/sun/star/ucb/InteractiveBadTransferURLException.hpp>
64 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
65 #include <com/sun/star/ucb/InteractiveLockingLockedException.hpp>
66 #include <com/sun/star/ucb/InteractiveLockingLockExpiredException.hpp>
67 #include <com/sun/star/ucb/InteractiveLockingNotLockedException.hpp>
68 #include <com/sun/star/ucb/InteractiveNetworkConnectException.hpp>
69 #include <com/sun/star/ucb/InteractiveNetworkGeneralException.hpp>
70 #include <com/sun/star/ucb/InteractiveNetworkReadException.hpp>
71 #include <com/sun/star/ucb/InteractiveNetworkResolveNameException.hpp>
72 #include <com/sun/star/ucb/InteractiveNetworkWriteException.hpp>
73 #include <com/sun/star/ucb/MissingInputStreamException.hpp>
74 #include <com/sun/star/ucb/MissingPropertiesException.hpp>
75 #include <com/sun/star/ucb/NameClash.hpp>
76 #include <com/sun/star/ucb/NameClashException.hpp>
77 #include <com/sun/star/ucb/OpenCommandArgument3.hpp>
78 #include <com/sun/star/ucb/OpenMode.hpp>
79 #include <com/sun/star/ucb/PostCommandArgument2.hpp>
80 #include <com/sun/star/ucb/PropertyCommandArgument.hpp>
81 #include <com/sun/star/ucb/TransferInfo.hpp>
82 #include <com/sun/star/ucb/UnsupportedCommandException.hpp>
83 #include <com/sun/star/ucb/UnsupportedDataSinkException.hpp>
84 #include <com/sun/star/ucb/UnsupportedNameClashException.hpp>
85 #include <com/sun/star/ucb/UnsupportedOpenModeException.hpp>
86 #include <com/sun/star/ucb/XCommandInfo.hpp>
87 #include <com/sun/star/ucb/XPersistentPropertySet.hpp>
88 #include <ucbhelper/macros.hxx>
89 
90 #include "webdavcontent.hxx"
91 #include "webdavprovider.hxx"
92 #include "webdavresultset.hxx"
93 #include "ContentProperties.hxx"
94 #include "NeonUri.hxx"
95 #include "UCBDeadPropertyValue.hxx"
96 
97 using namespace com::sun::star;
98 using namespace webdav_ucp;
99 
100 namespace
101 {
102  // implement a GET to substitute HEAD, when HEAD not available
103  void lcl_sendPartialGETRequest( bool &bError,
104  DAVException &aLastException,
105  const std::vector< OUString >& rProps,
106  std::vector< OUString > &aHeaderNames,
107  const std::unique_ptr< DAVResourceAccess > &xResAccess,
108  std::unique_ptr< ContentProperties > &xProps,
109  const uno::Reference< ucb::XCommandEnvironment >& xEnv )
110  {
111  DAVResource aResource;
112  DAVRequestHeaders aPartialGet;
113  aPartialGet.emplace_back( OUString( "Range" ), // see <https://tools.ietf.org/html/rfc7233#section-3.1>
114  OUString( "bytes=0-0" ) );
115 
116  bool bIsRequestSize = std::any_of(aHeaderNames.begin(), aHeaderNames.end(),
117  [](const OUString& rHeaderName) { return rHeaderName == "Content-Length"; });
118 
119  if ( bIsRequestSize )
120  {
121  // we need to know if the server accepts range requests for a resource
122  // and the range unit it uses
123  aHeaderNames.emplace_back( "Accept-Ranges" ); // see <https://tools.ietf.org/html/rfc7233#section-2.3>
124  aHeaderNames.emplace_back( "Content-Range" ); // see <https://tools.ietf.org/html/rfc7233#section-4.2>
125  }
126  try
127  {
128  xResAccess->GET0( aPartialGet, aHeaderNames, aResource, xEnv );
129  bError = false;
130 
131  if ( bIsRequestSize )
132  {
133  // the ContentProperties maps "Content-Length" to the UCB "Size" property
134  // This would have an unrealistic value of 1 byte because we did only a partial GET
135  // Solution: if "Content-Range" is present, map it with UCB "Size" property
136  OUString aAcceptRanges, aContentRange, aContentLength;
137  std::vector< DAVPropertyValue > &aResponseProps = aResource.properties;
138  for ( const auto& rResponseProp : aResponseProps )
139  {
140  if ( rResponseProp.Name == "Accept-Ranges" )
141  rResponseProp.Value >>= aAcceptRanges;
142  else if ( rResponseProp.Name == "Content-Range" )
143  rResponseProp.Value >>= aContentRange;
144  else if ( rResponseProp.Name == "Content-Length" )
145  rResponseProp.Value >>= aContentLength;
146  }
147 
148  sal_Int64 nSize = 1;
149  if ( aContentLength.getLength() )
150  {
151  nSize = aContentLength.toInt64();
152  }
153 
154  // according to <> http://tools.ietf.org/html/rfc2616#section-3.12
155  // <https://tools.ietf.org/html/rfc7233#section-2>
156  // needs some explanation for this
157  // probably some changes?
158  // the only range unit defined is "bytes" and implementations
159  // MAY ignore ranges specified using other units.
160  if ( nSize == 1 &&
161  aContentRange.getLength() &&
162  aAcceptRanges == "bytes" )
163  {
164  // Parse the Content-Range to get the size
165  // vid. http://tools.ietf.org/html/rfc2616#section-14.16
166  // Content-Range: <range unit> <bytes range>/<size>
167  sal_Int32 nSlash = aContentRange.lastIndexOf( '/' );
168  if ( nSlash != -1 )
169  {
170  OUString aSize = aContentRange.copy( nSlash + 1 );
171  // "*" means that the instance-length is unknown at the time when the response was generated
172  if ( aSize != "*" )
173  {
174  auto it = std::find_if(aResponseProps.begin(), aResponseProps.end(),
175  [](const DAVPropertyValue& rProp) { return rProp.Name == "Content-Length"; });
176  if (it != aResponseProps.end())
177  {
178  it->Value <<= aSize;
179  }
180  }
181  }
182  }
183  }
184 
185  if (xProps)
186  xProps->addProperties(
187  rProps,
188  ContentProperties( aResource ) );
189  else
190  xProps.reset ( new ContentProperties( aResource ) );
191  }
192  catch ( DAVException const & ex )
193  {
194  aLastException = ex;
195  }
196  }
197 }
198 
199 
200 // Static value, to manage a simple OPTIONS cache
201 // Key is the URL, element is the DAVOptions resulting from an OPTIONS call.
202 // Cached DAVOptions have a lifetime that depends on the errors received or not received
203 // and on the value of received options.
205 
206 
207 // Content Implementation.
208 
209 
210 // ctr for content on an existing webdav resource
211 Content::Content(
212  const uno::Reference< uno::XComponentContext >& rxContext,
213  ContentProvider* pProvider,
214  const uno::Reference< ucb::XContentIdentifier >& Identifier,
215  rtl::Reference< DAVSessionFactory > const & rSessionFactory )
216 : ContentImplHelper( rxContext, pProvider, Identifier ),
217  m_eResourceType( UNKNOWN ),
218  m_eResourceTypeForLocks( UNKNOWN ),
219  m_pProvider( pProvider ),
220  m_bTransient( false ),
221  m_bCollection( false ),
222  m_bDidGetOrHead( false )
223 {
224  try
225  {
226  initOptsCacheLifeTime();
227  m_xResAccess.reset( new DAVResourceAccess(
228  rxContext,
229  rSessionFactory,
230  Identifier->getContentIdentifier() ) );
231 
232  NeonUri aURI( Identifier->getContentIdentifier() );
233  m_aEscapedTitle = aURI.GetPathBaseName();
234  }
235  catch ( DAVException const & )
236  {
237  throw ucb::ContentCreationException();
238  }
239 }
240 
241 
242 // ctr for content on a non-existing webdav resource
243 Content::Content(
244  const uno::Reference< uno::XComponentContext >& rxContext,
245  ContentProvider* pProvider,
246  const uno::Reference< ucb::XContentIdentifier >& Identifier,
247  rtl::Reference< DAVSessionFactory > const & rSessionFactory,
248  bool isCollection )
249 : ContentImplHelper( rxContext, pProvider, Identifier ),
250  m_eResourceType( UNKNOWN ),
251  m_eResourceTypeForLocks( UNKNOWN ),
252  m_pProvider( pProvider ),
253  m_bTransient( true ),
254  m_bCollection( isCollection ),
255  m_bDidGetOrHead( false )
256 {
257  try
258  {
259  initOptsCacheLifeTime();
260  m_xResAccess.reset( new DAVResourceAccess(
261  rxContext, rSessionFactory, Identifier->getContentIdentifier() ) );
262  }
263  catch ( DAVException const & )
264  {
265  throw ucb::ContentCreationException();
266  }
267 
268  // Do not set m_aEscapedTitle here! Content::insert relays on this!!!
269 }
270 
271 
272 // virtual
273 Content::~Content()
274 {
275 }
276 
277 
278 // XInterface methods.
279 
280 
281 // virtual
282 void SAL_CALL Content::acquire()
283  noexcept
284 {
285  ContentImplHelper::acquire();
286 }
287 
288 
289 // virtual
290 void SAL_CALL Content::release()
291  noexcept
292 {
293  ContentImplHelper::release();
294 }
295 
296 
297 // virtual
298 uno::Any SAL_CALL Content::queryInterface( const uno::Type & rType )
299 {
300  // Note: isFolder may require network activities! So call it only
301  // if it is really necessary!!!
303  rType,
304  static_cast< ucb::XContentCreator * >( this ) );
305  if ( aRet.hasValue() )
306  {
307  try
308  {
309  uno::Reference< task::XInteractionHandler > xIH(
310  task::PasswordContainerInteractionHandler::create( m_xContext ) );
311 
312  // Supply a command env to isFolder() that contains an interaction
313  // handler that uses the password container service to obtain
314  // credentials without displaying a password gui.
315 
316  uno::Reference< ucb::XCommandEnvironment > xCmdEnv(
317  ucb::CommandEnvironment::create(
318  m_xContext,
319  xIH,
320  uno::Reference< ucb::XProgressHandler >() ) );
321 
322  return isFolder( xCmdEnv ) ? aRet : uno::Any();
323  }
324  catch ( uno::RuntimeException const & )
325  {
326  throw;
327  }
328  catch ( uno::Exception const & )
329  {
330  return uno::Any();
331  }
332  }
333  return aRet.hasValue() ? aRet : ContentImplHelper::queryInterface( rType );
334 }
335 
336 
337 // XTypeProvider methods.
338 
339 
341 
342 
343 // virtual
344 uno::Sequence< uno::Type > SAL_CALL Content::getTypes()
345 {
346  bool bFolder = false;
347  try
348  {
349  bFolder
350  = isFolder( uno::Reference< ucb::XCommandEnvironment >() );
351  }
352  catch ( uno::RuntimeException const & )
353  {
354  throw;
355  }
356  catch ( uno::Exception const & )
357  {
358  }
359 
360  if ( bFolder )
361  {
362  static cppu::OTypeCollection s_aFolderTypes(
363  CPPU_TYPE_REF( lang::XTypeProvider ),
364  CPPU_TYPE_REF( lang::XServiceInfo ),
365  CPPU_TYPE_REF( lang::XComponent ),
366  CPPU_TYPE_REF( ucb::XContent ),
367  CPPU_TYPE_REF( ucb::XCommandProcessor ),
368  CPPU_TYPE_REF( beans::XPropertiesChangeNotifier ),
369  CPPU_TYPE_REF( ucb::XCommandInfoChangeNotifier ),
370  CPPU_TYPE_REF( beans::XPropertyContainer ),
371  CPPU_TYPE_REF( beans::XPropertySetInfoChangeNotifier ),
372  CPPU_TYPE_REF( container::XChild ),
373  CPPU_TYPE_REF( ucb::XContentCreator ) );
374 
375  return s_aFolderTypes.getTypes();
376  }
377  else
378  {
379  static cppu::OTypeCollection s_aDocumentTypes(
380  CPPU_TYPE_REF( lang::XTypeProvider ),
381  CPPU_TYPE_REF( lang::XServiceInfo ),
382  CPPU_TYPE_REF( lang::XComponent ),
383  CPPU_TYPE_REF( ucb::XContent ),
384  CPPU_TYPE_REF( ucb::XCommandProcessor ),
385  CPPU_TYPE_REF( beans::XPropertiesChangeNotifier ),
386  CPPU_TYPE_REF( ucb::XCommandInfoChangeNotifier ),
387  CPPU_TYPE_REF( beans::XPropertyContainer ),
388  CPPU_TYPE_REF( beans::XPropertySetInfoChangeNotifier ),
389  CPPU_TYPE_REF( container::XChild ) );
390 
391  return s_aDocumentTypes.getTypes();
392  }
393 }
394 
395 
396 // XServiceInfo methods.
397 
398 
399 // virtual
401 {
402  return "com.sun.star.comp.ucb.WebDAVContent";
403 }
404 
405 
406 // virtual
407 uno::Sequence< OUString > SAL_CALL Content::getSupportedServiceNames()
408 {
409  return { WEBDAV_CONTENT_SERVICE_NAME };
410 }
411 
412 
413 // XContent methods.
414 
415 
416 // virtual
417 OUString SAL_CALL Content::getContentType()
418 {
419  bool bFolder = false;
420  try
421  {
422  bFolder
423  = isFolder( uno::Reference< ucb::XCommandEnvironment >() );
424  }
425  catch ( uno::RuntimeException const & )
426  {
427  throw;
428  }
429  catch ( uno::Exception const & )
430  {
431  }
432 
433  if ( bFolder )
434  return WEBDAV_COLLECTION_TYPE;
435 
436  return WEBDAV_CONTENT_TYPE;
437 }
438 
439 
440 // XCommandProcessor methods.
441 
442 
443 // virtual
444 uno::Any SAL_CALL Content::execute(
445  const ucb::Command& aCommand,
446  sal_Int32 /*CommandId*/,
447  const uno::Reference< ucb::XCommandEnvironment >& Environment )
448 {
449  SAL_INFO( "ucb.ucp.webdav", "Content::execute: start: command: " <<
450  aCommand.Name << ", env: " <<
451  (Environment.is() ? "present" : "missing") );
452 
453  uno::Any aRet;
454 
455  if ( aCommand.Name == "getPropertyValues" )
456  {
457 
458  // getPropertyValues
459 
460 
461  uno::Sequence< beans::Property > Properties;
462  if ( !( aCommand.Argument >>= Properties ) )
463  {
465  uno::makeAny( lang::IllegalArgumentException(
466  "Wrong argument type!",
467  static_cast< cppu::OWeakObject * >( this ),
468  -1 ) ),
469  Environment );
470  // Unreachable
471  }
472 
473  aRet <<= getPropertyValues( Properties, Environment );
474  }
475  else if ( aCommand.Name == "setPropertyValues" )
476  {
477 
478  // setPropertyValues
479 
480 
481  uno::Sequence< beans::PropertyValue > aProperties;
482  if ( !( aCommand.Argument >>= aProperties ) )
483  {
485  uno::makeAny( lang::IllegalArgumentException(
486  "Wrong argument type!",
487  static_cast< cppu::OWeakObject * >( this ),
488  -1 ) ),
489  Environment );
490  // Unreachable
491  }
492 
493  if ( !aProperties.hasElements() )
494  {
496  uno::makeAny( lang::IllegalArgumentException(
497  "No properties!",
498  static_cast< cppu::OWeakObject * >( this ),
499  -1 ) ),
500  Environment );
501  // Unreachable
502  }
503 
504  aRet <<= setPropertyValues( aProperties, Environment );
505  }
506  else if ( aCommand.Name == "getPropertySetInfo" )
507  {
508 
509  // getPropertySetInfo
510 
511 
512  // Note: Implemented by base class.
513  aRet <<= getPropertySetInfo( Environment,
514  false /* don't cache data */ );
515  }
516  else if ( aCommand.Name == "getCommandInfo" )
517  {
518 
519  // getCommandInfo
520 
521 
522  // Note: Implemented by base class.
523  aRet <<= getCommandInfo( Environment, false );
524  }
525  else if ( aCommand.Name == "open" )
526  {
527 
528  // open
529 
530 
531  ucb::OpenCommandArgument3 aOpenCommand;
532  ucb::OpenCommandArgument2 aTmp;
533  if ( !( aCommand.Argument >>= aTmp ) )
534  {
536  uno::makeAny( lang::IllegalArgumentException(
537  "Wrong argument type!",
538  static_cast< cppu::OWeakObject * >( this ),
539  -1 ) ),
540  Environment );
541  // Unreachable
542  }
543  if ( !( aCommand.Argument >>= aOpenCommand ) )
544  {
545  // compat mode, extract Arg2 info into newer structure
546  aOpenCommand.Mode = aTmp.Mode;
547  aOpenCommand.Priority = aTmp.Priority;
548  aOpenCommand.Sink = aTmp.Sink;
549  aOpenCommand.Properties = aTmp.Properties;
550  aOpenCommand.SortingInfo = aTmp.SortingInfo;
551  }
552 
553  aRet = open( aOpenCommand, Environment );
554 
555  }
556  else if ( aCommand.Name == "insert" )
557  {
558 
559  // insert
560 
561 
562  ucb::InsertCommandArgument arg;
563  if ( !( aCommand.Argument >>= arg ) )
564  {
566  uno::makeAny( lang::IllegalArgumentException(
567  "Wrong argument type!",
568  static_cast< cppu::OWeakObject * >( this ),
569  -1 ) ),
570  Environment );
571  // Unreachable
572  }
573 
574  insert( arg.Data, arg.ReplaceExisting, Environment );
575  }
576  else if ( aCommand.Name == "delete" )
577  {
578 
579  // delete
580 
581 
582  bool bDeletePhysical = false;
583  aCommand.Argument >>= bDeletePhysical;
584 
585 // KSO: Ignore parameter and destroy the content, if you don't support
586 // putting objects into trashcan. ( Since we do not have a trash can
587 // service yet (src603), you actually have no other choice. )
588 // if ( bDeletePhysical )
589 // {
590  try
591  {
592  std::unique_ptr< DAVResourceAccess > xResAccess;
593  {
594  osl::Guard< osl::Mutex > aGuard( m_aMutex );
595  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
596  }
597  aStaticDAVOptionsCache.removeDAVOptions( xResAccess->getURL() );
598  // clean cached value of PROPFIND property names
599  removeCachedPropertyNames( xResAccess->getURL() );
600  xResAccess->DESTROY( Environment );
601  {
602  osl::Guard< osl::Mutex > aGuard( m_aMutex );
603  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
604  }
605  }
606  catch ( DAVException const & e )
607  {
608  cancelCommandExecution( e, Environment, true );
609  // Unreachable
610  }
611 // }
612 
613  // Propagate destruction.
614  destroy( bDeletePhysical );
615 
616  // Remove own and all children's Additional Core Properties.
618  }
619  else if ( aCommand.Name == "transfer" && isFolder( Environment ) )
620  {
621 
622  // transfer
623  // ( Not available at documents )
624 
625 
626  ucb::TransferInfo transferArgs;
627  if ( !( aCommand.Argument >>= transferArgs ) )
628  {
630  uno::makeAny( lang::IllegalArgumentException(
631  "Wrong argument type!",
632  static_cast< cppu::OWeakObject * >( this ),
633  -1 ) ),
634  Environment );
635  // Unreachable
636  }
637 
638  transfer( transferArgs, Environment );
639  }
640  else if ( aCommand.Name == "post" )
641  {
642 
643  // post
644 
645 
646  ucb::PostCommandArgument2 aArg;
647  if ( !( aCommand.Argument >>= aArg ) )
648  {
650  uno::makeAny( lang::IllegalArgumentException(
651  "Wrong argument type!",
652  static_cast< cppu::OWeakObject * >( this ),
653  -1 ) ),
654  Environment );
655  // Unreachable
656  }
657 
658  post( aArg, Environment );
659  }
660  else if ( aCommand.Name == "lock" )
661  {
662 
663  // lock
664 
665  ResourceType eType = resourceTypeForLocks( Environment );
666  // when the resource is not yet present the lock is used to create it
667  // see: http://tools.ietf.org/html/rfc4918#section-7.3
668  // If the resource doesn't exists and the lock is not enabled (DAV with
669  // no lock or a simple web) the error will be dealt with inside lock() method
670  if ( eType == NOT_FOUND ||
671  eType == DAV )
672  {
673  lock( Environment );
674  if ( eType == NOT_FOUND )
675  {
676  m_eResourceType = UNKNOWN; // lock may have created it, need to check again
678  }
679  }
680  }
681  else if ( aCommand.Name == "unlock" )
682  {
683 
684  // unlock
685  // do not check for a DAV resource
686  // the lock store will be checked before sending
687  unlock( Environment );
688  }
689  else if ( aCommand.Name == "createNewContent" && isFolder( Environment ) )
690  {
691 
692  // createNewContent
693 
694 
695  ucb::ContentInfo aArg;
696  if ( !( aCommand.Argument >>= aArg ) )
697  {
699  uno::makeAny( lang::IllegalArgumentException(
700  "Wrong argument type!",
701  static_cast< cppu::OWeakObject * >( this ),
702  -1 ) ),
703  Environment );
704  // Unreachable
705  }
706 
707  aRet <<= createNewContent( aArg );
708  }
709  else if ( aCommand.Name == "addProperty" )
710  {
711  ucb::PropertyCommandArgument aPropArg;
712  if ( !( aCommand.Argument >>= aPropArg ))
713  {
715  uno::makeAny( lang::IllegalArgumentException(
716  "Wrong argument type!",
717  static_cast< cppu::OWeakObject * >( this ),
718  -1 ) ),
719  Environment );
720  }
721 
722  // TODO when/if XPropertyContainer is removed,
723  // the command execution can be canceled in addProperty
724  try
725  {
726  addProperty( aPropArg, Environment );
727  }
728  catch ( const beans::PropertyExistException &e )
729  {
730  ucbhelper::cancelCommandExecution( uno::makeAny( e ), Environment );
731  }
732  catch ( const beans::IllegalTypeException&e )
733  {
734  ucbhelper::cancelCommandExecution( uno::makeAny( e ), Environment );
735  }
736  catch ( const lang::IllegalArgumentException&e )
737  {
738  ucbhelper::cancelCommandExecution( uno::makeAny( e ), Environment );
739  }
740  }
741  else if ( aCommand.Name == "removeProperty" )
742  {
743  OUString sPropName;
744  if ( !( aCommand.Argument >>= sPropName ) )
745  {
747  uno::makeAny( lang::IllegalArgumentException(
748  "Wrong argument type!",
749  static_cast< cppu::OWeakObject * >( this ),
750  -1 ) ),
751  Environment );
752  }
753 
754  // TODO when/if XPropertyContainer is removed,
755  // the command execution can be canceled in removeProperty
756  try
757  {
758  removeProperty( sPropName, Environment );
759  }
760  catch( const beans::UnknownPropertyException &e )
761  {
762  ucbhelper::cancelCommandExecution( uno::makeAny( e ), Environment );
763  }
764  catch( const beans::NotRemoveableException &e )
765  {
766  ucbhelper::cancelCommandExecution( uno::makeAny( e ), Environment );
767  }
768  }
769  else
770  {
771 
772  // Unsupported command
773 
774 
776  uno::makeAny( ucb::UnsupportedCommandException(
777  aCommand.Name,
778  static_cast< cppu::OWeakObject * >( this ) ) ),
779  Environment );
780  // Unreachable
781  }
782 
783  SAL_INFO( "ucb.ucp.webdav", "Content::execute: end: command: " << aCommand.Name );
784 
785  return aRet;
786 }
787 
788 
789 // virtual
790 void SAL_CALL Content::abort( sal_Int32 /*CommandId*/ )
791 {
792  try
793  {
794  std::unique_ptr< DAVResourceAccess > xResAccess;
795  {
796  osl::MutexGuard aGuard( m_aMutex );
797  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
798  }
799  xResAccess->abort();
800  {
801  osl::Guard< osl::Mutex > aGuard( m_aMutex );
802  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
803  }
804  }
805  catch ( DAVException const & )
806  {
807  // abort failed!
808  }
809 }
810 
811 
812 // XPropertyContainer methods.
813 
814 
815 void Content::addProperty( const ucb::PropertyCommandArgument& aCmdArg,
816  const uno::Reference< ucb::XCommandEnvironment >& xEnv )
817 {
818 // if ( m_bTransient )
819 // @@@ ???
820 
821  if ( aCmdArg.Property.Name.isEmpty() )
822  throw lang::IllegalArgumentException(
823  "\"addProperty\" with empty Property.Name",
824  static_cast< cppu::OWeakObject * >( this ),
825  -1 );
826 
827  // Check property type.
828  if ( !UCBDeadPropertyValue::supportsType( aCmdArg.Property.Type ) )
829  {
830  throw beans::IllegalTypeException(
831  "\"addProperty\" unsupported Property.Type",
832  static_cast< cppu::OWeakObject * >( this ) );
833  }
834 
835  if ( aCmdArg.DefaultValue.hasValue()
836  && aCmdArg.DefaultValue.getValueType() != aCmdArg.Property.Type )
837  {
838  throw beans::IllegalTypeException(
839  "\"addProperty\" DefaultValue does not match Property.Type",
840  static_cast< ::cppu::OWeakObject * >( this ) );
841  }
842 
843 
844  // Make sure a property with the requested name does not already
845  // exist in dynamic and static(!) properties.
846 
847 
848  // Take into account special properties with custom namespace
849  // using <prop:the_propname xmlns:prop="the_namespace">
850  OUString aSpecialName;
851  bool bIsSpecial = DAVProperties::isUCBSpecialProperty(
852  aCmdArg.Property.Name, aSpecialName );
853 
854  // Note: This requires network access!
855  if ( getPropertySetInfo( xEnv, false /* don't cache data */ )
856  ->hasPropertyByName(
857  bIsSpecial ? aSpecialName : aCmdArg.Property.Name ) )
858  {
859  // Property does already exist.
860  throw beans::PropertyExistException();
861  }
862 
863 
864  // Add a new dynamic property.
865 
866 
867  ProppatchValue aValue(
868  PROPSET, aCmdArg.Property.Name, aCmdArg.DefaultValue );
869 
870  std::vector< ProppatchValue > aProppatchValues;
871  aProppatchValues.push_back( aValue );
872 
873  try
874  {
875  // Set property value at server.
876  std::unique_ptr< DAVResourceAccess > xResAccess;
877  {
878  osl::Guard< osl::Mutex > aGuard( m_aMutex );
879  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
880  }
881  aStaticDAVOptionsCache.removeDAVOptions( xResAccess->getURL() );
882  // clean cached value of PROPFIND property names
883  // PROPPATCH can change them
884  removeCachedPropertyNames( xResAccess->getURL() );
885  xResAccess->PROPPATCH( aProppatchValues, xEnv );
886  {
887  osl::Guard< osl::Mutex > aGuard( m_aMutex );
888  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
889  }
890 
891  // Notify propertyset info change listeners.
892  beans::PropertySetInfoChangeEvent evt(
893  static_cast< cppu::OWeakObject * >( this ),
894  bIsSpecial ? aSpecialName : aCmdArg.Property.Name,
895  -1, // No handle available
896  beans::PropertySetInfoChange::PROPERTY_INSERTED );
898  }
899  catch ( DAVException const & e )
900  {
901  if ( e.getStatus() == SC_FORBIDDEN )
902  {
903  // Support for setting arbitrary dead properties is optional!
904 
905  // Store property locally.
906  ContentImplHelper::addProperty(
907  bIsSpecial ? aSpecialName : aCmdArg.Property.Name,
908  aCmdArg.Property.Attributes, aCmdArg.DefaultValue );
909  }
910  else
911  {
913  {
914  try
915  {
916  ResourceType eType = getResourceType( xEnv );
917  switch ( eType )
918  {
919  case UNKNOWN:
920  case DAV:
921  throw lang::IllegalArgumentException();
922 
923  case FTP:
924  case NON_DAV:
925  // Store property locally.
926  ContentImplHelper::addProperty(
927  bIsSpecial ? aSpecialName : aCmdArg.Property.Name,
928  aCmdArg.Property.Attributes, aCmdArg.DefaultValue );
929  break;
930 
931  default:
932  SAL_WARN( "ucb.ucp.webdav", "Content::addProperty - "
933  "Unsupported resource type!" );
934  break;
935  }
936  }
937  catch ( uno::Exception const & )
938  {
939  SAL_WARN( "ucb.ucp.webdav", "Content::addProperty - "
940  "Unable to determine resource type!" );
941  }
942  }
943  else
944  {
945  SAL_WARN( "ucb.ucp.webdav", "Content::addProperty - "
946  "Unable to determine resource type!" );
947  }
948  }
949  }
950 }
951 
952 void Content::removeProperty( const OUString& Name,
953  const uno::Reference< ucb::XCommandEnvironment >& xEnv )
954 {
955 
956  // Try to remove property from server.
957 
958 
959  try
960  {
961  std::vector< ProppatchValue > aProppatchValues;
962  ProppatchValue aValue( PROPREMOVE, Name, uno::Any() );
963  aProppatchValues.push_back( aValue );
964 
965  // Remove property value from server.
966  std::unique_ptr< DAVResourceAccess > xResAccess;
967  {
968  osl::Guard< osl::Mutex > aGuard( m_aMutex );
969  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
970  }
971  aStaticDAVOptionsCache.removeDAVOptions( xResAccess->getURL() );
972  // clean cached value of PROPFIND property names
973  // PROPPATCH can change them
974  removeCachedPropertyNames( xResAccess->getURL() );
975  xResAccess->PROPPATCH( aProppatchValues, xEnv );
976  {
977  osl::Guard< osl::Mutex > aGuard( m_aMutex );
978  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
979  }
980 
981  // Notify propertyset info change listeners.
982  beans::PropertySetInfoChangeEvent evt(
983  static_cast< cppu::OWeakObject * >( this ),
984  Name,
985  -1, // No handle available
986  beans::PropertySetInfoChange::PROPERTY_REMOVED );
988  }
989  catch ( DAVException const & e )
990  {
991  if ( e.getStatus() == SC_FORBIDDEN )
992  {
993  // Support for setting arbitrary dead properties is optional!
994 
995  // Try to remove property from local store.
996  ContentImplHelper::removeProperty( Name );
997  }
998  else
999  {
1001  {
1002  try
1003  {
1004  ResourceType eType = getResourceType( xEnv );
1005  switch ( eType )
1006  {
1007  case UNKNOWN:
1008  case DAV:
1009  throw beans::UnknownPropertyException(Name);
1010 
1011  case FTP:
1012  case NON_DAV:
1013  // Try to remove property from local store.
1014  ContentImplHelper::removeProperty( Name );
1015  break;
1016 
1017  default:
1018  SAL_WARN( "ucb.ucp.webdav", "Content::removeProperty - "
1019  "Unsupported resource type!" );
1020  break;
1021  }
1022  }
1023  catch ( uno::Exception const & )
1024  {
1025  SAL_WARN( "ucb.ucp.webdav", "Content::removeProperty - "
1026  "Unable to determine resource type!" );
1027  }
1028  }
1029  else
1030  {
1031  SAL_WARN( "ucb.ucp.webdav", "Content::removeProperty - "
1032  "Unable to determine resource type!" );
1033 // throw beans::UnknownPropertyException();
1034  }
1035  }
1036  }
1037 }
1038 
1039 // virtual
1040 void SAL_CALL Content::addProperty( const OUString& Name,
1041  sal_Int16 Attributes,
1042  const uno::Any& DefaultValue )
1043 {
1044  beans::Property aProperty;
1045  aProperty.Name = Name;
1046  aProperty.Type = DefaultValue.getValueType();
1047  aProperty.Attributes = Attributes;
1048  aProperty.Handle = -1;
1049 
1050  addProperty( ucb::PropertyCommandArgument( aProperty, DefaultValue ),
1051  uno::Reference< ucb::XCommandEnvironment >());
1052 }
1053 
1054 // virtual
1055 void SAL_CALL Content::removeProperty( const OUString& Name )
1056 {
1057  removeProperty( Name,
1058  uno::Reference< ucb::XCommandEnvironment >() );
1059 }
1060 
1061 
1062 // XContentCreator methods.
1063 
1064 
1065 // virtual
1066 uno::Sequence< ucb::ContentInfo > SAL_CALL
1068 {
1069  osl::Guard< osl::Mutex > aGuard( m_aMutex );
1070 
1071  uno::Sequence< ucb::ContentInfo > aSeq( 2 );
1072 
1073  // document.
1074  aSeq.getArray()[ 0 ].Type = WEBDAV_CONTENT_TYPE;
1075  aSeq.getArray()[ 0 ].Attributes
1076  = ucb::ContentInfoAttribute::INSERT_WITH_INPUTSTREAM
1077  | ucb::ContentInfoAttribute::KIND_DOCUMENT;
1078 
1079  beans::Property aProp;
1081  "Title", aProp );
1082 
1083  uno::Sequence< beans::Property > aDocProps( 1 );
1084  aDocProps.getArray()[ 0 ] = aProp;
1085  aSeq.getArray()[ 0 ].Properties = aDocProps;
1086 
1087  // folder.
1088  aSeq.getArray()[ 1 ].Type = WEBDAV_COLLECTION_TYPE;
1089  aSeq.getArray()[ 1 ].Attributes
1090  = ucb::ContentInfoAttribute::KIND_FOLDER;
1091 
1092  uno::Sequence< beans::Property > aFolderProps( 1 );
1093  aFolderProps.getArray()[ 0 ] = aProp;
1094  aSeq.getArray()[ 1 ].Properties = aFolderProps;
1095  return aSeq;
1096 }
1097 
1098 
1099 // virtual
1100 uno::Reference< ucb::XContent > SAL_CALL
1101 Content::createNewContent( const ucb::ContentInfo& Info )
1102 {
1103  osl::Guard< osl::Mutex > aGuard( m_aMutex );
1104 
1105  if ( Info.Type.isEmpty() )
1106  return uno::Reference< ucb::XContent >();
1107 
1108  if ( ( Info.Type != WEBDAV_COLLECTION_TYPE ) && ( Info.Type != WEBDAV_CONTENT_TYPE ) )
1109  return uno::Reference< ucb::XContent >();
1110 
1111  OUString aURL = m_xIdentifier->getContentIdentifier();
1112 
1113  assert( !aURL.isEmpty() && "WebdavContent::createNewContent - empty identifier!" );
1114 
1115  if ( ( aURL.lastIndexOf( '/' ) + 1 ) != aURL.getLength() )
1116  aURL += "/";
1117 
1118  bool isCollection;
1119  if ( Info.Type == WEBDAV_COLLECTION_TYPE )
1120  {
1121  aURL += "New_Collection";
1122  isCollection = true;
1123  }
1124  else
1125  {
1126  aURL += "New_Content";
1127  isCollection = false;
1128  }
1129 
1130  uno::Reference< ucb::XContentIdentifier > xId(
1131  new ::ucbhelper::ContentIdentifier( aURL ) );
1132 
1133  // create the local content
1134  try
1135  {
1136  return new ::webdav_ucp::Content( m_xContext,
1137  m_pProvider,
1138  xId,
1139  m_xResAccess->getSessionFactory(),
1140  isCollection );
1141  }
1142  catch ( ucb::ContentCreationException & )
1143  {
1144  return uno::Reference< ucb::XContent >();
1145  }
1146 }
1147 
1148 
1149 // virtual
1151 {
1152  // <scheme>:// -> ""
1153  // <scheme>://foo -> ""
1154  // <scheme>://foo/ -> ""
1155  // <scheme>://foo/bar -> <scheme>://foo/
1156  // <scheme>://foo/bar/ -> <scheme>://foo/
1157  // <scheme>://foo/bar/abc -> <scheme>://foo/bar/
1158 
1159  OUString aURL = m_xIdentifier->getContentIdentifier();
1160 
1161  sal_Int32 nPos = aURL.lastIndexOf( '/' );
1162  if ( nPos == ( aURL.getLength() - 1 ) )
1163  {
1164  // Trailing slash found. Skip.
1165  nPos = aURL.lastIndexOf( '/', nPos );
1166  }
1167 
1168  sal_Int32 nPos1 = aURL.lastIndexOf( '/', nPos );
1169  if ( nPos1 != -1 )
1170  nPos1 = aURL.lastIndexOf( '/', nPos1 );
1171 
1172  if ( nPos1 == -1 )
1173  return OUString();
1174 
1175  return aURL.copy( 0, nPos + 1 );
1176 }
1177 
1178 
1179 // Non-interface methods.
1180 
1181 
1182 // static
1183 uno::Reference< sdbc::XRow > Content::getPropertyValues(
1184  const uno::Reference< uno::XComponentContext >& rxContext,
1185  const uno::Sequence< beans::Property >& rProperties,
1186  const ContentProperties& rData,
1188  const OUString& rContentId )
1189 {
1190  // Note: Empty sequence means "get values of all supported properties".
1191 
1193  = new ::ucbhelper::PropertyValueSet( rxContext );
1194 
1195  if ( rProperties.hasElements() )
1196  {
1197  uno::Reference< beans::XPropertySet > xAdditionalPropSet;
1198  bool bTriedToGetAdditionalPropSet = false;
1199 
1200  for ( const beans::Property& rProp : rProperties )
1201  {
1202  // Process standard UCB, DAV and HTTP properties.
1203  const uno::Any & rValue = rData.getValue( rProp.Name );
1204  if ( rValue.hasValue() )
1205  {
1206  xRow->appendObject( rProp, rValue );
1207  }
1208  else
1209  {
1210  // Process local Additional Properties.
1211  if ( !bTriedToGetAdditionalPropSet && !xAdditionalPropSet.is() )
1212  {
1213  xAdditionalPropSet =
1214  rProvider->getAdditionalPropertySet( rContentId,
1215  false );
1216  bTriedToGetAdditionalPropSet = true;
1217  }
1218 
1219  if ( !xAdditionalPropSet.is() ||
1220  !xRow->appendPropertySetValue(
1221  xAdditionalPropSet, rProp ) )
1222  {
1223  // Append empty entry.
1224  xRow->appendVoid( rProp );
1225  }
1226  }
1227  }
1228  }
1229  else
1230  {
1231  // Append all standard UCB, DAV and HTTP properties.
1232  const std::unique_ptr< PropertyValueMap > & xProps = rData.getProperties();
1233 
1234  ContentProvider * pProvider
1235  = static_cast< ContentProvider * >( rProvider.get() );
1236  beans::Property aProp;
1237 
1238  for ( const auto& rProp : *xProps )
1239  {
1240  pProvider->getProperty( rProp.first, aProp );
1241  xRow->appendObject( aProp, rProp.second.value() );
1242  }
1243 
1244  // Append all local Additional Properties.
1245  uno::Reference< beans::XPropertySet > xSet =
1246  rProvider->getAdditionalPropertySet( rContentId, false );
1247  xRow->appendPropertySet( xSet );
1248  }
1249 
1250  return xRow;
1251 }
1252 
1253 namespace {
1254 void GetPropsUsingHeadRequest(DAVResource& resource,
1255  const std::unique_ptr< DAVResourceAccess >& xResAccess,
1256  const std::vector< OUString >& aHTTPNames,
1257  const uno::Reference< ucb::XCommandEnvironment >& xEnv)
1258 {
1259  if (aHTTPNames.empty())
1260  return;
1261 
1262  DAVOptions aDAVOptions;
1263  OUString aTargetURL = xResAccess->getURL();
1264  // retrieve the cached options if any
1265  aStaticDAVOptionsCache.getDAVOptions(aTargetURL, aDAVOptions);
1266 
1267  // clean cached value of PROPFIND property names
1268  // PROPPATCH can change them
1270  // test if HEAD allowed, if not, throw, should be caught immediately
1271  // SC_GONE used internally by us, see comment in Content::getPropertyValues
1272  // in the catch scope
1273  if (aDAVOptions.getHttpResponseStatusCode() != SC_GONE &&
1274  !aDAVOptions.isHeadAllowed())
1275  {
1276  throw DAVException(DAVException::DAV_HTTP_ERROR, "405 Not Implemented", SC_METHOD_NOT_ALLOWED);
1277  }
1278  // if HEAD is enabled on this site
1279  // check if there is a relevant HTTP response status code cached
1280  if (aDAVOptions.getHttpResponseStatusCode() != SC_NONE)
1281  {
1282  // throws exception as if there was a server error, a DAV exception
1284  aDAVOptions.getHttpResponseStatusText(),
1285  aDAVOptions.getHttpResponseStatusCode());
1286  // Unreachable
1287  }
1288 
1289  xResAccess->HEAD(aHTTPNames, resource, xEnv);
1290 }
1291 }
1292 
1293 uno::Reference< sdbc::XRow > Content::getPropertyValues(
1294  const uno::Sequence< beans::Property >& rProperties,
1295  const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1296 {
1297  std::unique_ptr< ContentProperties > xProps;
1298  std::unique_ptr< ContentProperties > xCachedProps;
1299  std::unique_ptr< DAVResourceAccess > xResAccess;
1300  OUString aUnescapedTitle;
1301  bool bHasAll = false;
1302  uno::Reference< ucb::XContentIdentifier > xIdentifier;
1304 
1305  {
1306  osl::Guard< osl::Mutex > aGuard( m_aMutex );
1307 
1308  aUnescapedTitle = NeonUri::unescape( m_aEscapedTitle );
1309  xIdentifier.set( m_xIdentifier );
1310  xProvider = m_xProvider;
1311  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
1312 
1313  // First, ask cache...
1314  if (m_xCachedProps)
1315  {
1316  xCachedProps.reset(new ContentProperties(*m_xCachedProps));
1317 
1318  std::vector< OUString > aMissingProps;
1319  if ( xCachedProps->containsAllNames( rProperties, aMissingProps ) )
1320  {
1321  // All properties are already in cache! No server access needed.
1322  bHasAll = true;
1323  }
1324 
1325  // use the cached ContentProperties instance
1326  xProps.reset(new ContentProperties(*xCachedProps));
1327  }
1328  }
1329 
1330  if ( !m_bTransient && !bHasAll )
1331  {
1332 
1333  // Obtain values from server...
1334 
1335 
1336  // First, identify whether resource is DAV or not
1337  bool bNetworkAccessAllowed = true;
1338  ResourceType eType = getResourceType(
1339  xEnv, xResAccess, &bNetworkAccessAllowed );
1340 
1341  if ( eType == DAV )
1342  {
1343  // cache lookup... getResourceType may fill the props cache via
1344  // PROPFIND!
1345  if (m_xCachedProps)
1346  {
1347  xCachedProps.reset(new ContentProperties(*m_xCachedProps));
1348 
1349  std::vector< OUString > aMissingProps;
1350  if ( xCachedProps->containsAllNames(
1351  rProperties, aMissingProps ) )
1352  {
1353  // All properties are already in cache! No server access
1354  // needed.
1355  bHasAll = true;
1356  }
1357 
1358  // use the cached ContentProperties instance
1359  xProps.reset(new ContentProperties(*xCachedProps));
1360  }
1361 
1362  if ( !bHasAll )
1363  {
1364  // Only DAV resources support PROPFIND
1365  std::vector< OUString > aPropNames;
1366 
1367  // FIXME: Should m_aFailedPropNames be checked here? It got effectively
1368  // ignored since commit 26e6d4b05ab444e6a7529ffcac7fbe592fc94833.
1369  if ( rProperties.hasElements() )
1371  rProperties, aPropNames );
1372 
1373  if ( !aPropNames.empty() )
1374  {
1375  std::vector< DAVResource > resources;
1376  try
1377  {
1378  xResAccess->PROPFIND(
1379  DAVZERO, aPropNames, resources, xEnv );
1380 
1381  if ( 1 == resources.size() )
1382  {
1383 #if defined SAL_LOG_INFO
1384  {//debug
1385  // print received resources
1386  for ( const auto& rProp : resources[0].properties )
1387  {
1388  OUString aPropValue;
1389  bool bValue;
1390  uno::Sequence< ucb::LockEntry > aSupportedLocks;
1391  if( rProp.Value >>= aPropValue )
1392  SAL_INFO( "ucb.ucp.webdav", "PROPFIND (getPropertyValues) - returned property: " << rProp.Name << ":" << aPropValue );
1393  else if( rProp.Value >>= bValue )
1394  SAL_INFO( "ucb.ucp.webdav", "PROPFIND (getPropertyValues) - returned property: " << rProp.Name << ":" <<
1395  ( bValue ? "true" : "false" ) );
1396  else if( rProp.Value >>= aSupportedLocks )
1397  {
1398  SAL_INFO( "ucb.ucp.webdav", "PROPFIND (getPropertyValues) - returned property: " << rProp.Name << ":" );
1399  for ( sal_Int32 n = 0; n < aSupportedLocks.getLength(); ++n )
1400  {
1401  SAL_INFO( "ucb.ucp.webdav"," scope: "
1402  << ( aSupportedLocks[ n ].Scope == css::ucb::LockScope_SHARED ? "shared" : "exclusive" )
1403  << ", type: "
1404  << ( aSupportedLocks[ n ].Type != css::ucb::LockType_WRITE ? "" : "write" ) );
1405  }
1406  }
1407  }
1408  }
1409 #endif
1410  if (xProps)
1411  xProps->addProperties(
1412  aPropNames,
1413  ContentProperties( resources[ 0 ] ));
1414  else
1415  xProps.reset(
1416  new ContentProperties( resources[ 0 ] ) );
1417  }
1418  }
1419  catch ( DAVException const & e )
1420  {
1421  bNetworkAccessAllowed = bNetworkAccessAllowed
1423 
1424  if ( !bNetworkAccessAllowed )
1425  {
1426  cancelCommandExecution( e, xEnv );
1427  // unreachable
1428  }
1429  }
1430  }
1431  }
1432  }
1433 
1434  if ( bNetworkAccessAllowed )
1435  {
1436  // All properties obtained already?
1437  std::vector< OUString > aMissingProps;
1438  if ( !( xProps
1439  && xProps->containsAllNames(
1440  rProperties, aMissingProps ) )
1441  && !m_bDidGetOrHead )
1442  {
1443  // Possibly the missing props can be obtained using a HEAD
1444  // request.
1445 
1446  std::vector< OUString > aHeaderNames;
1448  rProperties,
1449  aHeaderNames );
1450 
1451  if( eType != DAV )
1452  {
1453  // in case of not DAV PROFIND (previously in program flow) failed
1454  // so we need to add the only prop that's common
1455  // to DAV and NON_DAV: MediaType, that maps to Content-Type
1456  aHeaderNames.emplace_back("Content-Type" );
1457  }
1458 
1459  if (!aHeaderNames.empty()) try
1460  {
1461  DAVResource resource;
1462  GetPropsUsingHeadRequest(resource, xResAccess, aHeaderNames, xEnv);
1463  m_bDidGetOrHead = true;
1464 
1465  if (xProps)
1466  xProps->addProperties(
1467  aMissingProps,
1468  ContentProperties(resource));
1469  else
1470  xProps.reset(new ContentProperties(resource));
1471 
1472  if (m_eResourceType == NON_DAV)
1473  xProps->addProperties(aMissingProps,
1475  aUnescapedTitle,
1476  false));
1477  }
1478  catch ( DAVException const & e )
1479  {
1480  // non "general-purpose servers" may not support HEAD requests
1481  // see http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.1
1482  // In this case, perform a partial GET only to get the header info
1483  // vid. http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
1484  // WARNING if the server does not support partial GETs,
1485  // the GET will transfer the whole content
1486  bool bError = true;
1487  DAVException aLastException = e;
1488  OUString aTargetURL = xResAccess->getURL();
1489 
1491  {
1492  // According to the spec. the origin server SHOULD return
1493  // * 405 (Method Not Allowed):
1494  // the method is known but not allowed for the requested resource
1495  // * 501 (Not Implemented):
1496  // the method is unrecognized or not implemented
1497  // * 404 (SC_NOT_FOUND)
1498  // is for google-code server and for MS IIS 10.0 Web server
1499  // when only GET is enabled
1500  if ( aLastException.getStatus() == SC_NOT_IMPLEMENTED ||
1501  aLastException.getStatus() == SC_METHOD_NOT_ALLOWED ||
1502  aLastException.getStatus() == SC_NOT_FOUND )
1503  {
1504  SAL_WARN( "ucb.ucp.webdav", "HEAD probably not implemented: fall back to a partial GET" );
1505  aStaticDAVOptionsCache.setHeadAllowed( aTargetURL, false );
1506  lcl_sendPartialGETRequest( bError,
1507  aLastException,
1508  aMissingProps,
1509  aHeaderNames,
1510  xResAccess,
1511  xProps,
1512  xEnv );
1513  m_bDidGetOrHead = !bError;
1514  }
1515  }
1516 
1517  if ( bError )
1518  {
1519  DAVOptions aDAVOptionsException;
1520 
1521  aDAVOptionsException.setURL( aTargetURL );
1522  // check if the error was SC_NOT_FOUND, meaning that the
1523  // GET fall back didn't succeeded and the element is really missing
1524  // we will consider the resource SC_GONE (410) for some time
1525  // we use SC_GONE because has the same meaning of SC_NOT_FOUND (404)
1526  // see:
1527  // <https://tools.ietf.org/html/rfc7231#section-6.5.9> (retrieved 2016-10-09)
1528  // apparently it's not used to mark the missing HEAD method (so far...)
1529  sal_uInt16 ResponseStatusCode =
1530  ( aLastException.getStatus() == SC_NOT_FOUND ) ?
1531  SC_GONE :
1532  aLastException.getStatus();
1533  aDAVOptionsException.setHttpResponseStatusCode( ResponseStatusCode );
1534  aDAVOptionsException.setHttpResponseStatusText( aLastException.getData() );
1535  aStaticDAVOptionsCache.addDAVOptions( aDAVOptionsException,
1537 
1538  if ( !shouldAccessNetworkAfterException( aLastException ) )
1539  {
1540  cancelCommandExecution( aLastException, xEnv );
1541  // unreachable
1542  }
1543  }
1544  }
1545  }
1546  }
1547 
1548  // might trigger HTTP redirect.
1549  // Therefore, title must be updated here.
1550  NeonUri aUri( xResAccess->getURL() );
1551  aUnescapedTitle = aUri.GetPathBaseNameUnescaped();
1552 
1553  if ( eType == UNKNOWN )
1554  {
1555  xProps.reset( new ContentProperties( aUnescapedTitle ) );
1556  }
1557 
1558  // For DAV resources we only know the Title, for non-DAV
1559  // resources we additionally know that it is a document.
1560 
1561  if ( eType == DAV )
1562  {
1563  if (!xProps)
1564  xProps.reset(new ContentProperties(aUnescapedTitle));
1565  else
1566  xProps->addProperty("Title", uno::makeAny(aUnescapedTitle), true);
1567  }
1568  else
1569  {
1570  if (!xProps)
1571  xProps.reset( new ContentProperties( aUnescapedTitle, false ) );
1572  else
1573  xProps->addProperty(
1574  "Title",
1575  uno::makeAny( aUnescapedTitle ),
1576  true );
1577 
1578  xProps->addProperty(
1579  "IsFolder",
1580  uno::makeAny( false ),
1581  true );
1582  xProps->addProperty(
1583  "IsDocument",
1584  uno::makeAny( true ),
1585  true );
1586  }
1587  }
1588  else
1589  {
1590  // No server access for just created (not yet committed) objects.
1591  // Only a minimal set of properties supported at this stage.
1592  if (m_bTransient)
1593  xProps.reset( new ContentProperties( aUnescapedTitle,
1594  m_bCollection ) );
1595  }
1596 
1597  // Add a default for the properties requested but not found.
1598  // Determine still missing properties, add a default.
1599  // Some client function doesn't expect a void uno::Any,
1600  // but instead wants some sort of default.
1601  std::vector< OUString > aMissingProps;
1602  if ( !xProps->containsAllNames(
1603  rProperties, aMissingProps ) )
1604  {
1605  //
1606  for ( const auto& rProp : aMissingProps )
1607  {
1608  // For the time being only a couple of properties need to be added
1609  if ( rProp == "DateModified" || rProp == "DateCreated" )
1610  {
1611  util::DateTime aDate;
1612  xProps->addProperty(
1613  rProp,
1614  uno::makeAny( aDate ),
1615  true );
1616  }
1617  // If WebDAV didn't return the resource type, assume default
1618  // This happens e.g. for lists exported by SharePoint
1619  else if ( rProp == "IsFolder" )
1620  {
1621  xProps->addProperty(
1622  rProp,
1623  uno::makeAny( false ),
1624  true );
1625  }
1626  else if ( rProp == "IsDocument" )
1627  {
1628  xProps->addProperty(
1629  rProp,
1630  uno::makeAny( true ),
1631  true );
1632  }
1633  }
1634  }
1635 
1636  for ( const auto& rProperty : rProperties )
1637  {
1638  const OUString rName = rProperty.Name;
1639  if ( rName == "BaseURI" )
1640  {
1641  // Add BaseURI property, if requested.
1642  xProps->addProperty(
1643  "BaseURI",
1644  uno::makeAny( getBaseURI( xResAccess ) ),
1645  true );
1646  }
1647  else if ( rName == "CreatableContentsInfo" )
1648  {
1649  // Add CreatableContentsInfo property, if requested.
1650  bool bFolder = false;
1651  xProps->getValue(
1652  "IsFolder" )
1653  >>= bFolder;
1654  xProps->addProperty(
1655  "CreatableContentsInfo",
1656  uno::makeAny( bFolder
1658  : uno::Sequence< ucb::ContentInfo >() ),
1659  true );
1660  }
1661  }
1662 
1663  uno::Reference< sdbc::XRow > xResultRow
1665  rProperties,
1666  *xProps,
1667  xProvider,
1668  xIdentifier->getContentIdentifier() );
1669 
1670  {
1671  osl::Guard< osl::Mutex > aGuard( m_aMutex );
1672 
1673  if (!m_xCachedProps)
1674  m_xCachedProps.reset(new CachableContentProperties(*xProps));
1675  else
1676  m_xCachedProps->addProperties(*xProps);
1677 
1678  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
1679  m_aEscapedTitle = NeonUri::escapeSegment( aUnescapedTitle );
1680  }
1681 
1682  return xResultRow;
1683 }
1684 
1685 
1686 uno::Sequence< uno::Any > Content::setPropertyValues(
1687  const uno::Sequence< beans::PropertyValue >& rValues,
1688  const uno::Reference< ucb::XCommandEnvironment >& xEnv )
1689 {
1690  uno::Reference< ucb::XContentIdentifier > xIdentifier;
1692  bool bTransient;
1693  std::unique_ptr< DAVResourceAccess > xResAccess;
1694 
1695  {
1696  osl::Guard< osl::Mutex > aGuard( m_aMutex );
1697 
1698  xProvider.set( m_pProvider );
1699  xIdentifier.set( m_xIdentifier );
1700  bTransient = m_bTransient;
1701  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
1702  }
1703 
1704  uno::Sequence< uno::Any > aRet( rValues.getLength() );
1705  uno::Sequence< beans::PropertyChangeEvent > aChanges( rValues.getLength() );
1706  sal_Int32 nChanged = 0;
1707 
1708  beans::PropertyChangeEvent aEvent;
1709  aEvent.Source = static_cast< cppu::OWeakObject * >( this );
1710  aEvent.Further = false;
1711  // aEvent.PropertyName =
1712  aEvent.PropertyHandle = -1;
1713  // aEvent.OldValue =
1714  // aEvent.NewValue =
1715 
1716  std::vector< ProppatchValue > aProppatchValues;
1717 
1718  uno::Reference< ucb::XPersistentPropertySet > xAdditionalPropSet;
1719  bool bTriedToGetAdditionalPropSet = false;
1720 
1721  bool bExchange = false;
1722  OUString aNewTitle;
1723  OUString aOldTitle;
1724  sal_Int32 nTitlePos = -1;
1725 
1726  uno::Reference< beans::XPropertySetInfo > xInfo;
1727 
1728  const beans::PropertyValue* pValues = rValues.getConstArray();
1729  sal_Int32 nCount = rValues.getLength();
1730  for ( sal_Int32 n = 0; n < nCount; ++n )
1731  {
1732  const beans::PropertyValue& rValue = pValues[ n ];
1733  const OUString & rName = rValue.Name;
1734 
1735  beans::Property aTmpProp;
1736  xProvider->getProperty( rName, aTmpProp );
1737 
1738  if ( aTmpProp.Attributes & beans::PropertyAttribute::READONLY )
1739  {
1740  // Read-only property!
1741  aRet[ n ] <<= lang::IllegalAccessException(
1742  "Property is read-only!",
1743  static_cast< cppu::OWeakObject * >( this ) );
1744  continue;
1745  }
1746 
1747 
1748  // Mandatory props.
1749 
1750 
1751  if ( rName == "ContentType" )
1752  {
1753  // Read-only property!
1754  aRet[ n ] <<= lang::IllegalAccessException(
1755  "Property is read-only!",
1756  static_cast< cppu::OWeakObject * >( this ) );
1757  }
1758  else if ( rName == "IsDocument" )
1759  {
1760  // Read-only property!
1761  aRet[ n ] <<= lang::IllegalAccessException(
1762  "Property is read-only!",
1763  static_cast< cppu::OWeakObject * >( this ) );
1764  }
1765  else if ( rName == "IsFolder" )
1766  {
1767  // Read-only property!
1768  aRet[ n ] <<= lang::IllegalAccessException(
1769  "Property is read-only!",
1770  static_cast< cppu::OWeakObject * >( this ) );
1771  }
1772  else if ( rName == "Title" )
1773  {
1774  OUString aNewValue;
1775  if ( rValue.Value >>= aNewValue )
1776  {
1777  // No empty titles!
1778  if ( !aNewValue.isEmpty() )
1779  {
1780  try
1781  {
1782  NeonUri aURI( xIdentifier->getContentIdentifier() );
1783  aOldTitle = aURI.GetPathBaseNameUnescaped();
1784 
1785  if ( aNewValue != aOldTitle )
1786  {
1787  // modified title -> modified URL -> exchange !
1788  if ( !bTransient )
1789  bExchange = true;
1790 
1791  // new value will be set later...
1792  aNewTitle = aNewValue;
1793 
1794  // remember position within sequence of values (for
1795  // error handling).
1796  nTitlePos = n;
1797  }
1798  }
1799  catch ( DAVException const & )
1800  {
1801  aRet[ n ] <<= lang::IllegalArgumentException(
1802  "Invalid content identifier!",
1803  static_cast< cppu::OWeakObject * >( this ),
1804  -1 );
1805  }
1806  }
1807  else
1808  {
1809  aRet[ n ] <<= lang::IllegalArgumentException(
1810  "Empty title not allowed!",
1811  static_cast< cppu::OWeakObject * >( this ),
1812  -1 );
1813  }
1814  }
1815  else
1816  {
1817  aRet[ n ] <<= beans::IllegalTypeException(
1818  "Property value has wrong type!",
1819  static_cast< cppu::OWeakObject * >( this ) );
1820  }
1821  }
1822  else
1823  {
1824 
1825  // Optional props.
1826 
1827 
1828  OUString aSpecialName;
1829  bool bIsSpecial = DAVProperties::isUCBSpecialProperty(
1830  rName, aSpecialName );
1831 
1832  if ( !xInfo.is() )
1833  xInfo = getPropertySetInfo( xEnv,
1834  false /* don't cache data */ );
1835 
1836  if ( !xInfo->hasPropertyByName(
1837  bIsSpecial ? aSpecialName : rName ) )
1838  {
1839  // Check, whether property exists. Skip otherwise.
1840  // PROPPATCH::set would add the property automatically, which
1841  // is not allowed for "setPropertyValues" command!
1842  aRet[ n ] <<= beans::UnknownPropertyException(
1843  "Property is unknown!",
1844  static_cast< cppu::OWeakObject * >( this ) );
1845  continue;
1846  }
1847 
1848  if ( rName == "Size" )
1849  {
1850  // Read-only property!
1851  aRet[ n ] <<= lang::IllegalAccessException(
1852  "Property is read-only!",
1853  static_cast< cppu::OWeakObject * >( this ) );
1854  }
1855  else if ( rName == "DateCreated" )
1856  {
1857  // Read-only property!
1858  aRet[ n ] <<= lang::IllegalAccessException(
1859  "Property is read-only!",
1860  static_cast< cppu::OWeakObject * >( this ) );
1861  }
1862  else if ( rName == "DateModified" )
1863  {
1864  // Read-only property!
1865  aRet[ n ] <<= lang::IllegalAccessException(
1866  "Property is read-only!",
1867  static_cast< cppu::OWeakObject * >( this ) );
1868  }
1869  else if ( rName == "MediaType" )
1870  {
1871  // Read-only property!
1872  // (but could be writable, if 'getcontenttype' would be)
1873  aRet[ n ] <<= lang::IllegalAccessException(
1874  "Property is read-only!",
1875  static_cast< cppu::OWeakObject * >( this ) );
1876  }
1877  if ( rName == "CreatableContentsInfo" )
1878  {
1879  // Read-only property!
1880  aRet[ n ] <<= lang::IllegalAccessException(
1881  "Property is read-only!",
1882  static_cast< cppu::OWeakObject * >( this ) );
1883  }
1884  else
1885  {
1886  if ( getResourceType( xEnv, xResAccess ) == DAV )
1887  {
1888  // Property value will be set on server.
1889  ProppatchValue aValue( PROPSET, rName, rValue.Value );
1890  aProppatchValues.push_back( aValue );
1891  }
1892  else
1893  {
1894  // Property value will be stored in local property store.
1895  if ( !bTriedToGetAdditionalPropSet &&
1896  !xAdditionalPropSet.is() )
1897  {
1898  xAdditionalPropSet
1899  = getAdditionalPropertySet( false );
1900  bTriedToGetAdditionalPropSet = true;
1901  }
1902 
1903  if ( xAdditionalPropSet.is() )
1904  {
1905  try
1906  {
1907  uno::Any aOldValue
1908  = xAdditionalPropSet->getPropertyValue( rName );
1909  if ( aOldValue != rValue.Value )
1910  {
1911  xAdditionalPropSet->setPropertyValue(
1912  rName, rValue.Value );
1913 
1914  aEvent.PropertyName = rName;
1915  aEvent.OldValue = aOldValue;
1916  aEvent.NewValue = rValue.Value;
1917 
1918  aChanges.getArray()[ nChanged ] = aEvent;
1919  nChanged++;
1920  }
1921  }
1922  catch ( beans::UnknownPropertyException const & e )
1923  {
1924  aRet[ n ] <<= e;
1925  }
1926  catch ( lang::WrappedTargetException const & e )
1927  {
1928  aRet[ n ] <<= e;
1929  }
1930  catch ( beans::PropertyVetoException const & e )
1931  {
1932  aRet[ n ] <<= e;
1933  }
1934  catch ( lang::IllegalArgumentException const & e )
1935  {
1936  aRet[ n ] <<= e;
1937  }
1938  }
1939  else
1940  {
1941  aRet[ n ] <<= uno::Exception(
1942  "No property set for storing the value!",
1943  static_cast< cppu::OWeakObject * >( this ) );
1944  }
1945  }
1946  }
1947  }
1948  } // for
1949 
1950  if ( !bTransient && !aProppatchValues.empty() )
1951  {
1952  try
1953  {
1954  // clean cached value of PROPFIND property names
1955  // PROPPATCH can change them
1956  removeCachedPropertyNames( xResAccess->getURL() );
1957  // Set property values at server.
1958  aStaticDAVOptionsCache.removeDAVOptions( xResAccess->getURL() );
1959  xResAccess->PROPPATCH( aProppatchValues, xEnv );
1960 
1961  for ( const auto& rProppatchValue : aProppatchValues )
1962  {
1963  aEvent.PropertyName = rProppatchValue.name;
1964  aEvent.OldValue = uno::Any(); // @@@ too expensive to obtain!
1965  aEvent.NewValue = rProppatchValue.value;
1966 
1967  aChanges.getArray()[ nChanged ] = aEvent;
1968  nChanged++;
1969  }
1970  }
1971  catch ( DAVException const & e )
1972  {
1973  SAL_WARN( "ucb.ucp.webdav", "Content::setPropertyValues - PROPPATCH failed!" );
1974  cancelCommandExecution( e, xEnv );
1975  // unreachable
1976  }
1977  }
1978 
1979  if ( bExchange )
1980  {
1981  // Assemble new content identifier...
1982 
1983  OUString aNewURL = getParentURL();
1984  if ( aNewURL.lastIndexOf( '/' ) != ( aNewURL.getLength() - 1 ) )
1985  aNewURL += "/";
1986 
1987  aNewURL += NeonUri::escapeSegment( aNewTitle );
1988 
1989  uno::Reference< ucb::XContentIdentifier > xNewId
1990  = new ::ucbhelper::ContentIdentifier( aNewURL );
1991 
1992  NeonUri sourceURI( xIdentifier->getContentIdentifier() );
1993  NeonUri targetURI( xNewId->getContentIdentifier() );
1994 
1995  try
1996  {
1997  targetURI.SetScheme( sourceURI.GetScheme() );
1998 
1999  // clean cached value of PROPFIND property names
2000  removeCachedPropertyNames( sourceURI.GetURI() );
2001  removeCachedPropertyNames( targetURI.GetURI() );
2002  aStaticDAVOptionsCache.removeDAVOptions( sourceURI.GetURI() );
2003  aStaticDAVOptionsCache.removeDAVOptions( targetURI.GetURI() );
2004  xResAccess->MOVE(
2005  sourceURI.GetPath(), targetURI.GetURI(), false, xEnv );
2006 
2007  // @@@ Should check for resources that could not be moved
2008  // (due to source access or target overwrite) and send
2009  // this information through the interaction handler.
2010 
2011  // @@@ Existing content should be checked to see if it needs
2012  // to be deleted at the source
2013 
2014  // @@@ Existing content should be checked to see if it has
2015  // been overwritten at the target
2016 
2017  if ( exchangeIdentity( xNewId ) )
2018  {
2019  xResAccess->setURL( aNewURL );
2020 
2021 // DAV resources store all additional props on server!
2022 // // Adapt Additional Core Properties.
2023 // renameAdditionalPropertySet( xOldId->getContentIdentifier(),
2024 // xNewId->getContentIdentifier(),
2025 // sal_True );
2026  }
2027  else
2028  {
2029  // Do not set new title!
2030  aNewTitle.clear();
2031 
2032  // Set error .
2033  aRet[ nTitlePos ] <<= uno::Exception(
2034  "Exchange failed!",
2035  static_cast< cppu::OWeakObject * >( this ) );
2036  }
2037  }
2038  catch ( DAVException const & e )
2039  {
2040  // Do not set new title!
2041  aNewTitle.clear();
2042 
2043  // Set error .
2044  aRet[ nTitlePos ] = MapDAVException( e, true );
2045  }
2046  }
2047 
2048  if ( !aNewTitle.isEmpty() )
2049  {
2050  osl::Guard< osl::Mutex > aGuard( m_aMutex );
2051 
2052  aEvent.PropertyName = "Title";
2053  aEvent.OldValue <<= aOldTitle;
2054  aEvent.NewValue <<= aNewTitle;
2055 
2056  m_aEscapedTitle = NeonUri::escapeSegment( aNewTitle );
2057 
2058  aChanges.getArray()[ nChanged ] = aEvent;
2059  nChanged++;
2060  }
2061 
2062  if ( nChanged > 0 )
2063  {
2064  aChanges.realloc( nChanged );
2065  notifyPropertiesChange( aChanges );
2066  }
2067 
2068  {
2069  osl::Guard< osl::Mutex > aGuard( m_aMutex );
2070  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
2071  }
2072 
2073  return aRet;
2074 }
2075 
2076 
2078  const ucb::OpenCommandArgument3 & rArg,
2079  const uno::Reference< ucb::XCommandEnvironment > & xEnv )
2080 {
2081  uno::Any aRet;
2082 
2083  bool bOpenFolder = ( ( rArg.Mode == ucb::OpenMode::ALL ) ||
2084  ( rArg.Mode == ucb::OpenMode::FOLDERS ) ||
2085  ( rArg.Mode == ucb::OpenMode::DOCUMENTS ) );
2086  if ( bOpenFolder )
2087  {
2088  if ( isFolder( xEnv ) )
2089  {
2090  // Open collection.
2091 
2092  uno::Reference< ucb::XDynamicResultSet > xSet
2093  = new DynamicResultSet( m_xContext, this, rArg, xEnv );
2094  aRet <<= xSet;
2095  }
2096  else
2097  {
2098  // Error: Not a folder!
2099 
2100  OUStringBuffer aMsg;
2101  if ( getResourceType( xEnv ) == FTP )
2102  {
2103  aMsg.append( "FTP over HTTP proxy: resource cannot "
2104  "be opened as folder! Wrong Open Mode!" );
2105  }
2106  else
2107  {
2108  aMsg.append( "Non-folder resource cannot be "
2109  "opened as folder! Wrong Open Mode!" );
2110  }
2111 
2113  uno::makeAny(
2114  lang::IllegalArgumentException(
2115  aMsg.makeStringAndClear(),
2116  static_cast< cppu::OWeakObject * >( this ),
2117  -1 ) ),
2118  xEnv );
2119  // Unreachable
2120  }
2121  }
2122 
2123  if ( rArg.Sink.is() )
2124  {
2125  // Open document.
2126 
2127  if ( ( rArg.Mode == ucb::OpenMode::DOCUMENT_SHARE_DENY_NONE ) ||
2128  ( rArg.Mode == ucb::OpenMode::DOCUMENT_SHARE_DENY_WRITE ) )
2129  {
2130  // Currently(?) unsupported.
2132  uno::makeAny(
2133  ucb::UnsupportedOpenModeException(
2134  OUString(),
2135  static_cast< cppu::OWeakObject * >( this ),
2136  sal_Int16( rArg.Mode ) ) ),
2137  xEnv );
2138  // Unreachable
2139  }
2140 
2141  uno::Reference< io::XOutputStream > xOut( rArg.Sink, uno::UNO_QUERY );
2142  if ( xOut.is() )
2143  {
2144  // PUSH: write data
2145  try
2146  {
2147  std::unique_ptr< DAVResourceAccess > xResAccess;
2148 
2149  {
2150  osl::MutexGuard aGuard( m_aMutex );
2151 
2152  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
2153  }
2154 
2155  xResAccess->setFlags( rArg.OpeningFlags );
2156  DAVResource aResource;
2157  std::vector< OUString > aHeaders;
2158 
2159  removeCachedPropertyNames( xResAccess->getURL() );
2160  xResAccess->GET( xOut, aHeaders, aResource, xEnv );
2161  m_bDidGetOrHead = true;
2162 
2163  {
2164  osl::MutexGuard aGuard( m_aMutex );
2165 
2166  // cache headers.
2167  if (!m_xCachedProps)
2168  m_xCachedProps.reset(
2169  new CachableContentProperties( ContentProperties( aResource ) ) );
2170  else
2171  m_xCachedProps->addProperties( ContentProperties( aResource ) );
2172 
2173  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
2174  }
2175  }
2176  catch ( DAVException const & e )
2177  {
2178  cancelCommandExecution( e, xEnv );
2179  // Unreachable
2180  }
2181  }
2182  else
2183  {
2184  uno::Reference< io::XActiveDataSink > xDataSink( rArg.Sink, uno::UNO_QUERY );
2185  if ( xDataSink.is() )
2186  {
2187  // PULL: wait for client read
2188  OUString aTargetURL = m_xIdentifier->getContentIdentifier();
2189  try
2190  {
2191  std::unique_ptr< DAVResourceAccess > xResAccess;
2192  {
2193  osl::MutexGuard aGuard( m_aMutex );
2194 
2195  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
2196  }
2197  xResAccess->setFlags( rArg.OpeningFlags );
2198 
2199  // fill inputstream sync; return if all data present
2200  DAVResource aResource;
2201  std::vector< OUString > aHeaders;
2202 
2203  aTargetURL = xResAccess->getURL();
2204  removeCachedPropertyNames( aTargetURL );
2205  // check if the resource was present on the server
2206  // first update it, if necessary
2207  // if the open is called directly, without the default open sequence,
2208  // e.g. the one used when opening a file looking for properties
2209  // first this call will have no effect, since OPTIONS would have already been called
2210  // as a consequence of getPropertyValues()
2211  DAVOptions aDAVOptions;
2212  getResourceOptions( xEnv, aDAVOptions, xResAccess );
2213 
2214  if ( aDAVOptions.getHttpResponseStatusCode() != SC_NONE )
2215  {
2216  // throws exception as if there was a server error, a DAV exception
2218  aDAVOptions.getHttpResponseStatusText(),
2219  aDAVOptions.getHttpResponseStatusCode() );
2220  }
2221  uno::Reference< io::XInputStream > xIn
2222  = xResAccess->GET( aHeaders, aResource, xEnv );
2223  m_bDidGetOrHead = true;
2224 
2225  {
2226  osl::MutexGuard aGuard( m_aMutex );
2227 
2228  // cache headers.
2229  if (!m_xCachedProps)
2230  m_xCachedProps.reset(
2231  new CachableContentProperties( ContentProperties( aResource ) ) );
2232  else
2233  m_xCachedProps->addProperties(
2234  aResource.properties );
2235 
2236  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
2237  }
2238 
2239  xDataSink->setInputStream( xIn );
2240  }
2241  catch ( DAVException const & e )
2242  {
2243  //TODO cache the http error if not yet cached
2244  cancelCommandExecution( e, xEnv );
2245  // Unreachable
2246  }
2247  }
2248  else
2249  {
2250  // Note: aOpenCommand.Sink may contain an XStream
2251  // implementation. Support for this type of
2252  // sink is optional...
2254  uno::makeAny(
2255  ucb::UnsupportedDataSinkException(
2256  OUString(),
2257  static_cast< cppu::OWeakObject * >( this ),
2258  rArg.Sink ) ),
2259  xEnv );
2260  // Unreachable
2261  }
2262  }
2263  }
2264 
2265  return aRet;
2266 }
2267 
2268 void Content::post(
2269  const ucb::PostCommandArgument2 & rArg,
2270  const uno::Reference< ucb::XCommandEnvironment > & xEnv )
2271 {
2272  uno::Reference< io::XActiveDataSink > xSink( rArg.Sink, uno::UNO_QUERY );
2273  if ( xSink.is() )
2274  {
2275  try
2276  {
2277  std::unique_ptr< DAVResourceAccess > xResAccess;
2278  {
2279  osl::MutexGuard aGuard( m_aMutex );
2280  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
2281  }
2282 
2283  removeCachedPropertyNames( xResAccess->getURL() );
2284  uno::Reference< io::XInputStream > xResult
2285  = xResAccess->POST( rArg.MediaType,
2286  rArg.Referer,
2287  rArg.Source,
2288  xEnv );
2289 
2290  {
2291  osl::MutexGuard aGuard( m_aMutex );
2292  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
2293  }
2294 
2295  xSink->setInputStream( xResult );
2296  }
2297  catch ( DAVException const & e )
2298  {
2299  cancelCommandExecution( e, xEnv, true );
2300  // Unreachable
2301  }
2302  }
2303  else
2304  {
2305  uno::Reference< io::XOutputStream > xResult( rArg.Sink, uno::UNO_QUERY );
2306  if ( xResult.is() )
2307  {
2308  try
2309  {
2310  std::unique_ptr< DAVResourceAccess > xResAccess;
2311  {
2312  osl::MutexGuard aGuard( m_aMutex );
2313  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
2314  }
2315 
2316  removeCachedPropertyNames( xResAccess->getURL() );
2317  xResAccess->POST( rArg.MediaType,
2318  rArg.Referer,
2319  rArg.Source,
2320  xResult,
2321  xEnv );
2322 
2323  {
2324  osl::MutexGuard aGuard( m_aMutex );
2325  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
2326  }
2327  }
2328  catch ( DAVException const & e )
2329  {
2330  cancelCommandExecution( e, xEnv, true );
2331  // Unreachable
2332  }
2333  }
2334  else
2335  {
2337  uno::makeAny(
2338  ucb::UnsupportedDataSinkException(
2339  OUString(),
2340  static_cast< cppu::OWeakObject * >( this ),
2341  rArg.Sink ) ),
2342  xEnv );
2343  // Unreachable
2344  }
2345  }
2346 }
2347 
2348 
2350 {
2351  // Obtain a list with a snapshot of all currently instantiated contents
2352  // from provider and extract the contents which are direct children
2353  // of this content.
2354 
2355  ::ucbhelper::ContentRefList aAllContents;
2356  m_xProvider->queryExistingContents( aAllContents );
2357 
2358  OUString aURL = m_xIdentifier->getContentIdentifier();
2359  sal_Int32 nURLPos = aURL.lastIndexOf( '/' );
2360 
2361  if ( nURLPos != ( aURL.getLength() - 1 ) )
2362  {
2363  // No trailing slash found. Append.
2364  aURL += "/";
2365  }
2366 
2367  sal_Int32 nLen = aURL.getLength();
2368 
2369  for ( const auto& rChild : aAllContents )
2370  {
2371  ::ucbhelper::ContentImplHelperRef xChild = rChild;
2372  OUString aChildURL
2373  = xChild->getIdentifier()->getContentIdentifier();
2374 
2375  // Is aURL a prefix of aChildURL?
2376  if ( ( aChildURL.getLength() > nLen ) &&
2377  ( aChildURL.startsWith( aURL ) ) )
2378  {
2379  sal_Int32 nPos = aChildURL.indexOf( '/', nLen );
2380 
2381  if ( ( nPos == -1 ) ||
2382  ( nPos == ( aChildURL.getLength() - 1 ) ) )
2383  {
2384  // No further slashes / only a final slash. It's a child!
2385  rChildren.emplace_back(
2386  static_cast< ::webdav_ucp::Content * >(
2387  xChild.get() ) );
2388  }
2389  }
2390  }
2391 }
2392 
2393 
2394 void Content::insert(
2395  const uno::Reference< io::XInputStream > & xInputStream,
2396  bool bReplaceExisting,
2397  const uno::Reference< ucb::XCommandEnvironment >& Environment )
2398 {
2399  bool bTransient, bCollection;
2400  OUString aEscapedTitle;
2401  std::unique_ptr< DAVResourceAccess > xResAccess;
2402 
2403  {
2404  osl::Guard< osl::Mutex > aGuard( m_aMutex );
2405 
2406  bTransient = m_bTransient;
2407  bCollection = m_bCollection;
2408  aEscapedTitle = m_aEscapedTitle;
2409  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
2410  }
2411 
2412  // Check, if all required properties are present.
2413 
2414  if ( aEscapedTitle.isEmpty() )
2415  {
2416  SAL_WARN( "ucb.ucp.webdav", "Content::insert - Title missing!" );
2417 
2418  uno::Sequence<OUString> aProps { "Title" };
2420  uno::makeAny( ucb::MissingPropertiesException(
2421  OUString(),
2422  static_cast< cppu::OWeakObject * >( this ),
2423  aProps ) ),
2424  Environment );
2425  // Unreachable
2426  }
2427 
2428  if ( !bReplaceExisting )
2429  {
2430  /* [RFC 2616] - HTTP
2431 
2432  The PUT method requests that the enclosed entity be stored under the
2433  supplied Request-URI. If the Request-URI refers to an already
2434  existing resource, the enclosed entity SHOULD be considered as a
2435  modified version of the one residing on the origin server.
2436  */
2437 
2438  /* [RFC 2518] - WebDAV
2439 
2440  MKCOL creates a new collection resource at the location specified by
2441  the Request-URI. If the resource identified by the Request-URI is
2442  non-null then the MKCOL MUST fail.
2443  */
2444 
2445  // ==> Complain on PUT, continue on MKCOL.
2446  if ( !bTransient || !bCollection )
2447  {
2448  ucb::UnsupportedNameClashException aEx(
2449  "Unable to write without overwrite!",
2450  static_cast< cppu::OWeakObject * >( this ),
2451  ucb::NameClash::ERROR );
2452 
2453  uno::Reference< task::XInteractionHandler > xIH;
2454 
2455  if ( Environment.is() )
2456  xIH = Environment->getInteractionHandler();
2457 
2458  if ( !xIH.is() )
2459  {
2460  // No IH; throw.
2461  throw aEx;
2462  }
2463 
2464  uno::Any aExAsAny( uno::makeAny( aEx ) );
2465 
2468  aExAsAny,
2469  ContinuationFlags::Approve | ContinuationFlags::Disapprove );
2470  xIH->handle( xRequest );
2471 
2472  const ContinuationFlags nResp = xRequest->getResponse();
2473 
2474  switch ( nResp )
2475  {
2476  case ContinuationFlags::NONE:
2477  // Not handled; throw.
2478  throw aEx;
2479 // break;
2480 
2481  case ContinuationFlags::Approve:
2482  // Continue -> Overwrite.
2483  bReplaceExisting = true;
2484  break;
2485 
2486  case ContinuationFlags::Disapprove:
2487  // Abort.
2488  throw ucb::CommandFailedException(
2489  OUString(),
2490  uno::Reference< uno::XInterface >(),
2491  aExAsAny );
2492 // break;
2493 
2494  default:
2495  SAL_WARN( "ucb.ucp.webdav", "Content::insert - "
2496  "Unknown interaction selection!" );
2497  throw ucb::CommandFailedException(
2498  "Unknown interaction selection!",
2499  uno::Reference< uno::XInterface >(),
2500  aExAsAny );
2501 // break;
2502  }
2503 
2504  }
2505  }
2506 
2507  if ( bTransient )
2508  {
2509  // Assemble new content identifier...
2510  OUString aURL = getParentURL();
2511  if ( aURL.lastIndexOf( '/' ) != ( aURL.getLength() - 1 ) )
2512  aURL += "/";
2513 
2514  aURL += aEscapedTitle;
2515 
2516  try
2517  {
2518  xResAccess->setURL( aURL );
2519 
2520  if ( bCollection )
2521  {
2522  aStaticDAVOptionsCache.removeDAVOptions( xResAccess->getURL() );
2523  removeCachedPropertyNames( xResAccess->getURL() );
2524  xResAccess->MKCOL( Environment );
2525  }
2526  else
2527  {
2528  // remove options from cache, PUT may change it
2529  // it will be refreshed when needed
2530  aStaticDAVOptionsCache.removeDAVOptions( xResAccess->getURL() );
2531  removeCachedPropertyNames( xResAccess->getURL() );
2532  xResAccess->PUT( xInputStream, Environment );
2533  // clean cached value of PROPFIND properties names
2534  }
2535  // no error , set the resourcetype to unknown type
2536  // the resource may have transitioned from NOT FOUND or UNKNOWN to something else
2537  // depending on the server behaviour
2538  // this will force a recheck of the resource type
2541  }
2542  catch ( DAVException const & except )
2543  {
2544  if ( bCollection )
2545  {
2546  if ( except.getStatus() == SC_METHOD_NOT_ALLOWED )
2547  {
2548  // [RFC 2518] - WebDAV
2549  // 405 (Method Not Allowed) - MKCOL can only be
2550  // executed on a deleted/non-existent resource.
2551 
2552  if ( bReplaceExisting )
2553  {
2554  // Destroy old resource.
2555  try
2556  {
2557  removeCachedPropertyNames( xResAccess->getURL() );
2558  xResAccess->DESTROY( Environment );
2559  }
2560  catch ( DAVException const & e )
2561  {
2562  cancelCommandExecution( e, Environment, true );
2563  // Unreachable
2564  }
2565 
2566  // Insert (recursion!).
2567  insert( xInputStream, bReplaceExisting, Environment );
2568 
2569  {
2570  osl::Guard< osl::Mutex > aGuard( m_aMutex );
2571  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
2572  }
2573 
2574  // Success!
2575  return;
2576  }
2577  else
2578  {
2579  OUString aTitle;
2580  try
2581  {
2582  NeonUri aURI( aURL );
2583  aTitle = aURI.GetPathBaseNameUnescaped();
2584  }
2585  catch ( DAVException const & )
2586  {
2587  }
2588 
2590  uno::makeAny(
2591  ucb::NameClashException(
2592  OUString(),
2593  static_cast< cppu::OWeakObject * >( this ),
2594  task::InteractionClassification_ERROR,
2595  aTitle ) ),
2596  Environment );
2597  // Unreachable
2598  }
2599  }
2600  }
2601 
2602  cancelCommandExecution( except, Environment, true );
2603  // Unreachable
2604  }
2605 
2606  {
2607  osl::Guard< osl::Mutex > aGuard( m_aMutex );
2608  m_xIdentifier = new ::ucbhelper::ContentIdentifier( aURL );
2609  }
2610 
2611  inserted();
2612 
2613  {
2614  osl::Guard< osl::Mutex > aGuard( m_aMutex );
2615  m_bTransient = false;
2616  }
2617  }
2618  else
2619  {
2620  if ( !xInputStream.is() )
2621  {
2623  uno::makeAny(
2624  ucb::MissingInputStreamException(
2625  OUString(),
2626  static_cast< cppu::OWeakObject * >( this ) ) ),
2627  Environment );
2628  // Unreachable
2629  }
2630 
2631  // save the URL since it may change due to redirection
2632  OUString aTargetUrl = xResAccess->getURL();
2633  try
2634  {
2635  removeCachedPropertyNames( xResAccess->getURL() );
2636  // remove options from cache, PUT may change it
2637  // it will be refreshed when needed
2638  aStaticDAVOptionsCache.removeDAVOptions( aTargetUrl );
2639  xResAccess->PUT( xInputStream, Environment );
2640  }
2641  catch ( DAVException const & e )
2642  {
2643  cancelCommandExecution( e, Environment, true );
2644  // Unreachable
2645  }
2646  }
2647 
2648  {
2649  osl::Guard< osl::Mutex > aGuard( m_aMutex );
2650  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
2651  }
2652 }
2653 
2654 
2655 void Content::transfer(
2656  const ucb::TransferInfo & rArgs,
2657  const uno::Reference< ucb::XCommandEnvironment >& Environment )
2658 {
2659  uno::Reference< ucb::XContentIdentifier > xIdentifier;
2660  uno::Reference< ucb::XContentProvider > xProvider;
2661  std::unique_ptr< DAVResourceAccess > xResAccess;
2662 
2663  {
2664  osl::Guard< osl::Mutex > aGuard( m_aMutex );
2665 
2666  xIdentifier.set( m_xIdentifier );
2667  xProvider = m_xProvider;
2668  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
2669  }
2670 
2671  NeonUri sourceURI( rArgs.SourceURL );
2672  NeonUri targetURI( xIdentifier->getContentIdentifier() );
2673 
2674  OUString aTargetURI;
2675  try
2676  {
2677  aTargetURI = targetURI.GetPathBaseNameUnescaped();
2678 
2679  // Check source's and target's URL scheme
2680 
2681  OUString aScheme = sourceURI.GetScheme().toAsciiLowerCase();
2682  if ( aScheme == VNDSUNSTARWEBDAV_URL_SCHEME)
2683  {
2684  sourceURI.SetScheme( HTTP_URL_SCHEME );
2685  }
2686  else if ( aScheme == VNDSUNSTARWEBDAVS_URL_SCHEME)
2687  {
2688  sourceURI.SetScheme( HTTPS_URL_SCHEME );
2689  }
2690  else if ( aScheme == DAV_URL_SCHEME )
2691  {
2692  sourceURI.SetScheme( HTTP_URL_SCHEME );
2693  }
2694  else if ( aScheme == DAVS_URL_SCHEME )
2695  {
2696  sourceURI.SetScheme( HTTPS_URL_SCHEME );
2697  }
2698  else if ( aScheme == WEBDAV_URL_SCHEME )
2699  {
2700  sourceURI.SetScheme( HTTP_URL_SCHEME );
2701  }
2702  else if ( aScheme == WEBDAVS_URL_SCHEME )
2703  {
2704  sourceURI.SetScheme( HTTPS_URL_SCHEME );
2705  }
2706  else
2707  {
2708  if ( aScheme != HTTP_URL_SCHEME && aScheme != HTTPS_URL_SCHEME )
2709  {
2711  uno::makeAny(
2712  ucb::InteractiveBadTransferURLException(
2713  "Unsupported URL scheme!",
2714  static_cast< cppu::OWeakObject * >( this ) ) ),
2715  Environment );
2716  // Unreachable
2717  }
2718  }
2719 
2720  aScheme = targetURI.GetScheme().toAsciiLowerCase();
2721  if ( aScheme == VNDSUNSTARWEBDAV_URL_SCHEME)
2722  targetURI.SetScheme( HTTP_URL_SCHEME );
2723  else if ( aScheme == VNDSUNSTARWEBDAVS_URL_SCHEME)
2724  targetURI.SetScheme( HTTPS_URL_SCHEME );
2725  else if ( aScheme == DAV_URL_SCHEME )
2726  targetURI.SetScheme( HTTP_URL_SCHEME );
2727  else if ( aScheme == DAVS_URL_SCHEME )
2728  targetURI.SetScheme( HTTPS_URL_SCHEME );
2729  else if ( aScheme == WEBDAV_URL_SCHEME )
2730  targetURI.SetScheme( HTTP_URL_SCHEME );
2731  else if ( aScheme == WEBDAVS_URL_SCHEME )
2732  targetURI.SetScheme( HTTPS_URL_SCHEME );
2733 
2734  // @@@ This implementation of 'transfer' only works
2735  // if the source and target are located at same host.
2736  // (Neon does not support cross-server copy/move)
2737 
2738  // Check for same host
2739 
2740  if ( !sourceURI.GetHost().isEmpty() &&
2741  ( sourceURI.GetHost() != targetURI.GetHost() ) )
2742  {
2744  uno::makeAny( ucb::InteractiveBadTransferURLException(
2745  "Different hosts!",
2746  static_cast< cppu::OWeakObject * >( this ) ) ),
2747  Environment );
2748  // Unreachable
2749  }
2750 
2751  OUString aTitle = rArgs.NewTitle;
2752 
2753  if ( aTitle.isEmpty() )
2754  aTitle = sourceURI.GetPathBaseNameUnescaped();
2755 
2756  if ( aTitle == "/" )
2757  {
2758  // kso: ???
2759  aTitle.clear();
2760  }
2761 
2762  targetURI.AppendPath( aTitle );
2763 
2764  OUString aTargetURL = xIdentifier->getContentIdentifier();
2765  if ( ( aTargetURL.lastIndexOf( '/' ) + 1 )
2766  != aTargetURL.getLength() )
2767  aTargetURL += "/";
2768 
2769  aTargetURL += aTitle;
2770 
2771  uno::Reference< ucb::XContentIdentifier > xTargetId
2772  = new ::ucbhelper::ContentIdentifier( aTargetURL );
2773 
2774  DAVResourceAccess aSourceAccess( m_xContext,
2775  xResAccess->getSessionFactory(),
2776  sourceURI.GetURI() );
2777 
2778  if ( rArgs.MoveData )
2779  {
2780  uno::Reference< ucb::XContentIdentifier > xId
2781  = new ::ucbhelper::ContentIdentifier( rArgs.SourceURL );
2782 
2783  // Note: The static cast is okay here, because its sure that
2784  // xProvider is always the WebDAVContentProvider.
2786  = static_cast< Content * >(
2787  xProvider->queryContent( xId ).get() );
2788 
2789  // [RFC 2518] - WebDAV
2790  // If a resource exists at the destination and the Overwrite
2791  // header is "T" then prior to performing the move the server
2792  // MUST perform a DELETE with "Depth: infinity" on the
2793  // destination resource. If the Overwrite header is set to
2794  // "F" then the operation will fail.
2795 
2796  aStaticDAVOptionsCache.removeDAVOptions( sourceURI.GetURI() );
2797  aStaticDAVOptionsCache.removeDAVOptions( targetURI.GetURI() );
2798  aSourceAccess.MOVE( sourceURI.GetPath(),
2799  targetURI.GetURI(),
2800  rArgs.NameClash
2801  == ucb::NameClash::OVERWRITE,
2802  Environment );
2803 
2804  if ( xSource.is() )
2805  {
2806  // Propagate destruction to listeners.
2807  xSource->destroy( true );
2808  }
2809 
2810 // DAV resources store all additional props on server!
2811 // // Rename own and all children's Additional Core Properties.
2812 // renameAdditionalPropertySet( xId->getContentIdentifier(),
2813 // xTargetId->getContentIdentifier(),
2814 // sal_True );
2815  }
2816  else
2817  {
2818  // [RFC 2518] - WebDAV
2819  // If a resource exists at the destination and the Overwrite
2820  // header is "T" then prior to performing the copy the server
2821  // MUST perform a DELETE with "Depth: infinity" on the
2822  // destination resource. If the Overwrite header is set to
2823  // "F" then the operation will fail.
2824 
2825  aStaticDAVOptionsCache.removeDAVOptions( sourceURI.GetURI() );
2826  aStaticDAVOptionsCache.removeDAVOptions( targetURI.GetURI() );
2827  aSourceAccess.COPY( sourceURI.GetPath(),
2828  targetURI.GetURI(),
2829  rArgs.NameClash
2830  == ucb::NameClash::OVERWRITE,
2831  Environment );
2832 
2833 // DAV resources store all additional props on server!
2834 // // Copy own and all children's Additional Core Properties.
2835 // copyAdditionalPropertySet( xId->getContentIdentifier(),
2836 // xTargetId->getContentIdentifier(),
2837 // sal_True );
2838  }
2839 
2840  // Note: The static cast is okay here, because its sure that
2841  // xProvider is always the WebDAVContentProvider.
2843  = static_cast< Content * >(
2844  xProvider->queryContent( xTargetId ).get() );
2845 
2846  // Announce transferred content in its new folder.
2847  xTarget->inserted();
2848  }
2849  catch ( ucb::IllegalIdentifierException const & )
2850  {
2851  // queryContent
2852  }
2853  catch ( DAVException const & e )
2854  {
2855  // [RFC 2518] - WebDAV
2856  // 412 (Precondition Failed) - The server was unable to maintain
2857  // the liveness of the properties listed in the propertybehavior
2858  // XML element or the Overwrite header is "F" and the state of
2859  // the destination resource is non-null.
2860 
2861  if ( e.getStatus() == SC_PRECONDITION_FAILED )
2862  {
2863  switch ( rArgs.NameClash )
2864  {
2865  case ucb::NameClash::ERROR:
2866  {
2868  uno::makeAny(
2869  ucb::NameClashException(
2870  OUString(),
2871  static_cast< cppu::OWeakObject * >( this ),
2872  task::InteractionClassification_ERROR,
2873  aTargetURI ) ),
2874  Environment );
2875  [[fallthrough]]; // Unreachable
2876  }
2877 
2878  case ucb::NameClash::OVERWRITE:
2879  break;
2880 
2881  case ucb::NameClash::KEEP: // deprecated
2882  case ucb::NameClash::RENAME:
2883  case ucb::NameClash::ASK:
2884  default:
2885  {
2887  uno::makeAny(
2888  ucb::UnsupportedNameClashException(
2889  OUString(),
2890  static_cast< cppu::OWeakObject * >( this ),
2891  rArgs.NameClash ) ),
2892  Environment );
2893  // Unreachable
2894  }
2895  }
2896  }
2897 
2898  cancelCommandExecution( e, Environment, true );
2899  // Unreachable
2900  }
2901 
2902  {
2903  osl::Guard< osl::Mutex > aGuard( m_aMutex );
2904  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
2905  }
2906 }
2907 
2908 
2909 void Content::destroy( bool bDeletePhysical )
2910 {
2911  // @@@ take care about bDeletePhysical -> trashcan support
2912  uno::Reference< ucb::XContent > xThis = this;
2913 
2914  deleted();
2915 
2916  osl::Guard< osl::Mutex > aGuard( m_aMutex );
2917 
2918  // Process instantiated children...
2919 
2921  queryChildren( aChildren );
2922 
2923  for ( auto& rChild : aChildren )
2924  {
2925  rChild->destroy( bDeletePhysical );
2926  }
2927 }
2928 
2929 // returns the resource type, to be checked for locks
2931  const uno::Reference< ucb::XCommandEnvironment >& Environment,
2932  const std::unique_ptr< DAVResourceAccess > & rResAccess)
2933 {
2934  ResourceType eResourceTypeForLocks = UNKNOWN;
2935  {
2936  osl::MutexGuard g(m_aMutex);
2937  //check if cache contains what we need, usually the first PROPFIND on the URI has supported lock
2938  if (m_xCachedProps)
2939  {
2940  uno::Sequence< ucb::LockEntry > aSupportedLocks;
2942  >>= aSupportedLocks ) //get the cached value for supportedlock
2943  {
2944  for ( const auto& rSupportedLock : std::as_const(aSupportedLocks) )
2945  {
2946  if ( rSupportedLock.Scope
2947  == ucb::LockScope_EXCLUSIVE &&
2948  rSupportedLock.Type
2949  == ucb::LockType_WRITE )
2950  eResourceTypeForLocks = DAV;
2951  }
2952  }
2953  }
2954  }
2955 
2956  const OUString & rURL = m_xIdentifier->getContentIdentifier();
2957 
2958  if ( eResourceTypeForLocks == UNKNOWN )
2959  {
2960  // resource type for lock/unlock operations still unknown, need to ask the server
2961 
2962  const OUString aScheme(
2963  rURL.copy( 0, rURL.indexOf( ':' ) ).toAsciiLowerCase() );
2964 
2965  if ( aScheme == FTP_URL_SCHEME )
2966  {
2967  eResourceTypeForLocks = FTP;
2968  }
2969  else
2970  {
2971  DAVOptions aDAVOptions;
2972  getResourceOptions( Environment, aDAVOptions, rResAccess );
2973  if( aDAVOptions.isClass1() ||
2974  aDAVOptions.isClass2() ||
2975  aDAVOptions.isClass3() )
2976  {
2977  // this is at least a DAV, lock to be confirmed
2978  // class 2 is needed for full lock support
2979  // see
2980  // <https://tools.ietf.org/html/rfc4918#section-18.2>
2981  eResourceTypeForLocks = DAV_NOLOCK;
2982  if( aDAVOptions.isClass2() )
2983  {
2984  // ok, possible lock, check for it
2985  try
2986  {
2987  // we need only DAV:supportedlock
2988  std::vector< DAVResource > resources;
2989  std::vector< OUString > aPropNames;
2990  uno::Sequence< beans::Property > aProperties( 1 );
2991  aProperties[ 0 ].Name = DAVProperties::SUPPORTEDLOCK;
2992 
2993  ContentProperties::UCBNamesToDAVNames( aProperties, aPropNames );
2994  rResAccess->PROPFIND( DAVZERO, aPropNames, resources, Environment );
2995 
2996  bool wasSupportedlockFound = false;
2997 
2998  // only one resource should be returned
2999  if ( resources.size() == 1 )
3000  {
3001  // we may have received a bunch of other properties
3002  // (some servers seems to do so)
3003  // but we need only supported lock for this check
3004  // all returned properties are in
3005  // resources.properties[n].Name/.Value
3006 
3007  for ( const auto& rProp : resources[0].properties )
3008  {
3009  if ( rProp.Name == DAVProperties::SUPPORTEDLOCK )
3010  {
3011  wasSupportedlockFound = true;
3012  uno::Sequence< ucb::LockEntry > aSupportedLocks;
3013  if ( rProp.Value >>= aSupportedLocks )
3014  {
3015  bool isSupported = std::any_of(std::cbegin(aSupportedLocks), std::cend(aSupportedLocks),
3016  [](const ucb::LockEntry& rLock) {
3017  // TODO: if the lock type is changed from 'exclusive write' to 'shared write'
3018  // e.g. to implement 'Calc shared file feature', the ucb::LockScope_EXCLUSIVE
3019  // value should be checked as well, adaptation the code may be needed
3020  return rLock.Scope == ucb::LockScope_EXCLUSIVE
3021  && rLock.Type == ucb::LockType_WRITE;
3022  });
3023  if (isSupported)
3024  {
3025  // requested locking mode is supported
3026  eResourceTypeForLocks = DAV;
3027  SAL_INFO( "ucb.ucp.webdav", "resourceTypeForLocks - URL: <"
3028  << m_xIdentifier->getContentIdentifier() << ">, DAV lock/unlock supported");
3029  }
3030  break;
3031  }
3032  }
3033  }
3034  }
3035  else
3036  {
3037  // PROPFIND failed; check if HEAD contains Content-Disposition: attachment (RFC1806, HTTP/1.1 19.5.1),
3038  // which supposedly means no lock for the resource (happens e.g. with SharePoint exported lists)
3039  OUString sContentDisposition;
3040  // First, check cached properties
3041  if (m_xCachedProps)
3042  {
3043  if ((m_xCachedProps->getValue("Content-Disposition") >>= sContentDisposition)
3044  && sContentDisposition.startsWithIgnoreAsciiCase("attachment"))
3045  {
3046  eResourceTypeForLocks = DAV_NOLOCK;
3047  wasSupportedlockFound = true;
3048  }
3049  }
3050  // If no data in cache, try HEAD request
3051  if (sContentDisposition.isEmpty() && !m_bDidGetOrHead) try
3052  {
3053  DAVResource resource;
3054  GetPropsUsingHeadRequest(resource, rResAccess, {"Content-Disposition"}, Environment);
3055  m_bDidGetOrHead = true;
3056  for (const auto& it : resource.properties)
3057  {
3058  if (it.Name.equalsIgnoreAsciiCase("Content-Disposition"))
3059  {
3060  if ((it.Value >>= sContentDisposition) && sContentDisposition.equalsIgnoreAsciiCase("attachment"))
3061  {
3062  eResourceTypeForLocks = DAV_NOLOCK;
3063  wasSupportedlockFound = true;
3064  }
3065  break;
3066  }
3067  }
3068  }
3069  catch (...){}
3070  }
3071  // check if this is still only a DAV_NOLOCK
3072  // a fallback for resources that do not have DAVProperties::SUPPORTEDLOCK property
3073  // we check for the returned OPTION if LOCK is allowed on the resource
3074  if ( !wasSupportedlockFound && eResourceTypeForLocks == DAV_NOLOCK )
3075  {
3076  SAL_INFO( "ucb.ucp.webdav", "This WebDAV server has no supportedlock property, check for allowed LOCK method in OPTIONS" );
3077  // ATTENTION: if the lock type is changed from 'exclusive write' to 'shared write'
3078  // e.g. to implement 'Calc shared file feature' on WebDAV directly, and we arrive to this fallback
3079  // and the LOCK is allowed, we should assume that only exclusive write lock is available
3080  // this is just a reminder...
3081  if ( aDAVOptions.isLockAllowed() )
3082  eResourceTypeForLocks = DAV;
3083  }
3084  }
3085  catch ( DAVException const & e )
3086  {
3087  rResAccess->resetUri();
3088  //grab the error code
3089  switch( e.getStatus() )
3090  {
3091  case SC_NOT_FOUND:
3092  SAL_WARN( "ucb.ucp.webdav", "resourceTypeForLocks() - URL: <"
3093  << m_xIdentifier->getContentIdentifier() << "> was not found. ");
3094  eResourceTypeForLocks = NOT_FOUND;
3095  break;
3096  // some servers returns SC_FORBIDDEN, instead
3097  // the meaning of SC_FORBIDDEN is, according to <http://tools.ietf.org/html/rfc7231#section-6.5.3>:
3098  // The 403 (Forbidden) status code indicates that the server understood
3099  // the request but refuses to authorize it
3100  case SC_FORBIDDEN:
3101  // Errors SC_NOT_IMPLEMENTED and SC_METHOD_NOT_ALLOWED are
3102  // part of base http 1.1 RFCs
3103  case SC_NOT_IMPLEMENTED: // <http://tools.ietf.org/html/rfc7231#section-6.6.2>
3104  case SC_METHOD_NOT_ALLOWED: // <http://tools.ietf.org/html/rfc7231#section-6.5.5>
3105  // they all mean the resource is NON_DAV
3106  SAL_WARN( "ucb.ucp.webdav", "resourceTypeForLocks() DAVException (SC_FORBIDDEN, SC_NOT_IMPLEMENTED or SC_METHOD_NOT_ALLOWED) - URL: <"
3107  << m_xIdentifier->getContentIdentifier() << ">, DAV ExceptionCode: " << e.getError() << ", HTTP error: " << e.getStatus() );
3108  eResourceTypeForLocks = NON_DAV;
3109  break;
3110  default:
3111  //fallthrough
3112  SAL_WARN( "ucb.ucp.webdav", "resourceTypeForLocks() DAVException - URL: <"
3113  << m_xIdentifier->getContentIdentifier() << ">, DAV ExceptionCode: " << e.getError() << ", HTTP error: " << e.getStatus() );
3114  eResourceTypeForLocks = UNKNOWN;
3115  }
3116  }
3117  }
3118  }
3119  else
3120  eResourceTypeForLocks = NON_DAV;
3121 
3122  }
3123  }
3124  osl::MutexGuard g(m_aMutex);
3126  {
3127  m_eResourceTypeForLocks = eResourceTypeForLocks;
3128  }
3129  else
3130  {
3131  SAL_WARN_IF(
3132  eResourceTypeForLocks != m_eResourceTypeForLocks, "ucb.ucp.webdav",
3133  "different resource types for <" << rURL << ">: "
3134  << +eResourceTypeForLocks << " vs. " << +m_eResourceTypeForLocks);
3135  }
3136  SAL_INFO( "ucb.ucp.webdav", "resourceTypeForLocks() - URL: <"
3137  << m_xIdentifier->getContentIdentifier() << ">, m_eResourceTypeForLocks: " << m_eResourceTypeForLocks );
3138  return m_eResourceTypeForLocks;
3139 }
3140 
3142  const uno::Reference< ucb::XCommandEnvironment >& Environment )
3143 {
3144  std::unique_ptr< DAVResourceAccess > xResAccess;
3145  {
3146  osl::MutexGuard aGuard( m_aMutex );
3147  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
3148  }
3149  Content::ResourceType ret = resourceTypeForLocks( Environment, xResAccess );
3150  {
3151  osl::Guard< osl::Mutex > aGuard( m_aMutex );
3152  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
3153  }
3154  return ret;
3155 }
3156 
3157 void Content::lock(
3158  const uno::Reference< ucb::XCommandEnvironment >& Environment )
3159 {
3160 // prepare aURL to be used in exception, see below
3161  OUString aURL;
3162  if ( m_bTransient )
3163  {
3164  aURL = getParentURL();
3165  if ( aURL.lastIndexOf('/') != ( aURL.getLength() - 1 ) )
3166  aURL += "/";
3167 
3168  aURL += m_aEscapedTitle;
3169  }
3170  else
3171  {
3172  aURL = m_xIdentifier->getContentIdentifier();
3173  }
3174 
3175  try
3176  {
3177  std::unique_ptr< DAVResourceAccess > xResAccess;
3178  {
3179  osl::Guard< osl::Mutex > aGuard( m_aMutex );
3180  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
3181  }
3182 
3183  uno::Any aOwnerAny;
3184  aOwnerAny
3185  <<= OUString("LibreOffice - http://www.libreoffice.org/");
3186 
3187  ucb::Lock aLock(
3188  ucb::LockScope_EXCLUSIVE,
3189  ucb::LockType_WRITE,
3190  ucb::LockDepth_ZERO,
3191  aOwnerAny,
3192  180, // lock timeout in secs
3193  //-1, // infinite lock
3194  uno::Sequence< OUString >() );
3195 
3196  // OPTIONS may change as a consequence of the lock operation
3197  aStaticDAVOptionsCache.removeDAVOptions( xResAccess->getURL() );
3198  removeCachedPropertyNames( xResAccess->getURL() );
3199  xResAccess->LOCK( aLock, Environment );
3200 
3201  {
3202  osl::Guard< osl::Mutex > aGuard( m_aMutex );
3203  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
3204  }
3205  }
3206  catch ( DAVException const & e )
3207  {
3208  // check if the exception thrown is 'already locked'
3209  // this exception is mapped directly to the ucb correct one, without
3210  // going into the cancelCommandExecution() user interaction
3211  // this exception should be managed by the issuer of 'lock' command
3212  switch( e.getError() )
3213  {
3215  {
3216  SAL_WARN( "ucb.ucp.webdav", "lock() resource already locked - URL: <"
3217  << m_xIdentifier->getContentIdentifier() << ">");
3218  throw
3219  ucb::InteractiveLockingLockedException(
3220  "Locked!",
3221  static_cast< cppu::OWeakObject * >( this ),
3222  task::InteractionClassification_ERROR,
3223  aURL,
3224  false );
3225  }
3226  break;
3228  {
3229  SAL_WARN( "ucb.ucp.webdav", "lock() DAVException Authentication error - URL: <"
3230  << m_xIdentifier->getContentIdentifier() << ">" );
3231  // DAVException::DAV_HTTP_AUTH exception can mean:
3232  // - interaction handler for credential management not present (happens, depending
3233  // on the LO framework processing)
3234  // - the remote site is a WebDAV with special configuration: read/only for read operations
3235  // and read/write for write operations, the user is not allowed to lock/write and
3236  // she cancelled the credentials request.
3237  // this is not actually an error, but the exception is sent directly from here, avoiding the automatic
3238  // management that takes part in cancelCommandExecution() below
3239  // Unfortunately there is no InteractiveNetwork*Exception available to signal this
3240  // since it mostly happens on read/only part of webdav, this appears to be the most correct exception available
3241  throw
3242  ucb::InteractiveNetworkWriteException(
3243  "Authentication error while trying to lock! Write only WebDAV perhaps?",
3244  static_cast< cppu::OWeakObject * >( this ),
3245  task::InteractionClassification_ERROR,
3246  e.getData() );
3247  }
3248  break;
3250  //grab the error code
3251  switch( e.getStatus() )
3252  {
3253  // The 'case SC_NOT_FOUND' just below tries to solve a problem in eXo Platform
3254  // WebDAV connector which apparently fail on resource first creation
3255  // rfc4918 section-7.3 (see link below)
3256  case SC_NOT_FOUND: // <http://tools.ietf.org/html/rfc7231#section-6.5.4>
3257  // The 'case SC_PRECONDITION_FAILED' just below tries to solve a problem
3258  // in SharePoint when locking the resource on first creation fails due to this:
3259  // <https://msdn.microsoft.com/en-us/library/jj575265%28v=office.12%29.aspx#id15>
3260  // (retrieved on 2015-08-14)
3261  case SC_PRECONDITION_FAILED: // <http://tools.ietf.org/html/rfc7232#section-4.2>
3262  // Errors SC_NOT_IMPLEMENTED and SC_METHOD_NOT_ALLOWED are
3263  // part of base http 1.1 RFCs
3264  case SC_NOT_IMPLEMENTED: // <http://tools.ietf.org/html/rfc7231#section-6.6.2>
3265  case SC_METHOD_NOT_ALLOWED: // <http://tools.ietf.org/html/rfc7231#section-6.5.5>
3266  SAL_WARN( "ucb.ucp.webdav", "lock() DAVException (SC_NOT_FOUND, SC_PRECONDITION_FAILED, SC_NOT_IMPLEMENTED or SC_METHOD_NOT_ALLOWED) - URL: <"
3267  << m_xIdentifier->getContentIdentifier() << ">, DAV ExceptionCode: " << e.getError() << ", HTTP error: " << e.getStatus() );
3268  // act as nothing happened
3269  // that's because when a resource is first created
3270  // the lock is sent before the put, so the resource
3271  // is actually created by LOCK, locking it before
3272  // the first PUT, but if LOCK is not supported
3273  // (simple web or DAV with lock disabled) we end with one of these http
3274  // errors.
3275  // These same errors may be reported when the LOCK on an unmapped
3276  // (i.e. non existent) resource is not implemented.
3277  // Detailed specification in:
3278  // <http://tools.ietf.org/html/rfc4918#section-7.3>
3279  return;
3280  default:
3281  //fallthrough
3282  ;
3283  }
3284  break;
3286  // we already hold the lock and it is in our internal lockstore
3287  // just return as if the lock was successful
3288  return;
3289  default:
3290  //fallthrough
3291  ;
3292  }
3293 
3294  SAL_WARN( "ucb.ucp.webdav","lock() DAVException - URL: <"
3295  << m_xIdentifier->getContentIdentifier() << ">, DAV ExceptionCode: " << e.getError() << ", HTTP error: " << e.getStatus() );
3296  cancelCommandExecution( e, Environment );
3297  // Unreachable
3298  }
3299 }
3300 
3301 
3302 void Content::unlock(
3303  const uno::Reference< ucb::XCommandEnvironment >& Environment )
3304 {
3305 
3306  try
3307  {
3308  std::unique_ptr< DAVResourceAccess > xResAccess;
3309  {
3310  osl::Guard< osl::Mutex > aGuard( m_aMutex );
3311  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
3312  }
3313 
3314  // check if the target URL is a Class1 DAV
3315  DAVOptions aDAVOptions;
3316  getResourceOptions( Environment, aDAVOptions, xResAccess );
3317 
3318  // at least class one is needed
3319  if( aDAVOptions.isClass1() )
3320  {
3321  // remove options from cache, unlock may change it
3322  // it will be refreshed when needed
3323  aStaticDAVOptionsCache.removeDAVOptions( xResAccess->getURL() );
3324  // clean cached value of PROPFIND properties names
3325  removeCachedPropertyNames( xResAccess->getURL() );
3326  xResAccess->UNLOCK( Environment );
3327  }
3328 
3329  {
3330  osl::Guard< osl::Mutex > aGuard( m_aMutex );
3331  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
3332  }
3333  }
3334  catch ( DAVException const & e )
3335  {
3336  switch( e.getError() )
3337  {
3339  SAL_WARN( "ucb.ucp.webdav", "unlock() DAVException::DAV_NOT_LOCKED - URL: <"
3340  << m_xIdentifier->getContentIdentifier() << ">");
3341  // means that we don't own any lock on this resource
3342  // intercepted here to remove a confusing indication to the user
3343  // unfortunately this happens in some WebDAV server configuration
3344  // acting as WebDAV and having lock/unlock enabled only
3345  // for authorized user.
3346  return;
3348  //grab the error code
3349  switch( e.getStatus() )
3350  {
3351  // Errors SC_NOT_IMPLEMENTED and SC_METHOD_NOT_ALLOWED are
3352  // part of base http 1.1 RFCs
3353  case SC_NOT_IMPLEMENTED: // <http://tools.ietf.org/html/rfc7231#section-6.6.2>
3354  case SC_METHOD_NOT_ALLOWED: // <http://tools.ietf.org/html/rfc7231#section-6.5.5>
3355  SAL_WARN( "ucb.ucp.webdav", "unlock() DAVException (SC_NOT_IMPLEMENTED or SC_METHOD_NOT_ALLOWED) - URL: <"
3356  << m_xIdentifier->getContentIdentifier() << ">, DAV ExceptionCode: " << e.getError() << ", HTTP error: " << e.getStatus() );
3357  return;
3358  default:
3359  //fallthrough
3360  ;
3361  }
3362  break;
3363  default:
3364  //fallthrough
3365  ;
3366  }
3367  SAL_WARN( "ucb.ucp.webdav","unlock() DAVException - URL: <"
3368  << m_xIdentifier->getContentIdentifier() << ">, DAV ExceptionCode: " << e.getError() << ", HTTP error: " << e.getStatus() );
3369  cancelCommandExecution( e, Environment );
3370  // Unreachable
3371  }
3372 }
3373 
3374 
3376  const uno::Reference< ucb::XContentIdentifier >& xNewId )
3377 {
3378  if ( !xNewId.is() )
3379  return false;
3380 
3381  osl::ClearableGuard< osl::Mutex > aGuard( m_aMutex );
3382 
3383  uno::Reference< ucb::XContent > xThis = this;
3384 
3385  // Already persistent?
3386  if ( m_bTransient )
3387  {
3388  SAL_WARN( "ucb.ucp.webdav", "Content::exchangeIdentity - Not persistent!" );
3389  return false;
3390  }
3391 
3392  // Exchange own identity.
3393 
3394  // Fail, if a content with given id already exists.
3395 // if ( !hasData( xNewId ) )
3396  {
3397  OUString aOldURL = m_xIdentifier->getContentIdentifier();
3398 
3399  aGuard.clear();
3400  if ( exchange( xNewId ) )
3401  {
3402  // Process instantiated children...
3403 
3404  ContentRefList aChildren;
3405  queryChildren( aChildren );
3406 
3407  for ( const auto& rChild : aChildren )
3408  {
3409  ContentRef xChild = rChild;
3410 
3411  // Create new content identifier for the child...
3412  uno::Reference< ucb::XContentIdentifier >
3413  xOldChildId = xChild->getIdentifier();
3414  OUString aOldChildURL
3415  = xOldChildId->getContentIdentifier();
3416  OUString aNewChildURL
3417  = aOldChildURL.replaceAt(
3418  0,
3419  aOldURL.getLength(),
3420  xNewId->getContentIdentifier() );
3421  uno::Reference< ucb::XContentIdentifier > xNewChildId
3422  = new ::ucbhelper::ContentIdentifier( aNewChildURL );
3423 
3424  if ( !xChild->exchangeIdentity( xNewChildId ) )
3425  return false;
3426  }
3427  return true;
3428  }
3429  }
3430 
3431  SAL_WARN( "ucb.ucp.webdav", "Content::exchangeIdentity - "
3432  "Panic! Cannot exchange identity!" );
3433  return false;
3434 }
3435 
3436 
3437 bool Content::isFolder(
3438  const uno::Reference< ucb::XCommandEnvironment >& xEnv )
3439 {
3440  {
3441  osl::MutexGuard aGuard( m_aMutex );
3442 
3443  if ( m_bTransient )
3444  return m_bCollection;
3445  }
3446 
3447  uno::Sequence< beans::Property > aProperties( 1 );
3448  aProperties[ 0 ].Name = "IsFolder";
3449  aProperties[ 0 ].Handle = -1;
3450  uno::Reference< sdbc::XRow > xRow( getPropertyValues( aProperties, xEnv ) );
3451  if ( xRow.is() )
3452  {
3453  try
3454  {
3455  return xRow->getBoolean( 1 );
3456  }
3457  catch ( sdbc::SQLException const & )
3458  {
3459  }
3460  }
3461 
3462  return false;
3463 }
3464 
3465 
3467 {
3468  // Map DAVException...
3469  uno::Any aException;
3470 
3471  OUString aURL;
3472  if ( m_bTransient )
3473  {
3474  aURL = getParentURL();
3475  if ( aURL.lastIndexOf('/') != ( aURL.getLength() - 1 ) )
3476  aURL += "/";
3477 
3478  aURL += m_aEscapedTitle;
3479  }
3480  else
3481  {
3482  aURL = m_xIdentifier->getContentIdentifier();
3483  }
3484 
3485  switch ( e.getStatus() )
3486  {
3487  case SC_NOT_FOUND:
3488  {
3489  uno::Sequence< uno::Any > aArgs( 1 );
3490  aArgs[ 0 ] <<= beans::PropertyValue(
3491  "Uri", -1,
3492  uno::makeAny(aURL),
3493  beans::PropertyState_DIRECT_VALUE);
3494 
3495  aException <<=
3496  ucb::InteractiveAugmentedIOException(
3497  "Not found!",
3498  static_cast< cppu::OWeakObject * >( this ),
3499  task::InteractionClassification_ERROR,
3500  ucb::IOErrorCode_NOT_EXISTING,
3501  aArgs );
3502  return aException;
3503  }
3504  default:
3505  break;
3506  }
3507 
3508  switch ( e.getError() )
3509  {
3511  {
3512  if ( bWrite )
3513  aException <<=
3514  ucb::InteractiveNetworkWriteException(
3515  e.getData(),
3516  static_cast< cppu::OWeakObject * >( this ),
3517  task::InteractionClassification_ERROR,
3518  e.getData() );
3519  else
3520  aException <<=
3521  ucb::InteractiveNetworkReadException(
3522  e.getData(),
3523  static_cast< cppu::OWeakObject * >( this ),
3524  task::InteractionClassification_ERROR,
3525  e.getData() );
3526  break;
3527  }
3528 
3530  aException <<=
3531  ucb::InteractiveNetworkResolveNameException(
3532  OUString(),
3533  static_cast< cppu::OWeakObject * >( this ),
3534  task::InteractionClassification_ERROR,
3535  e.getData() );
3536  break;
3537 
3538 // @@@ No matching InteractiveNetwork*Exception
3539 // case DAVException::DAV_HTTP_AUTH:
3540 // break;
3541 
3542 // @@@ No matching InteractiveNetwork*Exception
3543 // case DAVException::DAV_HTTP_AUTHPROXY:
3544 // break;
3545 
3548  aException <<=
3549  ucb::InteractiveNetworkConnectException(
3550  OUString(),
3551  static_cast< cppu::OWeakObject * >( this ),
3552  task::InteractionClassification_ERROR,
3553  e.getData() );
3554  break;
3555 
3556 // @@@ No matching InteractiveNetwork*Exception
3557 // case DAVException::DAV_HTTP_REDIRECT:
3558 // break;
3559 
3560 // @@@ No matching InteractiveNetwork*Exception
3561 // case DAVException::DAV_SESSION_CREATE:
3562 // break;
3563 
3565  aException <<=
3566  lang::IllegalArgumentException(
3567  OUString(),
3568  static_cast< cppu::OWeakObject * >( this ),
3569  -1 );
3570  break;
3571 
3573  aException <<=
3574  ucb::InteractiveLockingLockedException(
3575  "Locked!",
3576  static_cast< cppu::OWeakObject * >( this ),
3577  task::InteractionClassification_ERROR,
3578  aURL,
3579  false ); // not SelfOwned
3580  break;
3581 
3583  aException <<=
3584  ucb::InteractiveLockingLockedException(
3585  "Locked (self!)",
3586  static_cast< cppu::OWeakObject * >( this ),
3587  task::InteractionClassification_ERROR,
3588  aURL,
3589  true ); // SelfOwned
3590  break;
3591 
3593  aException <<=
3594  ucb::InteractiveLockingNotLockedException(
3595  "Not locked!",
3596  static_cast< cppu::OWeakObject * >( this ),
3597  task::InteractionClassification_ERROR,
3598  aURL );
3599  break;
3600 
3602  aException <<=
3603  ucb::InteractiveLockingLockExpiredException(
3604  "Lock expired!",
3605  static_cast< cppu::OWeakObject * >( this ),
3606  task::InteractionClassification_ERROR,
3607  aURL );
3608  break;
3609 
3610  default:
3611  aException <<=
3612  ucb::InteractiveNetworkGeneralException(
3613  OUString(),
3614  static_cast< cppu::OWeakObject * >( this ),
3615  task::InteractionClassification_ERROR );
3616  break;
3617  }
3618 
3619  return aException;
3620 }
3621 
3622 
3623 // static
3625 {
3626  return !(( e.getStatus() == SC_NOT_FOUND ) ||
3627  ( e.getStatus() == SC_GONE ) ||
3633 }
3634 
3635 
3637  const DAVException & e,
3638  const uno::Reference< ucb::XCommandEnvironment > & xEnv,
3639  bool bWrite /* = sal_False */ )
3640 {
3641  ucbhelper::cancelCommandExecution( MapDAVException( e, bWrite ), xEnv );
3642  // Unreachable
3643 }
3644 
3645 
3646 OUString
3647 Content::getBaseURI( const std::unique_ptr< DAVResourceAccess > & rResAccess )
3648 {
3649  osl::Guard< osl::Mutex > aGuard( m_aMutex );
3650 
3651  // First, try to obtain value of response header "Content-Location".
3652  if (m_xCachedProps)
3653  {
3654  OUString aLocation;
3655  m_xCachedProps->getValue( "Content-Location" ) >>= aLocation;
3656  if ( !aLocation.isEmpty() )
3657  {
3658  try
3659  {
3660  // Do not use m_xIdentifier->getContentIdentifier() because it
3661  // for example does not reflect redirects applied to requests
3662  // done using the original URI but m_xResAccess' URI does.
3663  return rtl::Uri::convertRelToAbs( rResAccess->getURL(),
3664  aLocation );
3665  }
3666  catch ( rtl::MalformedUriException const & )
3667  {
3668  }
3669  }
3670  }
3671 
3672  return rResAccess->getURL();
3673 }
3674 
3675 // resource type is the type of the WebDAV resource
3677  const uno::Reference< ucb::XCommandEnvironment >& xEnv,
3678  const std::unique_ptr< DAVResourceAccess > & rResAccess,
3679  bool * networkAccessAllowed)
3680 {
3681  {
3682  osl::MutexGuard g(m_aMutex);
3683  if (m_eResourceType != UNKNOWN) {
3684  return m_eResourceType;
3685  }
3686  }
3687 
3688  ResourceType eResourceType = UNKNOWN;
3689  DAVOptions aDAVOptions;
3690 
3691  const OUString & rURL = rResAccess->getURL();
3692  const OUString aScheme(
3693  rURL.copy( 0, rURL.indexOf( ':' ) ).toAsciiLowerCase() );
3694 
3695  if ( aScheme == FTP_URL_SCHEME )
3696  {
3697  eResourceType = FTP;
3698  }
3699  else
3700  {
3701  getResourceOptions( xEnv, aDAVOptions, rResAccess, networkAccessAllowed );
3702 
3703  // at least class one is needed
3704  if( aDAVOptions.isClass1() )
3705  {
3706  try
3707  {
3708  // Try to fetch some frequently used property value, e.g. those
3709  // used when loading documents... along with identifying whether
3710  // this is a DAV resource.
3711  std::vector< DAVResource > resources;
3712  std::vector< OUString > aPropNames;
3713  uno::Sequence< beans::Property > aProperties( 5 );
3714  aProperties[ 0 ].Name = "IsFolder";
3715  aProperties[ 1 ].Name = "IsDocument";
3716  aProperties[ 2 ].Name = "IsReadOnly";
3717  aProperties[ 3 ].Name = "MediaType";
3718  aProperties[ 4 ].Name = DAVProperties::SUPPORTEDLOCK;
3719 
3720  ContentProperties::UCBNamesToDAVNames( aProperties, aPropNames );
3721 
3722  rResAccess->PROPFIND( DAVZERO, aPropNames, resources, xEnv );
3723 
3724  if ( resources.size() == 1 )
3725  {
3726 #if defined SAL_LOG_INFO
3727  {//debug
3728  // print received resources
3729  for ( const auto& rProp : resources[0].properties )
3730  {
3731  OUString aPropValue;
3732  bool bValue;
3733  uno::Sequence< ucb::LockEntry > aSupportedLocks;
3734  if(rProp.Value >>= aPropValue )
3735  SAL_INFO( "ucb.ucp.webdav", "PROPFIND (getResourceType) - ret'd prop: " << rProp.Name << ":" << aPropValue );
3736  else if( rProp.Value >>= bValue )
3737  SAL_INFO( "ucb.ucp.webdav", "PROPFIND (getResourceType) - ret'd prop: " << rProp.Name << ":" <<
3738  ( bValue ? "true" : "false" ) );
3739  else if( rProp.Value >>= aSupportedLocks )
3740  {
3741  SAL_INFO( "ucb.ucp.webdav", "PROPFIND (getResourceType) - ret'd prop: " << rProp.Name << ":" );
3742  for ( sal_Int32 n = 0; n < aSupportedLocks.getLength(); ++n )
3743  {
3744  SAL_INFO( "ucb.ucp.webdav","PROPFIND (getResourceType) - supportedlock[" << n <<"]: scope: "
3745  << ( aSupportedLocks[ n ].Scope == css::ucb::LockScope_SHARED ? "shared" : "exclusive" )
3746  << ", type: "
3747  << ( aSupportedLocks[ n ].Type != css::ucb::LockType_WRITE ? "" : "write" ) );
3748  }
3749  }
3750  }
3751  }
3752 #endif
3753  osl::MutexGuard g(m_aMutex);
3754  m_xCachedProps.reset(
3755  new CachableContentProperties( ContentProperties( resources[ 0 ] ) ) );
3756  m_xCachedProps->containsAllNames(
3757  aProperties, m_aFailedPropNames );
3758  }
3759  eResourceType = DAV;
3760  }
3761  catch ( DAVException const & e )
3762  {
3763  rResAccess->resetUri();
3764 
3765  SAL_WARN( "ucb.ucp.webdav", "Content::getResourceType returned errors, DAV ExceptionCode: " << e.getError() << ", HTTP error: " << e.getStatus() );
3766 
3767  if ( e.getStatus() == SC_METHOD_NOT_ALLOWED )
3768  {
3769  // Status SC_METHOD_NOT_ALLOWED is a safe indicator that the
3770  // resource is NON_DAV
3771  eResourceType = NON_DAV;
3772  }
3773  else if (networkAccessAllowed != nullptr)
3774  {
3775  *networkAccessAllowed = *networkAccessAllowed
3777  }
3778  if ( e.getStatus() == SC_NOT_FOUND )
3779  {
3780  // arrives here if OPTIONS is still cached for a resource previously available
3781  // operate on the OPTIONS cache:
3782  // if OPTIONS was not found, do nothing
3783  // else OPTIONS returned on a resource not existent (example a server that allows lock on null resource) set
3784  // not found and adjust lifetime accordingly
3785  DAVOptions aDAVOptionsInner;
3786  if( aStaticDAVOptionsCache.getDAVOptions( rURL, aDAVOptionsInner ) )
3787  {
3788  // TODO? get redirected url
3789  aDAVOptionsInner.setHttpResponseStatusCode( e.getStatus() );
3790  aDAVOptionsInner.setHttpResponseStatusText( e.getData() );
3791  aStaticDAVOptionsCache.addDAVOptions( aDAVOptionsInner,
3793  }
3794  }
3795  // if the two net events below happen, something
3796  // is going on to the connection so break the command flow
3797  if ( ( e.getError() == DAVException::DAV_HTTP_TIMEOUT ) ||
3799  {
3800  cancelCommandExecution( e, xEnv );
3801  // unreachable
3802  }
3803  }
3804  }
3805  else
3806  {
3807  rResAccess->resetUri();
3808 
3809  // first check if the cached error can be mapped to DAVException::DAV_HTTP_TIMEOUT or mapped to DAVException::DAV_HTTP_CONNECT
3810  if ( aDAVOptions.getHttpResponseStatusCode() == USC_CONNECTION_TIMED_OUT )
3811  {
3812  // behave same as DAVException::DAV_HTTP_TIMEOUT or DAVException::DAV_HTTP_CONNECT was thrown
3813  try
3814  {
3815  // extract host name and connection port
3816  NeonUri theUri( rURL );
3817  const OUString& aHostName = theUri.GetHost();
3818  sal_Int32 nPort = theUri.GetPort();
3821  nPort ) );
3822  }
3823  catch ( DAVException& exp )
3824  {
3825  cancelCommandExecution( exp, xEnv );
3826  }
3827  }
3828 
3829  if ( aDAVOptions.getHttpResponseStatusCode() != SC_NOT_FOUND &&
3830  aDAVOptions.getHttpResponseStatusCode() != SC_GONE ) // the cached OPTIONS can have SC_GONE
3831  {
3832  eResourceType = NON_DAV;
3833  }
3834  else
3835  {
3836  //resource doesn't exist
3837  if ( networkAccessAllowed != nullptr )
3838  *networkAccessAllowed = false; }
3839  }
3840  }
3841 
3842  osl::MutexGuard g(m_aMutex);
3843  if (m_eResourceType == UNKNOWN) {
3844  m_eResourceType = eResourceType;
3845  } else {
3846  SAL_WARN_IF(
3847  eResourceType != m_eResourceType, "ucb.ucp.webdav",
3848  "different resource types for <" << rURL << ">: "
3849  << +eResourceType << " vs. " << +m_eResourceType);
3850  }
3851  SAL_INFO( "ucb.ucp.webdav", "m_eResourceType for <"<<rURL<<">: " << m_eResourceType );
3852  return m_eResourceType;
3853 }
3854 
3855 
3857  const uno::Reference< ucb::XCommandEnvironment >& xEnv )
3858 {
3859  std::unique_ptr< DAVResourceAccess > xResAccess;
3860  {
3861  osl::MutexGuard aGuard( m_aMutex );
3862  xResAccess.reset(new DAVResourceAccess(*m_xResAccess));
3863  }
3864  Content::ResourceType const ret = getResourceType( xEnv, xResAccess );
3865  {
3866  osl::Guard< osl::Mutex > aGuard( m_aMutex );
3867  m_xResAccess.reset(new DAVResourceAccess(*xResAccess));
3868  }
3869  return ret;
3870 }
3871 
3872 
3874 {
3875  // see description in
3876  // officecfg/registry/schema/org/openoffice/Inet.xcs
3877  // for use of these field values.
3878  sal_uInt32 nAtime;
3879  nAtime = officecfg::Inet::Settings::OptsCacheLifeImplWeb::get( m_xContext );
3880  m_nOptsCacheLifeImplWeb = std::max( sal_uInt32( 0 ),
3881  std::min( nAtime, sal_uInt32( 3600 ) ) );
3882 
3883  nAtime = officecfg::Inet::Settings::OptsCacheLifeDAV::get( m_xContext );
3884  m_nOptsCacheLifeDAV = std::max( sal_uInt32( 0 ),
3885  std::min( nAtime, sal_uInt32( 3600 ) ) );
3886 
3887  nAtime = officecfg::Inet::Settings::OptsCacheLifeDAVLocked::get( m_xContext );
3888  m_nOptsCacheLifeDAVLocked = std::max( sal_uInt32( 0 ),
3889  std::min( nAtime, sal_uInt32( 3600 ) ) );
3890 
3891  nAtime = officecfg::Inet::Settings::OptsCacheLifeNotImpl::get( m_xContext );
3892  m_nOptsCacheLifeNotImpl = std::max( sal_uInt32( 0 ),
3893  std::min( nAtime, sal_uInt32( 43200 ) ) );
3894 
3895  nAtime = officecfg::Inet::Settings::OptsCacheLifeNotFound::get( m_xContext );
3896  m_nOptsCacheLifeNotFound = std::max( sal_uInt32( 0 ),
3897  std::min( nAtime, sal_uInt32( 30 ) ) );
3898 }
3899 
3900 
3902  const css::uno::Reference< css::ucb::XCommandEnvironment >& xEnv,
3903  DAVOptions& rDAVOptions,
3904  const std::unique_ptr< DAVResourceAccess > & rResAccess,
3905  bool * networkAccessAllowed )
3906 {
3907  OUString aTargetURL = rResAccess->getURL();
3908  DAVOptions aDAVOptions;
3909  // first check if in cache, if not, then send method to server
3910  if ( !aStaticDAVOptionsCache.getDAVOptions( aTargetURL, aDAVOptions ) )
3911  {
3912  OUString aRedirURL;
3913  try
3914  {
3915  rResAccess->OPTIONS( aDAVOptions, xEnv );
3916  // IMPORTANT:the correctly implemented server will answer without errors, even if the resource is not present
3917  sal_uInt32 nLifeTime = ( aDAVOptions.isClass1() ||
3918  aDAVOptions.isClass2() ||
3919  aDAVOptions.isClass3() ) ?
3920  m_nOptsCacheLifeDAV : // a WebDAV site
3921  m_nOptsCacheLifeImplWeb; // a site implementing OPTIONS but
3922  // it's not DAV
3923  // if resource is locked, will use a
3924  // different lifetime
3925  if( aDAVOptions.isLocked() )
3926  nLifeTime = m_nOptsCacheLifeDAVLocked;
3927 
3928  // check if redirected
3929  aRedirURL = rResAccess->getURL();
3930  if( aRedirURL == aTargetURL)
3931  { // no redirection
3932  aRedirURL.clear();
3933  }
3934  // cache this URL's option
3935  aDAVOptions.setURL( aTargetURL );
3936  aDAVOptions.setRedirectedURL( aRedirURL );
3937  aStaticDAVOptionsCache.addDAVOptions( aDAVOptions,
3938  nLifeTime );
3939  }
3940  catch ( DAVException const & e )
3941  {
3942  // first, remove from cache, will be added if needed, depending on the error received
3943  aStaticDAVOptionsCache.removeDAVOptions( aTargetURL );
3944  rResAccess->resetUri();
3945 
3946  aDAVOptions.setURL( aTargetURL );
3947  aDAVOptions.setRedirectedURL( aRedirURL );
3948  switch( e.getError() )
3949  {
3952  {
3953  // something bad happened to the connection
3954  // not same as not found, this instead happens when the server doesn't exist or doesn't answer at all
3955  // probably a new bit stating 'timed out' should be added to opts var?
3956  // in any case abort the command
3957  SAL_WARN( "ucb.ucp.webdav", "OPTIONS - DAVException: DAV_HTTP_TIMEOUT or DAV_HTTP_CONNECT for URL <" << m_xIdentifier->getContentIdentifier() << ">" );
3958  // cache the internal unofficial status code
3959 
3961  // used only internally, so the text doesn't really matter...
3962  aStaticDAVOptionsCache.addDAVOptions( aDAVOptions,
3964  if ( networkAccessAllowed != nullptr )
3965  {
3966  *networkAccessAllowed = *networkAccessAllowed
3968  }
3969  }
3970  break;
3972  {
3973  SAL_WARN( "ucb.ucp.webdav", "OPTIONS - DAVException: DAV_HTTP_LOOKUP for URL <" << m_xIdentifier->getContentIdentifier() << ">" );
3975  // used only internally, so the text doesn't really matter...
3976  aStaticDAVOptionsCache.addDAVOptions( aDAVOptions,
3978  if ( networkAccessAllowed != nullptr )
3979  {
3980  *networkAccessAllowed = *networkAccessAllowed
3982  }
3983  }
3984  break;
3986  {
3987  SAL_WARN( "ucb.ucp.webdav", "OPTIONS - DAVException: DAV_HTTP_AUTH for URL <" << m_xIdentifier->getContentIdentifier() << ">" );
3988  // - the remote site is a WebDAV with special configuration: read/only for read operations
3989  // and read/write for write operations, the user is not allowed to lock/write and
3990  // she cancelled the credentials request.
3991  // this is not actually an error, it means only that for current user this is a standard web,
3992  // though possibly DAV enabled
3994  // used only internally, so the text doesn't really matter...
3995  aStaticDAVOptionsCache.addDAVOptions( aDAVOptions,
3997  if ( networkAccessAllowed != nullptr )
3998  {
3999  *networkAccessAllowed = *networkAccessAllowed
4001  }
4002  }
4003  break;
4005  {
4006  SAL_WARN( "ucb.ucp.webdav", "OPTIONS - DAVException: DAV_HTTP_AUTHPROXY for URL <" << m_xIdentifier->getContentIdentifier() << ">" );
4008  // used only internally, so the text doesn't really matter...
4009  aStaticDAVOptionsCache.addDAVOptions( aDAVOptions,
4011  if ( networkAccessAllowed != nullptr )
4012  {
4013  *networkAccessAllowed = *networkAccessAllowed
4015  }
4016  }
4017  break;
4019  {
4020  switch( e.getStatus() )
4021  {
4022  case SC_FORBIDDEN:
4023  {
4024  SAL_WARN( "ucb.ucp.webdav","OPTIONS - SC_FORBIDDEN for URL <" << m_xIdentifier->getContentIdentifier() << ">" );
4025  // cache it, so OPTIONS won't be called again, this URL does not support it
4026  aStaticDAVOptionsCache.addDAVOptions( aDAVOptions,
4028  }
4029  break;
4030  case SC_BAD_REQUEST:
4032  {
4033  SAL_WARN( "ucb.ucp.webdav","OPTIONS - SC_BAD_REQUEST or SC_INTERNAL_SERVER_ERROR for URL <" << m_xIdentifier->getContentIdentifier() << ">, HTTP error: "<< e.getStatus()
4034  << ", '" << e.getData() << "'" );
4035  // cache it, so OPTIONS won't be called again, this URL detect some problem while answering the method
4036  aDAVOptions.setHttpResponseStatusCode( e.getStatus() );
4037  aDAVOptions.setHttpResponseStatusText( e.getData() );
4038  aStaticDAVOptionsCache.addDAVOptions( aDAVOptions,
4040  }
4041  break;
4042  case SC_NOT_IMPLEMENTED:
4043  case SC_METHOD_NOT_ALLOWED:
4044  {
4045  // OPTIONS method must be implemented in DAV
4046  // resource is NON_DAV, or not advertising it
4047  SAL_WARN( "ucb.ucp.webdav","OPTIONS - SC_NOT_IMPLEMENTED or SC_METHOD_NOT_ALLOWED for URL <" << m_xIdentifier->getContentIdentifier() << ">, HTTP error: "<< e.getStatus()
4048  << ", '" << e.getData() << "'" );
4049  // cache it, so OPTIONS won't be called again, this URL does not support it
4050  aStaticDAVOptionsCache.addDAVOptions( aDAVOptions,
4052  }
4053  break;
4054  case SC_NOT_FOUND:
4055  {
4056  // Apparently on IIS 10.0, if you disabled OPTIONS method, this error is the one reported,
4057  // instead of SC_NOT_IMPLEMENTED or SC_METHOD_NOT_ALLOWED.
4058  // So check if this is an available resource, or a real 'Not Found' event.
4059  sal_uInt32 nLifeTime = m_nOptsCacheLifeNotFound;
4060  if( isResourceAvailable( xEnv, rResAccess, aDAVOptions ) )
4061  {
4062  SAL_WARN( "ucb.ucp.webdav", "OPTIONS - Got an SC_NOT_FOUND, but the URL <" << m_xIdentifier->getContentIdentifier() << "> resource exists" );
4063  nLifeTime = m_nOptsCacheLifeNotImpl;
4064  }
4065  else
4066  {
4067  SAL_WARN( "ucb.ucp.webdav", "OPTIONS - SC_NOT_FOUND for URL <" << m_xIdentifier->getContentIdentifier() << ">" );
4068  if ( networkAccessAllowed != nullptr )
4069  {
4070  *networkAccessAllowed = *networkAccessAllowed
4072  }
4073  }
4074  aStaticDAVOptionsCache.addDAVOptions( aDAVOptions,
4075  nLifeTime );
4076  }
4077  break;
4078  default:
4079  {
4080  SAL_WARN( "ucb.ucp.webdav", "OPTIONS - DAV_HTTP_ERROR, for URL <" << m_xIdentifier->getContentIdentifier() << ">, HTTP error: "<< e.getStatus()
4081  << ", '" << e.getData() << "'" );
4082  aDAVOptions.setHttpResponseStatusCode( e.getStatus() );
4083  aDAVOptions.setHttpResponseStatusText( e.getData() );
4084  // cache it, so OPTIONS won't be called again, this URL does not support it
4085  aStaticDAVOptionsCache.addDAVOptions( aDAVOptions,
4087  }
4088  break;
4089  }
4090  }
4091  break;
4092  // The 'DAVException::DAV_HTTP_REDIRECT' means we reached the maximum
4093  // number of redirections, consider the resource type as UNKNOWN
4094  // possibly a normal web site, not DAV
4096  default:
4097  {
4098  SAL_WARN( "ucb.ucp.webdav","OPTIONS - General DAVException (or max DAV_HTTP_REDIRECT reached) for URL <" << m_xIdentifier->getContentIdentifier() << ">, DAV ExceptionCode: "
4099  << e.getError() << ", HTTP error: "<< e.getStatus() );
4100  aStaticDAVOptionsCache.addDAVOptions( aDAVOptions,
4102  }
4103  break;
4104  }
4105  }
4106  }
4107  else
4108  {
4109  // check current response status code, perhaps we need to set networkAccessAllowed
4110  sal_uInt16 CachedResponseStatusCode = aDAVOptions.getHttpResponseStatusCode();
4111  if ( networkAccessAllowed != nullptr &&
4112  ( ( CachedResponseStatusCode == SC_NOT_FOUND ) ||
4113  ( CachedResponseStatusCode == SC_GONE ) ||
4114  ( CachedResponseStatusCode == USC_CONNECTION_TIMED_OUT ) ||
4115  ( CachedResponseStatusCode == USC_LOOKUP_FAILED ) ||
4116  ( CachedResponseStatusCode == USC_AUTH_FAILED ) ||
4117  ( CachedResponseStatusCode == USC_AUTHPROXY_FAILED )
4118  )
4119  )
4120  {
4121  *networkAccessAllowed = false;
4122  }
4123  }
4124  rDAVOptions = aDAVOptions;
4125 }
4126 
4127 
4128 //static
4129 bool Content::isResourceAvailable( const css::uno::Reference< css::ucb::XCommandEnvironment >& xEnv,
4130  const std::unique_ptr< DAVResourceAccess > & rResAccess,
4131  DAVOptions& rDAVOptions )
4132 {
4133  std::vector< OUString > aHeaderNames;
4134  DAVResource aResource;
4135 
4136  try
4137  {
4138  // To check for the physical URL resource availability, first
4139  // try using a simple HEAD command
4140  // if HEAD is successful, set element found,
4141  rResAccess->HEAD( aHeaderNames, aResource, xEnv );
4142  rDAVOptions.setHttpResponseStatusCode( 0 );
4143  rDAVOptions.setHttpResponseStatusText( OUString() );
4144  return true;
4145  }
4146  catch ( DAVException const & e )
4147  {
4149  {
4150  if ( e.getStatus() == SC_NOT_IMPLEMENTED ||
4152  e.getStatus() == SC_NOT_FOUND )
4153  {
4154  SAL_WARN( "ucb.ucp.webdav", "HEAD probably not implemented: fall back to a partial GET" );
4155  // set in cached OPTIONS "HEAD not implemented"
4156  // so it won't be used again on this resource
4157  rDAVOptions.setHeadAllowed( false );
4158  try
4159  {
4160  // do a GET with a payload of 0, the server does not
4161  // support HEAD (or has HEAD disabled)
4162  DAVRequestHeaders aPartialGet;
4163  aPartialGet.emplace_back(
4164  OUString( "Range" ),
4165  OUString( "bytes=0-0" ));
4166 
4167  rResAccess->GET0( aPartialGet,
4168  aHeaderNames,
4169  aResource,
4170  xEnv );
4171  return true;
4172  }
4173  catch ( DAVException const & ex )
4174  {
4175  if ( ex.getError() == DAVException::DAV_HTTP_ERROR )
4176  {
4177  rDAVOptions.setHttpResponseStatusCode( ex.getStatus() );
4178  rDAVOptions.setHttpResponseStatusText( ex.getData() );
4179  }
4180  }
4181  }
4182  else
4183  {
4184  rDAVOptions.setHttpResponseStatusCode( e.getStatus() );
4185  rDAVOptions.setHttpResponseStatusText( e.getData() );
4186  }
4187  }
4188  return false;
4189  }
4190  catch ( ... )
4191  {
4192  }
4193  // set SC_NOT_IMPLEMENTED since at a minimum GET must be implemented in a basic Web server
4195  rDAVOptions.setHttpResponseStatusText( OUString() );
4196  return false;
4197 }
4198 
4199 
4200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define WEBDAV_COLLECTION_TYPE
tools::SvRef< SvBaseLink > xSink
Type
css::uno::Reference< css::uno::XComponentContext > m_xContext
const sal_uInt16 USC_AUTHPROXY_FAILED
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
URL aURL
void transfer(const css::ucb::TransferInfo &rArgs, const css::uno::Reference< css::ucb::XCommandEnvironment > &Environment)
void setURL(const OUString &sURL)
bool hasValue()
std::vector< ContentImplHelperRef > ContentRefList
virtual void SAL_CALL acquire() noexcept override
static OUString unescape(const OUString &string)
Definition: NeonUri.cxx:257
void getResourceOptions(const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv, DAVOptions &rDAVOptions, const std::unique_ptr< DAVResourceAccess > &rResAccess, bool *networkAccessAllowed=nullptr)
const sal_uInt16 SC_BAD_REQUEST
OUString GetPathBaseNameUnescaped() const
Definition: NeonUri.cxx:233
void removeDAVOptions(const OUString &rURL)
Definition: DAVTypes.cxx:126
ResourceType m_eResourceTypeForLocks
bool isLockAllowed() const
std::vector< DAVRequestHeader > DAVRequestHeaders
bool exchangeIdentity(const css::uno::Reference< css::ucb::XContentIdentifier > &xNewId)
void addDAVOptions(DAVOptions &rDAVOptions, const sal_uInt32 nLifeTime)
Definition: DAVTypes.cxx:139
static void removeCachedPropertyNames(const OUString &rURL)
Reference< XInterface > xTarget
void setHeadAllowed(const OUString &rURL, bool HeadAllowed=true)
Definition: DAVTypes.cxx:167
sal_Int64 n
void insert(const css::uno::Reference< css::io::XInputStream > &xInputStream, bool bReplaceExisting, const css::uno::Reference< css::ucb::XCommandEnvironment > &Environment)
sal_uInt32 m_nOptsCacheLifeDAVLocked
static void UCBNamesToHTTPNames(const css::uno::Sequence< css::beans::Property > &rProps, std::vector< OUString > &resources)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
void setHttpResponseStatusCode(const sal_uInt16 nHttpResponseStatusCode)
OUString GetPathBaseName() const
Definition: NeonUri.cxx:199
virtual OUString getParentURL() override
static constexpr OUStringLiteral SUPPORTEDLOCK
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
#define DAVS_URL_SCHEME
std::vector< ContentRef > ContentRefList
#define WEBDAV_CONTENT_SERVICE_NAME
const ExceptionCode & getError() const
std::vector< OUString > m_aFailedPropNames
void setHeadAllowed(bool HeadAllowed=true)
#define WEBDAV_CONTENT_TYPE
css::uno::Reference< css::ucb::XPersistentPropertySet > getAdditionalPropertySet(bool bCreate)
void post(const css::ucb::PostCommandArgument2 &rArg, const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv)
PropertiesInfo aProperties
#define WEBDAV_URL_SCHEME
#define WEBDAVS_URL_SCHEME
virtual void SAL_CALL release() noexcept override
void removeProperty(const OUString &Name, const css::uno::Reference< css::ucb::XCommandEnvironment > &Environment)
int nCount
bool getDAVOptions(const OUString &rURL, DAVOptions &rDAVOptions)
Definition: DAVTypes.cxx:99
#define VNDSUNSTARWEBDAV_URL_SCHEME
const OUString & getHttpResponseStatusText() const
css::uno::Any open(const css::ucb::OpenCommandArgument3 &rArg, const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv)
void setRedirectedURL(const OUString &sRedirectedURL)
void notifyPropertiesChange(const css::uno::Sequence< css::beans::PropertyChangeEvent > &evt) const
UNKNOWN
const OUString & getData() const
virtual css::uno::Any SAL_CALL execute(const css::ucb::Command &aCommand, sal_Int32 CommandId, const css::uno::Reference< css::ucb::XCommandEnvironment > &Environment) override
const std::unique_ptr< PropertyValueMap > & getProperties() const
ContentProvider * m_pProvider
DocumentType eType
void cancelCommandExecution(const uno::Any &rException, const uno::Reference< ucb::XCommandEnvironment > &xEnv)
const sal_uInt16 SC_METHOD_NOT_ALLOWED
css::uno::Any MapDAVException(const DAVException &e, bool bWrite)
const sal_uInt16 SC_NOT_IMPLEMENTED
void lock(const css::uno::Reference< css::ucb::XCommandEnvironment > &Environment)
#define DAV_URL_SCHEME
ResourceType
sal_uInt16 getStatus() const
css::uno::Reference< css::sdbc::XRow > getPropertyValues(const css::uno::Sequence< css::beans::Property > &rProperties, const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv)
css::uno::Reference< css::ucb::XContentIdentifier > m_xIdentifier
Mutex aLock
static bool isResourceAvailable(const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv, const std::unique_ptr< DAVResourceAccess > &rResAccess, DAVOptions &rDAVOptions)
virtual css::uno::Reference< css::ucb::XContent > SAL_CALL createNewContent(const css::ucb::ContentInfo &Info) override
uno::Reference< sdbc::XRow > xRow
css::uno::Reference< css::beans::XPropertySetInfo > getPropertySetInfo(const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv, bool bCache=true)
#define VNDSUNSTARWEBDAVS_URL_SCHEME
Object Value
css::uno::Reference< css::ucb::XCommandInfo > getCommandInfo(const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv, bool bCache=true)
const PropertyStruct aPropNames[]
void getProperty(const OUString &rPropName, css::beans::Property &rProp)
sal_uInt16 getHttpResponseStatusCode() const
virtual css::uno::Sequence< css::ucb::ContentInfo > SAL_CALL queryCreatableContentsInfo() override
void cancelCommandExecution(const DAVException &e, const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv, bool bWrite=false)
XTYPEPROVIDER_COMMON_IMPL(Content)
SQLUSMALLINT SQLCHAR SQLSMALLINT SQLCHAR SQLSMALLINT SQLCHAR SQLSMALLINT SQLUSMALLINT Scope
const sal_uInt16 SC_GONE
ResourceType resourceTypeForLocks(const css::uno::Reference< css::ucb::XCommandEnvironment > &rEnvironment, const std::unique_ptr< DAVResourceAccess > &rResAccess)
const sal_uInt16 SC_FORBIDDEN
const sal_uInt16 SC_NONE
static bool shouldAccessNetworkAfterException(const DAVException &e)
static bool supportsType(const css::uno::Type &rType)
static bool isUCBSpecialProperty(const OUString &rFullName, OUString &rParsedName)
std::unique_ptr< DAVResourceAccess > m_xResAccess
const sal_uInt16 USC_LOOKUP_FAILED
virtual void SAL_CALL abort(sal_Int32 CommandId) override
rtl::Reference< ContentProviderImplHelper > m_xProvider
const PropertyValue * pValues
virtual OUString SAL_CALL getImplementationName() override
#define HTTP_URL_SCHEME
ContinuationFlags
const css::uno::Any & getValue(const OUString &rName) const
std::vector< DAVPropertyValue > properties
void notifyPropertySetInfoChange(const css::beans::PropertySetInfoChangeEvent &evt) const
void SetScheme(const OUString &scheme)
Definition: NeonUri.hxx:87
#define CPPU_TYPE_REF(T)
#define SAL_WARN_IF(condition, area, stream)
uno::Reference< ucb::XContentIdentifier > xId
bool isFolder(const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv)
void destroy(bool bDeletePhysical)
std::unique_ptr< CachableContentProperties > m_xCachedProps
OUString getBaseURI(const std::unique_ptr< DAVResourceAccess > &rResAccess)
const sal_uInt16 USC_CONNECTION_TIMED_OUT
#define FTP_URL_SCHEME
static OUString escapeSegment(const OUString &segment)
Definition: NeonUri.cxx:248
void setHttpResponseStatusText(const OUString &rHttpResponseStatusText)
#define SAL_INFO(area, stream)
void unlock(const css::uno::Reference< css::ucb::XCommandEnvironment > &Environment)
Sequence< sal_Int8 > aSeq
css::uno::Sequence< css::uno::Type > SAL_CALL getTypes()
const sal_uInt16 USC_AUTH_FAILED
const OUString & getURL() const
const sal_uInt16 SC_NOT_FOUND
ResourceType getResourceType(const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv)
void addProperty(const css::ucb::PropertyCommandArgument &aCmdArg, const css::uno::Reference< css::ucb::XCommandEnvironment > &Environment)
virtual OUString SAL_CALL getContentType() override
#define SAL_WARN(area, stream)
OUString aTargetURL
const sal_uInt16 SC_PRECONDITION_FAILED
bool exchange(const css::uno::Reference< css::ucb::XContentIdentifier > &rNewId)
void queryChildren(ContentRefList &rChildren)
static DAVOptionsCache aStaticDAVOptionsCache
bool isHeadAllowed() const
#define HTTPS_URL_SCHEME
css::uno::Sequence< css::uno::Any > setPropertyValues(const css::uno::Sequence< css::beans::PropertyValue > &rValues, const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv)
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
AnyEventRef aEvent
rtl::Reference< Content > ContentRef
static void UCBNamesToDAVNames(const css::uno::Sequence< css::beans::Property > &rProps, std::vector< OUString > &resources)
sal_uInt16 nPos
const sal_uInt16 SC_INTERNAL_SERVER_ERROR
static OUString makeConnectionEndPointString(const OUString &rHostName, int nPort)
Definition: NeonUri.cxx:265