diff options
| author | Shireesh Anjal <shireesh@gluster.com> | 2011-03-16 12:37:25 +0530 |
|---|---|---|
| committer | Shireesh Anjal <shireesh@gluster.com> | 2011-03-16 12:37:25 +0530 |
| commit | 608ec97c38e95b1e3bfec57daaa0244854c7c3be (patch) | |
| tree | 11723ccfae2ca07f5d35903b870a7b0958c1d817 | |
| parent | ba2140d703464f1dd378da6c5655aaa1d9020414 (diff) | |
Modified to return Status object from user login method instead of AuthStatus. Removed the AuthStatus class.
Signed-off-by: Shireesh Anjal <shireesh@gluster.com>
4 files changed, 35 insertions, 61 deletions
diff --git a/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java b/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java index d7293f87..17fa84a5 100644 --- a/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java +++ b/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java @@ -87,7 +87,7 @@ public abstract class AbstractClient { }
/**
- * Submits given Form to the resource and returns the object received as response
+ * Submits given Form using POST method to the resource and returns the object received as response
* @param responseClass Class of the object expected as response
* @param form Form to be submitted
* @return Object of given class received as response
@@ -99,7 +99,7 @@ public abstract class AbstractClient { }
/**
- * Submits given Form to the given sub-resource and returns the object received as response
+ * Submits given Form using POST method to the given sub-resource and returns the object received as response
* @param subResourceName Name of the sub-resource to which the request is to be posted
* @param responseClass Class of the object expected as response
* @param form Form to be submitted
@@ -112,6 +112,18 @@ public abstract class AbstractClient { }
/**
+ * Submits given Form using PUT method to the given sub-resource and returns the object received as response
+ * @param subResourceName Name of the sub-resource to which the request is to be posted
+ * @param responseClass Class of the object expected as response
+ * @param form Form to be submitted
+ * @return Object of given class received as response
+ */
+ protected Object putRequest(String subResourceName, Class responseClass, Form form) {
+ return resource.path(subResourceName).type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
+ .header("Authorization", authHeader).accept(MediaType.TEXT_XML).put(responseClass, form);
+ }
+
+ /**
* Submits given object to the resource and returns the object received as response
* @param responseClass Class of the object expected as response
* @param requestObject the Object to be submitted
@@ -119,7 +131,7 @@ public abstract class AbstractClient { */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object postObject(Class responseClass, Object requestObject) {
- return resource.type(MediaType.APPLICATION_XML).header(HTTP_HEADER_AUTH, authHeader).accept(MediaType.TEXT_XML)
+ return resource.type(MediaType.TEXT_XML).header(HTTP_HEADER_AUTH, authHeader).accept(MediaType.TEXT_XML)
.post(responseClass, requestObject);
}
diff --git a/com.gluster.storage.management.client/src/com/gluster/storage/management/client/UsersClient.java b/com.gluster.storage.management.client/src/com/gluster/storage/management/client/UsersClient.java index 33f62ec1..3f9b5d61 100644 --- a/com.gluster.storage.management.client/src/com/gluster/storage/management/client/UsersClient.java +++ b/com.gluster.storage.management.client/src/com/gluster/storage/management/client/UsersClient.java @@ -18,34 +18,27 @@ *******************************************************************************/ package com.gluster.storage.management.client; -import com.gluster.storage.management.core.model.AuthStatus; import com.gluster.storage.management.core.model.Status; import com.sun.jersey.api.representation.Form; public class UsersClient extends AbstractClient { private static final String RESOURCE_NAME = "users"; - private static final String PATH_LOGIN = "login"; - private static final String PATH_CHANGE_PASSWORD = "changepassword"; - private static final String QUERY_PARAM_USER = "user"; - private static final String QUERY_PARAM_PASSWORD = "password"; private static final String FORM_PARAM_OLD_PASSWORD = "oldpassword"; private static final String FORM_PARAM_NEW_PASSWORD = "newpassword"; private String user; - private String password; public UsersClient(String serverName, String user, String password) { super(serverName, user, password); this.user = user; - this.password = password; } public boolean authenticate() { - resource = resource.queryParam(QUERY_PARAM_USER, user).queryParam(QUERY_PARAM_PASSWORD, password); try { - AuthStatus authStatus = (AuthStatus) fetchSubResource(user + "/" + PATH_LOGIN, AuthStatus.class); - return authStatus.getIsAuthenticated(); + Status authStatus = (Status) fetchSubResource(user, Status.class); + return authStatus.isSuccess(); } catch(Exception e) { + e.printStackTrace(); return false; } @@ -57,15 +50,23 @@ public class UsersClient extends AbstractClient { Form form = new Form(); form.add(FORM_PARAM_OLD_PASSWORD, oldPassword); form.add(FORM_PARAM_NEW_PASSWORD, newPassword); - Status status = (Status) postRequest(user + "/" + PATH_CHANGE_PASSWORD, Status.class, form); + Status status = (Status) putRequest(user, Status.class, form); return status.isSuccess(); } public static void main(String[] args) { UsersClient authClient = new UsersClient("localhost", "gluster", "gluster"); + + // authenticate user System.out.println(authClient.authenticate()); - //System.out.println(authClient.changePassword("gluster", "gluster2", "gluster")); + + // change password to gluster1 + System.out.println(authClient.changePassword("gluster", "gluster", "gluster1")); + + // change it back to gluster + authClient = new UsersClient("localhost", "gluster", "gluster1"); + System.out.println(authClient.changePassword("gluster", "gluster1", "gluster")); } /* diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/AuthStatus.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/AuthStatus.java deleted file mode 100644 index 1d42f002..00000000 --- a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/AuthStatus.java +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> - * This file is part of Gluster Management Console. - * - * Gluster Management Console is free software; you can redistribute it and/or - * modify it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * Gluster Management Console is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License - * for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see - * <http://www.gnu.org/licenses/>. - *******************************************************************************/ -package com.gluster.storage.management.core.model; - -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement -public class AuthStatus { - private boolean isAuthenticated; - - public boolean getIsAuthenticated() { - return isAuthenticated; - } - - public void setIsAuthenticated(boolean authenticated) { - this.isAuthenticated = authenticated; - } -} diff --git a/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/UsersResource.java b/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/UsersResource.java index 02d1f763..51134317 100644 --- a/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/UsersResource.java +++ b/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/UsersResource.java @@ -20,11 +20,10 @@ package com.gluster.storage.management.server.resources; import javax.ws.rs.FormParam; import javax.ws.rs.GET; -import javax.ws.rs.POST; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import org.springframework.beans.factory.annotation.Autowired; @@ -34,7 +33,6 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.provisioning.JdbcUserDetailsManager; import org.springframework.stereotype.Component; -import com.gluster.storage.management.core.model.AuthStatus; import com.gluster.storage.management.core.model.Status; import com.sun.jersey.spi.resource.Singleton; @@ -68,20 +66,17 @@ public class UsersResource { * return false; } */ - @Path("{user}/login") + @Path("{user}") @GET @Produces(MediaType.TEXT_XML) - public AuthStatus login(@PathParam("user") String user, @QueryParam("password") String password) { - - AuthStatus authStatus = new AuthStatus(); + public Status login(@PathParam("user") String user) { // success only if the user passed in query is same as the one passed in security header - authStatus.setIsAuthenticated(SecurityContextHolder.getContext().getAuthentication().getName().equals(user)); - - return authStatus; + return (SecurityContextHolder.getContext().getAuthentication().getName().equals(user) ? Status.STATUS_SUCCESS + : Status.STATUS_FAILURE); } - @Path("{user}/changepassword") - @POST + @Path("{user}") + @PUT @Produces(MediaType.TEXT_XML) public Status changePassword(@FormParam("oldpassword") String oldPassword, @FormParam("newpassword") String newPassword) { |
