changed try-catch formatting

added deletePreferencesItem() to authentication module
This commit is contained in:
fgerlits 2005-01-13 14:43:42 +00:00
parent 5c5e8c7de9
commit db0fcb87fb
14 changed files with 261 additions and 77 deletions

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $
Version : $Revision: 1.6 $
Version : $Revision: 1.7 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/authentication/include/LiveSupport/Authentication/AuthenticationClientInterface.h,v $
------------------------------------------------------------------------------*/
@ -69,7 +69,7 @@ using namespace LiveSupport::Core;
* An interface for authentication clients.
*
* @author $Author: fgerlits $
* @version $Revision: 1.6 $
* @version $Revision: 1.7 $
*/
class AuthenticationClientInterface
{
@ -162,6 +162,29 @@ class AuthenticationClientInterface
Ptr<const Glib::ustring>::Ref value)
throw (XmlRpcException)
= 0;
/**
* Delete a `user preferences' item from the server.
*
* @param sessionId the ID of the current session (from login())
* @param key the name of the item
*
* @exception XmlRpcInvalidArgumentException
* bad sessionId argument
* @exception XmlRpcCommunicationException
* problem with performing XML-RPC call
* @exception XmlRpcMethodFaultException
* XML-RPC method returned fault response
* @exception XmlRpcMethodResponseException
* response from XML-RPC method is incorrect
* @exception XmlRpcException other error
* (TestAuthenticationClient only)
*/
virtual void
deletePreferencesItem(Ptr<SessionId>::Ref sessionId,
const Glib::ustring & key)
throw (XmlRpcException)
= 0;
};

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $
Version : $Revision: 1.4 $
Version : $Revision: 1.5 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/authentication/src/AuthenticationClientFactoryTest.cxx,v $
------------------------------------------------------------------------------*/
@ -199,7 +199,7 @@ AuthenticationClientFactoryTest :: preferencesTest(void)
}
CPPUNIT_ASSERT(*newPrefValue == "страстные");
// check another normal save and load
// check another normal save and load...
prefValue.reset(new const Glib::ustring("ne dobryj"));
try {
authentication->savePreferencesItem(sessionId, "hour", prefValue);
@ -207,6 +207,14 @@ AuthenticationClientFactoryTest :: preferencesTest(void)
CPPUNIT_FAIL(e.what());
}
// ... but now change session ID in the middle
try {
authentication->logout(sessionId);
sessionId = authentication->login("root", "q");
} catch (XmlRpcException &e) {
CPPUNIT_FAIL(e.what());
}
try {
newPrefValue = authentication->loadPreferencesItem(sessionId, "hour");
} catch (XmlRpcException &e) {
@ -214,6 +222,19 @@ AuthenticationClientFactoryTest :: preferencesTest(void)
}
CPPUNIT_ASSERT(*newPrefValue == *prefValue);
// check the delete method
try {
authentication->deletePreferencesItem(sessionId, "hour");
} catch (XmlRpcException &e) {
CPPUNIT_FAIL(e.what());
}
try {
newPrefValue = authentication->loadPreferencesItem(sessionId, "hour");
CPPUNIT_FAIL("Allowed to load preference after it was deleted");
} catch (XmlRpcException &e) {
}
// and log out
try {
authentication->logout(sessionId);

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $
Version : $Revision: 1.5 $
Version : $Revision: 1.6 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/authentication/src/TestAuthenticationClient.cxx,v $
------------------------------------------------------------------------------*/
@ -168,8 +168,7 @@ TestAuthenticationClient :: login(const std::string & login,
sessionIdList.insert(sessionIdStream.str());
sessionId.reset(new SessionId(sessionIdStream.str()));
return sessionId;
}
else {
} else {
throw XmlRpcException("incorrect login or password");
}
}
@ -185,8 +184,7 @@ TestAuthenticationClient :: logout(Ptr<SessionId>::Ref sessionId)
// this returns the number of entries found and erased
if (!sessionId || sessionIdList.erase(sessionId->getId())) {
return;
}
else {
} else {
throw XmlRpcException("logout() called without previous login()");
}
}
@ -203,10 +201,10 @@ TestAuthenticationClient :: loadPreferencesItem(
{
if (!sessionId
|| sessionIdList.find(sessionId->getId()) == sessionIdList.end()) {
throw XmlRpcException("loadPreferences() called before login()");
throw XmlRpcException("bad session ID");
}
preferencesType::iterator it;
PreferencesType::iterator it;
if ((it = preferences.find(key)) == preferences.end()) {
throw XmlRpcException("no such user preferences item");
@ -227,6 +225,11 @@ TestAuthenticationClient :: savePreferencesItem(
Ptr<const Glib::ustring>::Ref value)
throw (XmlRpcException)
{
if (!sessionId
|| sessionIdList.find(sessionId->getId()) == sessionIdList.end()) {
throw XmlRpcException("bad session ID");
}
if (sessionIdList.find(sessionId->getId()) == sessionIdList.end()) {
throw XmlRpcException("loadPreferences() called before login()");
}
@ -234,3 +237,27 @@ TestAuthenticationClient :: savePreferencesItem(
preferences[key] = value;
}
/*------------------------------------------------------------------------------
* Delete a `user preferences' item from the server.
*----------------------------------------------------------------------------*/
void
TestAuthenticationClient :: deletePreferencesItem(
Ptr<SessionId>::Ref sessionId,
const Glib::ustring & key)
throw (XmlRpcException)
{
if (!sessionId
|| sessionIdList.find(sessionId->getId()) == sessionIdList.end()) {
throw XmlRpcException("bad session ID");
}
PreferencesType::iterator it;
if ((it = preferences.find(key)) == preferences.end()) {
throw XmlRpcException("no such user preferences item");
}
preferences.erase(it);
}

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $
Version : $Revision: 1.7 $
Version : $Revision: 1.8 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/authentication/src/TestAuthenticationClient.h,v $
------------------------------------------------------------------------------*/
@ -65,7 +65,10 @@ using namespace LiveSupport::Core;
/* =============================================================== data types */
/**
* A dummy authentication client.
* A dummy authentication client. It only supports one user, whose name and
* password are read from a configuration file. It issues session IDs when
* login() is called, and deletes these session IDs when logout() is called.
* It also stores (key, value) pairs of user preferences for this one user.
*
* This object has to be configured with an XML configuration element
* called testAuthentication. This element contains a child element
@ -92,7 +95,7 @@ using namespace LiveSupport::Core;
* </code></pre>
*
* @author $Author: fgerlits $
* @version $Revision: 1.7 $
* @version $Revision: 1.8 $
*/
class TestAuthenticationClient :
virtual public Configurable,
@ -119,12 +122,12 @@ class TestAuthenticationClient :
* A type for the list of sessionId's.
*/
typedef std::set<std::string>
sessionIdListType;
SessionIdListType;
/**
* A list of the sessionId's we have issued.
*/
sessionIdListType sessionIdList;
SessionIdListType sessionIdList;
/**
* The number of the sessionId's we have issued.
@ -135,12 +138,12 @@ class TestAuthenticationClient :
* A type for the list of user preferences.
*/
typedef std::map<const Glib::ustring, Ptr<const Glib::ustring>::Ref>
preferencesType;
PreferencesType;
/**
* A list of the user preferences items stored.
*/
preferencesType preferences;
PreferencesType preferences;
public:
@ -228,6 +231,18 @@ class TestAuthenticationClient :
const Glib::ustring & key,
Ptr<const Glib::ustring>::Ref value)
throw (XmlRpcException);
/**
* Delete a `user preferences' item from the server.
*
* @param sessionId the ID of the current session (from login())
* @param key the name of the item
* @exception XmlRpcException invalid session ID, or no such key
*/
virtual void
deletePreferencesItem(Ptr<SessionId>::Ref sessionId,
const Glib::ustring & key)
throw (XmlRpcException);
};

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $
Version : $Revision: 1.5 $
Version : $Revision: 1.6 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/authentication/src/TestAuthenticationClientTest.cxx,v $
------------------------------------------------------------------------------*/
@ -207,7 +207,7 @@ TestAuthenticationClientTest :: preferencesTest(void)
}
CPPUNIT_ASSERT(*newPrefValue == "страстные");
// check another normal save and load
// check another normal save and load...
prefValue.reset(new const Glib::ustring("ne dobryj"));
try {
tac->savePreferencesItem(sessionId, "hour", prefValue);
@ -215,6 +215,14 @@ TestAuthenticationClientTest :: preferencesTest(void)
CPPUNIT_FAIL(e.what());
}
// ... but now change session ID in the middle
try {
tac->logout(sessionId);
sessionId = tac->login("root", "q");
} catch (XmlRpcException &e) {
CPPUNIT_FAIL(e.what());
}
try {
newPrefValue = tac->loadPreferencesItem(sessionId, "hour");
} catch (XmlRpcException &e) {
@ -222,6 +230,19 @@ TestAuthenticationClientTest :: preferencesTest(void)
}
CPPUNIT_ASSERT(*newPrefValue == *prefValue);
// check the delete method
try {
tac->deletePreferencesItem(sessionId, "hour");
} catch (XmlRpcException &e) {
CPPUNIT_FAIL(e.what());
}
try {
newPrefValue = tac->loadPreferencesItem(sessionId, "hour");
CPPUNIT_FAIL("Allowed to load preference after it was deleted");
} catch (XmlRpcException &e) {
}
// and log out
try {
tac->logout(sessionId);

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $
Version : $Revision: 1.7 $
Version : $Revision: 1.8 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/authentication/src/WebAuthenticationClient.cxx,v $
------------------------------------------------------------------------------*/
@ -139,6 +139,11 @@ static const std::string loadPreferencesMethodName = "locstor.loadPref";
*----------------------------------------------------------------------------*/
static const std::string savePreferencesMethodName = "locstor.savePref";
/*------------------------------------------------------------------------------
* The name of the delete preferences method on the server
*----------------------------------------------------------------------------*/
static const std::string deletePreferencesMethodName = "locstor.delPref";
/*------------------------------------------------------------------------------
* The name of the session ID parameter in the input structure
*----------------------------------------------------------------------------*/
@ -450,6 +455,59 @@ WebAuthenticationClient :: savePreferencesItem(
}
/*------------------------------------------------------------------------------
* Delete a `user preferences' item from the server.
*----------------------------------------------------------------------------*/
void
WebAuthenticationClient :: deletePreferencesItem(
Ptr<SessionId>::Ref sessionId,
const Glib::ustring & key)
throw (XmlRpcException)
{
if (!sessionId) {
throw Core::XmlRpcInvalidArgumentException("Missing session ID.");
}
XmlRpcValue parameters;
XmlRpcValue result;
XmlRpcClient xmlRpcClient(storageServerName.c_str(), storageServerPort,
storageServerPath.c_str(), false);
parameters.clear();
parameters[preferencesSessionIdParamName] = sessionId->getId();
parameters[preferencesKeyParamName] = std::string(key);
result.clear();
if (!xmlRpcClient.execute(deletePreferencesMethodName.c_str(),
parameters, result)) {
throw Core::XmlRpcCommunicationException(
"Could not execute XML-RPC method.");
}
if (xmlRpcClient.isFault()) {
std::stringstream eMsg;
eMsg << "XML-RPC method "
<< deletePreferencesMethodName
<< " returned fault response:\n"
<< result;
throw Core::XmlRpcMethodFaultException(eMsg.str());
}
if (! result.hasMember(preferencesStatusParamName)
|| result[preferencesStatusParamName].getType()
!= XmlRpcValue::TypeBoolean
|| ! bool(result[preferencesStatusParamName])) {
std::stringstream eMsg;
eMsg << "XML-RPC method "
<< deletePreferencesMethodName
<< " returned unexpected response:\n"
<< result;
throw Core::XmlRpcMethodResponseException(eMsg.str());
}
}
/*------------------------------------------------------------------------------
* Reset the list of preferences to its initial (empty) state.
*----------------------------------------------------------------------------*/

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $
Version : $Revision: 1.4 $
Version : $Revision: 1.5 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/authentication/src/WebAuthenticationClient.h,v $
------------------------------------------------------------------------------*/
@ -93,7 +93,7 @@ using namespace LiveSupport::Core;
* </code></pre>
*
* @author $Author: fgerlits $
* @version $Revision: 1.4 $
* @version $Revision: 1.5 $
*/
class WebAuthenticationClient :
virtual public Configurable,
@ -236,6 +236,26 @@ class WebAuthenticationClient :
Ptr<const Glib::ustring>::Ref value)
throw (XmlRpcException);
/**
* Delete a `user preferences' item from the server.
*
* @param sessionId the ID of the current session (from login())
* @param key the name of the item
*
* @exception XmlRpcInvalidArgumentException
* bad sessionId argument
* @exception XmlRpcCommunicationException
* problem with performing XML-RPC call
* @exception XmlRpcMethodFaultException
* XML-RPC method returned fault response
* @exception XmlRpcMethodResponseException
* response from XML-RPC method is incorrect
*/
virtual void
deletePreferencesItem(Ptr<SessionId>::Ref sessionId,
const Glib::ustring & key)
throw (XmlRpcException);
/**
* Reset the list of preferences to its initial (empty) state.
*

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $
Version : $Revision: 1.8 $
Version : $Revision: 1.9 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/authentication/src/WebAuthenticationClientTest.cxx,v $
------------------------------------------------------------------------------*/
@ -212,7 +212,7 @@ WebAuthenticationClientTest :: preferencesTest(void)
}
CPPUNIT_ASSERT(*newPrefValue == "страстные");
// check another normal save and load
// check another normal save and load ...
prefValue.reset(new const Glib::ustring("ne dobryj"));
try {
wac->savePreferencesItem(sessionId, "hour", prefValue);
@ -220,6 +220,14 @@ WebAuthenticationClientTest :: preferencesTest(void)
CPPUNIT_FAIL(e.what());
}
// ... but now change session ID in the middle
try {
wac->logout(sessionId);
sessionId = wac->login("root", "q");
} catch (XmlRpcException &e) {
CPPUNIT_FAIL(e.what());
}
try {
newPrefValue = wac->loadPreferencesItem(sessionId, "hour");
} catch (XmlRpcException &e) {
@ -227,6 +235,19 @@ WebAuthenticationClientTest :: preferencesTest(void)
}
CPPUNIT_ASSERT(*newPrefValue == *prefValue);
// check the delete method
try {
wac->deletePreferencesItem(sessionId, "hour");
} catch (XmlRpcException &e) {
CPPUNIT_FAIL(e.what());
}
try {
newPrefValue = wac->loadPreferencesItem(sessionId, "hour");
CPPUNIT_FAIL("Allowed to load preference after it was deleted");
} catch (XmlRpcException &e) {
}
// and log out
try {
wac->logout(sessionId);