merge the formfield patch from ooo-build
[ooovba.git] / ucb / source / ucp / webdav / DAVResourceAccess.cxx
blob6d0424341b8b85982db193c142dc228f1998aff3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DAVResourceAccess.cxx,v $
10 * $Revision: 1.29.16.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
34 #include "osl/diagnose.h"
36 #include "com/sun/star/task/XInteractionAbort.hpp"
37 #include "com/sun/star/ucb/XWebDAVCommandEnvironment.hpp"
39 #include "ucbhelper/simpleauthenticationrequest.hxx"
40 #include "comphelper/seekableinput.hxx"
42 #include "DAVAuthListenerImpl.hxx"
43 #include "DAVResourceAccess.hxx"
45 #include <comphelper/processfactory.hxx>
46 #include <ucbhelper/commandenvironment.hxx>
48 using namespace webdav_ucp;
49 using namespace com::sun::star;
51 //=========================================================================
52 //=========================================================================
54 // DAVAuthListener_Impl Implementation.
56 //=========================================================================
57 //=========================================================================
59 //=========================================================================
60 // virtual
61 int DAVAuthListener_Impl::authenticate(
62 const ::rtl::OUString & inRealm,
63 const ::rtl::OUString & inHostName,
64 ::rtl::OUString & inoutUserName,
65 ::rtl::OUString & outPassWord,
66 sal_Bool bAllowPersistentStoring,
67 sal_Bool bCanUseSystemCredentials )
69 uno::Reference< task::XInteractionHandler > xIH;
71 if ( m_xEnv.is() )
73 xIH = m_xEnv->getInteractionHandler();
75 else
77 xIH = DAVResourceAccess::createCommandEnvironment()->getInteractionHandler();
82 // #102871# - Supply username and password from previous try.
83 // Password container service depends on this!
84 if ( inoutUserName.getLength() == 0 )
85 inoutUserName = m_aPrevUsername;
87 if ( outPassWord.getLength() == 0 )
88 outPassWord = m_aPrevPassword;
90 rtl::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
91 = new ucbhelper::SimpleAuthenticationRequest( m_aURL,
92 inHostName,
93 inRealm,
94 inoutUserName,
95 outPassWord,
96 ::rtl::OUString(),
97 bAllowPersistentStoring,
98 bCanUseSystemCredentials );
99 xIH->handle( xRequest.get() );
101 rtl::Reference< ucbhelper::InteractionContinuation > xSelection
102 = xRequest->getSelection();
104 if ( xSelection.is() )
106 // Handler handled the request.
107 uno::Reference< task::XInteractionAbort > xAbort(
108 xSelection.get(), uno::UNO_QUERY );
109 if ( !xAbort.is() )
111 const rtl::Reference<
112 ucbhelper::InteractionSupplyAuthentication > & xSupp
113 = xRequest->getAuthenticationSupplier();
115 sal_Bool bUseSystemCredentials = sal_False;
117 if ( bCanUseSystemCredentials )
118 bUseSystemCredentials = xSupp->getUseSystemCredentials();
120 if ( bUseSystemCredentials )
122 // This is the (strange) way to tell neon to use
123 // system credentials.
124 inoutUserName = rtl::OUString();
125 outPassWord = rtl::OUString();
127 else
129 inoutUserName = xSupp->getUserName();
130 outPassWord = xSupp->getPassword();
133 // #102871# - Remember username and password.
134 m_aPrevUsername = inoutUserName;
135 m_aPrevPassword = outPassWord;
137 // go on.
138 return 0;
143 // Abort.
144 return -1;
147 //=========================================================================
148 //=========================================================================
150 // DAVResourceAccess Implementation.
152 //=========================================================================
153 //=========================================================================
155 //=========================================================================
156 DAVResourceAccess::DAVResourceAccess(
157 const uno::Reference< lang::XMultiServiceFactory > & rSMgr,
158 rtl::Reference< DAVSessionFactory > const & rSessionFactory,
159 const rtl::OUString & rURL )
160 : m_aURL( rURL ),
161 m_xSessionFactory( rSessionFactory ),
162 m_xSMgr( rSMgr )
166 //=========================================================================
167 DAVResourceAccess::DAVResourceAccess( const DAVResourceAccess & rOther )
168 : m_aURL( rOther.m_aURL ),
169 m_aPath( rOther.m_aPath ),
170 m_xSession( rOther.m_xSession ),
171 m_xSessionFactory( rOther.m_xSessionFactory ),
172 m_xSMgr( rOther.m_xSMgr ),
173 m_aRedirectURIs( rOther.m_aRedirectURIs )
177 //=========================================================================
178 DAVResourceAccess & DAVResourceAccess::operator=(
179 const DAVResourceAccess & rOther )
181 m_aURL = rOther.m_aURL;
182 m_aPath = rOther.m_aPath;
183 m_xSession = rOther.m_xSession;
184 m_xSessionFactory = rOther.m_xSessionFactory;
185 m_xSMgr = rOther.m_xSMgr;
186 m_aRedirectURIs = rOther.m_aRedirectURIs;
188 return *this;
191 //=========================================================================
192 void DAVResourceAccess::OPTIONS(
193 DAVCapabilities & rCapabilities,
194 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
195 throw( DAVException )
197 initialize();
199 bool bRetry;
200 int errorCount = 0;
203 bRetry = false;
206 DAVRequestHeaders aHeaders;
207 getUserRequestHeaders( xEnv,
208 getRequestURI(),
209 rtl::OUString::createFromAscii(
210 "OPTIONS" ),
211 aHeaders );
213 m_xSession->OPTIONS( getRequestURI(),
214 rCapabilities,
215 DAVRequestEnvironment(
216 getRequestURI(),
217 new DAVAuthListener_Impl( xEnv, m_aURL ),
218 aHeaders, xEnv) );
220 catch ( DAVException & e )
222 errorCount++;
223 bRetry = handleException( e, errorCount );
224 if ( !bRetry )
225 throw;
228 while ( bRetry );
231 //=========================================================================
232 void DAVResourceAccess::PROPFIND(
233 const Depth nDepth,
234 const std::vector< rtl::OUString > & rPropertyNames,
235 std::vector< DAVResource > & rResources,
236 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
237 throw( DAVException )
239 initialize();
241 int errorCount = 0;
242 bool bRetry;
245 bRetry = false;
248 DAVRequestHeaders aHeaders;
250 getUserRequestHeaders( xEnv,
251 getRequestURI(),
252 rtl::OUString::createFromAscii(
253 "PROPFIND" ),
254 aHeaders );
256 m_xSession->PROPFIND( getRequestURI(),
257 nDepth,
258 rPropertyNames,
259 rResources,
260 DAVRequestEnvironment(
261 getRequestURI(),
262 new DAVAuthListener_Impl( xEnv, m_aURL ),
263 aHeaders, xEnv ) );
265 catch ( DAVException & e )
267 errorCount++;
268 bRetry = handleException( e, errorCount );
269 if ( !bRetry )
270 throw;
273 while ( bRetry );
276 //=========================================================================
277 void DAVResourceAccess::PROPFIND(
278 const Depth nDepth,
279 std::vector< DAVResourceInfo > & rResInfo,
280 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
281 throw( DAVException )
283 initialize();
285 int errorCount = 0;
286 bool bRetry;
289 bRetry = false;
292 DAVRequestHeaders aHeaders;
293 getUserRequestHeaders( xEnv,
294 getRequestURI(),
295 rtl::OUString::createFromAscii(
296 "PROPFIND" ),
297 aHeaders );
299 m_xSession->PROPFIND( getRequestURI(),
300 nDepth,
301 rResInfo,
302 DAVRequestEnvironment(
303 getRequestURI(),
304 new DAVAuthListener_Impl( xEnv, m_aURL ),
305 aHeaders, xEnv ) ) ;
307 catch ( DAVException & e )
309 errorCount++;
310 bRetry = handleException( e, errorCount );
311 if ( !bRetry )
312 throw;
315 while ( bRetry );
318 //=========================================================================
319 void DAVResourceAccess::PROPPATCH(
320 const std::vector< ProppatchValue >& rValues,
321 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
322 throw( DAVException )
324 initialize();
326 int errorCount = 0;
327 bool bRetry;
330 bRetry = false;
333 DAVRequestHeaders aHeaders;
334 getUserRequestHeaders( xEnv,
335 getRequestURI(),
336 rtl::OUString::createFromAscii(
337 "PROPPATCH" ),
338 aHeaders );
340 m_xSession->PROPPATCH( getRequestURI(),
341 rValues,
342 DAVRequestEnvironment(
343 getRequestURI(),
344 new DAVAuthListener_Impl( xEnv, m_aURL ),
345 aHeaders, xEnv ) );
347 catch ( DAVException & e )
349 errorCount++;
350 bRetry = handleException( e, errorCount );
351 if ( !bRetry )
352 throw;
355 while ( bRetry );
358 //=========================================================================
359 void DAVResourceAccess::HEAD(
360 const std::vector< rtl::OUString > & rHeaderNames,
361 DAVResource & rResource,
362 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
363 throw( DAVException )
365 initialize();
366 int errorCount = 0;
367 bool bRetry;
370 bRetry = false;
373 DAVRequestHeaders aHeaders;
374 getUserRequestHeaders( xEnv,
375 getRequestURI(),
376 rtl::OUString::createFromAscii( "HEAD" ),
377 aHeaders );
379 m_xSession->HEAD( getRequestURI(),
380 rHeaderNames,
381 rResource,
382 DAVRequestEnvironment(
383 getRequestURI(),
384 new DAVAuthListener_Impl( xEnv, m_aURL ),
385 aHeaders, xEnv ) );
387 catch ( DAVException & e )
389 errorCount++;
390 bRetry = handleException( e, errorCount );
391 if ( !bRetry )
392 throw;
395 while ( bRetry );
398 //=========================================================================
399 uno::Reference< io::XInputStream > DAVResourceAccess::GET(
400 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
401 throw( DAVException )
403 initialize();
405 uno::Reference< io::XInputStream > xStream;
406 int errorCount = 0;
407 bool bRetry;
410 bRetry = false;
413 DAVRequestHeaders aHeaders;
414 getUserRequestHeaders( xEnv,
415 getRequestURI(),
416 rtl::OUString::createFromAscii( "GET" ),
417 aHeaders );
419 xStream = m_xSession->GET( getRequestURI(),
420 DAVRequestEnvironment(
421 getRequestURI(),
422 new DAVAuthListener_Impl( xEnv, m_aURL ),
423 aHeaders, xEnv ) );
425 catch ( DAVException & e )
427 errorCount++;
428 bRetry = handleException( e, errorCount );
429 if ( !bRetry )
430 throw;
433 while ( bRetry );
435 return xStream;
438 //=========================================================================
439 void DAVResourceAccess::GET(
440 uno::Reference< io::XOutputStream > & rStream,
441 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
442 throw( DAVException )
444 initialize();
446 int errorCount = 0;
447 bool bRetry;
450 bRetry = false;
453 DAVRequestHeaders aHeaders;
454 getUserRequestHeaders( xEnv,
455 getRequestURI(),
456 rtl::OUString::createFromAscii( "GET" ),
457 aHeaders );
459 m_xSession->GET( getRequestURI(),
460 rStream,
461 DAVRequestEnvironment(
462 getRequestURI(),
463 new DAVAuthListener_Impl( xEnv, m_aURL ),
464 aHeaders, xEnv ) );
466 catch ( DAVException & e )
468 errorCount++;
469 bRetry = handleException( e, errorCount );
470 if ( !bRetry )
471 throw;
474 while ( bRetry );
477 //=========================================================================
478 uno::Reference< io::XStream > DAVResourceAccess::GET(
479 const std::vector< rtl::OUString > & rHeaderNames,
480 DAVResource & rResource,
481 const uno::Reference< ucb::XCommandEnvironment > & xEnv,
482 sal_Bool bAllowEmpty )
483 throw( DAVException )
485 initialize();
487 uno::Reference< io::XStream > xStream;
488 int errorCount = 0;
489 bool bRetry;
492 bRetry = false;
495 DAVRequestHeaders aHeaders;
496 getUserRequestHeaders( xEnv,
497 getRequestURI(),
498 rtl::OUString::createFromAscii( "GET" ),
499 aHeaders );
501 xStream = m_xSession->GET( getRequestURI(),
502 rHeaderNames,
503 rResource,
504 DAVRequestEnvironment(
505 getRequestURI(),
506 new DAVAuthListener_Impl( xEnv, m_aURL ),
507 aHeaders, xEnv ),
508 bAllowEmpty );
510 catch ( DAVException & e )
512 errorCount++;
513 bRetry = handleException( e, errorCount );
514 if ( !bRetry )
515 throw;
518 while ( bRetry );
520 return xStream;
523 //=========================================================================
524 void DAVResourceAccess::GET(
525 uno::Reference< io::XOutputStream > & rStream,
526 const std::vector< rtl::OUString > & rHeaderNames,
527 DAVResource & rResource,
528 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
529 throw( DAVException )
531 initialize();
533 bool bRetry;
534 int errorCount = 0;
537 bRetry = false;
540 DAVRequestHeaders aHeaders;
541 getUserRequestHeaders( xEnv,
542 getRequestURI(),
543 rtl::OUString::createFromAscii( "GET" ),
544 aHeaders );
546 m_xSession->GET( getRequestURI(),
547 rStream,
548 rHeaderNames,
549 rResource,
550 DAVRequestEnvironment(
551 getRequestURI(),
552 new DAVAuthListener_Impl( xEnv, m_aURL ),
553 aHeaders, xEnv ) );
555 catch ( DAVException & e )
557 errorCount++;
558 bRetry = handleException( e, errorCount );
559 if ( !bRetry )
560 throw;
563 while ( bRetry );
566 //=========================================================================
567 void DAVResourceAccess::ABORT()
568 throw( DAVException )
570 initialize();
571 m_xSession->ABORT();
573 //=========================================================================
574 namespace {
576 void resetInputStream( const uno::Reference< io::XInputStream > & rStream )
577 throw( DAVException )
581 uno::Reference< io::XSeekable > xSeekable(
582 rStream, uno::UNO_QUERY );
583 if ( xSeekable.is() )
585 xSeekable->seek( 0 );
586 return;
589 catch ( lang::IllegalArgumentException const & )
592 catch ( io::IOException const & )
596 throw DAVException( DAVException::DAV_INVALID_ARG );
599 } // namespace
601 //=========================================================================
602 void DAVResourceAccess::PUT(
603 const uno::Reference< io::XInputStream > & rStream,
604 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
605 throw( DAVException )
607 initialize();
609 // Make stream seekable, if it not. Needed, if request must be retried.
610 uno::Reference< io::XInputStream > xSeekableStream
611 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
612 rStream, m_xSMgr );
614 int errorCount = 0;
615 bool bRetry = false;
618 if ( bRetry )
619 resetInputStream( xSeekableStream );
621 bRetry = false;
624 DAVRequestHeaders aHeaders;
625 getUserRequestHeaders( xEnv,
626 getRequestURI(),
627 rtl::OUString::createFromAscii( "PUT" ),
628 aHeaders );
630 m_xSession->PUT( getRequestURI(),
631 xSeekableStream,
632 DAVRequestEnvironment(
633 getRequestURI(),
634 new DAVAuthListener_Impl( xEnv, m_aURL ),
635 aHeaders, xEnv ) );
637 catch ( DAVException & e )
639 errorCount++;
640 bRetry = handleException( e, errorCount );
641 if ( !bRetry )
642 throw;
645 while ( bRetry );
648 //=========================================================================
649 void DAVResourceAccess::PUT(
650 const char * buffer, size_t size,
651 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
652 throw( DAVException )
654 initialize();
656 bool bRetry = false;
657 int errorCount = 0;
660 bRetry = false;
663 DAVRequestHeaders aHeaders;
664 getUserRequestHeaders( xEnv,
665 getRequestURI(),
666 rtl::OUString::createFromAscii( "PUT" ),
667 aHeaders );
669 m_xSession->PUT( getRequestURI(),
670 buffer, size,
671 DAVRequestEnvironment(
672 getRequestURI(),
673 new DAVAuthListener_Impl( xEnv, m_aURL ),
674 aHeaders, xEnv ) );
676 catch ( DAVException & e )
678 errorCount++;
679 bRetry = handleException( e, errorCount );
680 if ( !bRetry )
681 throw;
684 while ( bRetry );
687 //=========================================================================
688 uno::Reference< io::XInputStream > DAVResourceAccess::POST(
689 const rtl::OUString & rContentType,
690 const rtl::OUString & rReferer,
691 const uno::Reference< io::XInputStream > & rInputStream,
692 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
693 throw ( DAVException )
695 initialize();
697 // Make stream seekable, if it not. Needed, if request must be retried.
698 uno::Reference< io::XInputStream > xSeekableStream
699 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
700 rInputStream, m_xSMgr );
702 uno::Reference< io::XInputStream > xStream;
703 int errorCount = 0;
704 bool bRetry = false;
707 if ( bRetry )
709 resetInputStream( xSeekableStream );
710 bRetry = false;
715 DAVRequestHeaders aHeaders;
716 getUserRequestHeaders( xEnv,
717 getRequestURI(),
718 rtl::OUString::createFromAscii( "POST" ),
719 aHeaders );
721 xStream = m_xSession->POST( getRequestURI(),
722 rContentType,
723 rReferer,
724 xSeekableStream,
725 DAVRequestEnvironment(
726 getRequestURI(),
727 new DAVAuthListener_Impl( xEnv, m_aURL ),
728 aHeaders, xEnv ) );
730 catch ( DAVException & e )
732 errorCount++;
733 bRetry = handleException( e, errorCount );
734 if ( !bRetry )
735 throw;
737 if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
739 // #i74980# - Upon POST redirect, do a GET.
740 return GET( xEnv );
744 while ( bRetry );
746 return xStream;
749 //=========================================================================
751 void DAVResourceAccess::POST(
752 const rtl::OUString & rContentType,
753 const rtl::OUString & rReferer,
754 const uno::Reference< io::XInputStream > & rInputStream,
755 uno::Reference< io::XOutputStream > & rOutputStream,
756 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
757 throw ( DAVException )
759 initialize();
761 // Make stream seekable, if it not. Needed, if request must be retried.
762 uno::Reference< io::XInputStream > xSeekableStream
763 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
764 rInputStream, m_xSMgr );
766 int errorCount = 0;
767 bool bRetry = false;
770 if ( bRetry )
772 resetInputStream( xSeekableStream );
773 bRetry = false;
778 DAVRequestHeaders aHeaders;
779 getUserRequestHeaders( xEnv,
780 getRequestURI(),
781 rtl::OUString::createFromAscii( "POST" ),
782 aHeaders );
784 m_xSession->POST( getRequestURI(),
785 rContentType,
786 rReferer,
787 xSeekableStream,
788 rOutputStream,
789 DAVRequestEnvironment(
790 getRequestURI(),
791 new DAVAuthListener_Impl( xEnv, m_aURL ),
792 aHeaders, xEnv ) );
794 catch ( DAVException & e )
796 errorCount++;
797 bRetry = handleException( e, errorCount );
798 if ( !bRetry )
799 throw;
801 if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
803 // #i74980# - Upon POST redirect, do a GET.
804 GET( rOutputStream, xEnv );
805 return;
809 while ( bRetry );
812 //=========================================================================
813 void DAVResourceAccess::MKCOL(
814 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
815 throw( DAVException )
817 initialize();
818 int errorCount = 0;
819 bool bRetry;
822 bRetry = false;
825 DAVRequestHeaders aHeaders;
826 getUserRequestHeaders( xEnv,
827 getRequestURI(),
828 rtl::OUString::createFromAscii( "MKCOL" ),
829 aHeaders );
831 m_xSession->MKCOL( getRequestURI(),
832 DAVRequestEnvironment(
833 getRequestURI(),
834 new DAVAuthListener_Impl( xEnv, m_aURL ),
835 aHeaders, xEnv ) );
837 catch ( DAVException & e )
839 errorCount++;
840 bRetry = handleException( e, errorCount );
841 if ( !bRetry )
842 throw;
845 while ( bRetry );
848 //=========================================================================
849 void DAVResourceAccess::COPY(
850 const ::rtl::OUString & rSourcePath,
851 const ::rtl::OUString & rDestinationURI,
852 sal_Bool bOverwrite,
853 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
854 throw( DAVException )
856 initialize();
857 int errorCount = 0;
858 bool bRetry;
861 bRetry = false;
864 DAVRequestHeaders aHeaders;
865 getUserRequestHeaders( xEnv,
866 getRequestURI(),
867 rtl::OUString::createFromAscii( "COPY" ),
868 aHeaders );
870 m_xSession->COPY( rSourcePath,
871 rDestinationURI,
872 DAVRequestEnvironment(
873 getRequestURI(),
874 new DAVAuthListener_Impl( xEnv, m_aURL ),
875 aHeaders, xEnv ),
876 bOverwrite );
878 catch ( DAVException & e )
880 errorCount++;
881 bRetry = handleException( e, errorCount );
882 if ( !bRetry )
883 throw;
886 while ( bRetry );
889 //=========================================================================
890 void DAVResourceAccess::MOVE(
891 const ::rtl::OUString & rSourcePath,
892 const ::rtl::OUString & rDestinationURI,
893 sal_Bool bOverwrite,
894 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
895 throw( DAVException )
897 initialize();
898 int errorCount = 0;
899 bool bRetry;
902 bRetry = false;
905 DAVRequestHeaders aHeaders;
906 getUserRequestHeaders( xEnv,
907 getRequestURI(),
908 rtl::OUString::createFromAscii( "MOVE" ),
909 aHeaders );
911 m_xSession->MOVE( rSourcePath,
912 rDestinationURI,
913 DAVRequestEnvironment(
914 getRequestURI(),
915 new DAVAuthListener_Impl( xEnv, m_aURL ),
916 aHeaders, xEnv ),
917 bOverwrite );
919 catch ( DAVException & e )
921 errorCount++;
922 bRetry = handleException( e, errorCount );
923 if ( !bRetry )
924 throw;
927 while ( bRetry );
930 //=========================================================================
931 void DAVResourceAccess::DESTROY(
932 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
933 throw( DAVException )
935 initialize();
937 int errorCount = 0;
938 bool bRetry;
941 bRetry = false;
944 DAVRequestHeaders aHeaders;
945 getUserRequestHeaders( xEnv,
946 getRequestURI(),
947 rtl::OUString::createFromAscii(
948 "DESTROY" ),
949 aHeaders );
951 m_xSession->DESTROY( getRequestURI(),
952 DAVRequestEnvironment(
953 getRequestURI(),
954 new DAVAuthListener_Impl( xEnv, m_aURL ),
955 aHeaders, xEnv ) );
957 catch ( DAVException & e )
959 errorCount++;
960 bRetry = handleException( e, errorCount );
961 if ( !bRetry )
962 throw;
965 while ( bRetry );
968 //=========================================================================
969 void DAVResourceAccess::LOCK (
970 ucb::Lock & rLock,
971 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
972 throw( DAVException )
974 initialize();
976 DAVRequestHeaders aHeaders;
977 getUserRequestHeaders( xEnv,
978 getRequestURI(),
979 rtl::OUString::createFromAscii( "LOCK" ),
980 aHeaders );
982 m_xSession->LOCK( rLock,
983 DAVRequestEnvironment(
984 getRequestURI(),
985 new DAVAuthListener_Impl( xEnv, m_aURL ),
986 aHeaders, xEnv ) );
989 //=========================================================================
990 void DAVResourceAccess::UNLOCK (
991 ucb::Lock & rLock,
992 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
993 throw( DAVException )
995 initialize();
997 DAVRequestHeaders aHeaders;
998 getUserRequestHeaders( xEnv,
999 getRequestURI(),
1000 rtl::OUString::createFromAscii( "UNLOCK" ),
1001 aHeaders );
1003 m_xSession->UNLOCK( rLock,
1004 DAVRequestEnvironment(
1005 getRequestURI(),
1006 new DAVAuthListener_Impl( xEnv, m_aURL ),
1007 aHeaders, xEnv ) );
1010 //=========================================================================
1011 void DAVResourceAccess::setURL( const rtl::OUString & rNewURL )
1012 throw( DAVException )
1014 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1015 m_aURL = rNewURL;
1016 m_aPath = rtl::OUString(); // Next initialize() will create new session.
1019 //=========================================================================
1020 // init dav session and path
1021 void DAVResourceAccess::initialize()
1022 throw ( DAVException )
1024 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1025 if ( m_aPath.getLength() == 0 )
1027 NeonUri aURI( m_aURL );
1028 rtl::OUString aPath( aURI.GetPath() );
1030 /* #134089# - Check URI */
1031 if ( !aPath.getLength() )
1032 throw DAVException( DAVException::DAV_INVALID_ARG );
1034 /* #134089# - Check URI */
1035 if ( !aURI.GetHost().getLength() )
1036 throw DAVException( DAVException::DAV_INVALID_ARG );
1038 if ( !m_xSession.is() || !m_xSession->CanUse( m_aURL ) )
1040 m_xSession.clear();
1042 // create new webdav session
1043 m_xSession
1044 = m_xSessionFactory->createDAVSession( m_aURL, m_xSMgr );
1046 if ( !m_xSession.is() )
1047 return;
1050 // Own URI is needed for redirect cycle detection.
1051 m_aRedirectURIs.push_back( aURI );
1053 // Success.
1054 m_aPath = aPath;
1056 // Not only the path has to be encoded
1057 m_aURL = aURI.GetURI();
1061 //=========================================================================
1062 const rtl::OUString & DAVResourceAccess::getRequestURI() const
1064 OSL_ENSURE( m_xSession.is(),
1065 "DAVResourceAccess::getRequestURI - Not initialized!" );
1067 // In case a proxy is used we have to use the absolute URI for a request.
1068 if ( m_xSession->UsesProxy() )
1069 return m_aURL;
1071 return m_aPath;
1074 //=========================================================================
1075 // static
1076 void DAVResourceAccess::getUserRequestHeaders(
1077 const uno::Reference< ucb::XCommandEnvironment > & xEnv,
1078 const rtl::OUString & rURI,
1079 const rtl::OUString & rMethod,
1080 DAVRequestHeaders & rRequestHeaders )
1082 if ( xEnv.is() )
1084 uno::Reference< ucb::XWebDAVCommandEnvironment > xDAVEnv(
1085 xEnv, uno::UNO_QUERY );
1087 if ( xDAVEnv.is() )
1089 uno::Sequence< beans::NamedValue > aRequestHeaders
1090 = xDAVEnv->getUserRequestHeaders( rURI, rMethod );
1092 for ( sal_Int32 n = 0; n < aRequestHeaders.getLength(); ++n )
1094 rtl::OUString aValue;
1095 sal_Bool isString = aRequestHeaders[ n ].Value >>= aValue;
1097 if ( !isString )
1099 OSL_ENSURE( isString,
1100 "DAVResourceAccess::getUserRequestHeaders :"
1101 "Value is not a string! Ignoring..." );
1104 rRequestHeaders.push_back( DAVRequestHeader(
1105 aRequestHeaders[ n ].Name,
1106 aValue ) );
1112 // static
1113 com::sun::star::uno::Reference< com::sun::star::ucb::XCommandEnvironment > DAVResourceAccess::createCommandEnvironment( void )
1115 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY );
1116 uno::Reference< task::XInteractionHandler > xInteractionHandler = uno::Reference< task::XInteractionHandler > (
1117 xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uui.InteractionHandler") ) ), uno::UNO_QUERY );
1118 ucbhelper::CommandEnvironment* pCommandEnv = new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() );
1120 return uno::Reference< ucb::XCommandEnvironment >( static_cast< ucb::XCommandEnvironment* >( pCommandEnv ), uno::UNO_QUERY );
1124 //=========================================================================
1125 sal_Bool DAVResourceAccess::detectRedirectCycle(
1126 const rtl::OUString& rRedirectURL )
1127 throw ( DAVException )
1129 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1131 NeonUri aUri( rRedirectURL );
1133 std::vector< NeonUri >::const_iterator it = m_aRedirectURIs.begin();
1134 std::vector< NeonUri >::const_iterator end = m_aRedirectURIs.end();
1136 while ( it != end )
1138 if ( aUri == (*it) )
1139 return sal_True;
1141 it++;
1144 return sal_False;
1147 //=========================================================================
1148 void DAVResourceAccess::resetUri()
1150 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1151 if ( m_aRedirectURIs.size() > 0 )
1153 std::vector< NeonUri >::const_iterator it = m_aRedirectURIs.begin();
1155 NeonUri aUri( (*it) );
1156 m_aRedirectURIs.clear();
1157 setURL ( aUri.GetURI() );
1158 initialize();
1163 //=========================================================================
1164 sal_Bool DAVResourceAccess::handleException( DAVException & e, int errorCount )
1165 throw ( DAVException )
1167 switch ( e.getError() )
1169 case DAVException::DAV_HTTP_REDIRECT:
1170 if ( !detectRedirectCycle( e.getData() ) )
1172 // set new URL and path.
1173 setURL( e.getData() );
1174 initialize();
1175 return sal_True;
1177 return sal_False;
1178 // --> tkr #67048# copy & paste images doesn't display.
1179 // if we have a bad connection try again. Up to three times.
1180 case DAVException::DAV_HTTP_ERROR:
1181 // retry up to three times, if not a client-side error.
1182 if ( ( e.getStatus() < 400 || e.getStatus() > 499 ) && errorCount < 3)
1184 return sal_True;
1186 return sal_False;
1187 // <--
1188 // --> tkr: if connection has said retry then retry!
1189 case DAVException::DAV_HTTP_RETRY:
1190 return sal_True;
1191 // <--
1192 default:
1193 return sal_False; // Abort