diff options
author | Giuseppe Castagno <giuseppe.castagno@acca-esse.eu> | 2016-01-10 10:05:02 +0100 |
---|---|---|
committer | Giuseppe Castagno <giuseppe.castagno@acca-esse.eu> | 2016-07-29 11:45:01 +0000 |
commit | b641d83bb9f8adba1a487ca0e04d7151f96c3eea (patch) | |
tree | d07c2cfa3796d1c36441d666df4fac92197c3e28 | |
parent | 389d4d414291879b9097658080e405a06dc0c1fc (diff) |
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/27668
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Giuseppe Castagno <giuseppe.castagno@acca-esse.eu>
-rw-r--r-- | ucb/qa/cppunit/webdav/webdav_options.cxx | 28 | ||||
-rw-r--r-- | ucb/source/ucp/webdav-neon/DAVTypes.cxx | 80 | ||||
-rw-r--r-- | ucb/source/ucp/webdav-neon/DAVTypes.hxx | 24 |
3 files changed, 131 insertions, 1 deletions
diff --git a/ucb/qa/cppunit/webdav/webdav_options.cxx b/ucb/qa/cppunit/webdav/webdav_options.cxx index dd7ad24f8755..7c0ec7b54ddc 100644 --- a/ucb/qa/cppunit/webdav/webdav_options.cxx +++ b/ucb/qa/cppunit/webdav/webdav_options.cxx @@ -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 diff --git a/ucb/source/ucp/webdav-neon/DAVTypes.cxx b/ucb/source/ucp/webdav-neon/DAVTypes.cxx index 1fbf84d09821..c0eb4eaadcf7 100644 --- a/ucb/source/ucp/webdav-neon/DAVTypes.cxx +++ b/ucb/source/ucp/webdav-neon/DAVTypes.cxx @@ -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: */ diff --git a/ucb/source/ucp/webdav-neon/DAVTypes.hxx b/ucb/source/ucp/webdav-neon/DAVTypes.hxx index b1499878e4d7..f9bbb1f832a5 100644 --- a/ucb/source/ucp/webdav-neon/DAVTypes.hxx +++ b/ucb/source/ucp/webdav-neon/DAVTypes.hxx @@ -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 }; |