update dev300-m58
[ooovba.git] / ucb / source / ucp / webdav / DAVResourceAccess.cxx
bloba2af43b0886050fcc5d9f0b58f735588d980a8cb
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 const sal_Bool & bAllowPersistentStoring)
68 uno::Reference< task::XInteractionHandler > xIH;
70 if ( m_xEnv.is() )
71 xIH = m_xEnv->getInteractionHandler();
72 else
73 xIH = DAVResourceAccess::createCommandEnvironment()->getInteractionHandler();
75 if ( !xIH.is() )
76 return -1;
78 // #102871# - Supply username and password from previous try.
79 // Password container service depends on this!
80 if ( inoutUserName.getLength() == 0 )
81 inoutUserName = m_aPrevUsername;
83 if ( outPassWord.getLength() == 0 )
84 outPassWord = m_aPrevPassword;
86 rtl::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
87 = new ucbhelper::SimpleAuthenticationRequest( inHostName,
88 inRealm,
89 inoutUserName,
90 outPassWord,
91 ::rtl::OUString(),
92 bAllowPersistentStoring );
93 xIH->handle( xRequest.get() );
95 rtl::Reference< ucbhelper::InteractionContinuation > xSelection
96 = xRequest->getSelection();
98 if ( !xSelection.is() )
99 return -1;
101 // Handler handled the request.
102 uno::Reference< task::XInteractionAbort > xAbort(
103 xSelection.get(), uno::UNO_QUERY );
104 if ( xAbort.is() )
105 return -1;
107 const rtl::Reference< ucbhelper::InteractionSupplyAuthentication > & xSupp
108 = xRequest->getAuthenticationSupplier();
110 inoutUserName = xSupp->getUserName();
111 outPassWord = xSupp->getPassword();
113 // #102871# - Remember username and password.
114 m_aPrevUsername = inoutUserName;
115 m_aPrevPassword = outPassWord;
117 // go on.
118 return 0;
121 //=========================================================================
122 //=========================================================================
124 // DAVResourceAccess Implementation.
126 //=========================================================================
127 //=========================================================================
129 //=========================================================================
130 DAVResourceAccess::DAVResourceAccess(
131 const uno::Reference< lang::XMultiServiceFactory > & rSMgr,
132 rtl::Reference< DAVSessionFactory > const & rSessionFactory,
133 const rtl::OUString & rURL )
134 : m_aURL( rURL ),
135 m_xSessionFactory( rSessionFactory ),
136 m_xSMgr( rSMgr )
140 //=========================================================================
141 DAVResourceAccess::DAVResourceAccess( const DAVResourceAccess & rOther )
142 : m_aURL( rOther.m_aURL ),
143 m_aPath( rOther.m_aPath ),
144 m_xSession( rOther.m_xSession ),
145 m_xSessionFactory( rOther.m_xSessionFactory ),
146 m_xSMgr( rOther.m_xSMgr ),
147 m_aRedirectURIs( rOther.m_aRedirectURIs )
151 //=========================================================================
152 DAVResourceAccess & DAVResourceAccess::operator=(
153 const DAVResourceAccess & rOther )
155 m_aURL = rOther.m_aURL;
156 m_aPath = rOther.m_aPath;
157 m_xSession = rOther.m_xSession;
158 m_xSessionFactory = rOther.m_xSessionFactory;
159 m_xSMgr = rOther.m_xSMgr;
160 m_aRedirectURIs = rOther.m_aRedirectURIs;
162 return *this;
165 //=========================================================================
166 void DAVResourceAccess::OPTIONS(
167 DAVCapabilities & rCapabilities,
168 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
169 throw( DAVException )
171 initialize();
173 bool bRetry;
174 int errorCount = 0;
177 bRetry = false;
180 DAVRequestHeaders aHeaders;
181 getUserRequestHeaders( xEnv,
182 getRequestURI(),
183 rtl::OUString::createFromAscii(
184 "OPTIONS" ),
185 aHeaders );
187 m_xSession->OPTIONS( getRequestURI(),
188 rCapabilities,
189 DAVRequestEnvironment(
190 getRequestURI(),
191 new DAVAuthListener_Impl( xEnv ),
192 aHeaders, xEnv) );
194 catch ( DAVException & e )
196 errorCount++;
197 bRetry = handleException( e, errorCount );
198 if ( !bRetry )
199 throw;
202 while ( bRetry );
205 //=========================================================================
206 void DAVResourceAccess::PROPFIND(
207 const Depth nDepth,
208 const std::vector< rtl::OUString > & rPropertyNames,
209 std::vector< DAVResource > & rResources,
210 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
211 throw( DAVException )
213 initialize();
215 int errorCount = 0;
216 bool bRetry;
219 bRetry = false;
222 DAVRequestHeaders aHeaders;
224 getUserRequestHeaders( xEnv,
225 getRequestURI(),
226 rtl::OUString::createFromAscii(
227 "PROPFIND" ),
228 aHeaders );
230 m_xSession->PROPFIND( getRequestURI(),
231 nDepth,
232 rPropertyNames,
233 rResources,
234 DAVRequestEnvironment(
235 getRequestURI(),
236 new DAVAuthListener_Impl( xEnv ),
237 aHeaders, xEnv ) );
239 catch ( DAVException & e )
241 errorCount++;
242 bRetry = handleException( e, errorCount );
243 if ( !bRetry )
244 throw;
247 while ( bRetry );
250 //=========================================================================
251 void DAVResourceAccess::PROPFIND(
252 const Depth nDepth,
253 std::vector< DAVResourceInfo > & rResInfo,
254 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
255 throw( DAVException )
257 initialize();
259 int errorCount = 0;
260 bool bRetry;
263 bRetry = false;
266 DAVRequestHeaders aHeaders;
267 getUserRequestHeaders( xEnv,
268 getRequestURI(),
269 rtl::OUString::createFromAscii(
270 "PROPFIND" ),
271 aHeaders );
273 m_xSession->PROPFIND( getRequestURI(),
274 nDepth,
275 rResInfo,
276 DAVRequestEnvironment(
277 getRequestURI(),
278 new DAVAuthListener_Impl( xEnv ),
279 aHeaders, xEnv ) ) ;
281 catch ( DAVException & e )
283 errorCount++;
284 bRetry = handleException( e, errorCount );
285 if ( !bRetry )
286 throw;
289 while ( bRetry );
292 //=========================================================================
293 void DAVResourceAccess::PROPPATCH(
294 const std::vector< ProppatchValue >& rValues,
295 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
296 throw( DAVException )
298 initialize();
300 int errorCount = 0;
301 bool bRetry;
304 bRetry = false;
307 DAVRequestHeaders aHeaders;
308 getUserRequestHeaders( xEnv,
309 getRequestURI(),
310 rtl::OUString::createFromAscii(
311 "PROPPATCH" ),
312 aHeaders );
314 m_xSession->PROPPATCH( getRequestURI(),
315 rValues,
316 DAVRequestEnvironment(
317 getRequestURI(),
318 new DAVAuthListener_Impl( xEnv ),
319 aHeaders, xEnv ) );
321 catch ( DAVException & e )
323 errorCount++;
324 bRetry = handleException( e, errorCount );
325 if ( !bRetry )
326 throw;
329 while ( bRetry );
332 //=========================================================================
333 void DAVResourceAccess::HEAD(
334 const std::vector< rtl::OUString > & rHeaderNames,
335 DAVResource & rResource,
336 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
337 throw( DAVException )
339 initialize();
340 int errorCount = 0;
341 bool bRetry;
344 bRetry = false;
347 DAVRequestHeaders aHeaders;
348 getUserRequestHeaders( xEnv,
349 getRequestURI(),
350 rtl::OUString::createFromAscii( "HEAD" ),
351 aHeaders );
353 m_xSession->HEAD( getRequestURI(),
354 rHeaderNames,
355 rResource,
356 DAVRequestEnvironment(
357 getRequestURI(),
358 new DAVAuthListener_Impl( xEnv ),
359 aHeaders, xEnv ) );
361 catch ( DAVException & e )
363 errorCount++;
364 bRetry = handleException( e, errorCount );
365 if ( !bRetry )
366 throw;
369 while ( bRetry );
372 //=========================================================================
373 uno::Reference< io::XInputStream > DAVResourceAccess::GET(
374 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
375 throw( DAVException )
377 initialize();
379 uno::Reference< io::XInputStream > xStream;
380 int errorCount = 0;
381 bool bRetry;
384 bRetry = false;
387 DAVRequestHeaders aHeaders;
388 getUserRequestHeaders( xEnv,
389 getRequestURI(),
390 rtl::OUString::createFromAscii( "GET" ),
391 aHeaders );
393 xStream = m_xSession->GET( getRequestURI(),
394 DAVRequestEnvironment(
395 getRequestURI(),
396 new DAVAuthListener_Impl( xEnv ),
397 aHeaders, xEnv ) );
399 catch ( DAVException & e )
401 errorCount++;
402 bRetry = handleException( e, errorCount );
403 if ( !bRetry )
404 throw;
407 while ( bRetry );
409 return xStream;
412 //=========================================================================
413 void DAVResourceAccess::GET(
414 uno::Reference< io::XOutputStream > & rStream,
415 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
416 throw( DAVException )
418 initialize();
420 int errorCount = 0;
421 bool bRetry;
424 bRetry = false;
427 DAVRequestHeaders aHeaders;
428 getUserRequestHeaders( xEnv,
429 getRequestURI(),
430 rtl::OUString::createFromAscii( "GET" ),
431 aHeaders );
433 m_xSession->GET( getRequestURI(),
434 rStream,
435 DAVRequestEnvironment(
436 getRequestURI(),
437 new DAVAuthListener_Impl( xEnv ),
438 aHeaders, xEnv ) );
440 catch ( DAVException & e )
442 errorCount++;
443 bRetry = handleException( e, errorCount );
444 if ( !bRetry )
445 throw;
448 while ( bRetry );
451 //=========================================================================
452 uno::Reference< io::XStream > DAVResourceAccess::GET(
453 const std::vector< rtl::OUString > & rHeaderNames,
454 DAVResource & rResource,
455 const uno::Reference< ucb::XCommandEnvironment > & xEnv,
456 sal_Bool bAllowEmpty )
457 throw( DAVException )
459 initialize();
461 uno::Reference< io::XStream > xStream;
462 int errorCount = 0;
463 bool bRetry;
466 bRetry = false;
469 DAVRequestHeaders aHeaders;
470 getUserRequestHeaders( xEnv,
471 getRequestURI(),
472 rtl::OUString::createFromAscii( "GET" ),
473 aHeaders );
475 xStream = m_xSession->GET( getRequestURI(),
476 rHeaderNames,
477 rResource,
478 DAVRequestEnvironment(
479 getRequestURI(),
480 new DAVAuthListener_Impl( xEnv ),
481 aHeaders, xEnv ),
482 bAllowEmpty );
484 catch ( DAVException & e )
486 errorCount++;
487 bRetry = handleException( e, errorCount );
488 if ( !bRetry )
489 throw;
492 while ( bRetry );
494 return xStream;
497 //=========================================================================
498 void DAVResourceAccess::GET(
499 uno::Reference< io::XOutputStream > & rStream,
500 const std::vector< rtl::OUString > & rHeaderNames,
501 DAVResource & rResource,
502 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
503 throw( DAVException )
505 initialize();
507 bool bRetry;
508 int errorCount = 0;
511 bRetry = false;
514 DAVRequestHeaders aHeaders;
515 getUserRequestHeaders( xEnv,
516 getRequestURI(),
517 rtl::OUString::createFromAscii( "GET" ),
518 aHeaders );
520 m_xSession->GET( getRequestURI(),
521 rStream,
522 rHeaderNames,
523 rResource,
524 DAVRequestEnvironment(
525 getRequestURI(),
526 new DAVAuthListener_Impl( xEnv ),
527 aHeaders, xEnv ) );
529 catch ( DAVException & e )
531 errorCount++;
532 bRetry = handleException( e, errorCount );
533 if ( !bRetry )
534 throw;
537 while ( bRetry );
540 //=========================================================================
541 void DAVResourceAccess::ABORT()
542 throw( DAVException )
544 initialize();
545 m_xSession->ABORT();
547 //=========================================================================
548 namespace {
550 void resetInputStream( const uno::Reference< io::XInputStream > & rStream )
551 throw( DAVException )
555 uno::Reference< io::XSeekable > xSeekable(
556 rStream, uno::UNO_QUERY );
557 if ( xSeekable.is() )
559 xSeekable->seek( 0 );
560 return;
563 catch ( lang::IllegalArgumentException const & )
566 catch ( io::IOException const & )
570 throw DAVException( DAVException::DAV_INVALID_ARG );
573 } // namespace
575 //=========================================================================
576 void DAVResourceAccess::PUT(
577 const uno::Reference< io::XInputStream > & rStream,
578 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
579 throw( DAVException )
581 initialize();
583 // Make stream seekable, if it not. Needed, if request must be retried.
584 uno::Reference< io::XInputStream > xSeekableStream
585 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
586 rStream, m_xSMgr );
588 int errorCount = 0;
589 bool bRetry = false;
592 if ( bRetry )
593 resetInputStream( xSeekableStream );
595 bRetry = false;
598 DAVRequestHeaders aHeaders;
599 getUserRequestHeaders( xEnv,
600 getRequestURI(),
601 rtl::OUString::createFromAscii( "PUT" ),
602 aHeaders );
604 m_xSession->PUT( getRequestURI(),
605 xSeekableStream,
606 DAVRequestEnvironment(
607 getRequestURI(),
608 new DAVAuthListener_Impl( xEnv ),
609 aHeaders, xEnv ) );
611 catch ( DAVException & e )
613 errorCount++;
614 bRetry = handleException( e, errorCount );
615 if ( !bRetry )
616 throw;
619 while ( bRetry );
622 //=========================================================================
623 void DAVResourceAccess::PUT(
624 const char * buffer, size_t size,
625 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
626 throw( DAVException )
628 initialize();
630 bool bRetry = false;
631 int errorCount = 0;
634 bRetry = false;
637 DAVRequestHeaders aHeaders;
638 getUserRequestHeaders( xEnv,
639 getRequestURI(),
640 rtl::OUString::createFromAscii( "PUT" ),
641 aHeaders );
643 m_xSession->PUT( getRequestURI(),
644 buffer, size,
645 DAVRequestEnvironment(
646 getRequestURI(),
647 new DAVAuthListener_Impl( xEnv ),
648 aHeaders, xEnv ) );
650 catch ( DAVException & e )
652 errorCount++;
653 bRetry = handleException( e, errorCount );
654 if ( !bRetry )
655 throw;
658 while ( bRetry );
661 //=========================================================================
662 uno::Reference< io::XInputStream > DAVResourceAccess::POST(
663 const rtl::OUString & rContentType,
664 const rtl::OUString & rReferer,
665 const uno::Reference< io::XInputStream > & rInputStream,
666 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
667 throw ( DAVException )
669 initialize();
671 // Make stream seekable, if it not. Needed, if request must be retried.
672 uno::Reference< io::XInputStream > xSeekableStream
673 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
674 rInputStream, m_xSMgr );
676 uno::Reference< io::XInputStream > xStream;
677 int errorCount = 0;
678 bool bRetry = false;
681 if ( bRetry )
683 resetInputStream( xSeekableStream );
684 bRetry = false;
689 DAVRequestHeaders aHeaders;
690 getUserRequestHeaders( xEnv,
691 getRequestURI(),
692 rtl::OUString::createFromAscii( "POST" ),
693 aHeaders );
695 xStream = m_xSession->POST( getRequestURI(),
696 rContentType,
697 rReferer,
698 xSeekableStream,
699 DAVRequestEnvironment(
700 getRequestURI(),
701 new DAVAuthListener_Impl( xEnv ),
702 aHeaders, xEnv ) );
704 catch ( DAVException & e )
706 errorCount++;
707 bRetry = handleException( e, errorCount );
708 if ( !bRetry )
709 throw;
711 if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
713 // #i74980# - Upon POST redirect, do a GET.
714 return GET( xEnv );
718 while ( bRetry );
720 return xStream;
723 //=========================================================================
725 void DAVResourceAccess::POST(
726 const rtl::OUString & rContentType,
727 const rtl::OUString & rReferer,
728 const uno::Reference< io::XInputStream > & rInputStream,
729 uno::Reference< io::XOutputStream > & rOutputStream,
730 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
731 throw ( DAVException )
733 initialize();
735 // Make stream seekable, if it not. Needed, if request must be retried.
736 uno::Reference< io::XInputStream > xSeekableStream
737 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
738 rInputStream, m_xSMgr );
740 int errorCount = 0;
741 bool bRetry = false;
744 if ( bRetry )
746 resetInputStream( xSeekableStream );
747 bRetry = false;
752 DAVRequestHeaders aHeaders;
753 getUserRequestHeaders( xEnv,
754 getRequestURI(),
755 rtl::OUString::createFromAscii( "POST" ),
756 aHeaders );
758 m_xSession->POST( getRequestURI(),
759 rContentType,
760 rReferer,
761 xSeekableStream,
762 rOutputStream,
763 DAVRequestEnvironment(
764 getRequestURI(),
765 new DAVAuthListener_Impl( xEnv ),
766 aHeaders, xEnv ) );
768 catch ( DAVException & e )
770 errorCount++;
771 bRetry = handleException( e, errorCount );
772 if ( !bRetry )
773 throw;
775 if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
777 // #i74980# - Upon POST redirect, do a GET.
778 GET( rOutputStream, xEnv );
779 return;
783 while ( bRetry );
786 //=========================================================================
787 void DAVResourceAccess::MKCOL(
788 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
789 throw( DAVException )
791 initialize();
792 int errorCount = 0;
793 bool bRetry;
796 bRetry = false;
799 DAVRequestHeaders aHeaders;
800 getUserRequestHeaders( xEnv,
801 getRequestURI(),
802 rtl::OUString::createFromAscii( "MKCOL" ),
803 aHeaders );
805 m_xSession->MKCOL( getRequestURI(),
806 DAVRequestEnvironment(
807 getRequestURI(),
808 new DAVAuthListener_Impl( xEnv ),
809 aHeaders, xEnv ) );
811 catch ( DAVException & e )
813 errorCount++;
814 bRetry = handleException( e, errorCount );
815 if ( !bRetry )
816 throw;
819 while ( bRetry );
822 //=========================================================================
823 void DAVResourceAccess::COPY(
824 const ::rtl::OUString & rSourcePath,
825 const ::rtl::OUString & rDestinationURI,
826 sal_Bool bOverwrite,
827 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
828 throw( DAVException )
830 initialize();
831 int errorCount = 0;
832 bool bRetry;
835 bRetry = false;
838 DAVRequestHeaders aHeaders;
839 getUserRequestHeaders( xEnv,
840 getRequestURI(),
841 rtl::OUString::createFromAscii( "COPY" ),
842 aHeaders );
844 m_xSession->COPY( rSourcePath,
845 rDestinationURI,
846 DAVRequestEnvironment(
847 getRequestURI(),
848 new DAVAuthListener_Impl( xEnv ),
849 aHeaders, xEnv ),
850 bOverwrite );
852 catch ( DAVException & e )
854 errorCount++;
855 bRetry = handleException( e, errorCount );
856 if ( !bRetry )
857 throw;
860 while ( bRetry );
863 //=========================================================================
864 void DAVResourceAccess::MOVE(
865 const ::rtl::OUString & rSourcePath,
866 const ::rtl::OUString & rDestinationURI,
867 sal_Bool bOverwrite,
868 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
869 throw( DAVException )
871 initialize();
872 int errorCount = 0;
873 bool bRetry;
876 bRetry = false;
879 DAVRequestHeaders aHeaders;
880 getUserRequestHeaders( xEnv,
881 getRequestURI(),
882 rtl::OUString::createFromAscii( "MOVE" ),
883 aHeaders );
885 m_xSession->MOVE( rSourcePath,
886 rDestinationURI,
887 DAVRequestEnvironment(
888 getRequestURI(),
889 new DAVAuthListener_Impl( xEnv ),
890 aHeaders, xEnv ),
891 bOverwrite );
893 catch ( DAVException & e )
895 errorCount++;
896 bRetry = handleException( e, errorCount );
897 if ( !bRetry )
898 throw;
901 while ( bRetry );
904 //=========================================================================
905 void DAVResourceAccess::DESTROY(
906 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
907 throw( DAVException )
909 initialize();
911 int errorCount = 0;
912 bool bRetry;
915 bRetry = false;
918 DAVRequestHeaders aHeaders;
919 getUserRequestHeaders( xEnv,
920 getRequestURI(),
921 rtl::OUString::createFromAscii(
922 "DESTROY" ),
923 aHeaders );
925 m_xSession->DESTROY( getRequestURI(),
926 DAVRequestEnvironment(
927 getRequestURI(),
928 new DAVAuthListener_Impl( xEnv ),
929 aHeaders, xEnv ) );
931 catch ( DAVException & e )
933 errorCount++;
934 bRetry = handleException( e, errorCount );
935 if ( !bRetry )
936 throw;
939 while ( bRetry );
942 //=========================================================================
943 void DAVResourceAccess::LOCK (
944 ucb::Lock & rLock,
945 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
946 throw( DAVException )
948 initialize();
950 DAVRequestHeaders aHeaders;
951 getUserRequestHeaders( xEnv,
952 getRequestURI(),
953 rtl::OUString::createFromAscii( "LOCK" ),
954 aHeaders );
956 m_xSession->LOCK( rLock,
957 DAVRequestEnvironment(
958 getRequestURI(),
959 new DAVAuthListener_Impl( xEnv ),
960 aHeaders, xEnv ) );
963 //=========================================================================
964 void DAVResourceAccess::UNLOCK (
965 ucb::Lock & rLock,
966 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
967 throw( DAVException )
969 initialize();
971 DAVRequestHeaders aHeaders;
972 getUserRequestHeaders( xEnv,
973 getRequestURI(),
974 rtl::OUString::createFromAscii( "UNLOCK" ),
975 aHeaders );
977 m_xSession->UNLOCK( rLock,
978 DAVRequestEnvironment(
979 getRequestURI(),
980 new DAVAuthListener_Impl( xEnv ),
981 aHeaders, xEnv ) );
984 //=========================================================================
985 void DAVResourceAccess::setURL( const rtl::OUString & rNewURL )
986 throw( DAVException )
988 osl::Guard< osl::Mutex > aGuard( m_aMutex );
989 m_aURL = rNewURL;
990 m_aPath = rtl::OUString(); // Next initialize() will create new session.
993 //=========================================================================
994 // init dav session and path
995 void DAVResourceAccess::initialize()
996 throw ( DAVException )
998 osl::Guard< osl::Mutex > aGuard( m_aMutex );
999 if ( m_aPath.getLength() == 0 )
1001 NeonUri aURI( m_aURL );
1002 rtl::OUString aPath( aURI.GetPath() );
1004 /* #134089# - Check URI */
1005 if ( !aPath.getLength() )
1006 throw DAVException( DAVException::DAV_INVALID_ARG );
1008 /* #134089# - Check URI */
1009 if ( !aURI.GetHost().getLength() )
1010 throw DAVException( DAVException::DAV_INVALID_ARG );
1012 if ( !m_xSession.is() || !m_xSession->CanUse( m_aURL ) )
1014 m_xSession.clear();
1016 // create new webdav session
1017 m_xSession
1018 = m_xSessionFactory->createDAVSession( m_aURL, m_xSMgr );
1020 if ( !m_xSession.is() )
1021 return;
1024 // Own URI is needed for redirect cycle detection.
1025 m_aRedirectURIs.push_back( aURI );
1027 // Success.
1028 m_aPath = aPath;
1030 // Not only the path has to be encoded
1031 m_aURL = aURI.GetURI();
1035 //=========================================================================
1036 const rtl::OUString & DAVResourceAccess::getRequestURI() const
1038 OSL_ENSURE( m_xSession.is(),
1039 "DAVResourceAccess::getRequestURI - Not initialized!" );
1041 // In case a proxy is used we have to use the absolute URI for a request.
1042 if ( m_xSession->UsesProxy() )
1043 return m_aURL;
1045 return m_aPath;
1048 //=========================================================================
1049 // static
1050 void DAVResourceAccess::getUserRequestHeaders(
1051 const uno::Reference< ucb::XCommandEnvironment > & xEnv,
1052 const rtl::OUString & rURI,
1053 const rtl::OUString & rMethod,
1054 DAVRequestHeaders & rRequestHeaders )
1056 if ( xEnv.is() )
1058 uno::Reference< ucb::XWebDAVCommandEnvironment > xDAVEnv(
1059 xEnv, uno::UNO_QUERY );
1061 if ( xDAVEnv.is() )
1063 uno::Sequence< beans::NamedValue > aRequestHeaders
1064 = xDAVEnv->getUserRequestHeaders( rURI, rMethod );
1066 for ( sal_Int32 n = 0; n < aRequestHeaders.getLength(); ++n )
1068 rtl::OUString aValue;
1069 sal_Bool isString = aRequestHeaders[ n ].Value >>= aValue;
1071 if ( !isString )
1073 OSL_ENSURE( isString,
1074 "DAVResourceAccess::getUserRequestHeaders :"
1075 "Value is not a string! Ignoring..." );
1078 rRequestHeaders.push_back( DAVRequestHeader(
1079 aRequestHeaders[ n ].Name,
1080 aValue ) );
1086 // static
1087 com::sun::star::uno::Reference< com::sun::star::ucb::XCommandEnvironment > DAVResourceAccess::createCommandEnvironment( void )
1089 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY );
1090 uno::Reference< task::XInteractionHandler > xInteractionHandler = uno::Reference< task::XInteractionHandler > (
1091 xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uui.InteractionHandler") ) ), uno::UNO_QUERY );
1092 ucbhelper::CommandEnvironment* pCommandEnv = new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() );
1094 return uno::Reference< ucb::XCommandEnvironment >( static_cast< ucb::XCommandEnvironment* >( pCommandEnv ), uno::UNO_QUERY );
1098 //=========================================================================
1099 sal_Bool DAVResourceAccess::detectRedirectCycle(
1100 const rtl::OUString& rRedirectURL )
1101 throw ( DAVException )
1103 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1105 NeonUri aUri( rRedirectURL );
1107 std::vector< NeonUri >::const_iterator it = m_aRedirectURIs.begin();
1108 std::vector< NeonUri >::const_iterator end = m_aRedirectURIs.end();
1110 while ( it != end )
1112 if ( aUri == (*it) )
1113 return sal_True;
1115 it++;
1118 return sal_False;
1121 //=========================================================================
1122 void DAVResourceAccess::resetUri()
1124 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1125 if ( m_aRedirectURIs.size() > 0 )
1127 std::vector< NeonUri >::const_iterator it = m_aRedirectURIs.begin();
1129 NeonUri aUri( (*it) );
1130 m_aRedirectURIs.clear();
1131 setURL ( aUri.GetURI() );
1132 initialize();
1137 //=========================================================================
1138 sal_Bool DAVResourceAccess::handleException( DAVException & e, int errorCount )
1139 throw ( DAVException )
1141 switch ( e.getError() )
1143 case DAVException::DAV_HTTP_REDIRECT:
1144 if ( !detectRedirectCycle( e.getData() ) )
1146 // set new URL and path.
1147 setURL( e.getData() );
1148 initialize();
1149 return sal_True;
1151 return sal_False;
1152 // --> tkr #67048# copy & paste images doesn't display.
1153 // if we have a bad connection try again. Up to three times.
1154 case DAVException::DAV_HTTP_ERROR:
1155 // retry up to three times, if not a client-side error.
1156 if ( ( e.getStatus() < 400 || e.getStatus() > 499 ) && errorCount < 3)
1158 return sal_True;
1160 return sal_False;
1161 // <--
1162 // --> tkr: if connection has said retry then retry!
1163 case DAVException::DAV_HTTP_RETRY:
1164 return sal_True;
1165 // <--
1166 default:
1167 return sal_False; // Abort