bump product version to 5.0.4.1
[LibreOffice.git] / ucb / source / ucp / webdav / DAVResourceAccess.cxx
blobb18a50456a06792d88c309faf12e06c1580d3414
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 .
20 #include <osl/diagnose.h>
22 #include <com/sun/star/task/XInteractionAbort.hpp>
23 #include <com/sun/star/ucb/XWebDAVCommandEnvironment.hpp>
25 #include <ucbhelper/simpleauthenticationrequest.hxx>
26 #include <comphelper/seekableinput.hxx>
28 #include "DAVAuthListenerImpl.hxx"
29 #include "DAVResourceAccess.hxx"
31 using namespace http_dav_ucp;
32 using namespace com::sun::star;
37 // DAVAuthListener_Impl Implementation.
43 // virtual
44 int DAVAuthListener_Impl::authenticate(
45 const OUString & inRealm,
46 const OUString & inHostName,
47 OUString & inoutUserName,
48 OUString & outPassWord,
49 bool bCanUseSystemCredentials,
50 bool bUsePreviousCredentials )
52 if ( m_xEnv.is() )
54 uno::Reference< task::XInteractionHandler > xIH
55 = m_xEnv->getInteractionHandler();
57 if ( xIH.is() )
59 // Providing previously retrieved credentials will cause the password
60 // container to reject these. Thus, the credential input dialog will be shown again.
61 // #102871# - Supply username and password from previous try.
62 // Password container service depends on this!
63 if ( inoutUserName.isEmpty() && bUsePreviousCredentials )
64 inoutUserName = m_aPrevUsername;
66 if ( outPassWord.isEmpty() && bUsePreviousCredentials )
67 outPassWord = m_aPrevPassword;
69 rtl::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
70 = new ucbhelper::SimpleAuthenticationRequest(
71 m_aURL, inHostName, inRealm, inoutUserName,
72 outPassWord, OUString(),
73 true /*bAllowPersistentStoring*/,
74 bCanUseSystemCredentials );
75 xIH->handle( xRequest.get() );
77 rtl::Reference< ucbhelper::InteractionContinuation > xSelection
78 = xRequest->getSelection();
80 if ( xSelection.is() )
82 // Handler handled the request.
83 uno::Reference< task::XInteractionAbort > xAbort(
84 xSelection.get(), uno::UNO_QUERY );
85 if ( !xAbort.is() )
87 const rtl::Reference<
88 ucbhelper::InteractionSupplyAuthentication > & xSupp
89 = xRequest->getAuthenticationSupplier();
91 bool bUseSystemCredentials = false;
93 if ( bCanUseSystemCredentials )
94 bUseSystemCredentials
95 = xSupp->getUseSystemCredentials();
97 if ( bUseSystemCredentials )
99 // This is the (strange) way to tell neon to use
100 // system credentials.
101 inoutUserName.clear();
102 outPassWord.clear();
104 else
106 inoutUserName = xSupp->getUserName();
107 outPassWord = xSupp->getPassword();
110 // #102871# - Remember username and password.
111 m_aPrevUsername = inoutUserName;
112 m_aPrevPassword = outPassWord;
114 // go on.
115 return 0;
120 // Abort.
121 return -1;
127 // DAVResourceAccess Implementation.
133 DAVResourceAccess::DAVResourceAccess(
134 const uno::Reference< uno::XComponentContext > & rContext,
135 rtl::Reference< DAVSessionFactory > const & rSessionFactory,
136 const OUString & rURL )
137 : m_aURL( rURL ),
138 m_xSessionFactory( rSessionFactory ),
139 m_xContext( rContext )
144 DAVResourceAccess::DAVResourceAccess( const DAVResourceAccess & rOther )
145 : m_aURL( rOther.m_aURL ),
146 m_aPath( rOther.m_aPath ),
147 m_xSession( rOther.m_xSession ),
148 m_xSessionFactory( rOther.m_xSessionFactory ),
149 m_xContext( rOther.m_xContext ),
150 m_aRedirectURIs( rOther.m_aRedirectURIs )
155 DAVResourceAccess & DAVResourceAccess::operator=(
156 const DAVResourceAccess & rOther )
158 m_aURL = rOther.m_aURL;
159 m_aPath = rOther.m_aPath;
160 m_xSession = rOther.m_xSession;
161 m_xSessionFactory = rOther.m_xSessionFactory;
162 m_xContext = rOther.m_xContext;
163 m_aRedirectURIs = rOther.m_aRedirectURIs;
165 return *this;
169 void DAVResourceAccess::PROPFIND(
170 const Depth nDepth,
171 const std::vector< OUString > & rPropertyNames,
172 std::vector< DAVResource > & rResources,
173 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
174 throw( DAVException )
176 initialize();
178 int errorCount = 0;
179 bool bRetry;
182 bRetry = false;
185 DAVRequestHeaders aHeaders;
187 getUserRequestHeaders( xEnv,
188 getRequestURI(),
189 ucb::WebDAVHTTPMethod_PROPFIND,
190 aHeaders );
192 m_xSession->PROPFIND( getRequestURI(),
193 nDepth,
194 rPropertyNames,
195 rResources,
196 DAVRequestEnvironment(
197 getRequestURI(),
198 new DAVAuthListener_Impl( xEnv, m_aURL ),
199 aHeaders, xEnv ) );
201 catch ( DAVException & e )
203 errorCount++;
204 bRetry = handleException( e, errorCount );
205 if ( !bRetry )
206 throw;
209 while ( bRetry );
213 void DAVResourceAccess::PROPFIND(
214 const Depth nDepth,
215 std::vector< DAVResourceInfo > & rResInfo,
216 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
217 throw( DAVException )
219 initialize();
221 int errorCount = 0;
222 bool bRetry;
225 bRetry = false;
228 DAVRequestHeaders aHeaders;
229 getUserRequestHeaders( xEnv,
230 getRequestURI(),
231 ucb::WebDAVHTTPMethod_PROPFIND,
232 aHeaders );
234 m_xSession->PROPFIND( getRequestURI(),
235 nDepth,
236 rResInfo,
237 DAVRequestEnvironment(
238 getRequestURI(),
239 new DAVAuthListener_Impl( xEnv, m_aURL ),
240 aHeaders, xEnv ) ) ;
242 catch ( DAVException & e )
244 errorCount++;
245 bRetry = handleException( e, errorCount );
246 if ( !bRetry )
247 throw;
250 while ( bRetry );
254 void DAVResourceAccess::PROPPATCH(
255 const std::vector< ProppatchValue >& rValues,
256 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
257 throw( DAVException )
259 initialize();
261 int errorCount = 0;
262 bool bRetry;
265 bRetry = false;
268 DAVRequestHeaders aHeaders;
269 getUserRequestHeaders( xEnv,
270 getRequestURI(),
271 ucb::WebDAVHTTPMethod_PROPPATCH,
272 aHeaders );
274 m_xSession->PROPPATCH( getRequestURI(),
275 rValues,
276 DAVRequestEnvironment(
277 getRequestURI(),
278 new DAVAuthListener_Impl( xEnv, m_aURL ),
279 aHeaders, xEnv ) );
281 catch ( DAVException & e )
283 errorCount++;
284 bRetry = handleException( e, errorCount );
285 if ( !bRetry )
286 throw;
289 while ( bRetry );
293 void DAVResourceAccess::HEAD(
294 const std::vector< OUString > & rHeaderNames,
295 DAVResource & rResource,
296 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
297 throw( DAVException )
299 initialize();
301 int errorCount = 0;
302 bool bRetry;
305 bRetry = false;
308 DAVRequestHeaders aHeaders;
309 getUserRequestHeaders( xEnv,
310 getRequestURI(),
311 ucb::WebDAVHTTPMethod_HEAD,
312 aHeaders );
314 m_xSession->HEAD( getRequestURI(),
315 rHeaderNames,
316 rResource,
317 DAVRequestEnvironment(
318 getRequestURI(),
319 new DAVAuthListener_Impl( xEnv, m_aURL ),
320 aHeaders, xEnv ) );
322 catch ( DAVException & e )
324 errorCount++;
325 bRetry = handleException( e, errorCount );
326 if ( !bRetry )
327 throw;
330 while ( bRetry );
334 uno::Reference< io::XInputStream > DAVResourceAccess::GET(
335 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
336 throw( DAVException )
338 initialize();
340 uno::Reference< io::XInputStream > xStream;
341 int errorCount = 0;
342 bool bRetry;
345 bRetry = false;
348 DAVRequestHeaders aHeaders;
349 getUserRequestHeaders( xEnv,
350 getRequestURI(),
351 ucb::WebDAVHTTPMethod_GET,
352 aHeaders );
354 xStream = m_xSession->GET( getRequestURI(),
355 DAVRequestEnvironment(
356 getRequestURI(),
357 new DAVAuthListener_Impl(
358 xEnv, m_aURL ),
359 aHeaders, xEnv ) );
361 catch ( DAVException & e )
363 errorCount++;
364 bRetry = handleException( e, errorCount );
365 if ( !bRetry )
366 throw;
369 while ( bRetry );
371 return xStream;
375 void DAVResourceAccess::GET(
376 uno::Reference< io::XOutputStream > & rStream,
377 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
378 throw( DAVException )
380 initialize();
382 int errorCount = 0;
383 bool bRetry;
386 bRetry = false;
389 DAVRequestHeaders aHeaders;
390 getUserRequestHeaders( xEnv,
391 getRequestURI(),
392 ucb::WebDAVHTTPMethod_GET,
393 aHeaders );
395 m_xSession->GET( getRequestURI(),
396 rStream,
397 DAVRequestEnvironment(
398 getRequestURI(),
399 new DAVAuthListener_Impl( xEnv, m_aURL ),
400 aHeaders, xEnv ) );
402 catch ( DAVException & e )
404 errorCount++;
405 bRetry = handleException( e, errorCount );
406 if ( !bRetry )
407 throw;
410 while ( bRetry );
414 uno::Reference< io::XInputStream > DAVResourceAccess::GET(
415 const std::vector< OUString > & rHeaderNames,
416 DAVResource & rResource,
417 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
418 throw( DAVException )
420 initialize();
422 uno::Reference< io::XInputStream > xStream;
423 int errorCount = 0;
424 bool bRetry;
427 bRetry = false;
430 DAVRequestHeaders aHeaders;
431 getUserRequestHeaders( xEnv,
432 getRequestURI(),
433 ucb::WebDAVHTTPMethod_GET,
434 aHeaders );
436 xStream = m_xSession->GET( getRequestURI(),
437 rHeaderNames,
438 rResource,
439 DAVRequestEnvironment(
440 getRequestURI(),
441 new DAVAuthListener_Impl(
442 xEnv, m_aURL ),
443 aHeaders, xEnv ) );
445 catch ( DAVException & e )
447 errorCount++;
448 bRetry = handleException( e, errorCount );
449 if ( !bRetry )
450 throw;
453 while ( bRetry );
455 return xStream;
459 uno::Reference< io::XInputStream > DAVResourceAccess::GET(
460 DAVRequestHeaders &rRequestHeaders,
461 const std::vector< OUString > & rHeaderNames,
462 DAVResource & rResource,
463 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
464 throw( DAVException )
466 initialize();
468 uno::Reference< io::XInputStream > xStream;
469 int errorCount = 0;
470 bool bRetry;
473 bRetry = false;
476 getUserRequestHeaders( xEnv,
477 getRequestURI(),
478 ucb::WebDAVHTTPMethod_GET,
479 rRequestHeaders );
481 xStream = m_xSession->GET( getRequestURI(),
482 rHeaderNames,
483 rResource,
484 DAVRequestEnvironment(
485 getRequestURI(),
486 new DAVAuthListener_Impl(
487 xEnv, m_aURL ),
488 rRequestHeaders, xEnv ) );
490 catch ( DAVException & e )
492 errorCount++;
493 bRetry = handleException( e, errorCount );
494 if ( !bRetry )
495 throw;
498 while ( bRetry );
500 return xStream;
504 void DAVResourceAccess::GET(
505 uno::Reference< io::XOutputStream > & rStream,
506 const std::vector< OUString > & rHeaderNames,
507 DAVResource & rResource,
508 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
509 throw( DAVException )
511 initialize();
513 bool bRetry;
514 int errorCount = 0;
517 bRetry = false;
520 DAVRequestHeaders aHeaders;
521 getUserRequestHeaders( xEnv,
522 getRequestURI(),
523 ucb::WebDAVHTTPMethod_GET,
524 aHeaders );
526 m_xSession->GET( getRequestURI(),
527 rStream,
528 rHeaderNames,
529 rResource,
530 DAVRequestEnvironment(
531 getRequestURI(),
532 new DAVAuthListener_Impl( xEnv, m_aURL ),
533 aHeaders, xEnv ) );
535 catch ( DAVException & e )
537 errorCount++;
538 bRetry = handleException( e, errorCount );
539 if ( !bRetry )
540 throw;
543 while ( bRetry );
547 void DAVResourceAccess::abort()
548 throw( DAVException )
550 // 17.11.09 (tkr): abort currently disabled caused by issue i106766
551 // initialize();
552 // m_xSession->abort();
553 SAL_INFO("ucb.ucp.webdav", "Not implemented. -> #i106766#" );
557 namespace {
559 void resetInputStream( const uno::Reference< io::XInputStream > & rStream )
560 throw( DAVException )
564 uno::Reference< io::XSeekable > xSeekable(
565 rStream, uno::UNO_QUERY );
566 if ( xSeekable.is() )
568 xSeekable->seek( 0 );
569 return;
572 catch ( lang::IllegalArgumentException const & )
575 catch ( io::IOException const & )
579 throw DAVException( DAVException::DAV_INVALID_ARG );
582 } // namespace
585 void DAVResourceAccess::PUT(
586 const uno::Reference< io::XInputStream > & rStream,
587 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
588 throw( DAVException )
590 initialize();
592 // Make stream seekable, if it not. Needed, if request must be retried.
593 uno::Reference< io::XInputStream > xSeekableStream
594 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
595 rStream, m_xContext );
597 int errorCount = 0;
598 bool bRetry = false;
601 if ( bRetry )
602 resetInputStream( xSeekableStream );
604 bRetry = false;
607 DAVRequestHeaders aHeaders;
608 getUserRequestHeaders( xEnv,
609 getRequestURI(),
610 ucb::WebDAVHTTPMethod_PUT,
611 aHeaders );
613 m_xSession->PUT( getRequestURI(),
614 xSeekableStream,
615 DAVRequestEnvironment(
616 getRequestURI(),
617 new DAVAuthListener_Impl( xEnv, m_aURL ),
618 aHeaders, xEnv ) );
620 catch ( DAVException & e )
622 errorCount++;
623 bRetry = handleException( e, errorCount );
624 if ( !bRetry )
625 throw;
628 while ( bRetry );
632 uno::Reference< io::XInputStream > DAVResourceAccess::POST(
633 const OUString & rContentType,
634 const OUString & rReferer,
635 const uno::Reference< io::XInputStream > & rInputStream,
636 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
637 throw ( DAVException )
639 initialize();
641 // Make stream seekable, if it not. Needed, if request must be retried.
642 uno::Reference< io::XInputStream > xSeekableStream
643 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
644 rInputStream, m_xContext );
646 uno::Reference< io::XInputStream > xStream;
647 int errorCount = 0;
648 bool bRetry = false;
651 if ( bRetry )
653 resetInputStream( xSeekableStream );
654 bRetry = false;
659 DAVRequestHeaders aHeaders;
660 getUserRequestHeaders( xEnv,
661 getRequestURI(),
662 ucb::WebDAVHTTPMethod_POST,
663 aHeaders );
665 xStream = m_xSession->POST( getRequestURI(),
666 rContentType,
667 rReferer,
668 xSeekableStream,
669 DAVRequestEnvironment(
670 getRequestURI(),
671 new DAVAuthListener_Impl(
672 xEnv, m_aURL ),
673 aHeaders, xEnv ) );
675 catch ( DAVException & e )
677 errorCount++;
678 bRetry = handleException( e, errorCount );
679 if ( !bRetry )
680 throw;
682 if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
684 // #i74980# - Upon POST redirect, do a GET.
685 return GET( xEnv );
689 while ( bRetry );
691 return xStream;
695 void DAVResourceAccess::POST(
696 const OUString & rContentType,
697 const OUString & rReferer,
698 const uno::Reference< io::XInputStream > & rInputStream,
699 uno::Reference< io::XOutputStream > & rOutputStream,
700 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
701 throw ( DAVException )
703 initialize();
705 // Make stream seekable, if it not. Needed, if request must be retried.
706 uno::Reference< io::XInputStream > xSeekableStream
707 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
708 rInputStream, m_xContext );
710 int errorCount = 0;
711 bool bRetry = false;
714 if ( bRetry )
716 resetInputStream( xSeekableStream );
717 bRetry = false;
722 DAVRequestHeaders aHeaders;
723 getUserRequestHeaders( xEnv,
724 getRequestURI(),
725 ucb::WebDAVHTTPMethod_POST,
726 aHeaders );
728 m_xSession->POST( getRequestURI(),
729 rContentType,
730 rReferer,
731 xSeekableStream,
732 rOutputStream,
733 DAVRequestEnvironment(
734 getRequestURI(),
735 new DAVAuthListener_Impl( xEnv, m_aURL ),
736 aHeaders, xEnv ) );
738 catch ( DAVException & e )
740 errorCount++;
741 bRetry = handleException( e, errorCount );
742 if ( !bRetry )
743 throw;
745 if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
747 // #i74980# - Upon POST redirect, do a GET.
748 GET( rOutputStream, xEnv );
749 return;
753 while ( bRetry );
757 void DAVResourceAccess::MKCOL(
758 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
759 throw( DAVException )
761 initialize();
763 int errorCount = 0;
764 bool bRetry;
767 bRetry = false;
770 DAVRequestHeaders aHeaders;
771 getUserRequestHeaders( xEnv,
772 getRequestURI(),
773 ucb::WebDAVHTTPMethod_MKCOL,
774 aHeaders );
776 m_xSession->MKCOL( getRequestURI(),
777 DAVRequestEnvironment(
778 getRequestURI(),
779 new DAVAuthListener_Impl( xEnv, m_aURL ),
780 aHeaders, xEnv ) );
782 catch ( DAVException & e )
784 errorCount++;
785 bRetry = handleException( e, errorCount );
786 if ( !bRetry )
787 throw;
790 while ( bRetry );
794 void DAVResourceAccess::COPY(
795 const OUString & rSourcePath,
796 const OUString & rDestinationURI,
797 bool bOverwrite,
798 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
799 throw( DAVException )
801 initialize();
803 int errorCount = 0;
804 bool bRetry;
807 bRetry = false;
810 DAVRequestHeaders aHeaders;
811 getUserRequestHeaders( xEnv,
812 getRequestURI(),
813 ucb::WebDAVHTTPMethod_COPY,
814 aHeaders );
816 m_xSession->COPY( rSourcePath,
817 rDestinationURI,
818 DAVRequestEnvironment(
819 getRequestURI(),
820 new DAVAuthListener_Impl( xEnv, m_aURL ),
821 aHeaders, xEnv ),
822 bOverwrite );
824 catch ( DAVException & e )
826 errorCount++;
827 bRetry = handleException( e, errorCount );
828 if ( !bRetry )
829 throw;
832 while ( bRetry );
836 void DAVResourceAccess::MOVE(
837 const OUString & rSourcePath,
838 const OUString & rDestinationURI,
839 bool bOverwrite,
840 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
841 throw( DAVException )
843 initialize();
845 int errorCount = 0;
846 bool bRetry;
849 bRetry = false;
852 DAVRequestHeaders aHeaders;
853 getUserRequestHeaders( xEnv,
854 getRequestURI(),
855 ucb::WebDAVHTTPMethod_MOVE,
856 aHeaders );
858 m_xSession->MOVE( rSourcePath,
859 rDestinationURI,
860 DAVRequestEnvironment(
861 getRequestURI(),
862 new DAVAuthListener_Impl( xEnv, m_aURL ),
863 aHeaders, xEnv ),
864 bOverwrite );
866 catch ( DAVException & e )
868 errorCount++;
869 bRetry = handleException( e, errorCount );
870 if ( !bRetry )
871 throw;
874 while ( bRetry );
878 void DAVResourceAccess::DESTROY(
879 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
880 throw( DAVException )
882 initialize();
884 int errorCount = 0;
885 bool bRetry;
888 bRetry = false;
891 DAVRequestHeaders aHeaders;
892 getUserRequestHeaders( xEnv,
893 getRequestURI(),
894 ucb::WebDAVHTTPMethod_DELETE,
895 aHeaders );
897 m_xSession->DESTROY( getRequestURI(),
898 DAVRequestEnvironment(
899 getRequestURI(),
900 new DAVAuthListener_Impl( xEnv, m_aURL ),
901 aHeaders, xEnv ) );
903 catch ( DAVException & e )
905 errorCount++;
906 bRetry = handleException( e, errorCount );
907 if ( !bRetry )
908 throw;
911 while ( bRetry );
915 // set new lock.
916 void DAVResourceAccess::LOCK(
917 ucb::Lock & inLock,
918 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
919 throw ( DAVException )
921 initialize();
923 int errorCount = 0;
924 bool bRetry;
927 bRetry = false;
930 DAVRequestHeaders aHeaders;
931 getUserRequestHeaders( xEnv,
932 getRequestURI(),
933 ucb::WebDAVHTTPMethod_LOCK,
934 aHeaders );
936 m_xSession->LOCK( getRequestURI(),
937 inLock,
938 DAVRequestEnvironment(
939 getRequestURI(),
940 new DAVAuthListener_Impl( xEnv, m_aURL ),
941 aHeaders, xEnv ) );
943 catch ( DAVException & e )
945 errorCount++;
946 bRetry = handleException( e, errorCount );
947 if ( !bRetry )
948 throw;
951 while ( bRetry );
954 #if 0 // currently not used, but please don't remove code
956 // refresh existing lock.
957 sal_Int64 DAVResourceAccess::LOCK(
958 sal_Int64 nTimeout,
959 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
960 throw ( DAVException )
962 initialize();
964 sal_Int64 nNewTimeout = 0;
965 int errorCount = 0;
966 bool bRetry;
969 bRetry = false;
972 DAVRequestHeaders aHeaders;
973 getUserRequestHeaders( xEnv,
974 getRequestURI(),
975 ucb::WebDAVHTTPMethod_LOCK,
976 aHeaders );
978 nNewTimeout = m_xSession->LOCK( getRequestURI(),
979 nTimeout,
980 DAVRequestEnvironment(
981 getRequestURI(),
982 new DAVAuthListener_Impl(
983 xEnv, m_aURL ),
984 aHeaders, xEnv ) );
986 catch ( DAVException & e )
988 errorCount++;
989 bRetry = handleException( e, errorCount );
990 if ( !bRetry )
991 throw;
994 while ( bRetry );
996 return nNewTimeout;
998 #endif
1001 void DAVResourceAccess::UNLOCK(
1002 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
1003 throw ( DAVException )
1005 initialize();
1007 int errorCount = 0;
1008 bool bRetry;
1011 bRetry = false;
1014 DAVRequestHeaders aHeaders;
1015 getUserRequestHeaders( xEnv,
1016 getRequestURI(),
1017 ucb::WebDAVHTTPMethod_UNLOCK,
1018 aHeaders );
1020 m_xSession->UNLOCK( getRequestURI(),
1021 DAVRequestEnvironment(
1022 getRequestURI(),
1023 new DAVAuthListener_Impl( xEnv, m_aURL ),
1024 aHeaders, xEnv ) );
1026 catch ( DAVException & e )
1028 errorCount++;
1029 bRetry = handleException( e, errorCount );
1030 if ( !bRetry )
1031 throw;
1034 while ( bRetry );
1038 void DAVResourceAccess::setURL( const OUString & rNewURL )
1039 throw( DAVException )
1041 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1042 m_aURL = rNewURL;
1043 m_aPath.clear(); // Next initialize() will create new session.
1047 // init dav session and path
1048 void DAVResourceAccess::initialize()
1049 throw ( DAVException )
1051 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1052 if ( m_aPath.isEmpty() )
1054 SerfUri aURI( m_aURL );
1055 OUString aPath( aURI.GetPath() );
1057 /* #134089# - Check URI */
1058 if ( aPath.isEmpty() )
1059 throw DAVException( DAVException::DAV_INVALID_ARG );
1061 /* #134089# - Check URI */
1062 if ( aURI.GetHost().isEmpty() )
1063 throw DAVException( DAVException::DAV_INVALID_ARG );
1065 if ( !m_xSession.is() || !m_xSession->CanUse( m_aURL ) )
1067 m_xSession.clear();
1069 // create new webdav session
1070 m_xSession
1071 = m_xSessionFactory->createDAVSession( m_aURL, m_xContext );
1073 if ( !m_xSession.is() )
1074 return;
1077 // Own URI is needed for redirect cycle detection.
1078 m_aRedirectURIs.push_back( aURI );
1080 // Success.
1081 m_aPath = aPath;
1083 // Not only the path has to be encoded
1084 m_aURL = aURI.GetURI();
1089 const OUString & DAVResourceAccess::getRequestURI() const
1091 SAL_WARN_IF( !m_xSession.is(), "ucb.ucp.webdav",
1092 "DAVResourceAccess::getRequestURI - Not initialized!" );
1094 // In case a proxy is used we have to use the absolute URI for a request.
1095 if ( m_xSession->UsesProxy() )
1096 return m_aURL;
1098 return m_aPath;
1102 // static
1103 void DAVResourceAccess::getUserRequestHeaders(
1104 const uno::Reference< ucb::XCommandEnvironment > & xEnv,
1105 const rtl::OUString & rURI,
1106 ucb::WebDAVHTTPMethod eMethod,
1107 DAVRequestHeaders & rRequestHeaders )
1109 if ( !xEnv.is() )
1110 return;
1112 uno::Reference< ucb::XWebDAVCommandEnvironment > xDAVEnv(
1113 xEnv, uno::UNO_QUERY );
1115 if ( !xDAVEnv.is() )
1116 return;
1118 uno::Sequence< beans::StringPair > aRequestHeaders
1119 = xDAVEnv->getUserRequestHeaders( rURI, eMethod );
1121 for ( sal_Int32 n = 0; n < aRequestHeaders.getLength(); ++n )
1123 rRequestHeaders.push_back(
1124 DAVRequestHeader( aRequestHeaders[ n ].First,
1125 aRequestHeaders[ n ].Second ) );
1130 bool DAVResourceAccess::detectRedirectCycle(
1131 const OUString& rRedirectURL )
1132 throw ( DAVException )
1134 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1136 SerfUri aUri( rRedirectURL );
1138 std::vector< SerfUri >::const_iterator it = m_aRedirectURIs.begin();
1139 std::vector< SerfUri >::const_iterator end = m_aRedirectURIs.end();
1141 while ( it != end )
1143 if ( aUri == (*it) )
1144 return true;
1146 ++it;
1149 return false;
1153 void DAVResourceAccess::resetUri()
1155 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1156 if ( ! m_aRedirectURIs.empty() )
1158 std::vector< SerfUri >::const_iterator it = m_aRedirectURIs.begin();
1160 SerfUri aUri( (*it) );
1161 m_aRedirectURIs.clear();
1162 setURL ( aUri.GetURI() );
1163 initialize();
1168 bool DAVResourceAccess::handleException( DAVException & e, int errorCount )
1169 throw ( DAVException )
1171 switch ( e.getError() )
1173 case DAVException::DAV_HTTP_REDIRECT:
1174 if ( !detectRedirectCycle( e.getData() ) )
1176 // set new URL and path.
1177 setURL( e.getData() );
1178 initialize();
1179 return true;
1181 return false;
1182 // --> tkr #67048# copy & paste images doesn't display.
1183 // if we have a bad connection try again. Up to three times.
1184 case DAVException::DAV_HTTP_ERROR:
1185 // retry up to three times, if not a client-side error.
1186 if ( ( e.getStatus() < 400 || e.getStatus() >= 500 ||
1187 e.getStatus() == 413 ) &&
1188 errorCount < 3 )
1190 return true;
1192 return false;
1193 // <--
1194 // --> tkr: if connection has said retry then retry!
1195 case DAVException::DAV_HTTP_RETRY:
1196 return true;
1197 // <--
1198 default:
1199 return false; // Abort
1203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */