GUACAMOLE-5: Expose UserContext schema as a resource, not a separate REST service. Refactor URLs accordingly.

This commit is contained in:
Michael Jumper
2016-07-12 14:00:17 -07:00
parent 3658b91fe1
commit 89eabc1d70
5 changed files with 162 additions and 202 deletions

View File

@@ -38,7 +38,6 @@ import org.apache.guacamole.rest.connection.ConnectionModule;
import org.apache.guacamole.rest.connectiongroup.ConnectionGroupModule; import org.apache.guacamole.rest.connectiongroup.ConnectionGroupModule;
import org.apache.guacamole.rest.language.LanguageRESTService; import org.apache.guacamole.rest.language.LanguageRESTService;
import org.apache.guacamole.rest.patch.PatchRESTService; import org.apache.guacamole.rest.patch.PatchRESTService;
import org.apache.guacamole.rest.schema.SchemaRESTService;
import org.apache.guacamole.rest.tunnel.TunnelRESTService; import org.apache.guacamole.rest.tunnel.TunnelRESTService;
import org.apache.guacamole.rest.user.UserModule; import org.apache.guacamole.rest.user.UserModule;
@@ -89,7 +88,6 @@ public class RESTServiceModule extends ServletModule {
// Set up the API endpoints // Set up the API endpoints
bind(LanguageRESTService.class); bind(LanguageRESTService.class);
bind(PatchRESTService.class); bind(PatchRESTService.class);
bind(SchemaRESTService.class);
bind(TokenRESTService.class); bind(TokenRESTService.class);
bind(TunnelRESTService.class); bind(TunnelRESTService.class);

View File

@@ -1,196 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.guacamole.rest.schema;
import com.google.inject.Inject;
import java.util.Collection;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
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.apache.guacamole.GuacamoleException;
import org.apache.guacamole.environment.Environment;
import org.apache.guacamole.environment.LocalEnvironment;
import org.apache.guacamole.form.Form;
import org.apache.guacamole.net.auth.UserContext;
import org.apache.guacamole.GuacamoleSession;
import org.apache.guacamole.rest.ObjectRetrievalService;
import org.apache.guacamole.rest.auth.AuthenticationService;
import org.apache.guacamole.protocols.ProtocolInfo;
/**
* A REST service which provides access to descriptions of the properties,
* attributes, etc. of objects used within the Guacamole REST API.
*
* @author Michael Jumper
*/
@Path("/schema/{dataSource}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class SchemaRESTService {
/**
* A service for authenticating users from auth tokens.
*/
@Inject
private AuthenticationService authenticationService;
/**
* Service for convenient retrieval of objects.
*/
@Inject
private ObjectRetrievalService retrievalService;
/**
* Retrieves the possible attributes of a user object.
*
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param authProviderIdentifier
* The unique identifier of the AuthenticationProvider associated with
* the UserContext dictating the available user attributes.
*
* @return
* A collection of forms which describe the possible attributes of a
* user object.
*
* @throws GuacamoleException
* If an error occurs while retrieving the possible attributes.
*/
@GET
@Path("/users/attributes")
public Collection<Form> getUserAttributes(@QueryParam("token") String authToken,
@PathParam("dataSource") String authProviderIdentifier)
throws GuacamoleException {
// Retrieve all possible user attributes
GuacamoleSession session = authenticationService.getGuacamoleSession(authToken);
UserContext userContext = retrievalService.retrieveUserContext(session, authProviderIdentifier);
return userContext.getUserAttributes();
}
/**
* Retrieves the possible attributes of a connection object.
*
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param authProviderIdentifier
* The unique identifier of the AuthenticationProvider associated with
* the UserContext dictating the available connection attributes.
*
* @return
* A collection of forms which describe the possible attributes of a
* connection object.
*
* @throws GuacamoleException
* If an error occurs while retrieving the possible attributes.
*/
@GET
@Path("/connections/attributes")
public Collection<Form> getConnectionAttributes(@QueryParam("token") String authToken,
@PathParam("dataSource") String authProviderIdentifier)
throws GuacamoleException {
// Retrieve all possible connection attributes
GuacamoleSession session = authenticationService.getGuacamoleSession(authToken);
UserContext userContext = retrievalService.retrieveUserContext(session, authProviderIdentifier);
return userContext.getConnectionAttributes();
}
/**
* Retrieves the possible attributes of a connection group object.
*
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param authProviderIdentifier
* The unique identifier of the AuthenticationProvider associated with
* the UserContext dictating the available connection group
* attributes.
*
* @return
* A collection of forms which describe the possible attributes of a
* connection group object.
*
* @throws GuacamoleException
* If an error occurs while retrieving the possible attributes.
*/
@GET
@Path("/connectionGroups/attributes")
public Collection<Form> getConnectionGroupAttributes(@QueryParam("token") String authToken,
@PathParam("dataSource") String authProviderIdentifier)
throws GuacamoleException {
// Retrieve all possible connection group attributes
GuacamoleSession session = authenticationService.getGuacamoleSession(authToken);
UserContext userContext = retrievalService.retrieveUserContext(session, authProviderIdentifier);
return userContext.getConnectionGroupAttributes();
}
/**
* Gets a map of protocols defined in the system - protocol name to protocol.
*
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param authProviderIdentifier
* The unique identifier of the AuthenticationProvider associated with
* the UserContext dictating the protocols available. Currently, the
* UserContext actually does not dictate this, the the same set of
* protocols will be retrieved for all users, though the identifier
* given here will be validated.
*
* @return
* A map of protocol information, where each key is the unique name
* associated with that protocol.
*
* @throws GuacamoleException
* If an error occurs while retrieving the available protocols.
*/
@GET
@Path("/protocols")
public Map<String, ProtocolInfo> getProtocols(@QueryParam("token") String authToken,
@PathParam("dataSource") String authProviderIdentifier)
throws GuacamoleException {
// Verify the given auth token and auth provider identifier are valid
GuacamoleSession session = authenticationService.getGuacamoleSession(authToken);
retrievalService.retrieveUserContext(session, authProviderIdentifier);
// Get and return a map of all protocols.
Environment env = new LocalEnvironment();
return env.getProtocols();
}
}

View File

@@ -0,0 +1,142 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.guacamole.rest.schema;
import java.util.Collection;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.environment.Environment;
import org.apache.guacamole.environment.LocalEnvironment;
import org.apache.guacamole.form.Form;
import org.apache.guacamole.net.auth.UserContext;
import org.apache.guacamole.protocols.ProtocolInfo;
/**
* A REST resource which provides access to descriptions of the properties,
* attributes, etc. of objects within a particular UserContext.
*
* @author Michael Jumper
*/
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class SchemaResource {
/**
* The UserContext whose schema is exposed by this SchemaResource.
*/
private final UserContext userContext;
/**
* Creates a new SchemaResource which exposes meta information describing
* the kind of data within the given UserContext.
*
* @param userContext
* The UserContext whose schema should be exposed by this
* SchemaResource.
*/
public SchemaResource(UserContext userContext) {
this.userContext = userContext;
}
/**
* Retrieves the possible attributes of a user object.
*
* @return
* A collection of forms which describe the possible attributes of a
* user object.
*
* @throws GuacamoleException
* If an error occurs while retrieving the possible attributes.
*/
@GET
@Path("userAttributes")
public Collection<Form> getUserAttributes() throws GuacamoleException {
// Retrieve all possible user attributes
return userContext.getUserAttributes();
}
/**
* Retrieves the possible attributes of a connection object.
*
* @return
* A collection of forms which describe the possible attributes of a
* connection object.
*
* @throws GuacamoleException
* If an error occurs while retrieving the possible attributes.
*/
@GET
@Path("connectionAttributes")
public Collection<Form> getConnectionAttributes()
throws GuacamoleException {
// Retrieve all possible connection attributes
return userContext.getConnectionAttributes();
}
/**
* Retrieves the possible attributes of a connection group object.
*
* @return
* A collection of forms which describe the possible attributes of a
* connection group object.
*
* @throws GuacamoleException
* If an error occurs while retrieving the possible attributes.
*/
@GET
@Path("connectionGroupAttributes")
public Collection<Form> getConnectionGroupAttributes()
throws GuacamoleException {
// Retrieve all possible connection group attributes
return userContext.getConnectionGroupAttributes();
}
/**
* Gets a map of protocols defined in the system - protocol name to protocol.
*
* @return
* A map of protocol information, where each key is the unique name
* associated with that protocol.
*
* @throws GuacamoleException
* If an error occurs while retrieving the available protocols.
*/
@GET
@Path("protocols")
public Map<String, ProtocolInfo> getProtocols() throws GuacamoleException {
// Get and return a map of all protocols.
Environment env = new LocalEnvironment();
return env.getProtocols();
}
}

View File

@@ -38,6 +38,7 @@ import org.apache.guacamole.rest.activeconnection.APIActiveConnection;
import org.apache.guacamole.rest.connection.APIConnection; import org.apache.guacamole.rest.connection.APIConnection;
import org.apache.guacamole.rest.connectiongroup.APIConnectionGroup; import org.apache.guacamole.rest.connectiongroup.APIConnectionGroup;
import org.apache.guacamole.rest.history.HistoryResource; import org.apache.guacamole.rest.history.HistoryResource;
import org.apache.guacamole.rest.schema.SchemaResource;
import org.apache.guacamole.rest.user.APIUser; import org.apache.guacamole.rest.user.APIUser;
/** /**
@@ -183,4 +184,19 @@ public class UserContextResource {
return new HistoryResource(userContext); return new HistoryResource(userContext);
} }
/**
* Returns a new resource which represents meta information describing the
* kind of data which within the UserContext exposed by this
* UserContextResource.
*
* @return
* A new resource which represents the meta information describing the
* kind of data within the UserContext exposed by this
* UserContextResource.
*/
@Path("schema")
public SchemaResource getSchemaResource() {
return new SchemaResource(userContext);
}
} }

