Kaydet (Commit) b641d83b authored tarafından Giuseppe Castagno's avatar Giuseppe Castagno

tdf#101094 (10) OPTIONS: Add a simple options cache class

Added behavioral unit tests as well.

Change-Id: I30f84c8f814d3460a421428ebe0d2fbc32c5c433
Reviewed-on: https://gerrit.libreoffice.org/27668Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarGiuseppe Castagno <giuseppe.castagno@acca-esse.eu>
üst 389d4d41
......@@ -29,12 +29,15 @@ namespace
void DAVTypesCheckReset( webdav_ucp::DAVOptions aDavType );
void DAVTypesTest();
void DAVOptsCacheTests();
// Change the following lines only, if you add, remove or rename
// member functions of the current class,
// because these macros are need by auto register mechanism.
CPPUNIT_TEST_SUITE( webdav_opts_test );
CPPUNIT_TEST( DAVTypesTest );
CPPUNIT_TEST( DAVOptsCacheTests );
CPPUNIT_TEST_SUITE_END();
}; // class webdav_local_test
......@@ -247,6 +250,31 @@ namespace
CPPUNIT_ASSERT_EQUAL( false , aDavOpt == aDavOptTarget );
}
void webdav_opts_test::DAVOptsCacheTests()
{
// define a local cache to test
webdav_ucp::DAVOptionsCache aDAVOptsCache;
// the value to cache
webdav_ucp::DAVOptions aDavOpt;
// the returned value to test
webdav_ucp::DAVOptions aDavOptCached;
// init the values
OUString aAllowedMethods = "OPTIONS,GET,HEAD,POST,DELETE,TRACE,PROPFIND,PROPPATCH,COPY,MOVE,PUT,LOCK,UNLOCK";
OUString aURL = "http://a%20fake%20url/to%20test/another-url";
OUString aRedirectedURL = "http://a%20fake%20url/to%20test/another-url/redirected";
aDavOpt.setURL( aURL );
aDavOpt.setRedirectedURL( aRedirectedURL );
aDavOpt.setResourceFound();
aDavOpt.setClass1();
aDavOpt.setClass2();
aDavOpt.setClass3();
aDavOpt.setAllowedMethods( aAllowedMethods );
// add to cache
aDAVOptsCache.addDAVOptions( aDavOpt, 30000 );
CPPUNIT_ASSERT_EQUAL( true ,aDAVOptsCache.getDAVOptions( aURL, aDavOptCached ) );
CPPUNIT_ASSERT_EQUAL( true , aDavOpt == aDavOptCached );
}
CPPUNIT_TEST_SUITE_REGISTRATION( webdav_opts_test );
} // namespace rtl_random
......
......@@ -17,7 +17,7 @@
using namespace webdav_ucp;
using namespace com::sun::star;
// DAVCapabilities implementation
// DAVOptions implementation
DAVOptions::DAVOptions() :
m_isResourceFound( false ),
......@@ -64,4 +64,82 @@ bool DAVOptions::operator==( const DAVOptions& rOpts ) const
}
// DAVOptionsCache implementation
DAVOptionsCache::DAVOptionsCache()
{
}
DAVOptionsCache::~DAVOptionsCache()
{
}
bool DAVOptionsCache::getDAVOptions( const OUString & rURL, DAVOptions & rDAVOptions )
{
osl::MutexGuard aGuard( m_aMutex );
OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL ) ) );
normalizeURLLastChar( aEncodedUrl );
// search the URL in the static map
DAVOptionsMap::iterator it;
it = m_aTheCache.find( aEncodedUrl );
if ( it == m_aTheCache.end() )
return false;
else
{
// check if the capabilities are stale, before restoring
TimeValue t1;
osl_getSystemTime( &t1 );
if ( (*it).second.getStaleTime() < t1.Seconds )
{
// if stale, remove from cache, do not restore
removeDAVOptions( rURL );
return false;
// return false instead
}
rDAVOptions = (*it).second;
return true;
}
}
void DAVOptionsCache::removeDAVOptions( const OUString & rURL )
{
osl::MutexGuard aGuard( m_aMutex );
OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL ) ) );
normalizeURLLastChar( aEncodedUrl );
DAVOptionsMap::iterator it;
it = m_aTheCache.find( aEncodedUrl );
if ( it != m_aTheCache.end() )
{
m_aTheCache.erase( it );
}
}
void DAVOptionsCache::addDAVOptions( DAVOptions & rDAVOptions, const sal_uInt32 nLifeTime )
{
osl::MutexGuard aGuard( m_aMutex );
OUString aURL( rDAVOptions.getURL() );
OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( aURL ) ) );
normalizeURLLastChar( aEncodedUrl );
rDAVOptions.setURL( aEncodedUrl );
// unchanged, it may be used to access a server
OUString aRedirURL( rDAVOptions.getRedirectedURL() );
rDAVOptions.setRedirectedURL( aRedirURL );
TimeValue t1;
osl_getSystemTime( &t1 );
rDAVOptions.setStaleTime( t1.Seconds + nLifeTime );
m_aTheCache[ aEncodedUrl ] = rDAVOptions;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
......@@ -137,6 +137,30 @@ namespace webdav_ucp
};
typedef std::map< OUString, DAVOptions > DAVOptionsMap;
class DAVOptionsCache
{
DAVOptionsMap m_aTheCache;
osl::Mutex m_aMutex;
public:
explicit DAVOptionsCache();
~DAVOptionsCache();
bool getDAVOptions( const OUString & rURL, DAVOptions & rDAVOptions );
void removeDAVOptions( const OUString & rURL );
void addDAVOptions( DAVOptions & rDAVOptions, const sal_uInt32 nLifeTime );
private:
/// remove the last '/' in aUrl, if it exists
static void normalizeURLLastChar( OUString& aUrl ) {
if ( aUrl.getLength() > 1 &&
( ( aUrl.lastIndexOf( '/' ) + 1 ) == aUrl.getLength() ) )
aUrl = aUrl.copy(0, aUrl.getLength() - 1 );
};
};
enum Depth { DAVZERO = 0, DAVONE = 1, DAVINFINITY = -1 };
enum ProppatchOperation { PROPSET = 0, PROPREMOVE = 1 };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment