LibreOffice Module ucb (master)  1
DAVTypes.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 
11 #include <osl/time.h>
12 
13 #include "DAVTypes.hxx"
14 #include "../inc/urihelper.hxx"
15 #include "NeonUri.hxx"
16 
17 using namespace webdav_ucp;
18 using namespace com::sun::star;
19 
20 // DAVOptions implementation
21 
23  m_isClass1( false ),
24  m_isClass2( false ),
25  m_isClass3( false ),
26  m_isHeadAllowed( true ),
27  m_isLocked( false ),
28  m_aAllowedMethods(),
29  m_nStaleTime( 0 ),
30  m_nRequestedTimeLife( 0 ),
31  m_sURL(),
32  m_sRedirectedURL(),
33  m_nHttpResponseStatusCode( 0 ),
34  m_sHttpResponseStatusText()
35 {
36 }
37 
39  m_isClass1( rOther.m_isClass1 ),
40  m_isClass2( rOther.m_isClass2 ),
41  m_isClass3( rOther.m_isClass3 ),
42  m_isHeadAllowed( rOther.m_isHeadAllowed ),
43  m_isLocked( rOther.m_isLocked ),
44  m_aAllowedMethods( rOther.m_aAllowedMethods ),
45  m_nStaleTime( rOther.m_nStaleTime ),
46  m_nRequestedTimeLife( rOther.m_nRequestedTimeLife ),
47  m_sURL( rOther.m_sURL ),
48  m_sRedirectedURL( rOther.m_sRedirectedURL),
49  m_nHttpResponseStatusCode( rOther.m_nHttpResponseStatusCode ),
50  m_sHttpResponseStatusText( rOther.m_sHttpResponseStatusText )
51 {
52 }
53 
55 {
56 }
57 
59 {
60  m_isClass1 = rOpts.m_isClass1;
61  m_isClass2 = rOpts.m_isClass2;
62  m_isClass3 = rOpts.m_isClass3;
63  m_isLocked = rOpts.m_isLocked;
66  m_nStaleTime = rOpts.m_nStaleTime;
68  m_sURL = rOpts.m_sURL;
72  return *this;
73 }
74 
75 bool DAVOptions::operator==( const DAVOptions& rOpts ) const
76 {
77  return
78  m_isClass1 == rOpts.m_isClass1 &&
79  m_isClass2 == rOpts.m_isClass2 &&
80  m_isClass3 == rOpts.m_isClass3 &&
81  m_isLocked == rOpts.m_isLocked &&
84  m_nStaleTime == rOpts.m_nStaleTime &&
86  m_sURL == rOpts.m_sURL &&
90 }
91 
92 
93 // DAVOptionsCache implementation
94 
96 {
97 }
98 
100 {
101 }
102 
103 bool DAVOptionsCache::getDAVOptions( const OUString & rURL, DAVOptions & rDAVOptions )
104 {
105  osl::MutexGuard aGuard( m_aMutex );
106  OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL ) ) );
107  normalizeURLLastChar( aEncodedUrl );
108 
109  // search the URL in the static map
110  DAVOptionsMap::iterator it = m_aTheCache.find( aEncodedUrl );
111  if ( it == m_aTheCache.end() )
112  return false;
113  else
114  {
115  // check if the capabilities are stale, before restoring
116  TimeValue t1;
117  osl_getSystemTime( &t1 );
118  if ( (*it).second.getStaleTime() < t1.Seconds )
119  {
120  // if stale, remove from cache, do not restore
121  m_aTheCache.erase( it );
122  return false;
123  // return false instead
124  }
125  rDAVOptions = (*it).second;
126  return true;
127  }
128 }
129 
130 void DAVOptionsCache::removeDAVOptions( const OUString & rURL )
131 {
132  osl::MutexGuard aGuard( m_aMutex );
133  OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL ) ) );
134  normalizeURLLastChar( aEncodedUrl );
135 
136  DAVOptionsMap::iterator it = m_aTheCache.find( aEncodedUrl );
137  if ( it != m_aTheCache.end() )
138  {
139  m_aTheCache.erase( it );
140  }
141 }
142 
143 void DAVOptionsCache::addDAVOptions( DAVOptions & rDAVOptions, const sal_uInt32 nLifeTime )
144 {
145  osl::MutexGuard aGuard( m_aMutex );
146  OUString aURL( rDAVOptions.getURL() );
147 
148  OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( aURL ) ) );
149  normalizeURLLastChar( aEncodedUrl );
150  rDAVOptions.setURL( aEncodedUrl );
151 
152 // unchanged, it may be used to access a server
153  OUString aRedirURL( rDAVOptions.getRedirectedURL() );
154  rDAVOptions.setRedirectedURL( aRedirURL );
155 
156  // check if already cached
157  DAVOptionsMap::iterator it = m_aTheCache.find( aEncodedUrl );
158  if ( it != m_aTheCache.end() )
159  { // already in cache, check LifeTime
160  if ( (*it).second.getRequestedTimeLife() == nLifeTime )
161  return; // same lifetime, do nothing
162  }
163  // not in cache, add it
164  TimeValue t1;
165  osl_getSystemTime( &t1 );
166  rDAVOptions.setStaleTime( t1.Seconds + nLifeTime );
167 
168  m_aTheCache[ aEncodedUrl ] = rDAVOptions;
169 }
170 
171 void DAVOptionsCache::setHeadAllowed( const OUString & rURL, const bool HeadAllowed )
172 {
173  osl::MutexGuard aGuard( m_aMutex );
174  OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL ) ) );
175  normalizeURLLastChar( aEncodedUrl );
176 
177  DAVOptionsMap::iterator it = m_aTheCache.find( aEncodedUrl );
178  if ( it != m_aTheCache.end() )
179  {
180  // first check for stale
181  TimeValue t1;
182  osl_getSystemTime( &t1 );
183  if( (*it).second.getStaleTime() < t1.Seconds )
184  {
185  m_aTheCache.erase( it );
186  return;
187  }
188  // check if the resource was present on server
189  (*it).second.setHeadAllowed( HeadAllowed );
190  }
191 }
192 
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
sal_uInt32 m_nRequestedTimeLife
void setURL(const OUString &sURL)
static OUString unescape(const OUString &string)
Definition: NeonUri.cxx:257
void setStaleTime(const sal_uInt32 nStaleTime)
void removeDAVOptions(const OUString &rURL)
Definition: DAVTypes.cxx:130
void addDAVOptions(DAVOptions &rDAVOptions, const sal_uInt32 nLifeTime)
Definition: DAVTypes.cxx:143
void setHeadAllowed(const OUString &rURL, bool HeadAllowed=true)
Definition: DAVTypes.cxx:171
OUString encodeURI(const OUString &rURI)
Definition: urihelper.hxx:45
sal_uInt32 m_nStaleTime
target time when this capability becomes stale
OUString m_sHttpResponseStatusText
The cached string with the server returned HTTP response status code string, corresponds to m_nHttpRe...
static void normalizeURLLastChar(OUString &aUrl)
remove the last '/' in aUrl, if it exists
sal_uInt16 m_nHttpResponseStatusCode
The cached HTT response status code. It's 0 if the code was dealt with and there is no need to cache ...
bool getDAVOptions(const OUString &rURL, DAVOptions &rDAVOptions)
Definition: DAVTypes.cxx:103
bool m_isHeadAllowed
for server that do not implement it
void setRedirectedURL(const OUString &sRedirectedURL)
OUString m_sURL
const OUString & getRedirectedURL() const
DAVOptions & operator=(const DAVOptions &rOpts)
Definition: DAVTypes.cxx:58
const OUString & getURL() const
OUString m_aAllowedMethods
contains the methods allowed on this resource
bool m_isLocked
Internally used to maintain the locked state of the resource, only if it's a Class 2 resource...
bool operator==(const DAVOptions &rOpts) const
Definition: DAVTypes.cxx:75