Skip to content
Snippets Groups Projects
Commit 1e3b2597 authored by sveseli's avatar sveseli
Browse files

added common objects and exceptions as base for api

parent 8744a941
No related branches found
No related tags found
No related merge requests found
Showing
with 917 additions and 0 deletions
package gov.anl.aps.dm.common.constants;
/**
* HTTP headers specific to DM.
*/
public class DmHttpHeader {
public static final String DM_SET_COOKIE_HEADER = "Set-Cookie";
public static final String DM_EXCEPTION_TYPE_HEADER = "Dm-Exception-Type";
public static final String DM_STATUS_CODE_HEADER = "Dm-Status-Code";
public static final String DM_STATUS_MESSAGE_HEADER = "Dm-Status-Message";
public static final String DM_SESSION_ROLE_HEADER = "Dm-Session-Role";
}
package gov.anl.aps.dm.common.constants;
/**
* DM property names.
*/
public class DmProperty {
public static final String WEB_SERVICE_URL_PROPERTY_NAME = "dm.webService.url";
}
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_ALREADY_EXISTS = 14;
public static final int DM_OBJECT_NOT_FOUND = 15;
public static final int DM_INVALID_OBJECT_STATE = 16;
public static final int DM_IMAGE_PROCESSING_FAILED = 17;
public static final int DM_EXTERNAL_SERVICE_ERROR = 18;
}
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_INTERNAL_ERROR:
exc = new InternalError();
break;
case DmStatus.DM_INVALID_ARGUMENT:
exc = new InvalidArgument();
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;
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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment