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_nStaleTime( 0 ),
29  m_nRequestedTimeLife( 0 ),
30  m_nHttpResponseStatusCode( 0 )
31 {
32 }
33 
35  m_isClass1( rOther.m_isClass1 ),
36  m_isClass2( rOther.m_isClass2 ),
37  m_isClass3( rOther.m_isClass3 ),
38  m_isHeadAllowed( rOther.m_isHeadAllowed ),
39  m_isLocked( rOther.m_isLocked ),
40  m_aAllowedMethods( rOther.m_aAllowedMethods ),
41  m_nStaleTime( rOther.m_nStaleTime ),
42  m_nRequestedTimeLife( rOther.m_nRequestedTimeLife ),
43  m_sURL( rOther.m_sURL ),
44  m_sRedirectedURL( rOther.m_sRedirectedURL),
45  m_nHttpResponseStatusCode( rOther.m_nHttpResponseStatusCode ),
46  m_sHttpResponseStatusText( rOther.m_sHttpResponseStatusText )
47 {
48 }
49 
51 {
52 }
53 
55 {
56  m_isClass1 = rOpts.m_isClass1;
57  m_isClass2 = rOpts.m_isClass2;
58  m_isClass3 = rOpts.m_isClass3;
59  m_isLocked = rOpts.m_isLocked;
62  m_nStaleTime = rOpts.m_nStaleTime;
64  m_sURL = rOpts.m_sURL;
68  return *this;
69 }
70 
71 bool DAVOptions::operator==( const DAVOptions& rOpts ) const
72 {
73  return
74  m_isClass1 == rOpts.m_isClass1 &&
75  m_isClass2 == rOpts.m_isClass2 &&
76  m_isClass3 == rOpts.m_isClass3 &&
77  m_isLocked == rOpts.m_isLocked &&
80  m_nStaleTime == rOpts.m_nStaleTime &&
82  m_sURL == rOpts.m_sURL &&
86 }
87 
88 
89 // DAVOptionsCache implementation
90 
92 {
93 }
94 
96 {
97 }
98 
99 bool DAVOptionsCache::getDAVOptions( const OUString & rURL, DAVOptions & rDAVOptions )
100 {
101  osl::MutexGuard aGuard( m_aMutex );
102  OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL ) ) );
103  normalizeURLLastChar( aEncodedUrl );
104 
105  // search the URL in the static map
106  DAVOptionsMap::iterator it = m_aTheCache.find( aEncodedUrl );
107  if ( it == m_aTheCache.end() )
108  return false;
109  else
110  {
111  // check if the capabilities are stale, before restoring
112  TimeValue t1;
113  osl_getSystemTime( &t1 );
114  if ( (*it).second.getStaleTime() < t1.Seconds )
115  {
116  // if stale, remove from cache, do not restore
117  m_aTheCache.erase( it );
118  return false;
119  // return false instead
120  }
121  rDAVOptions = (*it).second;
122  return true;
123  }
124 }
125 
126 void DAVOptionsCache::removeDAVOptions( const OUString & rURL )
127 {
128  osl::MutexGuard aGuard( m_aMutex );
129  OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL ) ) );
130  normalizeURLLastChar( aEncodedUrl );
131 
132  DAVOptionsMap::iterator it = m_aTheCache.find( aEncodedUrl );
133  if ( it != m_aTheCache.end() )
134  {
135  m_aTheCache.erase( it );
136  }
137 }
138 
139 void DAVOptionsCache::addDAVOptions( DAVOptions & rDAVOptions, const sal_uInt32 nLifeTime )
140 {
141  osl::MutexGuard aGuard( m_aMutex );
142  OUString aURL( rDAVOptions.getURL() );
143 
144  OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( aURL ) ) );
145  normalizeURLLastChar( aEncodedUrl );
146  rDAVOptions.setURL( aEncodedUrl );
147 
148 // unchanged, it may be used to access a server
149  OUString aRedirURL( rDAVOptions.getRedirectedURL() );
150  rDAVOptions.setRedirectedURL( aRedirURL );
151 
152  // check if already cached
153  DAVOptionsMap::iterator it = m_aTheCache.find( aEncodedUrl );
154  if ( it != m_aTheCache.end() )
155  { // already in cache, check LifeTime
156  if ( (*it).second.getRequestedTimeLife() == nLifeTime )
157  return; // same lifetime, do nothing
158  }
159  // not in cache, add it
160  TimeValue t1;
161  osl_getSystemTime( &t1 );
162  rDAVOptions.setStaleTime( t1.Seconds + nLifeTime );
163 
164  m_aTheCache[ aEncodedUrl ] = rDAVOptions;
165 }
166 
167 void DAVOptionsCache::setHeadAllowed( const OUString & rURL, const bool HeadAllowed )
168 {
169  osl::MutexGuard aGuard( m_aMutex );
170  OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL ) ) );
171  normalizeURLLastChar( aEncodedUrl );
172 
173  DAVOptionsMap::iterator it = m_aTheCache.find( aEncodedUrl );
174  if ( it == m_aTheCache.end() )
175  return;
176 
177  // first check for stale
178  TimeValue t1;
179  osl_getSystemTime( &t1 );
180  if( (*it).second.getStaleTime() < t1.Seconds )
181  {
182  m_aTheCache.erase( it );
183  return;
184  }
185  // check if the resource was present on server
186  (*it).second.setHeadAllowed( HeadAllowed );
187 }
188 
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
URL aURL
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:126
void addDAVOptions(DAVOptions &rDAVOptions, const sal_uInt32 nLifeTime)
Definition: DAVTypes.cxx:139
void setHeadAllowed(const OUString &rURL, bool HeadAllowed=true)
Definition: DAVTypes.cxx:167
OUString encodeURI(const OUString &rURI)
Definition: urihelper.hxx:44
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:99
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:54
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:71