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;
111  it = m_aTheCache.find( aEncodedUrl );
112  if ( it == m_aTheCache.end() )
113  return false;
114  else
115  {
116  // check if the capabilities are stale, before restoring
117  TimeValue t1;
118  osl_getSystemTime( &t1 );
119  if ( (*it).second.getStaleTime() < t1.Seconds )
120  {
121  // if stale, remove from cache, do not restore
122  m_aTheCache.erase( it );
123  return false;
124  // return false instead
125  }
126  rDAVOptions = (*it).second;
127  return true;
128  }
129 }
130 
131 void DAVOptionsCache::removeDAVOptions( const OUString & rURL )
132 {
133  osl::MutexGuard aGuard( m_aMutex );
134  OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL ) ) );
135  normalizeURLLastChar( aEncodedUrl );
136 
137  DAVOptionsMap::iterator it;
138  it = m_aTheCache.find( aEncodedUrl );
139  if ( it != m_aTheCache.end() )
140  {
141  m_aTheCache.erase( it );
142  }
143 }
144 
145 void DAVOptionsCache::addDAVOptions( DAVOptions & rDAVOptions, const sal_uInt32 nLifeTime )
146 {
147  osl::MutexGuard aGuard( m_aMutex );
148  OUString aURL( rDAVOptions.getURL() );
149 
150  OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( aURL ) ) );
151  normalizeURLLastChar( aEncodedUrl );
152  rDAVOptions.setURL( aEncodedUrl );
153 
154 // unchanged, it may be used to access a server
155  OUString aRedirURL( rDAVOptions.getRedirectedURL() );
156  rDAVOptions.setRedirectedURL( aRedirURL );
157 
158  // check if already cached
159  DAVOptionsMap::iterator it;
160  it = m_aTheCache.find( aEncodedUrl );
161  if ( it != m_aTheCache.end() )
162  { // already in cache, check LifeTime
163  if ( (*it).second.getRequestedTimeLife() == nLifeTime )
164  return; // same lifetime, do nothing
165  }
166  // not in cache, add it
167  TimeValue t1;
168  osl_getSystemTime( &t1 );
169  rDAVOptions.setStaleTime( t1.Seconds + nLifeTime );
170 
171  m_aTheCache[ aEncodedUrl ] = rDAVOptions;
172 }
173 
174 void DAVOptionsCache::setHeadAllowed( const OUString & rURL, const bool HeadAllowed )
175 {
176  osl::MutexGuard aGuard( m_aMutex );
177  OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL ) ) );
178  normalizeURLLastChar( aEncodedUrl );
179 
180  DAVOptionsMap::iterator it;
181  it = m_aTheCache.find( aEncodedUrl );
182  if ( it != m_aTheCache.end() )
183  {
184  // first check for stale
185  TimeValue t1;
186  osl_getSystemTime( &t1 );
187  if( (*it).second.getStaleTime() < t1.Seconds )
188  {
189  m_aTheCache.erase( it );
190  return;
191  }
192  // check if the resource was present on server
193  (*it).second.setHeadAllowed( HeadAllowed );
194  }
195 }
196 
197 /* 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:131
void addDAVOptions(DAVOptions &rDAVOptions, const sal_uInt32 nLifeTime)
Definition: DAVTypes.cxx:145
void setHeadAllowed(const OUString &rURL, bool HeadAllowed=true)
Definition: DAVTypes.cxx:174
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