From 6f15c20e50cc21249a3ad77786d051d49067cb9f Mon Sep 17 00:00:00 2001 From: James Muehlner Date: Mon, 16 Sep 2013 21:37:24 -0700 Subject: [PATCH] Ticket #362: Cleaned up a little bit. --- .../net/basic/rest/auth/APIAuthToken.java | 49 +++++++++++++++++++ .../net/basic/rest/auth/LoginRESTService.java | 18 +++++-- .../net/basic/rest/connection/Connection.java | 36 +++++++++++++- .../connection/ConnectionRESTService.java | 16 +++--- 4 files changed, 106 insertions(+), 13 deletions(-) create mode 100644 guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/APIAuthToken.java diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/APIAuthToken.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/APIAuthToken.java new file mode 100644 index 000000000..47bdffcc4 --- /dev/null +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/APIAuthToken.java @@ -0,0 +1,49 @@ +package org.glyptodon.guacamole.net.basic.rest.auth; + +/* + * Guacamole - Clientless Remote Desktop + * Copyright (C) 2010 Michael Jumper + * + * This program 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. + * + * This program 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 . + */ + +/** + * A simple object to represent an auth token in the API. + * + * @author James Muehlner + */ +public class APIAuthToken { + + /** + * The auth token. + */ + private String authToken; + + /** + * Get the auth token. + * @return The auth token. + */ + public String getAuthToken() { + return authToken; + } + + /** + * Create a new APIAuthToken Object with the given auth token. + * + * @param authToken The auth token to create the new APIAuthToken with. + */ + public APIAuthToken(String authToken) { + this.authToken = authToken; + } +} diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/LoginRESTService.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/LoginRESTService.java index f0ae5a9a7..7d79b4f65 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/LoginRESTService.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/LoginRESTService.java @@ -1,16 +1,20 @@ package org.glyptodon.guacamole.net.basic.rest.auth; import com.google.inject.Inject; +import java.util.HashMap; +import java.util.Map; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.glyptodon.guacamole.GuacamoleException; import org.glyptodon.guacamole.net.auth.AuthenticationProvider; import org.glyptodon.guacamole.net.auth.Credentials; import org.glyptodon.guacamole.net.auth.UserContext; -import org.glyptodon.guacamole.net.basic.rest.RESTModule; +import org.glyptodon.guacamole.net.basic.rest.APIError; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,6 +46,7 @@ import org.slf4j.LoggerFactory; @Path("/api/login") +@Produces(MediaType.APPLICATION_JSON) public class LoginRESTService { /** @@ -76,7 +81,7 @@ public class LoginRESTService { * @return The auth token for the newly logged-in user. */ @POST - public String login(@QueryParam("username") String username, + public APIAuthToken login(@QueryParam("username") String username, @QueryParam("password") String password) { Credentials credentials = new Credentials(); @@ -89,18 +94,21 @@ public class LoginRESTService { userContext = authProvider.getUserContext(credentials); } catch(GuacamoleException e) { logger.error("Exception caught while authenticating user.", e); - throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR); + throw new WebApplicationException( + Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e).build()); } // authentication failed. if(userContext == null) - throw new WebApplicationException(Response.Status.UNAUTHORIZED); + throw new WebApplicationException( + Response.status(Response.Status.UNAUTHORIZED) + .entity(new APIError("Permission Denied.")).build()); String authToken = authTokenGenerator.getToken(); tokenUserMap.put(authToken, userContext); - return authToken; + return new APIAuthToken(authToken); } } diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/Connection.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/Connection.java index 9e4406f3d..53012d400 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/Connection.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/Connection.java @@ -69,34 +69,66 @@ public class Connection { this.history = connection.getHistory(); } + /** + * Get the name of this connection. + * @return The name of this connection. + */ public String getName() { return name; } + /** + * Set the name of this connection. + * @param name The name of this connection. + */ public void setName(String name) { this.name = name; } + /** + * Get the identifier of this connection. + * @return The identifier of this connection. + */ public String getIdentifier() { return identifier; } - + + /** + * Set the identifier of this connection. + * @param identifier The identifier of this connection. + */ public void setIdentifier(String identifier) { this.identifier = identifier; } + /** + * Get the configuration for this connection. + * @return The configuration for this connection. + */ public GuacamoleConfiguration getConfiguration() { return configuration; } - + + /** + * Set the configuration for this connection. + * @param configuration The configuration for this connection. + */ public void setConfiguration(GuacamoleConfiguration configuration) { this.configuration = configuration; } + /** + * Get the history records for this connection. + * @return The history records for this connection. + */ public List getHistory() { return history; } + /** + * Set the history records for this connection. + * @param history The history records for this connection. + */ public void setHistory(List history) { this.history = history; } diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionRESTService.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionRESTService.java index 27961ade7..1bc98328e 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionRESTService.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionRESTService.java @@ -43,7 +43,7 @@ import org.glyptodon.guacamole.net.basic.rest.auth.TokenUserContextMap; * @author James Muehlner */ @Path("/api/connection") -@RequestScoped +@Produces(MediaType.APPLICATION_JSON) public class ConnectionRESTService { /** @@ -59,14 +59,14 @@ public class ConnectionRESTService { private ConnectionService connectionService; @GET - @Produces(MediaType.APPLICATION_JSON) public List getConnections(@QueryParam("token") String authToken, @QueryParam("parentID") String parentID) { UserContext userContext = tokenUserMap.get(authToken); // authentication failed. if(userContext == null) - throw new WebApplicationException(Response.Status.UNAUTHORIZED); - + throw new WebApplicationException( + Response.status(Response.Status.UNAUTHORIZED) + .entity(new APIError("Permission Denied.")).build()); try { // If the parent connection group is passed in, try to find it. @@ -99,9 +99,13 @@ public class ConnectionRESTService { return connectionService.convertConnectionList(connections); } catch(GuacamoleSecurityException e) { - throw new WebApplicationException(e, Response.Status.UNAUTHORIZED); + throw new WebApplicationException( + Response.status(Response.Status.UNAUTHORIZED) + .entity(new APIError("Permission Denied.")).build()); } catch(GuacamoleException e) { - throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR); + throw new WebApplicationException( + Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(e).build()); } }