uri-resolver/src/main/java/org/gcube/datatransfer/resolver/services/error/ExceptionManager.java

104 lines
2.7 KiB
Java

/**
*
*/
package org.gcube.datatransfer.resolver.services.error;
import java.net.URI;
import java.net.URISyntaxException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response.Status;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
import org.gcube.datatransfer.resolver.services.exceptions.WrongParameterException;
/**
* The Class ExceptionManager.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Oct 23, 2018
*/
public class ExceptionManager {
/**
* Throw internal error exception.
*
* @param httpRequest the http request
* @param errorMessage the error message
* @param thrownBy the thrown by
* @param helpURI the help uri
*/
public static void throwInternalErrorException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){
URI theURI = checkURI(helpURI);
throw new InternalServerException(httpRequest, Status.INTERNAL_SERVER_ERROR, errorMessage, thrownBy, theURI);
}
/**
* Throw bad request exception.
*
* @param httpRequest the http request
* @param errorMessage the error message
* @param thrownBy the thrown by
* @param helpURI the help uri
*/
public static void throwBadRequestException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){
URI theURI = checkURI(helpURI);
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, errorMessage, thrownBy, theURI);
}
/**
* Throw wrong parameter exception.
*
* @param httpRequest the http request
* @param errorMessage the error message
* @param thrownBy the thrown by
* @param helpURI the help uri
*/
public static void throwWrongParameterException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){
URI theURI = checkURI(helpURI);
throw new WrongParameterException(httpRequest, Status.BAD_REQUEST, errorMessage, thrownBy, theURI);
}
/**
* Throw not found exception.
*
* @param httpRequest the http request
* @param errorMessage the error message
* @param thrownBy the thrown by
* @param helpURI the help uri
*/
public static void throwNotFoundException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){
URI theURI = checkURI(helpURI);
throw new WrongParameterException(httpRequest, Status.NOT_FOUND, errorMessage, thrownBy, theURI);
}
/**
* Check uri.
*
* @param helpURI the help uri
* @return the uri
*/
public static URI checkURI(String helpURI){
URI theURI = null;
try {
theURI = helpURI==null?new URI(helpURI):null;
}
catch (URISyntaxException e) {
//silent
}
return theURI;
}
}