Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / ucb / source / ucp / webdav / DAVResourceAccess.cxx
blob90001a818645ded9c13545f5a7f2d1839127455e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <com/sun/star/task/XInteractionAbort.hpp>
22 #include <com/sun/star/ucb/XWebDAVCommandEnvironment.hpp>
24 #include <ucbhelper/simpleauthenticationrequest.hxx>
25 #include <comphelper/seekableinput.hxx>
26 #include <sal/log.hxx>
28 #include "DAVAuthListenerImpl.hxx"
29 #include "DAVResourceAccess.hxx"
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 #include <com/sun/star/io/IOException.hpp>
34 using namespace http_dav_ucp;
35 using namespace com::sun::star;
38 // DAVAuthListener_Impl Implementation.
41 // virtual
42 int DAVAuthListener_Impl::authenticate(
43 const OUString & inRealm,
44 const OUString & inHostName,
45 OUString & inoutUserName,
46 OUString & outPassWord,
47 bool bCanUseSystemCredentials,
48 bool bUsePreviousCredentials )
50 if ( m_xEnv.is() )
52 uno::Reference< task::XInteractionHandler > xIH
53 = m_xEnv->getInteractionHandler();
55 if ( xIH.is() )
57 // Providing previously retrieved credentials will cause the password
58 // container to reject these. Thus, the credential input dialog will be shown again.
59 // #102871# - Supply username and password from previous try.
60 // Password container service depends on this!
61 if ( inoutUserName.isEmpty() && bUsePreviousCredentials )
62 inoutUserName = m_aPrevUsername;
64 if ( outPassWord.isEmpty() && bUsePreviousCredentials )
65 outPassWord = m_aPrevPassword;
67 rtl::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
68 = new ucbhelper::SimpleAuthenticationRequest(
69 m_aURL, inHostName, inRealm, inoutUserName,
70 outPassWord,
71 true /*bAllowPersistentStoring*/,
72 bCanUseSystemCredentials );
73 xIH->handle( xRequest.get() );
75 rtl::Reference< ucbhelper::InteractionContinuation > xSelection
76 = xRequest->getSelection();
78 if ( xSelection.is() )
80 // Handler handled the request.
81 uno::Reference< task::XInteractionAbort > xAbort(
82 xSelection.get(), uno::UNO_QUERY );
83 if ( !xAbort.is() )
85 const rtl::Reference<
86 ucbhelper::InteractionSupplyAuthentication > & xSupp
87 = xRequest->getAuthenticationSupplier();
89 bool bUseSystemCredentials = false;
91 if ( bCanUseSystemCredentials )
92 bUseSystemCredentials
93 = xSupp->getUseSystemCredentials();
95 if ( bUseSystemCredentials )
97 // This is the (strange) way to tell neon to use
98 // system credentials.
99 inoutUserName.clear();
100 outPassWord.clear();
102 else
104 inoutUserName = xSupp->getUserName();
105 outPassWord = xSupp->getPassword();
108 // #102871# - Remember username and password.
109 m_aPrevUsername = inoutUserName;
110 m_aPrevPassword = outPassWord;
112 // go on.
113 return 0;
118 // Abort.
119 return -1;
123 // DAVResourceAccess Implementation.
126 DAVResourceAccess::DAVResourceAccess(
127 const uno::Reference< uno::XComponentContext > & rContext,
128 rtl::Reference< DAVSessionFactory > const & rSessionFactory,
129 const OUString & rURL )
130 : m_aURL( rURL ),
131 m_xSessionFactory( rSessionFactory ),
132 m_xContext( rContext )
137 DAVResourceAccess::DAVResourceAccess( const DAVResourceAccess & rOther )
138 : m_aURL( rOther.m_aURL ),
139 m_aPath( rOther.m_aPath ),
140 m_xSession( rOther.m_xSession ),
141 m_xSessionFactory( rOther.m_xSessionFactory ),
142 m_xContext( rOther.m_xContext ),
143 m_aRedirectURIs( rOther.m_aRedirectURIs )
148 DAVResourceAccess & DAVResourceAccess::operator=(
149 const DAVResourceAccess & rOther )
151 m_aURL = rOther.m_aURL;
152 m_aPath = rOther.m_aPath;
153 m_xSession = rOther.m_xSession;
154 m_xSessionFactory = rOther.m_xSessionFactory;
155 m_xContext = rOther.m_xContext;
156 m_aRedirectURIs = rOther.m_aRedirectURIs;
158 return *this;
162 void DAVResourceAccess::PROPFIND(
163 const Depth nDepth,
164 const std::vector< OUString > & rPropertyNames,
165 std::vector< DAVResource > & rResources,
166 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
168 initialize();
170 int errorCount = 0;
171 bool bRetry;
174 bRetry = false;
177 DAVRequestHeaders aHeaders;
179 getUserRequestHeaders( xEnv,
180 getRequestURI(),
181 ucb::WebDAVHTTPMethod_PROPFIND,
182 aHeaders );
184 m_xSession->PROPFIND( getRequestURI(),
185 nDepth,
186 rPropertyNames,
187 rResources,
188 DAVRequestEnvironment(
189 getRequestURI(),
190 new DAVAuthListener_Impl( xEnv, m_aURL ),
191 aHeaders, xEnv ) );
193 catch ( DAVException & e )
195 errorCount++;
196 bRetry = handleException( e, errorCount );
197 if ( !bRetry )
198 throw;
201 while ( bRetry );
205 void DAVResourceAccess::PROPFIND(
206 const Depth nDepth,
207 std::vector< DAVResourceInfo > & rResInfo,
208 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
210 initialize();
212 int errorCount = 0;
213 bool bRetry;
216 bRetry = false;
219 DAVRequestHeaders aHeaders;
220 getUserRequestHeaders( xEnv,
221 getRequestURI(),
222 ucb::WebDAVHTTPMethod_PROPFIND,
223 aHeaders );
225 m_xSession->PROPFIND( getRequestURI(),
226 nDepth,
227 rResInfo,
228 DAVRequestEnvironment(
229 getRequestURI(),
230 new DAVAuthListener_Impl( xEnv, m_aURL ),
231 aHeaders, xEnv ) ) ;
233 catch ( DAVException & e )
235 errorCount++;
236 bRetry = handleException( e, errorCount );
237 if ( !bRetry )
238 throw;
241 while ( bRetry );
245 void DAVResourceAccess::PROPPATCH(
246 const std::vector< ProppatchValue >& rValues,
247 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
249 initialize();
251 int errorCount = 0;
252 bool bRetry;
255 bRetry = false;
258 DAVRequestHeaders aHeaders;
259 getUserRequestHeaders( xEnv,
260 getRequestURI(),
261 ucb::WebDAVHTTPMethod_PROPPATCH,
262 aHeaders );
264 m_xSession->PROPPATCH( getRequestURI(),
265 rValues,
266 DAVRequestEnvironment(
267 getRequestURI(),
268 new DAVAuthListener_Impl( xEnv, m_aURL ),
269 aHeaders, xEnv ) );
271 catch ( DAVException & e )
273 errorCount++;
274 bRetry = handleException( e, errorCount );
275 if ( !bRetry )
276 throw;
279 while ( bRetry );
283 void DAVResourceAccess::HEAD(
284 const std::vector< OUString > & rHeaderNames,
285 DAVResource & rResource,
286 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
288 initialize();
290 int errorCount = 0;
291 bool bRetry;
294 bRetry = false;
297 DAVRequestHeaders aHeaders;
298 getUserRequestHeaders( xEnv,
299 getRequestURI(),
300 ucb::WebDAVHTTPMethod_HEAD,
301 aHeaders );
303 m_xSession->HEAD( getRequestURI(),
304 rHeaderNames,
305 rResource,
306 DAVRequestEnvironment(
307 getRequestURI(),
308 new DAVAuthListener_Impl( xEnv, m_aURL ),
309 aHeaders, xEnv ) );
311 catch ( DAVException & e )
313 errorCount++;
314 bRetry = handleException( e, errorCount );
315 if ( !bRetry )
316 throw;
319 while ( bRetry );
323 uno::Reference< io::XInputStream > DAVResourceAccess::GET(
324 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
326 initialize();
328 uno::Reference< io::XInputStream > xStream;
329 int errorCount = 0;
330 bool bRetry;
333 bRetry = false;
336 DAVRequestHeaders aHeaders;
337 getUserRequestHeaders( xEnv,
338 getRequestURI(),
339 ucb::WebDAVHTTPMethod_GET,
340 aHeaders );
342 xStream = m_xSession->GET( getRequestURI(),
343 DAVRequestEnvironment(
344 getRequestURI(),
345 new DAVAuthListener_Impl(
346 xEnv, m_aURL ),
347 aHeaders, xEnv ) );
349 catch ( DAVException & e )
351 errorCount++;
352 bRetry = handleException( e, errorCount );
353 if ( !bRetry )
354 throw;
357 while ( bRetry );
359 return xStream;
363 void DAVResourceAccess::GET(
364 uno::Reference< io::XOutputStream > & rStream,
365 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
367 initialize();
369 int errorCount = 0;
370 bool bRetry;
373 bRetry = false;
376 DAVRequestHeaders aHeaders;
377 getUserRequestHeaders( xEnv,
378 getRequestURI(),
379 ucb::WebDAVHTTPMethod_GET,
380 aHeaders );
382 m_xSession->GET( getRequestURI(),
383 rStream,
384 DAVRequestEnvironment(
385 getRequestURI(),
386 new DAVAuthListener_Impl( xEnv, m_aURL ),
387 aHeaders, xEnv ) );
389 catch ( DAVException & e )
391 errorCount++;
392 bRetry = handleException( e, errorCount );
393 if ( !bRetry )
394 throw;
397 while ( bRetry );
401 uno::Reference< io::XInputStream > DAVResourceAccess::GET(
402 const std::vector< OUString > & rHeaderNames,
403 DAVResource & rResource,
404 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
406 initialize();
408 uno::Reference< io::XInputStream > xStream;
409 int errorCount = 0;
410 bool bRetry;
413 bRetry = false;
416 DAVRequestHeaders aHeaders;
417 getUserRequestHeaders( xEnv,
418 getRequestURI(),
419 ucb::WebDAVHTTPMethod_GET,
420 aHeaders );
422 xStream = m_xSession->GET( getRequestURI(),
423 rHeaderNames,
424 rResource,
425 DAVRequestEnvironment(
426 getRequestURI(),
427 new DAVAuthListener_Impl(
428 xEnv, m_aURL ),
429 aHeaders, xEnv ) );
431 catch ( DAVException & e )
433 errorCount++;
434 bRetry = handleException( e, errorCount );
435 if ( !bRetry )
436 throw;
439 while ( bRetry );
441 return xStream;
445 uno::Reference< io::XInputStream > DAVResourceAccess::GET(
446 DAVRequestHeaders &rRequestHeaders,
447 const std::vector< OUString > & rHeaderNames,
448 DAVResource & rResource,
449 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
451 initialize();
453 uno::Reference< io::XInputStream > xStream;
454 int errorCount = 0;
455 bool bRetry;
458 bRetry = false;
461 getUserRequestHeaders( xEnv,
462 getRequestURI(),
463 ucb::WebDAVHTTPMethod_GET,
464 rRequestHeaders );
466 xStream = m_xSession->GET( getRequestURI(),
467 rHeaderNames,
468 rResource,
469 DAVRequestEnvironment(
470 getRequestURI(),
471 new DAVAuthListener_Impl(
472 xEnv, m_aURL ),
473 rRequestHeaders, xEnv ) );
475 catch ( DAVException & e )
477 errorCount++;
478 bRetry = handleException( e, errorCount );
479 if ( !bRetry )
480 throw;
483 while ( bRetry );
485 return xStream;
489 void DAVResourceAccess::GET(
490 uno::Reference< io::XOutputStream > & rStream,
491 const std::vector< OUString > & rHeaderNames,
492 DAVResource & rResource,
493 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
495 initialize();
497 bool bRetry;
498 int errorCount = 0;
501 bRetry = false;
504 DAVRequestHeaders aHeaders;
505 getUserRequestHeaders( xEnv,
506 getRequestURI(),
507 ucb::WebDAVHTTPMethod_GET,
508 aHeaders );
510 m_xSession->GET( getRequestURI(),
511 rStream,
512 rHeaderNames,
513 rResource,
514 DAVRequestEnvironment(
515 getRequestURI(),
516 new DAVAuthListener_Impl( xEnv, m_aURL ),
517 aHeaders, xEnv ) );
519 catch ( DAVException & e )
521 errorCount++;
522 bRetry = handleException( e, errorCount );
523 if ( !bRetry )
524 throw;
527 while ( bRetry );
531 void DAVResourceAccess::abort()
533 // 17.11.09 (tkr): abort currently disabled caused by issue i106766
534 // initialize();
535 // m_xSession->abort();
536 SAL_INFO("ucb.ucp.webdav", "Not implemented. -> #i106766#" );
540 namespace {
542 /// @throws DAVException
543 void resetInputStream( const uno::Reference< io::XInputStream > & rStream )
547 uno::Reference< io::XSeekable > xSeekable(
548 rStream, uno::UNO_QUERY );
549 if ( xSeekable.is() )
551 xSeekable->seek( 0 );
552 return;
555 catch ( lang::IllegalArgumentException const & )
558 catch ( io::IOException const & )
562 throw DAVException( DAVException::DAV_INVALID_ARG );
565 } // namespace
568 void DAVResourceAccess::PUT(
569 const uno::Reference< io::XInputStream > & rStream,
570 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
572 initialize();
574 // Make stream seekable, if it not. Needed, if request must be retried.
575 uno::Reference< io::XInputStream > xSeekableStream
576 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
577 rStream, m_xContext );
579 int errorCount = 0;
580 bool bRetry = false;
583 if ( bRetry )
584 resetInputStream( xSeekableStream );
586 bRetry = false;
589 DAVRequestHeaders aHeaders;
590 getUserRequestHeaders( xEnv,
591 getRequestURI(),
592 ucb::WebDAVHTTPMethod_PUT,
593 aHeaders );
595 m_xSession->PUT( getRequestURI(),
596 xSeekableStream,
597 DAVRequestEnvironment(
598 getRequestURI(),
599 new DAVAuthListener_Impl( xEnv, m_aURL ),
600 aHeaders, xEnv ) );
602 catch ( DAVException & e )
604 errorCount++;
605 bRetry = handleException( e, errorCount );
606 if ( !bRetry )
607 throw;
610 while ( bRetry );
614 uno::Reference< io::XInputStream > DAVResourceAccess::POST(
615 const OUString & rContentType,
616 const OUString & rReferer,
617 const uno::Reference< io::XInputStream > & rInputStream,
618 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
620 initialize();
622 // Make stream seekable, if it not. Needed, if request must be retried.
623 uno::Reference< io::XInputStream > xSeekableStream
624 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
625 rInputStream, m_xContext );
627 uno::Reference< io::XInputStream > xStream;
628 int errorCount = 0;
629 bool bRetry = false;
632 if ( bRetry )
634 resetInputStream( xSeekableStream );
635 bRetry = false;
640 DAVRequestHeaders aHeaders;
641 getUserRequestHeaders( xEnv,
642 getRequestURI(),
643 ucb::WebDAVHTTPMethod_POST,
644 aHeaders );
646 xStream = m_xSession->POST( getRequestURI(),
647 rContentType,
648 rReferer,
649 xSeekableStream,
650 DAVRequestEnvironment(
651 getRequestURI(),
652 new DAVAuthListener_Impl(
653 xEnv, m_aURL ),
654 aHeaders, xEnv ) );
656 catch ( DAVException & e )
658 errorCount++;
659 bRetry = handleException( e, errorCount );
660 if ( !bRetry )
661 throw;
663 if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
665 // #i74980# - Upon POST redirect, do a GET.
666 return GET( xEnv );
670 while ( bRetry );
672 return xStream;
676 void DAVResourceAccess::POST(
677 const OUString & rContentType,
678 const OUString & rReferer,
679 const uno::Reference< io::XInputStream > & rInputStream,
680 uno::Reference< io::XOutputStream > & rOutputStream,
681 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
683 initialize();
685 // Make stream seekable, if it not. Needed, if request must be retried.
686 uno::Reference< io::XInputStream > xSeekableStream
687 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
688 rInputStream, m_xContext );
690 int errorCount = 0;
691 bool bRetry = false;
694 if ( bRetry )
696 resetInputStream( xSeekableStream );
697 bRetry = false;
702 DAVRequestHeaders aHeaders;
703 getUserRequestHeaders( xEnv,
704 getRequestURI(),
705 ucb::WebDAVHTTPMethod_POST,
706 aHeaders );
708 m_xSession->POST( getRequestURI(),
709 rContentType,
710 rReferer,
711 xSeekableStream,
712 rOutputStream,
713 DAVRequestEnvironment(
714 getRequestURI(),
715 new DAVAuthListener_Impl( xEnv, m_aURL ),
716 aHeaders, xEnv ) );
718 catch ( DAVException & e )
720 errorCount++;
721 bRetry = handleException( e, errorCount );
722 if ( !bRetry )
723 throw;
725 if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
727 // #i74980# - Upon POST redirect, do a GET.
728 GET( rOutputStream, xEnv );
729 return;
733 while ( bRetry );
737 void DAVResourceAccess::MKCOL(
738 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
740 initialize();
742 int errorCount = 0;
743 bool bRetry;
746 bRetry = false;
749 DAVRequestHeaders aHeaders;
750 getUserRequestHeaders( xEnv,
751 getRequestURI(),
752 ucb::WebDAVHTTPMethod_MKCOL,
753 aHeaders );
755 m_xSession->MKCOL( getRequestURI(),
756 DAVRequestEnvironment(
757 getRequestURI(),
758 new DAVAuthListener_Impl( xEnv, m_aURL ),
759 aHeaders, xEnv ) );
761 catch ( DAVException & e )
763 errorCount++;
764 bRetry = handleException( e, errorCount );
765 if ( !bRetry )
766 throw;
769 while ( bRetry );
773 void DAVResourceAccess::COPY(
774 const OUString & rSourcePath,
775 const OUString & rDestinationURI,
776 bool bOverwrite,
777 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
779 initialize();
781 int errorCount = 0;
782 bool bRetry;
785 bRetry = false;
788 DAVRequestHeaders aHeaders;
789 getUserRequestHeaders( xEnv,
790 getRequestURI(),
791 ucb::WebDAVHTTPMethod_COPY,
792 aHeaders );
794 m_xSession->COPY( rSourcePath,
795 rDestinationURI,
796 DAVRequestEnvironment(
797 getRequestURI(),
798 new DAVAuthListener_Impl( xEnv, m_aURL ),
799 aHeaders, xEnv ),
800 bOverwrite );
802 catch ( DAVException & e )
804 errorCount++;
805 bRetry = handleException( e, errorCount );
806 if ( !bRetry )
807 throw;
810 while ( bRetry );
814 void DAVResourceAccess::MOVE(
815 const OUString & rSourcePath,
816 const OUString & rDestinationURI,
817 bool bOverwrite,
818 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
820 initialize();
822 int errorCount = 0;
823 bool bRetry;
826 bRetry = false;
829 DAVRequestHeaders aHeaders;
830 getUserRequestHeaders( xEnv,
831 getRequestURI(),
832 ucb::WebDAVHTTPMethod_MOVE,
833 aHeaders );
835 m_xSession->MOVE( rSourcePath,
836 rDestinationURI,
837 DAVRequestEnvironment(
838 getRequestURI(),
839 new DAVAuthListener_Impl( xEnv, m_aURL ),
840 aHeaders, xEnv ),
841 bOverwrite );
843 catch ( DAVException & e )
845 errorCount++;
846 bRetry = handleException( e, errorCount );
847 if ( !bRetry )
848 throw;
851 while ( bRetry );
855 void DAVResourceAccess::DESTROY(
856 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
858 initialize();
860 int errorCount = 0;
861 bool bRetry;
864 bRetry = false;
867 DAVRequestHeaders aHeaders;
868 getUserRequestHeaders( xEnv,
869 getRequestURI(),
870 ucb::WebDAVHTTPMethod_DELETE,
871 aHeaders );
873 m_xSession->DESTROY( getRequestURI(),
874 DAVRequestEnvironment(
875 getRequestURI(),
876 new DAVAuthListener_Impl( xEnv, m_aURL ),
877 aHeaders, xEnv ) );
879 catch ( DAVException & e )
881 errorCount++;
882 bRetry = handleException( e, errorCount );
883 if ( !bRetry )
884 throw;
887 while ( bRetry );
891 // set new lock.
892 void DAVResourceAccess::LOCK(
893 ucb::Lock & inLock,
894 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
896 initialize();
898 int errorCount = 0;
899 bool bRetry;
902 bRetry = false;
905 DAVRequestHeaders aHeaders;
906 getUserRequestHeaders( xEnv,
907 getRequestURI(),
908 ucb::WebDAVHTTPMethod_LOCK,
909 aHeaders );
911 m_xSession->LOCK( getRequestURI(),
912 inLock,
913 DAVRequestEnvironment(
914 getRequestURI(),
915 new DAVAuthListener_Impl( xEnv, m_aURL ),
916 aHeaders, xEnv ) );
918 catch ( DAVException & e )
920 errorCount++;
921 bRetry = handleException( e, errorCount );
922 if ( !bRetry )
923 throw;
926 while ( bRetry );
929 void DAVResourceAccess::UNLOCK(
930 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
932 initialize();
934 int errorCount = 0;
935 bool bRetry;
938 bRetry = false;
941 DAVRequestHeaders aHeaders;
942 getUserRequestHeaders( xEnv,
943 getRequestURI(),
944 ucb::WebDAVHTTPMethod_UNLOCK,
945 aHeaders );
947 m_xSession->UNLOCK( getRequestURI(),
948 DAVRequestEnvironment(
949 getRequestURI(),
950 new DAVAuthListener_Impl( xEnv, m_aURL ),
951 aHeaders, xEnv ) );
953 catch ( DAVException & e )
955 errorCount++;
956 bRetry = handleException( e, errorCount );
957 if ( !bRetry )
958 throw;
961 while ( bRetry );
965 void DAVResourceAccess::setURL( const OUString & rNewURL )
967 osl::Guard< osl::Mutex > aGuard( m_aMutex );
968 m_aURL = rNewURL;
969 m_aPath.clear(); // Next initialize() will create new session.
973 // init dav session and path
974 void DAVResourceAccess::initialize()
976 osl::Guard< osl::Mutex > aGuard( m_aMutex );
977 if ( m_aPath.isEmpty() )
979 SerfUri aURI( m_aURL );
980 OUString aPath( aURI.GetPath() );
982 /* #134089# - Check URI */
983 if ( aPath.isEmpty() )
984 throw DAVException( DAVException::DAV_INVALID_ARG );
986 /* #134089# - Check URI */
987 if ( aURI.GetHost().isEmpty() )
988 throw DAVException( DAVException::DAV_INVALID_ARG );
990 if ( !m_xSession.is() || !m_xSession->CanUse( m_aURL ) )
992 m_xSession.clear();
994 // create new webdav session
995 m_xSession
996 = m_xSessionFactory->createDAVSession( m_aURL, m_xContext );
998 if ( !m_xSession.is() )
999 return;
1002 // Own URI is needed for redirect cycle detection.
1003 m_aRedirectURIs.push_back( aURI );
1005 // Success.
1006 m_aPath = aPath;
1008 // Not only the path has to be encoded
1009 m_aURL = aURI.GetURI();
1014 const OUString & DAVResourceAccess::getRequestURI() const
1016 SAL_WARN_IF( !m_xSession.is(), "ucb.ucp.webdav",
1017 "DAVResourceAccess::getRequestURI - Not initialized!" );
1019 // In case a proxy is used we have to use the absolute URI for a request.
1020 if ( m_xSession->UsesProxy() )
1021 return m_aURL;
1023 return m_aPath;
1027 // static
1028 void DAVResourceAccess::getUserRequestHeaders(
1029 const uno::Reference< ucb::XCommandEnvironment > & xEnv,
1030 const OUString & rURI,
1031 ucb::WebDAVHTTPMethod eMethod,
1032 DAVRequestHeaders & rRequestHeaders )
1034 if ( !xEnv.is() )
1035 return;
1037 uno::Reference< ucb::XWebDAVCommandEnvironment > xDAVEnv(
1038 xEnv, uno::UNO_QUERY );
1040 if ( !xDAVEnv.is() )
1041 return;
1043 uno::Sequence< beans::StringPair > aRequestHeaders
1044 = xDAVEnv->getUserRequestHeaders( rURI, eMethod );
1046 for ( sal_Int32 n = 0; n < aRequestHeaders.getLength(); ++n )
1048 rRequestHeaders.push_back(
1049 DAVRequestHeader( aRequestHeaders[ n ].First,
1050 aRequestHeaders[ n ].Second ) );
1055 bool DAVResourceAccess::detectRedirectCycle(
1056 const OUString& rRedirectURL )
1058 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1060 SerfUri aUri( rRedirectURL );
1062 return std::any_of(m_aRedirectURIs.begin(), m_aRedirectURIs.end(),
1063 [&aUri](const SerfUri& rUri) { return aUri == rUri; });
1067 void DAVResourceAccess::resetUri()
1069 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1070 if ( ! m_aRedirectURIs.empty() )
1072 std::vector< SerfUri >::const_iterator it = m_aRedirectURIs.begin();
1074 SerfUri aUri( *it );
1075 m_aRedirectURIs.clear();
1076 setURL ( aUri.GetURI() );
1077 initialize();
1082 bool DAVResourceAccess::handleException( DAVException & e, int errorCount )
1084 switch ( e.getError() )
1086 case DAVException::DAV_HTTP_REDIRECT:
1087 if ( !detectRedirectCycle( e.getData() ) )
1089 // set new URL and path.
1090 setURL( e.getData() );
1091 initialize();
1092 return true;
1094 return false;
1095 // --> tkr #67048# copy & paste images doesn't display.
1096 // if we have a bad connection try again. Up to three times.
1097 case DAVException::DAV_HTTP_ERROR:
1098 // retry up to three times, if not a client-side error.
1099 if ( ( e.getStatus() < 400 || e.getStatus() >= 500 ||
1100 e.getStatus() == 413 ) &&
1101 errorCount < 3 )
1103 return true;
1105 return false;
1106 // <--
1107 // --> tkr: if connection has said retry then retry!
1108 case DAVException::DAV_HTTP_RETRY:
1109 return true;
1110 // <--
1111 default:
1112 return false; // Abort
1116 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */