12f86921cSAndrew Rist /**************************************************************
22f86921cSAndrew Rist  *
32f86921cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42f86921cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52f86921cSAndrew Rist  * distributed with this work for additional information
62f86921cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72f86921cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
82f86921cSAndrew Rist  * "License"); you may not use this file except in compliance
92f86921cSAndrew Rist  * with the License.  You may obtain a copy of the License at
102f86921cSAndrew Rist  *
112f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122f86921cSAndrew Rist  *
132f86921cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
142f86921cSAndrew Rist  * software distributed under the License is distributed on an
152f86921cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162f86921cSAndrew Rist  * KIND, either express or implied.  See the License for the
172f86921cSAndrew Rist  * specific language governing permissions and limitations
182f86921cSAndrew Rist  * under the License.
192f86921cSAndrew Rist  *
202f86921cSAndrew Rist  *************************************************************/
212f86921cSAndrew Rist 
222f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucb.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "osl/diagnose.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "com/sun/star/task/XInteractionAbort.hpp"
30cdf0e10cSrcweir #include "com/sun/star/ucb/XWebDAVCommandEnvironment.hpp"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "ucbhelper/simpleauthenticationrequest.hxx"
33cdf0e10cSrcweir #include "comphelper/seekableinput.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include "DAVAuthListenerImpl.hxx"
36cdf0e10cSrcweir #include "DAVResourceAccess.hxx"
37cdf0e10cSrcweir 
3859ddfc10SAndre Fischer using namespace http_dav_ucp;
39cdf0e10cSrcweir using namespace com::sun::star;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //=========================================================================
42cdf0e10cSrcweir //=========================================================================
43cdf0e10cSrcweir //
44cdf0e10cSrcweir // DAVAuthListener_Impl Implementation.
45cdf0e10cSrcweir //
46cdf0e10cSrcweir //=========================================================================
47cdf0e10cSrcweir //=========================================================================
48cdf0e10cSrcweir 
49cdf0e10cSrcweir //=========================================================================
50cdf0e10cSrcweir // virtual
51cdf0e10cSrcweir int DAVAuthListener_Impl::authenticate(
52cdf0e10cSrcweir     const ::rtl::OUString & inRealm,
53cdf0e10cSrcweir     const ::rtl::OUString & inHostName,
54cdf0e10cSrcweir     ::rtl::OUString & inoutUserName,
55cdf0e10cSrcweir     ::rtl::OUString & outPassWord,
56fb7f54d2SOliver-Rainer Wittmann     sal_Bool bCanUseSystemCredentials,
57fb7f54d2SOliver-Rainer Wittmann     sal_Bool bUsePreviousCredentials )
58cdf0e10cSrcweir {
59cdf0e10cSrcweir     if ( m_xEnv.is() )
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         uno::Reference< task::XInteractionHandler > xIH
62cdf0e10cSrcweir             = m_xEnv->getInteractionHandler();
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         if ( xIH.is() )
65cdf0e10cSrcweir         {
66fb7f54d2SOliver-Rainer Wittmann             // Providing previously retrieved credentials will cause the password
67fb7f54d2SOliver-Rainer Wittmann             // container to reject these. Thus, the credential input dialog will be shown again.
68cdf0e10cSrcweir             // #102871# - Supply username and password from previous try.
69cdf0e10cSrcweir             // Password container service depends on this!
70fb7f54d2SOliver-Rainer Wittmann             if ( inoutUserName.getLength() == 0 && bUsePreviousCredentials )
71cdf0e10cSrcweir                 inoutUserName = m_aPrevUsername;
72cdf0e10cSrcweir 
73fb7f54d2SOliver-Rainer Wittmann             if ( outPassWord.getLength() == 0 && bUsePreviousCredentials )
74cdf0e10cSrcweir                 outPassWord = m_aPrevPassword;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir             rtl::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
77cdf0e10cSrcweir                 = new ucbhelper::SimpleAuthenticationRequest(
78cdf0e10cSrcweir                     m_aURL, inHostName, inRealm, inoutUserName,
79cdf0e10cSrcweir                     outPassWord, ::rtl::OUString(),
80cdf0e10cSrcweir                     true /*bAllowPersistentStoring*/,
81cdf0e10cSrcweir                     bCanUseSystemCredentials );
82cdf0e10cSrcweir             xIH->handle( xRequest.get() );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir             rtl::Reference< ucbhelper::InteractionContinuation > xSelection
85cdf0e10cSrcweir                 = xRequest->getSelection();
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             if ( xSelection.is() )
88cdf0e10cSrcweir             {
89cdf0e10cSrcweir                 // Handler handled the request.
90cdf0e10cSrcweir                 uno::Reference< task::XInteractionAbort > xAbort(
91cdf0e10cSrcweir                     xSelection.get(), uno::UNO_QUERY );
92cdf0e10cSrcweir                 if ( !xAbort.is() )
93cdf0e10cSrcweir                 {
94cdf0e10cSrcweir                     const rtl::Reference<
95cdf0e10cSrcweir                         ucbhelper::InteractionSupplyAuthentication > & xSupp
96cdf0e10cSrcweir                         = xRequest->getAuthenticationSupplier();
97cdf0e10cSrcweir 
98cdf0e10cSrcweir                     sal_Bool bUseSystemCredentials = sal_False;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir                     if ( bCanUseSystemCredentials )
101cdf0e10cSrcweir                         bUseSystemCredentials
102cdf0e10cSrcweir                             = xSupp->getUseSystemCredentials();
103cdf0e10cSrcweir 
104cdf0e10cSrcweir                     if ( bUseSystemCredentials )
105cdf0e10cSrcweir                     {
106cdf0e10cSrcweir                         // This is the (strange) way to tell neon to use
107cdf0e10cSrcweir                         // system credentials.
108cdf0e10cSrcweir                         inoutUserName = rtl::OUString();
109cdf0e10cSrcweir                         outPassWord   = rtl::OUString();
110cdf0e10cSrcweir                     }
111cdf0e10cSrcweir                     else
112cdf0e10cSrcweir                     {
113cdf0e10cSrcweir                         inoutUserName = xSupp->getUserName();
114cdf0e10cSrcweir                         outPassWord   = xSupp->getPassword();
115cdf0e10cSrcweir                     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir                     // #102871# - Remember username and password.
118cdf0e10cSrcweir                     m_aPrevUsername = inoutUserName;
119cdf0e10cSrcweir                     m_aPrevPassword = outPassWord;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir                     // go on.
122cdf0e10cSrcweir                     return 0;
123cdf0e10cSrcweir                 }
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir     // Abort.
128cdf0e10cSrcweir     return -1;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir //=========================================================================
132cdf0e10cSrcweir //=========================================================================
133cdf0e10cSrcweir //
134cdf0e10cSrcweir // DAVResourceAccess Implementation.
135cdf0e10cSrcweir //
136cdf0e10cSrcweir //=========================================================================
137cdf0e10cSrcweir //=========================================================================
138cdf0e10cSrcweir 
139cdf0e10cSrcweir //=========================================================================
140cdf0e10cSrcweir DAVResourceAccess::DAVResourceAccess(
141cdf0e10cSrcweir     const uno::Reference< lang::XMultiServiceFactory > & rSMgr,
142cdf0e10cSrcweir     rtl::Reference< DAVSessionFactory > const & rSessionFactory,
143cdf0e10cSrcweir     const rtl::OUString & rURL )
144cdf0e10cSrcweir : m_aURL( rURL ),
145cdf0e10cSrcweir   m_xSessionFactory( rSessionFactory ),
146cdf0e10cSrcweir   m_xSMgr( rSMgr )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir //=========================================================================
151cdf0e10cSrcweir DAVResourceAccess::DAVResourceAccess( const DAVResourceAccess & rOther )
152cdf0e10cSrcweir : m_aURL( rOther.m_aURL ),
153cdf0e10cSrcweir   m_aPath( rOther.m_aPath ),
154cdf0e10cSrcweir   m_xSession( rOther.m_xSession ),
155cdf0e10cSrcweir   m_xSessionFactory( rOther.m_xSessionFactory ),
156cdf0e10cSrcweir   m_xSMgr( rOther.m_xSMgr ),
157cdf0e10cSrcweir   m_aRedirectURIs( rOther.m_aRedirectURIs )
158cdf0e10cSrcweir {
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir //=========================================================================
162cdf0e10cSrcweir DAVResourceAccess & DAVResourceAccess::operator=(
163cdf0e10cSrcweir     const DAVResourceAccess & rOther )
164cdf0e10cSrcweir {
165cdf0e10cSrcweir     m_aURL            = rOther.m_aURL;
166cdf0e10cSrcweir     m_aPath           = rOther.m_aPath;
167cdf0e10cSrcweir     m_xSession        = rOther.m_xSession;
168cdf0e10cSrcweir     m_xSessionFactory = rOther.m_xSessionFactory;
169cdf0e10cSrcweir     m_xSMgr           = rOther.m_xSMgr;
170cdf0e10cSrcweir     m_aRedirectURIs   = rOther.m_aRedirectURIs;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     return *this;
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir //=========================================================================
176cdf0e10cSrcweir void DAVResourceAccess::PROPFIND(
177cdf0e10cSrcweir     const Depth nDepth,
178cdf0e10cSrcweir     const std::vector< rtl::OUString > & rPropertyNames,
179cdf0e10cSrcweir     std::vector< DAVResource > & rResources,
180cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
181cdf0e10cSrcweir   throw( DAVException )
182cdf0e10cSrcweir {
183cdf0e10cSrcweir     initialize();
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     int errorCount = 0;
186cdf0e10cSrcweir     bool bRetry;
187cdf0e10cSrcweir     do
188cdf0e10cSrcweir     {
189cdf0e10cSrcweir         bRetry = false;
190cdf0e10cSrcweir         try
191cdf0e10cSrcweir         {
192cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
195cdf0e10cSrcweir                                    getRequestURI(),
196cdf0e10cSrcweir                                    rtl::OUString::createFromAscii(
197cdf0e10cSrcweir                                        "PROPFIND" ),
198cdf0e10cSrcweir                                    aHeaders );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir             m_xSession->PROPFIND( getRequestURI(),
201cdf0e10cSrcweir                                   nDepth,
202cdf0e10cSrcweir                                   rPropertyNames,
203cdf0e10cSrcweir                                   rResources,
204cdf0e10cSrcweir                                   DAVRequestEnvironment(
205cdf0e10cSrcweir                                       getRequestURI(),
206cdf0e10cSrcweir                                       new DAVAuthListener_Impl( xEnv, m_aURL ),
207cdf0e10cSrcweir                                       aHeaders, xEnv ) );
208cdf0e10cSrcweir         }
209cdf0e10cSrcweir         catch ( DAVException & e )
210cdf0e10cSrcweir         {
211cdf0e10cSrcweir             errorCount++;
212cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
213cdf0e10cSrcweir             if ( !bRetry )
214cdf0e10cSrcweir                 throw;
215cdf0e10cSrcweir         }
216cdf0e10cSrcweir     }
217cdf0e10cSrcweir     while ( bRetry );
218cdf0e10cSrcweir }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir //=========================================================================
221cdf0e10cSrcweir void DAVResourceAccess::PROPFIND(
222cdf0e10cSrcweir     const Depth nDepth,
223cdf0e10cSrcweir     std::vector< DAVResourceInfo > & rResInfo,
224cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
225cdf0e10cSrcweir   throw( DAVException )
226cdf0e10cSrcweir {
227cdf0e10cSrcweir     initialize();
228cdf0e10cSrcweir 
229cdf0e10cSrcweir     int errorCount = 0;
230cdf0e10cSrcweir     bool bRetry;
231cdf0e10cSrcweir     do
232cdf0e10cSrcweir     {
233cdf0e10cSrcweir         bRetry = false;
234cdf0e10cSrcweir         try
235cdf0e10cSrcweir         {
236cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
237cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
238cdf0e10cSrcweir                                    getRequestURI(),
239cdf0e10cSrcweir                                    rtl::OUString::createFromAscii(
240cdf0e10cSrcweir                                        "PROPFIND" ),
241cdf0e10cSrcweir                                    aHeaders );
242cdf0e10cSrcweir 
243cdf0e10cSrcweir             m_xSession->PROPFIND( getRequestURI(),
244cdf0e10cSrcweir                                   nDepth,
245cdf0e10cSrcweir                                   rResInfo,
246cdf0e10cSrcweir                                   DAVRequestEnvironment(
247cdf0e10cSrcweir                                       getRequestURI(),
248cdf0e10cSrcweir                                       new DAVAuthListener_Impl( xEnv, m_aURL ),
249cdf0e10cSrcweir                                       aHeaders, xEnv ) ) ;
250cdf0e10cSrcweir         }
251cdf0e10cSrcweir         catch ( DAVException & e )
252cdf0e10cSrcweir         {
253cdf0e10cSrcweir             errorCount++;
254cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
255cdf0e10cSrcweir             if ( !bRetry )
256cdf0e10cSrcweir                 throw;
257cdf0e10cSrcweir         }
258cdf0e10cSrcweir     }
259cdf0e10cSrcweir     while ( bRetry );
260cdf0e10cSrcweir }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir //=========================================================================
263cdf0e10cSrcweir void DAVResourceAccess::PROPPATCH(
264cdf0e10cSrcweir     const std::vector< ProppatchValue >& rValues,
265cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment >& xEnv )
266cdf0e10cSrcweir   throw( DAVException )
267cdf0e10cSrcweir {
268cdf0e10cSrcweir     initialize();
269cdf0e10cSrcweir 
270cdf0e10cSrcweir     int errorCount = 0;
271cdf0e10cSrcweir     bool bRetry;
272cdf0e10cSrcweir     do
273cdf0e10cSrcweir     {
274cdf0e10cSrcweir         bRetry = false;
275cdf0e10cSrcweir         try
276cdf0e10cSrcweir         {
277cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
278cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
279cdf0e10cSrcweir                                    getRequestURI(),
280cdf0e10cSrcweir                                    rtl::OUString::createFromAscii(
281cdf0e10cSrcweir                                        "PROPPATCH" ),
282cdf0e10cSrcweir                                    aHeaders );
283cdf0e10cSrcweir 
284cdf0e10cSrcweir             m_xSession->PROPPATCH( getRequestURI(),
285cdf0e10cSrcweir                                    rValues,
286cdf0e10cSrcweir                                    DAVRequestEnvironment(
287cdf0e10cSrcweir                                        getRequestURI(),
288cdf0e10cSrcweir                                        new DAVAuthListener_Impl( xEnv, m_aURL ),
289cdf0e10cSrcweir                                        aHeaders, xEnv ) );
290cdf0e10cSrcweir         }
291cdf0e10cSrcweir         catch ( DAVException & e )
292cdf0e10cSrcweir         {
293cdf0e10cSrcweir             errorCount++;
294cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
295cdf0e10cSrcweir             if ( !bRetry )
296cdf0e10cSrcweir                 throw;
297cdf0e10cSrcweir         }
298cdf0e10cSrcweir     }
299cdf0e10cSrcweir     while ( bRetry );
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
302cdf0e10cSrcweir //=========================================================================
303cdf0e10cSrcweir void DAVResourceAccess::HEAD(
304cdf0e10cSrcweir     const std::vector< rtl::OUString > & rHeaderNames,
305cdf0e10cSrcweir     DAVResource & rResource,
306cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment >& xEnv )
307cdf0e10cSrcweir   throw( DAVException )
308cdf0e10cSrcweir {
309cdf0e10cSrcweir     initialize();
310cdf0e10cSrcweir 
311cdf0e10cSrcweir     int errorCount = 0;
312cdf0e10cSrcweir     bool bRetry;
313cdf0e10cSrcweir     do
314cdf0e10cSrcweir     {
315cdf0e10cSrcweir         bRetry = false;
316cdf0e10cSrcweir         try
317cdf0e10cSrcweir         {
318cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
319cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
320cdf0e10cSrcweir                                    getRequestURI(),
321cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "HEAD" ),
322cdf0e10cSrcweir                                    aHeaders );
323cdf0e10cSrcweir 
324cdf0e10cSrcweir             m_xSession->HEAD( getRequestURI(),
325cdf0e10cSrcweir                               rHeaderNames,
326cdf0e10cSrcweir                               rResource,
327cdf0e10cSrcweir                               DAVRequestEnvironment(
328cdf0e10cSrcweir                                   getRequestURI(),
329cdf0e10cSrcweir                                   new DAVAuthListener_Impl( xEnv, m_aURL ),
330cdf0e10cSrcweir                                   aHeaders, xEnv ) );
331cdf0e10cSrcweir         }
332cdf0e10cSrcweir         catch ( DAVException & e )
333cdf0e10cSrcweir         {
334cdf0e10cSrcweir             errorCount++;
335cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
336cdf0e10cSrcweir             if ( !bRetry )
337cdf0e10cSrcweir                 throw;
338cdf0e10cSrcweir         }
339cdf0e10cSrcweir     }
340cdf0e10cSrcweir     while ( bRetry );
341cdf0e10cSrcweir }
342cdf0e10cSrcweir 
343cdf0e10cSrcweir //=========================================================================
344cdf0e10cSrcweir uno::Reference< io::XInputStream > DAVResourceAccess::GET(
345cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
346cdf0e10cSrcweir   throw( DAVException )
347cdf0e10cSrcweir {
348cdf0e10cSrcweir     initialize();
349cdf0e10cSrcweir 
350cdf0e10cSrcweir     uno::Reference< io::XInputStream > xStream;
351cdf0e10cSrcweir     int errorCount = 0;
352cdf0e10cSrcweir     bool bRetry;
353cdf0e10cSrcweir     do
354cdf0e10cSrcweir     {
355cdf0e10cSrcweir         bRetry = false;
356cdf0e10cSrcweir         try
357cdf0e10cSrcweir         {
358cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
359cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
360cdf0e10cSrcweir                                    getRequestURI(),
361cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "GET" ),
362cdf0e10cSrcweir                                    aHeaders );
363cdf0e10cSrcweir 
364cdf0e10cSrcweir             xStream = m_xSession->GET( getRequestURI(),
365cdf0e10cSrcweir                                        DAVRequestEnvironment(
366cdf0e10cSrcweir                                            getRequestURI(),
367cdf0e10cSrcweir                                            new DAVAuthListener_Impl(
368cdf0e10cSrcweir                                                xEnv, m_aURL ),
369cdf0e10cSrcweir                                            aHeaders, xEnv ) );
370cdf0e10cSrcweir         }
371cdf0e10cSrcweir         catch ( DAVException & e )
372cdf0e10cSrcweir         {
373cdf0e10cSrcweir             errorCount++;
374cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
375cdf0e10cSrcweir             if ( !bRetry )
376cdf0e10cSrcweir                 throw;
377cdf0e10cSrcweir         }
378cdf0e10cSrcweir     }
379cdf0e10cSrcweir     while ( bRetry );
380cdf0e10cSrcweir 
381cdf0e10cSrcweir     return xStream;
382cdf0e10cSrcweir }
383cdf0e10cSrcweir 
384cdf0e10cSrcweir //=========================================================================
385cdf0e10cSrcweir void DAVResourceAccess::GET(
386cdf0e10cSrcweir     uno::Reference< io::XOutputStream > & rStream,
387cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
388cdf0e10cSrcweir   throw( DAVException )
389cdf0e10cSrcweir {
390cdf0e10cSrcweir     initialize();
391cdf0e10cSrcweir 
392cdf0e10cSrcweir     int errorCount = 0;
393cdf0e10cSrcweir     bool bRetry;
394cdf0e10cSrcweir     do
395cdf0e10cSrcweir     {
396cdf0e10cSrcweir         bRetry = false;
397cdf0e10cSrcweir         try
398cdf0e10cSrcweir         {
399cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
400cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
401cdf0e10cSrcweir                                    getRequestURI(),
402cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "GET" ),
403cdf0e10cSrcweir                                    aHeaders );
404cdf0e10cSrcweir 
405cdf0e10cSrcweir             m_xSession->GET( getRequestURI(),
406cdf0e10cSrcweir                              rStream,
407cdf0e10cSrcweir                              DAVRequestEnvironment(
408cdf0e10cSrcweir                                  getRequestURI(),
409cdf0e10cSrcweir                                  new DAVAuthListener_Impl( xEnv, m_aURL ),
410cdf0e10cSrcweir                                  aHeaders, xEnv ) );
411cdf0e10cSrcweir         }
412cdf0e10cSrcweir         catch ( DAVException & e )
413cdf0e10cSrcweir         {
414cdf0e10cSrcweir             errorCount++;
415cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
416cdf0e10cSrcweir             if ( !bRetry )
417cdf0e10cSrcweir                 throw;
418cdf0e10cSrcweir         }
419cdf0e10cSrcweir     }
420cdf0e10cSrcweir     while ( bRetry );
421cdf0e10cSrcweir }
422cdf0e10cSrcweir 
423cdf0e10cSrcweir //=========================================================================
424cdf0e10cSrcweir uno::Reference< io::XInputStream > DAVResourceAccess::GET(
425cdf0e10cSrcweir     const std::vector< rtl::OUString > & rHeaderNames,
426cdf0e10cSrcweir     DAVResource & rResource,
427cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
428cdf0e10cSrcweir   throw( DAVException )
429cdf0e10cSrcweir {
430cdf0e10cSrcweir     initialize();
431cdf0e10cSrcweir 
432cdf0e10cSrcweir     uno::Reference< io::XInputStream > xStream;
433cdf0e10cSrcweir     int errorCount = 0;
434cdf0e10cSrcweir     bool bRetry;
435cdf0e10cSrcweir     do
436cdf0e10cSrcweir     {
437cdf0e10cSrcweir         bRetry = false;
438cdf0e10cSrcweir         try
439cdf0e10cSrcweir         {
440cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
441cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
442cdf0e10cSrcweir                                    getRequestURI(),
443cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "GET" ),
444cdf0e10cSrcweir                                    aHeaders );
445cdf0e10cSrcweir 
446cdf0e10cSrcweir             xStream = m_xSession->GET( getRequestURI(),
447cdf0e10cSrcweir                                        rHeaderNames,
448cdf0e10cSrcweir                                        rResource,
449cdf0e10cSrcweir                                        DAVRequestEnvironment(
450cdf0e10cSrcweir                                            getRequestURI(),
451cdf0e10cSrcweir                                            new DAVAuthListener_Impl(
452cdf0e10cSrcweir                                                xEnv, m_aURL ),
453cdf0e10cSrcweir                                            aHeaders, xEnv ) );
454cdf0e10cSrcweir         }
455cdf0e10cSrcweir         catch ( DAVException & e )
456cdf0e10cSrcweir         {
457cdf0e10cSrcweir             errorCount++;
458cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
459cdf0e10cSrcweir             if ( !bRetry )
460cdf0e10cSrcweir                 throw;
461cdf0e10cSrcweir         }
462cdf0e10cSrcweir     }
463cdf0e10cSrcweir     while ( bRetry );
464cdf0e10cSrcweir 
465cdf0e10cSrcweir     return xStream;
466cdf0e10cSrcweir }
467cdf0e10cSrcweir 
468*c5b3447dSAriel Constenla-Haile //=========================================================================
469*c5b3447dSAriel Constenla-Haile uno::Reference< io::XInputStream > DAVResourceAccess::GET(
470*c5b3447dSAriel Constenla-Haile     DAVRequestHeaders &rRequestHeaders,
471*c5b3447dSAriel Constenla-Haile     const std::vector< rtl::OUString > & rHeaderNames,
472*c5b3447dSAriel Constenla-Haile     DAVResource & rResource,
473*c5b3447dSAriel Constenla-Haile     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
474*c5b3447dSAriel Constenla-Haile   throw( DAVException )
475*c5b3447dSAriel Constenla-Haile {
476*c5b3447dSAriel Constenla-Haile     initialize();
477*c5b3447dSAriel Constenla-Haile 
478*c5b3447dSAriel Constenla-Haile     uno::Reference< io::XInputStream > xStream;
479*c5b3447dSAriel Constenla-Haile     int errorCount = 0;
480*c5b3447dSAriel Constenla-Haile     bool bRetry;
481*c5b3447dSAriel Constenla-Haile     do
482*c5b3447dSAriel Constenla-Haile     {
483*c5b3447dSAriel Constenla-Haile         bRetry = false;
484*c5b3447dSAriel Constenla-Haile         try
485*c5b3447dSAriel Constenla-Haile         {
486*c5b3447dSAriel Constenla-Haile             getUserRequestHeaders( xEnv,
487*c5b3447dSAriel Constenla-Haile                                    getRequestURI(),
488*c5b3447dSAriel Constenla-Haile                                    rtl::OUString::createFromAscii( "GET" ),
489*c5b3447dSAriel Constenla-Haile                                    rRequestHeaders );
490*c5b3447dSAriel Constenla-Haile 
491*c5b3447dSAriel Constenla-Haile             xStream = m_xSession->GET( getRequestURI(),
492*c5b3447dSAriel Constenla-Haile                                        rHeaderNames,
493*c5b3447dSAriel Constenla-Haile                                        rResource,
494*c5b3447dSAriel Constenla-Haile                                        DAVRequestEnvironment(
495*c5b3447dSAriel Constenla-Haile                                            getRequestURI(),
496*c5b3447dSAriel Constenla-Haile                                            new DAVAuthListener_Impl(
497*c5b3447dSAriel Constenla-Haile                                                xEnv, m_aURL ),
498*c5b3447dSAriel Constenla-Haile                                            rRequestHeaders, xEnv ) );
499*c5b3447dSAriel Constenla-Haile         }
500*c5b3447dSAriel Constenla-Haile         catch ( DAVException & e )
501*c5b3447dSAriel Constenla-Haile         {
502*c5b3447dSAriel Constenla-Haile             errorCount++;
503*c5b3447dSAriel Constenla-Haile             bRetry = handleException( e, errorCount );
504*c5b3447dSAriel Constenla-Haile             if ( !bRetry )
505*c5b3447dSAriel Constenla-Haile                 throw;
506*c5b3447dSAriel Constenla-Haile         }
507*c5b3447dSAriel Constenla-Haile     }
508*c5b3447dSAriel Constenla-Haile     while ( bRetry );
509*c5b3447dSAriel Constenla-Haile 
510*c5b3447dSAriel Constenla-Haile     return xStream;
511*c5b3447dSAriel Constenla-Haile }
512*c5b3447dSAriel Constenla-Haile 
513cdf0e10cSrcweir //=========================================================================
514cdf0e10cSrcweir void DAVResourceAccess::GET(
515cdf0e10cSrcweir     uno::Reference< io::XOutputStream > & rStream,
516cdf0e10cSrcweir     const std::vector< rtl::OUString > & rHeaderNames,
517cdf0e10cSrcweir     DAVResource & rResource,
518cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
519cdf0e10cSrcweir   throw( DAVException )
520cdf0e10cSrcweir {
521cdf0e10cSrcweir     initialize();
522cdf0e10cSrcweir 
523cdf0e10cSrcweir     bool bRetry;
524cdf0e10cSrcweir     int errorCount = 0;
525cdf0e10cSrcweir     do
526cdf0e10cSrcweir     {
527cdf0e10cSrcweir         bRetry = false;
528cdf0e10cSrcweir         try
529cdf0e10cSrcweir         {
530cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
531cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
532cdf0e10cSrcweir                                    getRequestURI(),
533cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "GET" ),
534cdf0e10cSrcweir                                    aHeaders );
535cdf0e10cSrcweir 
536cdf0e10cSrcweir             m_xSession->GET( getRequestURI(),
537cdf0e10cSrcweir                              rStream,
538cdf0e10cSrcweir                              rHeaderNames,
539cdf0e10cSrcweir                              rResource,
540cdf0e10cSrcweir                              DAVRequestEnvironment(
541cdf0e10cSrcweir                                  getRequestURI(),
542cdf0e10cSrcweir                                  new DAVAuthListener_Impl( xEnv, m_aURL ),
543cdf0e10cSrcweir                                  aHeaders, xEnv ) );
544cdf0e10cSrcweir         }
545cdf0e10cSrcweir         catch ( DAVException & e )
546cdf0e10cSrcweir         {
547cdf0e10cSrcweir             errorCount++;
548cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
549cdf0e10cSrcweir             if ( !bRetry )
550cdf0e10cSrcweir                 throw;
551cdf0e10cSrcweir         }
552cdf0e10cSrcweir     }
553cdf0e10cSrcweir     while ( bRetry );
554cdf0e10cSrcweir }
555cdf0e10cSrcweir 
556cdf0e10cSrcweir //=========================================================================
557cdf0e10cSrcweir void DAVResourceAccess::abort()
558cdf0e10cSrcweir   throw( DAVException )
559cdf0e10cSrcweir {
560cdf0e10cSrcweir     // 17.11.09 (tkr): abort currently disabled caused by issue i106766
561cdf0e10cSrcweir     // initialize();
562cdf0e10cSrcweir     // m_xSession->abort();
563cdf0e10cSrcweir     OSL_TRACE( "Not implemented. -> #i106766#" );
564cdf0e10cSrcweir }
565cdf0e10cSrcweir 
566cdf0e10cSrcweir //=========================================================================
567cdf0e10cSrcweir namespace {
568cdf0e10cSrcweir 
569cdf0e10cSrcweir     void resetInputStream( const uno::Reference< io::XInputStream > & rStream )
570cdf0e10cSrcweir         throw( DAVException )
571cdf0e10cSrcweir     {
572cdf0e10cSrcweir         try
573cdf0e10cSrcweir         {
574cdf0e10cSrcweir             uno::Reference< io::XSeekable > xSeekable(
575cdf0e10cSrcweir                 rStream, uno::UNO_QUERY );
576cdf0e10cSrcweir             if ( xSeekable.is() )
577cdf0e10cSrcweir             {
578cdf0e10cSrcweir                 xSeekable->seek( 0 );
579cdf0e10cSrcweir                 return;
580cdf0e10cSrcweir             }
581cdf0e10cSrcweir         }
582cdf0e10cSrcweir         catch ( lang::IllegalArgumentException const & )
583cdf0e10cSrcweir         {
584cdf0e10cSrcweir         }
585cdf0e10cSrcweir         catch ( io::IOException const & )
586cdf0e10cSrcweir         {
587cdf0e10cSrcweir         }
588cdf0e10cSrcweir 
589cdf0e10cSrcweir         throw DAVException( DAVException::DAV_INVALID_ARG );
590cdf0e10cSrcweir     }
591cdf0e10cSrcweir 
592cdf0e10cSrcweir } // namespace
593cdf0e10cSrcweir 
594cdf0e10cSrcweir //=========================================================================
595cdf0e10cSrcweir void DAVResourceAccess::PUT(
596cdf0e10cSrcweir     const uno::Reference< io::XInputStream > & rStream,
597cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
598cdf0e10cSrcweir   throw( DAVException )
599cdf0e10cSrcweir {
600cdf0e10cSrcweir     initialize();
601cdf0e10cSrcweir 
602cdf0e10cSrcweir     // Make stream seekable, if it not. Needed, if request must be retried.
603cdf0e10cSrcweir     uno::Reference< io::XInputStream > xSeekableStream
604cdf0e10cSrcweir         = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
605cdf0e10cSrcweir             rStream, m_xSMgr );
606cdf0e10cSrcweir 
607cdf0e10cSrcweir     int errorCount = 0;
608cdf0e10cSrcweir     bool bRetry = false;
609cdf0e10cSrcweir     do
610cdf0e10cSrcweir     {
611cdf0e10cSrcweir         if ( bRetry )
612cdf0e10cSrcweir             resetInputStream( xSeekableStream );
613cdf0e10cSrcweir 
614cdf0e10cSrcweir         bRetry = false;
615cdf0e10cSrcweir         try
616cdf0e10cSrcweir         {
617cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
618cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
619cdf0e10cSrcweir                                    getRequestURI(),
620cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "PUT" ),
621cdf0e10cSrcweir                                    aHeaders );
622cdf0e10cSrcweir 
623cdf0e10cSrcweir             m_xSession->PUT( getRequestURI(),
624cdf0e10cSrcweir                              xSeekableStream,
625cdf0e10cSrcweir                              DAVRequestEnvironment(
626cdf0e10cSrcweir                                  getRequestURI(),
627cdf0e10cSrcweir                                  new DAVAuthListener_Impl( xEnv, m_aURL ),
628cdf0e10cSrcweir                                  aHeaders, xEnv ) );
629cdf0e10cSrcweir         }
630cdf0e10cSrcweir         catch ( DAVException & e )
631cdf0e10cSrcweir         {
632cdf0e10cSrcweir             errorCount++;
633cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
634cdf0e10cSrcweir             if ( !bRetry )
635cdf0e10cSrcweir                 throw;
636cdf0e10cSrcweir         }
637cdf0e10cSrcweir     }
638cdf0e10cSrcweir     while ( bRetry );
639cdf0e10cSrcweir }
640cdf0e10cSrcweir 
641cdf0e10cSrcweir //=========================================================================
642cdf0e10cSrcweir uno::Reference< io::XInputStream > DAVResourceAccess::POST(
643cdf0e10cSrcweir     const rtl::OUString & rContentType,
644cdf0e10cSrcweir     const rtl::OUString & rReferer,
645cdf0e10cSrcweir     const uno::Reference< io::XInputStream > & rInputStream,
646cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment >& xEnv )
647cdf0e10cSrcweir   throw ( DAVException )
648cdf0e10cSrcweir {
649cdf0e10cSrcweir     initialize();
650cdf0e10cSrcweir 
651cdf0e10cSrcweir     // Make stream seekable, if it not. Needed, if request must be retried.
652cdf0e10cSrcweir     uno::Reference< io::XInputStream > xSeekableStream
653cdf0e10cSrcweir         = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
654cdf0e10cSrcweir             rInputStream, m_xSMgr );
655cdf0e10cSrcweir 
656cdf0e10cSrcweir     uno::Reference< io::XInputStream > xStream;
657cdf0e10cSrcweir     int errorCount = 0;
658cdf0e10cSrcweir     bool bRetry = false;
659cdf0e10cSrcweir     do
660cdf0e10cSrcweir     {
661cdf0e10cSrcweir         if ( bRetry )
662cdf0e10cSrcweir         {
663cdf0e10cSrcweir             resetInputStream( xSeekableStream );
664cdf0e10cSrcweir             bRetry = false;
665cdf0e10cSrcweir         }
666cdf0e10cSrcweir 
667cdf0e10cSrcweir         try
668cdf0e10cSrcweir         {
669cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
670cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
671cdf0e10cSrcweir                                    getRequestURI(),
672cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "POST" ),
673cdf0e10cSrcweir                                    aHeaders );
674cdf0e10cSrcweir 
675cdf0e10cSrcweir             xStream = m_xSession->POST( getRequestURI(),
676cdf0e10cSrcweir                                         rContentType,
677cdf0e10cSrcweir                                         rReferer,
678cdf0e10cSrcweir                                         xSeekableStream,
679cdf0e10cSrcweir                                         DAVRequestEnvironment(
680cdf0e10cSrcweir                                             getRequestURI(),
681cdf0e10cSrcweir                                             new DAVAuthListener_Impl(
682cdf0e10cSrcweir                                                 xEnv, m_aURL ),
683cdf0e10cSrcweir                                             aHeaders, xEnv ) );
684cdf0e10cSrcweir         }
685cdf0e10cSrcweir         catch ( DAVException & e )
686cdf0e10cSrcweir         {
687cdf0e10cSrcweir             errorCount++;
688cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
689cdf0e10cSrcweir             if ( !bRetry )
690cdf0e10cSrcweir                 throw;
691cdf0e10cSrcweir 
692cdf0e10cSrcweir             if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
693cdf0e10cSrcweir             {
694cdf0e10cSrcweir                 // #i74980# - Upon POST redirect, do a GET.
695cdf0e10cSrcweir                 return GET( xEnv );
696cdf0e10cSrcweir             }
697cdf0e10cSrcweir         }
698cdf0e10cSrcweir     }
699cdf0e10cSrcweir     while ( bRetry );
700cdf0e10cSrcweir 
701cdf0e10cSrcweir     return xStream;
702cdf0e10cSrcweir }
703cdf0e10cSrcweir 
704cdf0e10cSrcweir //=========================================================================
705cdf0e10cSrcweir void DAVResourceAccess::POST(
706cdf0e10cSrcweir     const rtl::OUString & rContentType,
707cdf0e10cSrcweir     const rtl::OUString & rReferer,
708cdf0e10cSrcweir     const uno::Reference< io::XInputStream > & rInputStream,
709cdf0e10cSrcweir     uno::Reference< io::XOutputStream > & rOutputStream,
710cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment >& xEnv )
711cdf0e10cSrcweir   throw ( DAVException )
712cdf0e10cSrcweir {
713cdf0e10cSrcweir     initialize();
714cdf0e10cSrcweir 
715cdf0e10cSrcweir     // Make stream seekable, if it not. Needed, if request must be retried.
716cdf0e10cSrcweir     uno::Reference< io::XInputStream > xSeekableStream
717cdf0e10cSrcweir         = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
718cdf0e10cSrcweir             rInputStream, m_xSMgr );
719cdf0e10cSrcweir 
720cdf0e10cSrcweir     int errorCount = 0;
721cdf0e10cSrcweir     bool bRetry  = false;
722cdf0e10cSrcweir     do
723cdf0e10cSrcweir     {
724cdf0e10cSrcweir         if ( bRetry )
725cdf0e10cSrcweir         {
726cdf0e10cSrcweir             resetInputStream( xSeekableStream );
727cdf0e10cSrcweir             bRetry = false;
728cdf0e10cSrcweir         }
729cdf0e10cSrcweir 
730cdf0e10cSrcweir         try
731cdf0e10cSrcweir         {
732cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
733cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
734cdf0e10cSrcweir                                    getRequestURI(),
735cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "POST" ),
736cdf0e10cSrcweir                                    aHeaders );
737cdf0e10cSrcweir 
738cdf0e10cSrcweir             m_xSession->POST( getRequestURI(),
739cdf0e10cSrcweir                               rContentType,
740cdf0e10cSrcweir                               rReferer,
741cdf0e10cSrcweir                               xSeekableStream,
742cdf0e10cSrcweir                               rOutputStream,
743cdf0e10cSrcweir                               DAVRequestEnvironment(
744cdf0e10cSrcweir                                   getRequestURI(),
745cdf0e10cSrcweir                                   new DAVAuthListener_Impl( xEnv, m_aURL ),
746cdf0e10cSrcweir                                   aHeaders, xEnv ) );
747cdf0e10cSrcweir         }
748cdf0e10cSrcweir         catch ( DAVException & e )
749cdf0e10cSrcweir         {
750cdf0e10cSrcweir             errorCount++;
751cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
752cdf0e10cSrcweir             if ( !bRetry )
753cdf0e10cSrcweir                 throw;
754cdf0e10cSrcweir 
755cdf0e10cSrcweir             if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
756cdf0e10cSrcweir             {
757cdf0e10cSrcweir                 // #i74980# - Upon POST redirect, do a GET.
758cdf0e10cSrcweir                 GET( rOutputStream, xEnv );
759cdf0e10cSrcweir                 return;
760cdf0e10cSrcweir             }
761cdf0e10cSrcweir         }
762cdf0e10cSrcweir     }
763cdf0e10cSrcweir     while ( bRetry );
764cdf0e10cSrcweir }
765cdf0e10cSrcweir 
766cdf0e10cSrcweir //=========================================================================
767cdf0e10cSrcweir void DAVResourceAccess::MKCOL(
768cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
769cdf0e10cSrcweir   throw( DAVException )
770cdf0e10cSrcweir {
771cdf0e10cSrcweir     initialize();
772cdf0e10cSrcweir 
773cdf0e10cSrcweir     int errorCount = 0;
774cdf0e10cSrcweir     bool bRetry;
775cdf0e10cSrcweir     do
776cdf0e10cSrcweir     {
777cdf0e10cSrcweir         bRetry = false;
778cdf0e10cSrcweir         try
779cdf0e10cSrcweir         {
780cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
781cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
782cdf0e10cSrcweir                                    getRequestURI(),
783cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "MKCOL" ),
784cdf0e10cSrcweir                                    aHeaders );
785cdf0e10cSrcweir 
786cdf0e10cSrcweir             m_xSession->MKCOL( getRequestURI(),
787cdf0e10cSrcweir                                DAVRequestEnvironment(
788cdf0e10cSrcweir                                    getRequestURI(),
789cdf0e10cSrcweir                                    new DAVAuthListener_Impl( xEnv, m_aURL ),
790cdf0e10cSrcweir                                    aHeaders, xEnv ) );
791cdf0e10cSrcweir         }
792cdf0e10cSrcweir         catch ( DAVException & e )
793cdf0e10cSrcweir         {
794cdf0e10cSrcweir             errorCount++;
795cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
796cdf0e10cSrcweir             if ( !bRetry )
797cdf0e10cSrcweir                 throw;
798cdf0e10cSrcweir         }
799cdf0e10cSrcweir     }
800cdf0e10cSrcweir     while ( bRetry );
801cdf0e10cSrcweir }
802cdf0e10cSrcweir 
803cdf0e10cSrcweir //=========================================================================
804cdf0e10cSrcweir void DAVResourceAccess::COPY(
805cdf0e10cSrcweir     const ::rtl::OUString & rSourcePath,
806cdf0e10cSrcweir     const ::rtl::OUString & rDestinationURI,
807cdf0e10cSrcweir     sal_Bool bOverwrite,
808cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
809cdf0e10cSrcweir   throw( DAVException )
810cdf0e10cSrcweir {
811cdf0e10cSrcweir     initialize();
812cdf0e10cSrcweir 
813cdf0e10cSrcweir     int errorCount = 0;
814cdf0e10cSrcweir     bool bRetry;
815cdf0e10cSrcweir     do
816cdf0e10cSrcweir     {
817cdf0e10cSrcweir         bRetry = false;
818cdf0e10cSrcweir         try
819cdf0e10cSrcweir         {
820cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
821cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
822cdf0e10cSrcweir                                    getRequestURI(),
823cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "COPY" ),
824cdf0e10cSrcweir                                    aHeaders );
825cdf0e10cSrcweir 
826cdf0e10cSrcweir             m_xSession->COPY( rSourcePath,
827cdf0e10cSrcweir                               rDestinationURI,
828cdf0e10cSrcweir                               DAVRequestEnvironment(
829cdf0e10cSrcweir                                   getRequestURI(),
830cdf0e10cSrcweir                                   new DAVAuthListener_Impl( xEnv, m_aURL ),
831cdf0e10cSrcweir                                   aHeaders, xEnv ),
832cdf0e10cSrcweir                               bOverwrite );
833cdf0e10cSrcweir         }
834cdf0e10cSrcweir         catch ( DAVException & e )
835cdf0e10cSrcweir         {
836cdf0e10cSrcweir             errorCount++;
837cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
838cdf0e10cSrcweir             if ( !bRetry )
839cdf0e10cSrcweir                 throw;
840cdf0e10cSrcweir         }
841cdf0e10cSrcweir     }
842cdf0e10cSrcweir     while ( bRetry );
843cdf0e10cSrcweir }
844cdf0e10cSrcweir 
845cdf0e10cSrcweir //=========================================================================
846cdf0e10cSrcweir void DAVResourceAccess::MOVE(
847cdf0e10cSrcweir     const ::rtl::OUString & rSourcePath,
848cdf0e10cSrcweir     const ::rtl::OUString & rDestinationURI,
849cdf0e10cSrcweir     sal_Bool bOverwrite,
850cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
851cdf0e10cSrcweir   throw( DAVException )
852cdf0e10cSrcweir {
853cdf0e10cSrcweir     initialize();
854cdf0e10cSrcweir 
855cdf0e10cSrcweir     int errorCount = 0;
856cdf0e10cSrcweir     bool bRetry;
857cdf0e10cSrcweir     do
858cdf0e10cSrcweir     {
859cdf0e10cSrcweir         bRetry = false;
860cdf0e10cSrcweir         try
861cdf0e10cSrcweir         {
862cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
863cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
864cdf0e10cSrcweir                                    getRequestURI(),
865cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "MOVE" ),
866cdf0e10cSrcweir                                    aHeaders );
867cdf0e10cSrcweir 
868cdf0e10cSrcweir             m_xSession->MOVE( rSourcePath,
869cdf0e10cSrcweir                               rDestinationURI,
870cdf0e10cSrcweir                               DAVRequestEnvironment(
871cdf0e10cSrcweir                                   getRequestURI(),
872cdf0e10cSrcweir                                   new DAVAuthListener_Impl( xEnv, m_aURL ),
873cdf0e10cSrcweir                                   aHeaders, xEnv ),
874cdf0e10cSrcweir                               bOverwrite );
875cdf0e10cSrcweir         }
876cdf0e10cSrcweir         catch ( DAVException & e )
877cdf0e10cSrcweir         {
878cdf0e10cSrcweir             errorCount++;
879cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
880cdf0e10cSrcweir             if ( !bRetry )
881cdf0e10cSrcweir                 throw;
882cdf0e10cSrcweir         }
883cdf0e10cSrcweir     }
884cdf0e10cSrcweir     while ( bRetry );
885cdf0e10cSrcweir }
886cdf0e10cSrcweir 
887cdf0e10cSrcweir //=========================================================================
888cdf0e10cSrcweir void DAVResourceAccess::DESTROY(
889cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
890cdf0e10cSrcweir   throw( DAVException )
891cdf0e10cSrcweir {
892cdf0e10cSrcweir     initialize();
893cdf0e10cSrcweir 
894cdf0e10cSrcweir     int errorCount = 0;
895cdf0e10cSrcweir     bool bRetry;
896cdf0e10cSrcweir     do
897cdf0e10cSrcweir     {
898cdf0e10cSrcweir         bRetry = false;
899cdf0e10cSrcweir         try
900cdf0e10cSrcweir         {
901cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
902cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
903cdf0e10cSrcweir                                    getRequestURI(),
904cdf0e10cSrcweir                                    rtl::OUString::createFromAscii(
905cdf0e10cSrcweir                                        "DESTROY" ),
906cdf0e10cSrcweir                                    aHeaders );
907cdf0e10cSrcweir 
908cdf0e10cSrcweir             m_xSession->DESTROY( getRequestURI(),
909cdf0e10cSrcweir                                  DAVRequestEnvironment(
910cdf0e10cSrcweir                                      getRequestURI(),
911cdf0e10cSrcweir                                      new DAVAuthListener_Impl( xEnv, m_aURL ),
912cdf0e10cSrcweir                                      aHeaders, xEnv ) );
913cdf0e10cSrcweir         }
914cdf0e10cSrcweir         catch ( DAVException & e )
915cdf0e10cSrcweir         {
916cdf0e10cSrcweir             errorCount++;
917cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
918cdf0e10cSrcweir             if ( !bRetry )
919cdf0e10cSrcweir                 throw;
920cdf0e10cSrcweir         }
921cdf0e10cSrcweir     }
922cdf0e10cSrcweir     while ( bRetry );
923cdf0e10cSrcweir }
924cdf0e10cSrcweir 
925cdf0e10cSrcweir //=========================================================================
926cdf0e10cSrcweir // set new lock.
927cdf0e10cSrcweir void DAVResourceAccess::LOCK(
928cdf0e10cSrcweir     ucb::Lock & inLock,
929cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
930cdf0e10cSrcweir   throw ( DAVException )
931cdf0e10cSrcweir {
932cdf0e10cSrcweir     initialize();
933cdf0e10cSrcweir 
934cdf0e10cSrcweir     int errorCount = 0;
935cdf0e10cSrcweir     bool bRetry;
936cdf0e10cSrcweir     do
937cdf0e10cSrcweir     {
938cdf0e10cSrcweir         bRetry = false;
939cdf0e10cSrcweir         try
940cdf0e10cSrcweir         {
941cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
942cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
943cdf0e10cSrcweir                                    getRequestURI(),
944cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "LOCK" ),
945cdf0e10cSrcweir                                    aHeaders );
946cdf0e10cSrcweir 
947cdf0e10cSrcweir             m_xSession->LOCK( getRequestURI(),
948cdf0e10cSrcweir                               inLock,
949cdf0e10cSrcweir                               DAVRequestEnvironment(
950cdf0e10cSrcweir                                   getRequestURI(),
951cdf0e10cSrcweir                                   new DAVAuthListener_Impl( xEnv, m_aURL ),
952cdf0e10cSrcweir                                   aHeaders, xEnv ) );
953cdf0e10cSrcweir         }
954cdf0e10cSrcweir         catch ( DAVException & e )
955cdf0e10cSrcweir         {
956cdf0e10cSrcweir             errorCount++;
957cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
958cdf0e10cSrcweir             if ( !bRetry )
959cdf0e10cSrcweir                 throw;
960cdf0e10cSrcweir         }
961cdf0e10cSrcweir     }
962cdf0e10cSrcweir     while ( bRetry );
963cdf0e10cSrcweir }
964cdf0e10cSrcweir 
965cdf0e10cSrcweir #if 0 // currently not used, but please don't remove code
966cdf0e10cSrcweir //=========================================================================
967cdf0e10cSrcweir // refresh existing lock.
968cdf0e10cSrcweir sal_Int64 DAVResourceAccess::LOCK(
969cdf0e10cSrcweir     sal_Int64 nTimeout,
970cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
971cdf0e10cSrcweir   throw ( DAVException )
972cdf0e10cSrcweir {
973cdf0e10cSrcweir     initialize();
974cdf0e10cSrcweir 
975cdf0e10cSrcweir     sal_Int64 nNewTimeout = 0;
976cdf0e10cSrcweir     int errorCount = 0;
977cdf0e10cSrcweir     bool bRetry;
978cdf0e10cSrcweir     do
979cdf0e10cSrcweir     {
980cdf0e10cSrcweir         bRetry = false;
981cdf0e10cSrcweir         try
982cdf0e10cSrcweir         {
983cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
984cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
985cdf0e10cSrcweir                                    getRequestURI(),
986cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "LOCK" ),
987cdf0e10cSrcweir                                    aHeaders );
988cdf0e10cSrcweir 
989cdf0e10cSrcweir             nNewTimeout = m_xSession->LOCK( getRequestURI(),
990cdf0e10cSrcweir                                             nTimeout,
991cdf0e10cSrcweir                                             DAVRequestEnvironment(
992cdf0e10cSrcweir                                                 getRequestURI(),
993cdf0e10cSrcweir                                                 new DAVAuthListener_Impl(
994cdf0e10cSrcweir                                                     xEnv, m_aURL ),
995cdf0e10cSrcweir                                             aHeaders, xEnv ) );
996cdf0e10cSrcweir         }
997cdf0e10cSrcweir         catch ( DAVException & e )
998cdf0e10cSrcweir         {
999cdf0e10cSrcweir             errorCount++;
1000cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
1001cdf0e10cSrcweir             if ( !bRetry )
1002cdf0e10cSrcweir                 throw;
1003cdf0e10cSrcweir         }
1004cdf0e10cSrcweir     }
1005cdf0e10cSrcweir     while ( bRetry );
1006cdf0e10cSrcweir 
1007cdf0e10cSrcweir     return nNewTimeout;
1008cdf0e10cSrcweir }
1009cdf0e10cSrcweir #endif
1010cdf0e10cSrcweir 
1011cdf0e10cSrcweir //=========================================================================
1012cdf0e10cSrcweir void DAVResourceAccess::UNLOCK(
1013cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv )
1014cdf0e10cSrcweir   throw ( DAVException )
1015cdf0e10cSrcweir {
1016cdf0e10cSrcweir     initialize();
1017cdf0e10cSrcweir 
1018cdf0e10cSrcweir     int errorCount = 0;
1019cdf0e10cSrcweir     bool bRetry;
1020cdf0e10cSrcweir     do
1021cdf0e10cSrcweir     {
1022cdf0e10cSrcweir         bRetry = false;
1023cdf0e10cSrcweir         try
1024cdf0e10cSrcweir         {
1025cdf0e10cSrcweir             DAVRequestHeaders aHeaders;
1026cdf0e10cSrcweir             getUserRequestHeaders( xEnv,
1027cdf0e10cSrcweir                                    getRequestURI(),
1028cdf0e10cSrcweir                                    rtl::OUString::createFromAscii( "UNLOCK" ),
1029cdf0e10cSrcweir                                    aHeaders );
1030cdf0e10cSrcweir 
1031cdf0e10cSrcweir             m_xSession->UNLOCK( getRequestURI(),
1032cdf0e10cSrcweir                                 DAVRequestEnvironment(
1033cdf0e10cSrcweir                                     getRequestURI(),
1034cdf0e10cSrcweir                                     new DAVAuthListener_Impl( xEnv, m_aURL ),
1035cdf0e10cSrcweir                                     aHeaders, xEnv ) );
1036cdf0e10cSrcweir         }
1037cdf0e10cSrcweir         catch ( DAVException & e )
1038cdf0e10cSrcweir         {
1039cdf0e10cSrcweir             errorCount++;
1040cdf0e10cSrcweir             bRetry = handleException( e, errorCount );
1041cdf0e10cSrcweir             if ( !bRetry )
1042cdf0e10cSrcweir                 throw;
1043cdf0e10cSrcweir         }
1044cdf0e10cSrcweir     }
1045cdf0e10cSrcweir     while ( bRetry );
1046cdf0e10cSrcweir }
1047cdf0e10cSrcweir 
1048cdf0e10cSrcweir //=========================================================================
1049cdf0e10cSrcweir void DAVResourceAccess::setURL( const rtl::OUString & rNewURL )
1050cdf0e10cSrcweir     throw( DAVException )
1051cdf0e10cSrcweir {
1052cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard( m_aMutex );
1053cdf0e10cSrcweir     m_aURL  = rNewURL;
1054cdf0e10cSrcweir     m_aPath = rtl::OUString(); // Next initialize() will create new session.
1055cdf0e10cSrcweir }
1056cdf0e10cSrcweir 
1057cdf0e10cSrcweir //=========================================================================
1058cdf0e10cSrcweir // init dav session and path
1059cdf0e10cSrcweir void DAVResourceAccess::initialize()
1060cdf0e10cSrcweir     throw ( DAVException )
1061cdf0e10cSrcweir {
1062cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard( m_aMutex );
1063cdf0e10cSrcweir     if ( m_aPath.getLength() == 0 )
1064cdf0e10cSrcweir     {
106559ddfc10SAndre Fischer         SerfUri aURI( m_aURL );
1066cdf0e10cSrcweir         rtl::OUString aPath( aURI.GetPath() );
1067cdf0e10cSrcweir 
1068cdf0e10cSrcweir         /* #134089# - Check URI */
1069cdf0e10cSrcweir         if ( !aPath.getLength() )
1070cdf0e10cSrcweir             throw DAVException( DAVException::DAV_INVALID_ARG );
1071cdf0e10cSrcweir 
1072cdf0e10cSrcweir         /* #134089# - Check URI */
1073cdf0e10cSrcweir         if ( !aURI.GetHost().getLength() )
1074cdf0e10cSrcweir             throw DAVException( DAVException::DAV_INVALID_ARG );
1075cdf0e10cSrcweir 
1076cdf0e10cSrcweir         if ( !m_xSession.is() || !m_xSession->CanUse( m_aURL ) )
1077cdf0e10cSrcweir         {
1078cdf0e10cSrcweir             m_xSession.clear();
1079cdf0e10cSrcweir 
1080cdf0e10cSrcweir             // create new webdav session
1081cdf0e10cSrcweir             m_xSession
1082cdf0e10cSrcweir                 = m_xSessionFactory->createDAVSession( m_aURL, m_xSMgr );
1083cdf0e10cSrcweir 
1084cdf0e10cSrcweir             if ( !m_xSession.is() )
1085cdf0e10cSrcweir                 return;
1086cdf0e10cSrcweir         }
1087cdf0e10cSrcweir 
1088cdf0e10cSrcweir         // Own URI is needed for redirect cycle detection.
1089cdf0e10cSrcweir         m_aRedirectURIs.push_back( aURI );
1090cdf0e10cSrcweir 
1091cdf0e10cSrcweir         // Success.
1092cdf0e10cSrcweir         m_aPath = aPath;
1093cdf0e10cSrcweir 
1094cdf0e10cSrcweir         // Not only the path has to be encoded
1095cdf0e10cSrcweir         m_aURL = aURI.GetURI();
1096cdf0e10cSrcweir     }
1097cdf0e10cSrcweir }
1098cdf0e10cSrcweir 
1099cdf0e10cSrcweir //=========================================================================
1100cdf0e10cSrcweir const rtl::OUString & DAVResourceAccess::getRequestURI() const
1101cdf0e10cSrcweir {
1102cdf0e10cSrcweir     OSL_ENSURE( m_xSession.is(),
1103cdf0e10cSrcweir                 "DAVResourceAccess::getRequestURI - Not initialized!" );
1104cdf0e10cSrcweir 
1105cdf0e10cSrcweir     // In case a proxy is used we have to use the absolute URI for a request.
1106cdf0e10cSrcweir     if ( m_xSession->UsesProxy() )
1107cdf0e10cSrcweir         return m_aURL;
1108cdf0e10cSrcweir 
1109cdf0e10cSrcweir     return m_aPath;
1110cdf0e10cSrcweir }
1111cdf0e10cSrcweir 
1112cdf0e10cSrcweir //=========================================================================
1113cdf0e10cSrcweir // static
1114cdf0e10cSrcweir void DAVResourceAccess::getUserRequestHeaders(
1115cdf0e10cSrcweir     const uno::Reference< ucb::XCommandEnvironment > & xEnv,
1116cdf0e10cSrcweir     const rtl::OUString & rURI,
1117cdf0e10cSrcweir     const rtl::OUString & rMethod,
1118cdf0e10cSrcweir     DAVRequestHeaders & rRequestHeaders )
1119cdf0e10cSrcweir {
1120cdf0e10cSrcweir     if ( xEnv.is() )
1121cdf0e10cSrcweir     {
1122cdf0e10cSrcweir         uno::Reference< ucb::XWebDAVCommandEnvironment > xDAVEnv(
1123cdf0e10cSrcweir             xEnv, uno::UNO_QUERY );
1124cdf0e10cSrcweir 
1125cdf0e10cSrcweir         if ( xDAVEnv.is() )
1126cdf0e10cSrcweir         {
1127cdf0e10cSrcweir             uno::Sequence< beans::NamedValue > aRequestHeaders
1128cdf0e10cSrcweir                 = xDAVEnv->getUserRequestHeaders( rURI, rMethod );
1129cdf0e10cSrcweir 
1130cdf0e10cSrcweir             for ( sal_Int32 n = 0; n < aRequestHeaders.getLength(); ++n )
1131cdf0e10cSrcweir             {
1132cdf0e10cSrcweir                 rtl::OUString aValue;
1133cdf0e10cSrcweir                 sal_Bool isString = aRequestHeaders[ n ].Value >>= aValue;
1134cdf0e10cSrcweir 
1135cdf0e10cSrcweir                 if ( !isString )
1136cdf0e10cSrcweir                 {
1137cdf0e10cSrcweir                     OSL_ENSURE( isString,
1138cdf0e10cSrcweir                         "DAVResourceAccess::getUserRequestHeaders :"
1139cdf0e10cSrcweir                         "Value is not a string! Ignoring..." );
1140cdf0e10cSrcweir                 }
1141cdf0e10cSrcweir 
1142cdf0e10cSrcweir                 rRequestHeaders.push_back(
1143cdf0e10cSrcweir                     DAVRequestHeader( aRequestHeaders[ n ].Name, aValue ) );
1144cdf0e10cSrcweir             }
1145cdf0e10cSrcweir         }
1146cdf0e10cSrcweir     }
1147cdf0e10cSrcweir }
1148cdf0e10cSrcweir 
1149cdf0e10cSrcweir //=========================================================================
1150cdf0e10cSrcweir sal_Bool DAVResourceAccess::detectRedirectCycle(
1151cdf0e10cSrcweir                                 const rtl::OUString& rRedirectURL )
1152cdf0e10cSrcweir     throw ( DAVException )
1153cdf0e10cSrcweir {
1154cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard( m_aMutex );
1155cdf0e10cSrcweir 
115659ddfc10SAndre Fischer     SerfUri aUri( rRedirectURL );
1157cdf0e10cSrcweir 
115859ddfc10SAndre Fischer     std::vector< SerfUri >::const_iterator it  = m_aRedirectURIs.begin();
115959ddfc10SAndre Fischer     std::vector< SerfUri >::const_iterator end = m_aRedirectURIs.end();
1160cdf0e10cSrcweir 
1161cdf0e10cSrcweir     while ( it != end )
1162cdf0e10cSrcweir     {
1163cdf0e10cSrcweir         if ( aUri == (*it) )
1164cdf0e10cSrcweir             return sal_True;
1165cdf0e10cSrcweir 
1166cdf0e10cSrcweir         it++;
1167cdf0e10cSrcweir     }
1168cdf0e10cSrcweir 
1169cdf0e10cSrcweir     return sal_False;
1170cdf0e10cSrcweir }
1171cdf0e10cSrcweir 
1172cdf0e10cSrcweir //=========================================================================
1173cdf0e10cSrcweir void DAVResourceAccess::resetUri()
1174cdf0e10cSrcweir {
1175cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard( m_aMutex );
1176cdf0e10cSrcweir     if ( m_aRedirectURIs.size() > 0 )
1177cdf0e10cSrcweir     {
117859ddfc10SAndre Fischer         std::vector< SerfUri >::const_iterator it  = m_aRedirectURIs.begin();
1179cdf0e10cSrcweir 
118059ddfc10SAndre Fischer         SerfUri aUri( (*it) );
1181cdf0e10cSrcweir         m_aRedirectURIs.clear();
1182cdf0e10cSrcweir         setURL ( aUri.GetURI() );
1183cdf0e10cSrcweir         initialize();
1184cdf0e10cSrcweir     }
1185cdf0e10cSrcweir }
1186cdf0e10cSrcweir 
1187cdf0e10cSrcweir //=========================================================================
1188cdf0e10cSrcweir sal_Bool DAVResourceAccess::handleException( DAVException & e, int errorCount )
1189cdf0e10cSrcweir     throw ( DAVException )
1190cdf0e10cSrcweir {
1191cdf0e10cSrcweir     switch ( e.getError() )
1192cdf0e10cSrcweir     {
1193cdf0e10cSrcweir     case DAVException::DAV_HTTP_REDIRECT:
1194cdf0e10cSrcweir         if ( !detectRedirectCycle( e.getData() ) )
1195cdf0e10cSrcweir         {
1196cdf0e10cSrcweir             // set new URL and path.
1197cdf0e10cSrcweir             setURL( e.getData() );
1198cdf0e10cSrcweir             initialize();
1199cdf0e10cSrcweir             return sal_True;
1200cdf0e10cSrcweir         }
1201cdf0e10cSrcweir         return sal_False;
1202cdf0e10cSrcweir     // --> tkr #67048# copy & paste images doesn't display.
1203cdf0e10cSrcweir     // if we have a bad connection try again. Up to three times.
1204cdf0e10cSrcweir     case DAVException::DAV_HTTP_ERROR:
1205cdf0e10cSrcweir         // retry up to three times, if not a client-side error.
120649989859SOliver-Rainer Wittmann         if ( ( e.getStatus() < 400 || e.getStatus() >= 500 ||
120749989859SOliver-Rainer Wittmann                e.getStatus() == 413 ) &&
1208cdf0e10cSrcweir              errorCount < 3 )
1209cdf0e10cSrcweir         {
1210cdf0e10cSrcweir             return sal_True;
1211cdf0e10cSrcweir         }
1212cdf0e10cSrcweir         return sal_False;
1213cdf0e10cSrcweir     // <--
1214cdf0e10cSrcweir     // --> tkr: if connection has said retry then retry!
1215cdf0e10cSrcweir     case DAVException::DAV_HTTP_RETRY:
1216cdf0e10cSrcweir         return sal_True;
1217cdf0e10cSrcweir     // <--
1218cdf0e10cSrcweir     default:
1219cdf0e10cSrcweir         return sal_False; // Abort
1220cdf0e10cSrcweir     }
1221cdf0e10cSrcweir }
1222