GUAC-932: Clean up REST JavaScript for connections and groups. Update Java style to match.

This commit is contained in:
Michael Jumper
2014-12-17 23:59:29 -08:00
parent 0dd2cd87fb
commit 3c5213e103
4 changed files with 287 additions and 257 deletions

View File

@@ -36,17 +36,17 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;
import org.glyptodon.guacamole.GuacamoleClientException;
import org.glyptodon.guacamole.GuacamoleException;
import org.glyptodon.guacamole.GuacamoleResourceNotFoundException;
import org.glyptodon.guacamole.net.auth.Connection;
import org.glyptodon.guacamole.net.auth.ConnectionGroup;
import org.glyptodon.guacamole.net.auth.ConnectionRecord;
import org.glyptodon.guacamole.net.auth.Directory;
import org.glyptodon.guacamole.net.auth.UserContext;
import org.glyptodon.guacamole.net.basic.rest.AuthProviderRESTExposure;
import org.glyptodon.guacamole.net.basic.rest.HTTPException;
import org.glyptodon.guacamole.net.basic.rest.auth.AuthenticationService;
import org.glyptodon.guacamole.net.basic.rest.connectiongroup.APIConnectionGroup;
import org.glyptodon.guacamole.protocol.GuacamoleConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -56,7 +56,7 @@ import org.slf4j.LoggerFactory;
*
* @author James Muehlner
*/
@Path("/connection")
@Path("/connections")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ConnectionRESTService {
@@ -73,13 +73,20 @@ public class ConnectionRESTService {
private AuthenticationService authenticationService;
/**
* Gets an individual connection.
* Retrieves an individual connection.
*
* @param authToken The authentication token that is used to authenticate
* the user performing the operation.
* @param connectionID The ID of the Connection..
* @return The connection.
* @throws GuacamoleException If a problem is encountered while retrieving the connection.
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param connectionID
* The identifier of the connection to retrieve.
*
* @return
* The connection having the given identifier.
*
* @throws GuacamoleException
* If an error occurs while retrieving the connection.
*/
@GET
@Path("/{connectionID}")
@@ -97,7 +104,7 @@ public class ConnectionRESTService {
// Get the connection
Connection connection = connectionDirectory.get(connectionID);
if (connection == null)
throw new HTTPException(Status.NOT_FOUND, "No Connection found with the provided ID.");
throw new GuacamoleResourceNotFoundException("No such connection: \"" + connectionID + "\"");
return new APIConnection(connection);
@@ -111,7 +118,7 @@ public class ConnectionRESTService {
* performing the operation.
*
* @param connectionID
* The ID of the connection.
* The identifier of the connection.
*
* @return
* A map of parameter name/value pairs.
@@ -135,7 +142,7 @@ public class ConnectionRESTService {
// Get the connection
Connection connection = connectionDirectory.get(connectionID);
if (connection == null)
throw new HTTPException(Status.NOT_FOUND, "No Connection found with the provided ID.");
throw new GuacamoleResourceNotFoundException("No such connection: \"" + connectionID + "\"");
// Retrieve connection configuration
GuacamoleConfiguration config = connection.getConfiguration();
@@ -157,7 +164,7 @@ public class ConnectionRESTService {
* performing the operation.
*
* @param connectionID
* The ID of the connection.
* The identifier of the connection.
*
* @return
* A list of connection records, describing the start and end times of
@@ -182,7 +189,7 @@ public class ConnectionRESTService {
// Get the connection
Connection connection = connectionDirectory.get(connectionID);
if (connection == null)
throw new HTTPException(Status.NOT_FOUND, "No Connection found with the provided ID.");
throw new GuacamoleResourceNotFoundException("No such connection: \"" + connectionID + "\"");
return connection.getHistory();
@@ -191,10 +198,15 @@ public class ConnectionRESTService {
/**
* Deletes an individual connection.
*
* @param authToken The authentication token that is used to authenticate
* the user performing the operation.
* @param connectionID The ID of the Connection to delete.
* @throws GuacamoleException If a problem is encountered while deleting the connection.
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param connectionID
* The identifier of the connection to delete.
*
* @throws GuacamoleException
* If an error occurs while deleting the connection.
*/
@DELETE
@Path("/{connectionID}")
@@ -211,72 +223,112 @@ public class ConnectionRESTService {
// Make sure the connection is there before trying to delete
if (connectionDirectory.get(connectionID) == null)
throw new HTTPException(Status.NOT_FOUND, "No Connection found with the provided ID.");
throw new GuacamoleResourceNotFoundException("No such connection: \"" + connectionID + "\"");
// Delete the connection
connectionDirectory.remove(connectionID);
}
/**
* Creates a new connection and returns the identifier of the new connection.
* If a parentID is provided, the connection will be created in the
* connection group with the parentID. Otherwise, the root connection group
* will be used.
* Retrieves a single connection group from the given user context. If
* the given identifier is null or the root identifier, the root connection
* group will be returned.
*
* @param userContext
* The user context to retrieve the connection group from.
*
* @param identifier
* The identifier of the connection group to retrieve.
*
* @return
* The connection group having the given identifier, or the root
* connection group if the identifier is null or the root identifier.
*
* @throws GuacamoleException
* If an error occurs while retrieving the connection group, or if the
* connection group does not exist.
*/
private ConnectionGroup retrieveConnectionGroup(UserContext userContext,
String identifier) throws GuacamoleException {
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
// Use root group if identifier is null (or the standard root identifier)
if (identifier == null || identifier.equals(APIConnectionGroup.ROOT_IDENTIFIER))
return rootGroup;
// Pull specified connection group otherwise
Directory<String, ConnectionGroup> directory = rootGroup.getConnectionGroupDirectory();
ConnectionGroup connectionGroup = directory.get(identifier);
if (connectionGroup == null)
throw new GuacamoleResourceNotFoundException("No such connection group: \"" + identifier + "\"");
return connectionGroup;
}
/**
* Creates a new connection and returns the identifier of the new
* connection.
*
* @param authToken The authentication token that is used to authenticate
* the user performing the operation.
* @param parentID The ID of the ConnectionGroup the connections
* belong to. If null, the root connection group will be used.
* @param connection The connection to create.
* @return The identifier of the new connection.
* @throws GuacamoleException If a problem is encountered while creating the connection.
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param connection
* The connection to create.
*
* @return
* The identifier of the new connection.
*
* @throws GuacamoleException
* If an error occurs while creating the connection.
*/
@POST
@AuthProviderRESTExposure
public String createConnection(@QueryParam("token") String authToken,
@QueryParam("parentID") String parentID, APIConnection connection) throws GuacamoleException {
public String createConnection(@QueryParam("token") String authToken,
APIConnection connection) throws GuacamoleException {
UserContext userContext = authenticationService.getUserContext(authToken);
// Validate that connection data was provided
if (connection == null)
throw new GuacamoleClientException("A connection is required for this request.");
throw new GuacamoleClientException("Connection JSON must be submitted when creating connections.");
// If the parent connection group is passed in, try to find it.
ConnectionGroup parentConnectionGroup;
if (parentID == null)
parentConnectionGroup = userContext.getRootConnectionGroup();
// Retrieve parent group
String parentID = connection.getParentIdentifier();
ConnectionGroup parentConnectionGroup = retrieveConnectionGroup(userContext, parentID);
else {
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
Directory<String, ConnectionGroup> connectionGroupDirectory = rootGroup.getConnectionGroupDirectory();
parentConnectionGroup = connectionGroupDirectory.get(parentID);
}
if (parentConnectionGroup == null)
throw new HTTPException(Status.NOT_FOUND, "No ConnectionGroup found with the provided parentID.");
Directory<String, Connection> connectionDirectory =
parentConnectionGroup.getConnectionDirectory();
// Create the connection
// Add the new connection
Directory<String, Connection> connectionDirectory = parentConnectionGroup.getConnectionDirectory();
connectionDirectory.add(new APIConnectionWrapper(connection));
// Return the new connection identifier
return connection.getIdentifier();
}
/**
* Updates a connection.
* Updates an existing connection. If the parent identifier of the
* connection is changed, the connection will also be moved to the new
* parent group.
*
* @param authToken The authentication token that is used to authenticate
* the user performing the operation.
* @param connectionID The ID of the Connection to move.
* @param connection The connection to update.
* @throws GuacamoleException If a problem is encountered while updating the connection.
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param connectionID
* The identifier of the connection to update.
*
* @param connection
* The connection data to update the specified connection with.
*
* @throws GuacamoleException
* If an error occurs while updating the connection.
*/
@POST
@PUT
@Path("/{connectionID}")
@AuthProviderRESTExposure
public void updateConnection(@QueryParam("token") String authToken,
@@ -284,62 +336,27 @@ public class ConnectionRESTService {
UserContext userContext = authenticationService.getUserContext(authToken);
// Validate that connection data was provided
if (connection == null)
throw new GuacamoleClientException("A connection is required for this request.");
throw new GuacamoleClientException("Connection JSON must be submitted when updating connections.");
// Get the connection directory
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
Directory<String, Connection> connectionDirectory =
rootGroup.getConnectionDirectory();
Connection connectionFromAuthProvider = connectionDirectory.get(connectionID);
// Make sure the connection is there before trying to update
if (connectionFromAuthProvider == null)
throw new HTTPException(Status.NOT_FOUND, "No Connection found with the provided ID.");
Connection existingConnection = connectionDirectory.get(connectionID);
if (existingConnection == null)
throw new GuacamoleResourceNotFoundException("No such connection: \"" + connectionID + "\"");
// Copy the information from this connection over to an object from the Auth Provider
APIConnectionWrapper wrappedConnection = new APIConnectionWrapper(connection);
connectionFromAuthProvider.setConfiguration(wrappedConnection.getConfiguration());
connectionFromAuthProvider.setName(wrappedConnection.getName());
Connection wrappedConnection = new APIConnectionWrapper(connection);
// Update the connection
connectionDirectory.update(connectionFromAuthProvider);
existingConnection.setConfiguration(wrappedConnection.getConfiguration());
existingConnection.setName(wrappedConnection.getName());
connectionDirectory.update(existingConnection);
}
/**
* Moves an individual connection to a different connection group.
*
* @param authToken The authentication token that is used to authenticate
* the user performing the operation.
* @param connectionID The ID of the Connection to move.
* @param parentID The ID of the ConnectionGroup the connection is to be moved to.
* @throws GuacamoleException If a problem is encountered while moving the connection.
*/
@PUT
@Path("/{connectionID}")
@AuthProviderRESTExposure
public void moveConnection(@QueryParam("token") String authToken,
@PathParam("connectionID") String connectionID, @QueryParam("parentID") String parentID)
throws GuacamoleException {
UserContext userContext = authenticationService.getUserContext(authToken);
// Get the connection directory
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
Directory<String, Connection> connectionDirectory =
rootGroup.getConnectionDirectory();
// Find the new parent connection group
Directory<String, ConnectionGroup> connectionGroupDirectory = rootGroup.getConnectionGroupDirectory();
ConnectionGroup parentConnectionGroup = connectionGroupDirectory.get(parentID);
if (parentConnectionGroup == null)
throw new HTTPException(Status.NOT_FOUND, "No ConnectionGroup found with the provided parentID.");
// Move the connection
connectionDirectory.move(connectionID, parentConnectionGroup.getConnectionDirectory());
}
}

View File

@@ -58,7 +58,7 @@ import org.slf4j.LoggerFactory;
*
* @author James Muehlner
*/
@Path("/connectionGroup")
@Path("/connectionGroups")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ConnectionGroupRESTService {
@@ -256,10 +256,15 @@ public class ConnectionGroupRESTService {
/**
* Deletes an individual connection group.
*
* @param authToken The authentication token that is used to authenticate
* the user performing the operation.
* @param connectionGroupID The ID of the ConnectionGroup to delete.
* @throws GuacamoleException If a problem is encountered while deleting the connection group.
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param connectionGroupID
* The identifier of the connection group to delete.
*
* @throws GuacamoleException
* If an error occurs while deleting the connection group.
*/
@DELETE
@Path("/{connectionGroupID}")
@@ -281,7 +286,7 @@ public class ConnectionGroupRESTService {
// Make sure the connection is there before trying to delete
if (connectionGroupDirectory.get(connectionGroupID) == null)
throw new HTTPException(Status.NOT_FOUND, "No ConnectionGroup found with the provided ID.");
throw new GuacamoleResourceNotFoundException("No such connection group: \"" + connectionGroupID + "\"");
// Delete the connection group
connectionGroupDirectory.remove(connectionGroupID);
@@ -294,42 +299,49 @@ public class ConnectionGroupRESTService {
* connection group with the parentID. Otherwise, the root connection group
* will be used.
*
* @param authToken The authentication token that is used to authenticate
* the user performing the operation.
* @param parentID The ID of the ConnectionGroup the connection groups
* belong to. If null, the root connection group will be used.
* @param connectionGroup The connection group to create.
* @return The identifier of the new connection group.
* @throws GuacamoleException If a problem is encountered while creating the connection group.
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param connectionGroup
* The connection group to create.
*
* @return
* The identifier of the new connection group.
*
* @throws GuacamoleException
* If an error occurs while creating the connection group.
*/
@POST
@AuthProviderRESTExposure
public String createConnectionGroup(@QueryParam("token") String authToken,
@QueryParam("parentID") String parentID, APIConnectionGroup connectionGroup) throws GuacamoleException {
public String createConnectionGroup(@QueryParam("token") String authToken,
APIConnectionGroup connectionGroup) throws GuacamoleException {
UserContext userContext = authenticationService.getUserContext(authToken);
// Validate that connection group data was provided
if (connectionGroup == null)
throw new GuacamoleClientException("A connection group is required for this request.");
throw new GuacamoleClientException("Connection group JSON must be submitted when creating connections groups.");
// If the parent connection group is passed in, try to find it.
String parentID = connectionGroup.getParentIdentifier();
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
// Use root group if no parent is specified
ConnectionGroup parentConnectionGroup;
if (parentID == null)
parentConnectionGroup = userContext.getRootConnectionGroup();
parentConnectionGroup = rootGroup;
// Pull specified connection group otherwise
else {
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
Directory<String, ConnectionGroup> connectionGroupDirectory = rootGroup.getConnectionGroupDirectory();
parentConnectionGroup = connectionGroupDirectory.get(parentID);
if (parentConnectionGroup == null)
throw new GuacamoleResourceNotFoundException("No such connection group: \"" + parentID + "\"");
}
if (parentConnectionGroup == null)
throw new HTTPException(Status.NOT_FOUND, "No ConnectionGroup found with the provided parentID.");
Directory<String, ConnectionGroup> connectionGroupDirectory =
parentConnectionGroup.getConnectionGroupDirectory();
// Create the connection group
// Add the new connection group
Directory<String, ConnectionGroup> connectionGroupDirectory = parentConnectionGroup.getConnectionGroupDirectory();
connectionGroupDirectory.add(new APIConnectionGroupWrapper(connectionGroup));
// Return the new connection group identifier
@@ -338,15 +350,24 @@ public class ConnectionGroupRESTService {
}
/**
* Updates a connection group.
* Updates a connection group. If the parent identifier of the
* connection group is changed, the connection group will also be moved to
* the new parent group.
*
* @param authToken The authentication token that is used to authenticate
* the user performing the operation.
* @param connectionGroupID The ID of the ConnectionGroup to update.
* @param connectionGroup The connection group to update.
* @throws GuacamoleException If a problem is encountered while updating the connection group.
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param connectionGroupID
* The identifier of the existing connection group to update.
*
* @param connectionGroup
* The data to update the existing connection group with.
*
* @throws GuacamoleException
* If an error occurs while updating the connection group.
*/
@POST
@PUT
@Path("/{connectionGroupID}")
@AuthProviderRESTExposure
public void updateConnectionGroup(@QueryParam("token") String authToken,
@@ -355,8 +376,9 @@ public class ConnectionGroupRESTService {
UserContext userContext = authenticationService.getUserContext(authToken);
// Validate that connection group data was provided
if (connectionGroup == null)
throw new GuacamoleClientException("A connection group is required for this request.");
throw new GuacamoleClientException("Connection group JSON must be submitted when updating connection groups.");
// Get the connection directory
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
@@ -370,51 +392,11 @@ public class ConnectionGroupRESTService {
// Make sure the connection group is there before trying to update
if (connectionGroupDirectory.get(connectionGroupID) == null)
throw new HTTPException(Status.NOT_FOUND, "No ConnectionGroup found with the provided ID.");
throw new GuacamoleResourceNotFoundException("No such connection group: \"" + connectionGroupID + "\"");
// Update the connection group
connectionGroupDirectory.update(new APIConnectionGroupWrapper(connectionGroup));
}
/**
* Moves an individual connection group to a different connection group.
*
* @param authToken The authentication token that is used to authenticate
* the user performing the operation.
* @param connectionGroupID The ID of the ConnectionGroup to move.
* @param parentID The ID of the ConnectionGroup the connection group is to be moved to.
* @throws GuacamoleException If a problem is encountered while moving the connection group.
*/
@PUT
@Path("/{connectionGroupID}")
@AuthProviderRESTExposure
public void moveConnectionGroup(@QueryParam("token") String authToken,
@PathParam("connectionGroupID") String connectionGroupID,
@QueryParam("parentID") String parentID) throws GuacamoleException {
UserContext userContext = authenticationService.getUserContext(authToken);
// Get the connection group directory
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
// Use the root group if it was asked for
if (connectionGroupID != null && connectionGroupID.equals(APIConnectionGroup.ROOT_IDENTIFIER))
connectionGroupID = rootGroup.getIdentifier();
Directory<String, ConnectionGroup> connectionGroupDirectory =
rootGroup.getConnectionGroupDirectory();
// Find the new parent connection group
Directory<String, ConnectionGroup> newConnectionGroupDirectory = rootGroup.getConnectionGroupDirectory();
ConnectionGroup parentConnectionGroup = newConnectionGroupDirectory.get(parentID);
if (parentConnectionGroup == null)
throw new HTTPException(Status.NOT_FOUND, "No ConnectionGroup found with the provided parentID.");
// Move the connection group
connectionGroupDirectory.move(connectionGroupID, parentConnectionGroup.getConnectionGroupDirectory());
}
}

View File

@@ -66,7 +66,7 @@ angular.module('rest').factory('connectionGroupService', ['$http', 'authenticati
// Retrieve connection group
return $http({
method : 'GET',
url : 'api/connectionGroup/' + encodeURIComponent(connectionGroupID) + '/tree',
url : 'api/connectionGroups/' + encodeURIComponent(connectionGroupID) + '/tree',
params : httpParameters
});
@@ -98,7 +98,7 @@ angular.module('rest').factory('connectionGroupService', ['$http', 'authenticati
// Retrieve connection group
return $http({
method : 'GET',
url : 'api/connectionGroup/' + encodeURIComponent(connectionGroupID),
url : 'api/connectionGroups/' + encodeURIComponent(connectionGroupID),
params : httpParameters
});
@@ -119,50 +119,38 @@ angular.module('rest').factory('connectionGroupService', ['$http', 'authenticati
*/
service.saveConnectionGroup = function saveConnectionGroup(connectionGroup) {
// Build HTTP parameters set
var httpParameters = {
token : authenticationService.getCurrentToken()
};
// If connection group is new, add it and set the identifier automatically
if (!connectionGroup.identifier) {
return $http.post("api/connectionGroup/?token=" + authenticationService.getCurrentToken(), connectionGroup).success(
return $http({
method : 'POST',
url : 'api/connectionGroups',
params : httpParameters,
data : connectionGroup
})
// Set the identifier on the new connection group
function setConnectionGroupID(connectionGroupID){
connectionGroup.identifier = connectionGroupID;
}
);
// Set the identifier on the new connection group
.success(function connectionGroupCreated(identifier){
connectionGroup.identifier = identifier;
});
}
// Otherwise, update the existing connection group
else {
return $http.post(
"api/connectionGroup/" + connectionGroup.identifier +
"?token=" + authenticationService.getCurrentToken(),
connectionGroup);
return $http({
method : 'PUT',
url : 'api/connectionGroups/' + encodeURIComponent(connectionGroup.identifier),
params : httpParameters,
data : connectionGroup
});
}
};
/**
* FIXME: Why is this different from save?
*
* Makes a request to the REST API to move a connection group to a
* different group, returning a promise that can be used for processing the
* results of the call.
*
* @param {ConnectionGroup} connectionGroup The connection group to move.
*
* @returns {Promise}
* A promise for the HTTP call which will succeed if and only if the
* move operation is successful.
*/
service.moveConnectionGroup = function moveConnectionGroup(connectionGroup) {
return $http.put(
"api/connectionGroup/" + connectionGroup.identifier +
"?token=" + authenticationService.getCurrentToken() +
"&parentID=" + connectionGroup.parentIdentifier,
connectionGroup);
};
/**
* Makes a request to the REST API to delete a connection group, returning
* a promise that can be used for processing the results of the call.
@@ -174,9 +162,19 @@ angular.module('rest').factory('connectionGroupService', ['$http', 'authenticati
* delete operation is successful.
*/
service.deleteConnectionGroup = function deleteConnectionGroup(connectionGroup) {
return $http['delete'](
"api/connectionGroup/" + connectionGroup.identifier +
"?token=" + authenticationService.getCurrentToken());
// Build HTTP parameters set
var httpParameters = {
token : authenticationService.getCurrentToken()
};
// Delete connection group
return $http({
method : 'DELETE',
url : 'api/connectionGroups/' + encodeURIComponent(connectionGroup.identifier),
params : httpParameters
});
};
return service;

View File

@@ -44,7 +44,19 @@ angular.module('rest').factory('connectionService', ['$http', 'authenticationSer
* });
*/
service.getConnection = function getConnection(id) {
return $http.get("api/connection/" + id + "?token=" + authenticationService.getCurrentToken());
// Build HTTP parameters set
var httpParameters = {
token : authenticationService.getCurrentToken()
};
// Retrieve connection
return $http({
method : 'GET',
url : 'api/connections/' + encodeURIComponent(id),
params : httpParameters
});
};
/**
@@ -60,7 +72,19 @@ angular.module('rest').factory('connectionService', ['$http', 'authenticationSer
* @link{ConnectionHistoryEntry} objects upon success.
*/
service.getConnectionHistory = function getConnectionHistory(id) {
return $http.get("api/connection/" + id + "/history?token=" + authenticationService.getCurrentToken());
// Build HTTP parameters set
var httpParameters = {
token : authenticationService.getCurrentToken()
};
// Retrieve connection history
return $http({
method : 'GET',
url : 'api/connections/' + encodeURIComponent(id) + '/history',
params : httpParameters
});
};
/**
@@ -76,7 +100,19 @@ angular.module('rest').factory('connectionService', ['$http', 'authenticationSer
* pairs upon success.
*/
service.getConnectionParameters = function getConnectionParameters(id) {
return $http.get("api/connection/" + id + "/parameters?token=" + authenticationService.getCurrentToken());
// Build HTTP parameters set
var httpParameters = {
token : authenticationService.getCurrentToken()
};
// Retrieve connection parameters
return $http({
method : 'GET',
url : 'api/connections/' + encodeURIComponent(id) + '/parameters',
params : httpParameters
});
};
/**
@@ -94,51 +130,38 @@ angular.module('rest').factory('connectionService', ['$http', 'authenticationSer
*/
service.saveConnection = function saveConnection(connection) {
// Build HTTP parameters set
var httpParameters = {
token : authenticationService.getCurrentToken()
};
// If connection is new, add it and set the identifier automatically
if (!connection.identifier) {
return $http.post("api/connection/?token=" + authenticationService.getCurrentToken(), connection).success(
return $http({
method : 'POST',
url : 'api/connections',
params : httpParameters,
data : connection
})
// Set the identifier on the new connection
function setConnectionID(connectionID){
connection.identifier = connectionID;
}
);
// Set the identifier on the new connection
.success(function connectionCreated(identifier){
connection.identifier = identifier;
});
}
// Otherwise, update the existing connection
else {
return $http.post(
"api/connection/" + connection.identifier +
"?token=" + authenticationService.getCurrentToken(),
connection);
return $http({
method : 'PUT',
url : 'api/connections/' + encodeURIComponent(connection.identifier),
params : httpParameters,
data : connection
});
}
};
/**
* FIXME: Why is this different from save?
*
* Makes a request to the REST API to move a connection to a different
* group, returning a promise that can be used for processing the results
* of the call.
*
* @param {Connection} connection The connection to move.
*
* @returns {Promise}
* A promise for the HTTP call which will succeed if and only if the
* move operation is successful.
*/
service.moveConnection = function moveConnection(connection) {
return $http.put(
"api/connection/" + connection.identifier +
"?token=" + authenticationService.getCurrentToken() +
"&parentID=" + connection.parentIdentifier,
connection);
};
/**
* Makes a request to the REST API to delete a connection,
* returning a promise that can be used for processing the results of the call.
@@ -150,9 +173,19 @@ angular.module('rest').factory('connectionService', ['$http', 'authenticationSer
* delete operation is successful.
*/
service.deleteConnection = function deleteConnection(connection) {
return $http['delete'](
"api/connection/" + connection.identifier +
"?token=" + authenticationService.getCurrentToken());
// Build HTTP parameters set
var httpParameters = {
token : authenticationService.getCurrentToken()
};
// Delete connection
return $http({
method : 'DELETE',
url : 'api/connections/' + encodeURIComponent(connection.identifier),
params : httpParameters
});
};
return service;