View File

@@ -58,7 +58,7 @@ angular.module('rest').factory('schemaService', ['$injector',
return $http({ return $http({
cache : cacheService.schema, cache : cacheService.schema,
method : 'GET', method : 'GET',
url : 'api/schema/' + encodeURIComponent(dataSource) + '/users/attributes', url : 'api/data/' + encodeURIComponent(dataSource) + '/schema/userAttributes',
params : httpParameters params : httpParameters
}); });
@@ -92,7 +92,7 @@ angular.module('rest').factory('schemaService', ['$injector',
return $http({ return $http({
cache : cacheService.schema, cache : cacheService.schema,
method : 'GET', method : 'GET',
url : 'api/schema/' + encodeURIComponent(dataSource) + '/connections/attributes', url : 'api/data/' + encodeURIComponent(dataSource) + '/schema/connectionAttributes',
params : httpParameters params : httpParameters
}); });
@@ -126,7 +126,7 @@ angular.module('rest').factory('schemaService', ['$injector',
return $http({ return $http({
cache : cacheService.schema, cache : cacheService.schema,
method : 'GET', method : 'GET',
url : 'api/schema/' + encodeURIComponent(dataSource) + '/connectionGroups/attributes', url : 'api/data/' + encodeURIComponent(dataSource) + '/schema/connectionGroupAttributes',
params : httpParameters params : httpParameters
}); });
@@ -157,7 +157,7 @@ angular.module('rest').factory('schemaService', ['$injector',
return $http({ return $http({
cache : cacheService.schema, cache : cacheService.schema,
method : 'GET', method : 'GET',
url : 'api/schema/' + encodeURIComponent(dataSource) + '/protocols', url : 'api/data/' + encodeURIComponent(dataSource) + '/schema/protocols',
params : httpParameters params : httpParameters
}); });