Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • DM/dm-docs
  • hammonds/dm-docs
  • hparraga/dm-docs
3 results
Show changes
Showing
with 1058 additions and 0 deletions
package gov.anl.aps.dm.common.constants;
/**
* DM role enum.
*/
public enum DmRole {
USER("user"),
ADMIN("admin");
private final String type;
private DmRole(String type) {
this.type = type;
}
@Override
public String toString() {
return type;
}
public static DmRole fromString(String type) {
DmRole role = null;
switch (type) {
case "user":
role = USER;
break;
case "admin":
role = ADMIN;
break;
}
return role;
}
}
package gov.anl.aps.dm.common.constants;
/**
* DM service protocol enum.
*/
public enum DmServiceProtocol {
HTTP("http"),
HTTPS("https");
private final String type;
private DmServiceProtocol(String type) {
this.type = type;
}
@Override
public String toString() {
return type;
}
public static DmServiceProtocol fromString(String type) {
DmServiceProtocol protocol = null;
switch (type) {
case "http":
protocol = HTTP;
break;
case "https":
protocol = HTTPS;
break;
}
return protocol;
}
}
package gov.anl.aps.dm.common.constants;
/**
* DM status codes.
*/
public class DmStatus {
public static final int DM_OK = 0;
public static final int DM_ERROR = 1;
public static final int DM_INTERNAL_ERROR = 2;
public static final int DM_COMMUNICATION_ERROR = 3;
public static final int DM_CONFIGURATION_ERROR = 4;
public static final int DM_AUTHORIZATION_ERROR = 5;
public static final int DM_AUTHENTICATION_ERROR = 6;
public static final int DM_DB_ERROR = 7;
public static final int DM_URL_ERROR = 8;
public static final int DM_TIMEOUT_ERROR = 9;
public static final int DM_INVALID_ARGUMENT = 10;
public static final int DM_INVALID_REQUEST = 11;
public static final int DM_INVALID_SESSION = 12;
public static final int DM_COMMAND_FAILED = 13;
public static final int DM_OBJECT_NOT_FOUND = 14;
public static final int DM_OBJECT_ALREADY_EXISTS = 15;
public static final int DM_INVALID_OBJECT_STATE = 16;
public static final int DM_FILE_PROCESSING_ERROR = 17;
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Authentication error exception.
*/
public class AuthenticationError extends DmException {
/**
* Default constructor.
*/
public AuthenticationError() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public AuthenticationError(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public AuthenticationError(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public AuthenticationError(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_AUTHENTICATION_ERROR;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Authorization error exception.
*/
public class AuthorizationError extends DmException {
/**
* Default constructor.
*/
public AuthorizationError() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public AuthorizationError(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public AuthorizationError(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public AuthorizationError(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_AUTHORIZATION_ERROR;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Communication error exception.
*/
public class CommunicationError extends DmException {
/**
* Default constructor.
*/
public CommunicationError() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public CommunicationError(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public CommunicationError(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public CommunicationError(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_COMMUNICATION_ERROR;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Configuration error exception.
*/
public class ConfigurationError extends DmException {
/**
* Default constructor.
*/
public ConfigurationError() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public ConfigurationError(String message) {
super(message);
}
/**
* Constructor sing throwable object.
*
* @param throwable throwable object
*/
public ConfigurationError(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public ConfigurationError(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_CONFIGURATION_ERROR;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* DB error exception.
*/
public class DbError extends DmException {
/**
* Default constructor.
*/
public DbError() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public DbError(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public DbError(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public DbError(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_DB_ERROR;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Generic DM exception, used as base class for all DM exceptions.
*/
public class DmException extends Exception {
/**
* Exception keys.
*/
public static final String SIGNATURE_KEY = "__dm_exception__";
public static final String TYPE_KEY = "type";
public static final String CODE_KEY = "code";
public static final String ARGS_KEY = "args";
private String error = null;
/**
* Default constructor.
*/
public DmException() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public DmException(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public DmException(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public DmException(String message, Throwable throwable) {
super(message, throwable);
}
public int getErrorCode() {
return DmStatus.DM_ERROR;
}
public void setErrorMessage(String error) {
this.error = error;
}
public String getErrorMessage() {
if (error != null) {
return error;
}
return super.getMessage();
}
/**
* Convert exception to string, overriding string output if error message is
* set.
*
* @return exception string
*/
@Override
public String toString() {
if (error != null) {
return error;
} else {
return super.toString();
}
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
import org.apache.log4j.Logger;
/**
* DM exception factory class.
*
*/
public class DmExceptionFactory {
private static final Logger logger
= Logger.getLogger(DmExceptionFactory.class.getName());
/**
* Generate DM exception.
*
* @param type exception type
* @param code exception code
* @param error exception error message
* @return generated DM exception
*/
public static DmException generateDmException(String type, int code, String error) {
DmException exc = new DmException();
try {
String fullType = "gov.anl.aps.dm.common.exceptions." + type;
// Having trouble getting code below to work in all cases, so
// use code for now.
// exc = (DmException) Class.forName(fullType).newInstance();
switch (code) {
case DmStatus.DM_AUTHORIZATION_ERROR:
exc = new AuthorizationError();
break;
case DmStatus.DM_COMMUNICATION_ERROR:
exc = new CommunicationError();
break;
case DmStatus.DM_CONFIGURATION_ERROR:
exc = new ConfigurationError();
break;
case DmStatus.DM_INTERNAL_ERROR:
exc = new InternalError();
break;
case DmStatus.DM_DB_ERROR:
exc = new DbError();
break;
case DmStatus.DM_INVALID_ARGUMENT:
exc = new InvalidArgument();
break;
case DmStatus.DM_INVALID_REQUEST:
exc = new InvalidRequest();
break;
case DmStatus.DM_INVALID_SESSION:
exc = new InvalidSession();
break;
case DmStatus.DM_OBJECT_ALREADY_EXISTS:
exc = new ObjectAlreadyExists();
break;
case DmStatus.DM_OBJECT_NOT_FOUND:
exc = new ObjectNotFound();
break;
case DmStatus.DM_INVALID_OBJECT_STATE:
exc = new InvalidObjectState();
break;
case DmStatus.DM_TIMEOUT_ERROR:
exc = new TimeoutError();
break;
default:
exc = (DmException) Class.forName(fullType).newInstance();
}
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) {
String err = "Cannot generate exception of type " + type + ": " + ex;
logger.error(err);
}
exc.setErrorMessage(error);
return exc;
}
/**
* Throw DM exception.
*
* @param type exception type
* @param code exception code
* @param error exception error message
* @throws DmException generated DM exception whenever this method is
* called
*/
public static void throwDmException(String type, int code, String error) throws DmException {
DmException exc = generateDmException(type, code, error);
throw exc;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Internal error exception.
*/
public class InternalError extends DmException {
/**
* Default constructor.
*/
public InternalError() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public InternalError(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public InternalError(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public InternalError(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_INTERNAL_ERROR;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Invalid argument exception.
*/
public class InvalidArgument extends DmException {
/**
* Default constructor.
*/
public InvalidArgument() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public InvalidArgument(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public InvalidArgument(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public InvalidArgument(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_INVALID_ARGUMENT;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Invalid object state exception.
*/
public class InvalidObjectState extends DmException {
/**
* Default constructor.
*/
public InvalidObjectState() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public InvalidObjectState(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public InvalidObjectState(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public InvalidObjectState(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_INVALID_OBJECT_STATE;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Invalid request exception.
*/
public class InvalidRequest extends DmException {
/**
* Default constructor.
*/
public InvalidRequest() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public InvalidRequest(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public InvalidRequest(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public InvalidRequest(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_INVALID_REQUEST;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Invalid session exception.
*/
public class InvalidSession extends DmException {
/**
* Default constructor.
*/
public InvalidSession() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public InvalidSession(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public InvalidSession(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public InvalidSession(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_INVALID_SESSION;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Object already exists exception.
*/
public class ObjectAlreadyExists extends DmException {
/**
* Default constructor.
*/
public ObjectAlreadyExists() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public ObjectAlreadyExists(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public ObjectAlreadyExists(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public ObjectAlreadyExists(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_OBJECT_ALREADY_EXISTS;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Object not found exception.
*/
public class ObjectNotFound extends DmException {
/**
* Default constructor.
*/
public ObjectNotFound() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public ObjectNotFound(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public ObjectNotFound(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public ObjectNotFound(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_OBJECT_NOT_FOUND;
}
}
package gov.anl.aps.dm.common.exceptions;
import gov.anl.aps.dm.common.constants.DmStatus;
/**
* Timeout error.
*/
public class TimeoutError extends DmException {
/**
* Default constructor.
*/
public TimeoutError() {
super();
}
/**
* Constructor using error message.
*
* @param message error message
*/
public TimeoutError(String message) {
super(message);
}
/**
* Constructor using throwable object.
*
* @param throwable throwable object
*/
public TimeoutError(Throwable throwable) {
super(throwable);
}
/**
* Constructor using error message and throwable object.
*
* @param message error message
* @param throwable throwable object
*/
public TimeoutError(String message, Throwable throwable) {
super(message, throwable);
}
@Override
public int getErrorCode() {
return DmStatus.DM_TIMEOUT_ERROR;
}
}
package gov.anl.aps.dm.common.objects;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import gov.anl.aps.dm.common.exceptions.DmException;
import java.io.Serializable;
/**
* Base DM object class.
*/
public class DmObject implements Serializable {
protected Long id = null;
protected String name = null;
protected String description = null;
public DmObject() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
/**
* Conversion to JSON string representation.
*
* @return JSON string
*/
public String toJson() {
Gson gson = new GsonBuilder().create();
return gson.toJson(this);
}
/**
* Encode object.
*
* @throws DmException in case of any errors
*/
public void encode() throws DmException {
}
/**
* Decode object.
*
* @throws DmException in case of any errors
*/
public void decode() throws DmException {
}
}
package gov.anl.aps.dm.common.objects;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import gov.anl.aps.dm.common.exceptions.DmException;
import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;
/**
* DM object factory class.
*/
public class DmObjectFactory {
private static final Logger logger = Logger.getLogger(DmObjectFactory.class.getName());
private static final Gson gson = new GsonBuilder().create();
/**
* Create object from JSON string.
*
* @param <T> template class
* @param jsonString JSON string
* @param objectClass object class
* @return generated object
*/
public static <T extends Object> T createObject(String jsonString, Class<T> objectClass) {
logger.debug("Converting JSON string to object " + objectClass + ": " + jsonString);
T object = gson.fromJson(jsonString, objectClass);
return object;
}
/**
* Create DM object from JSON string.
*
* @param <T> template class
* @param jsonString JSON string
* @param dmClass DM object class
* @return generated DM object
* @throws DmException in case of any errors
*/
public static <T extends DmObject> T createDmObject(String jsonString, Class<T> dmClass) throws DmException {
logger.debug("Converting JSON string to DM object " + dmClass + ": " + jsonString);
T dmObject = gson.fromJson(jsonString, dmClass);
dmObject.decode();
return dmObject;
}
/**
* Create list of DM objects from JSON string.
*
* @param <T> template class
* @param jsonString DM string
* @return generated list of DM objects
*/
public static <T extends DmObject> List<T> createDmObjectList(String jsonString) {
// This method does not appear to work as template, so we have
// to write specific methods for each object type.
logger.debug("Converting JSON string to dm object list: " + jsonString);
Type dmType = new TypeToken<LinkedList<T>>() {
}.getType();
List<T> dmObjectList = gson.fromJson(jsonString, dmType);
return dmObjectList;
}
/**
* Create list of string objects from JSON string.
*
* @param jsonString JSON string
* @return generated list of string objects
*/
public static List<String> createStringObjectList(String jsonString) {
logger.debug("Converting JSON string to string object list: " + jsonString);
Type dmType = new TypeToken<LinkedList<String>>() {
}.getType();
List<String> dmObjectList = gson.fromJson(jsonString, dmType);
return dmObjectList;
}
}