Rewrite CodeException as a specialized WebApplicationException. Integrate CodeException in the create context.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@161922 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Manuele Simi 2017-12-28 05:06:00 +00:00
parent 36dd483b04
commit 33c6212530
11 changed files with 147 additions and 74 deletions

View File

@ -1,25 +1,31 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gcube.resource-management</groupId>
<artifactId>resource-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>manager-io</artifactId>
<name>I/O</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gcube.resource-management</groupId>
<artifactId>resource-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>manager-io</artifactId>
<name>I/O</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,20 @@
package org.gcube.resourcemanagement.manager.io.exceptions;
import javax.ws.rs.core.GenericEntity;
/**
* Don't forget to comment!
*
* @author Manuele Simi (ISTI CNR)
*
*/
public class CodeEntity extends GenericEntity<SerializableErrorCode> {
/**
* @param entity
*/
protected CodeEntity(SerializableErrorCode entity) {
super(entity);
}
}

View File

@ -1,20 +1,31 @@
package org.gcube.resourcemanagement.manager.io.exceptions;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
/**
* Exception returned by the Resource Manager.
*
* @author Manuele Simi (ISTI CNR)
*
*/
public class CodeException extends Exception {
public class CodeException extends WebApplicationException {
private static final long serialVersionUID = 333945715086602250L;
private final int errorCode;
private final String errorMsg;
public CodeException(Response.Status status, RMCode code) {
super(Response.status(status).entity(new CodeEntity(new SerializableErrorCode(code.getId(), code.getMessage())))
.build());
this.errorMsg = code.getMessage();
this.errorCode = code.getId();
}
public CodeException(RMCode code) {
super();
super(Response.status(Response.Status.NOT_ACCEPTABLE)
.entity(new CodeEntity(new SerializableErrorCode(code.getId(), code.getMessage()))).build());
this.errorMsg = code.getMessage();
this.errorCode = code.getId();
}

View File

@ -8,16 +8,16 @@ package org.gcube.resourcemanagement.manager.io.exceptions;
*/
public enum RMCode implements ErrorCode {
INVALID_REQUEST(0, "The request is invalid"),
MISSING_PARAMETER(1,"Required query parameter is missing"),
MISSING_HEADER(2, "Required header is missing"),
CONTEXT_ALREADY_EXIST(3, "Required header is missing"),
INVALID_REQUEST(0, "The request is invalid."),
MISSING_PARAMETER(1,"Required query parameter is missing."),
MISSING_HEADER(2, "Required header is missing."),
CONTEXT_ALREADY_EXIST(3, "Context already exists at the same level of the hierarchy."),
INVALID_REQUEST_FOR_RR(4, "Failed to validate the request. The request was not submitted to the Resource Registry."),
GENERIC_ERROR_FROM_RR(0, "The Resource Registry returned an error.");
private final int id;
private final String msg;
private int id;
private String msg;
private RMCode(int id, String msg) {
this.id = id;
this.msg = msg;

View File

@ -1,29 +0,0 @@
package org.gcube.resourcemanagement.manager.io.exceptions;
/**
* Exception returned by the Resource Manager.
*
* @author Manuele Simi (ISTI CNR)
*
*/
public class RMException extends Exception {
private static final long serialVersionUID = 333945715086602250L;
private final int errorCode;
private final String errorMsg;
public RMException(RMCode code) {
this.errorMsg = code.getMessage();
this.errorCode = code.getId();
}
public int getErrorCode() {
return errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
}

View File

@ -0,0 +1,41 @@
package org.gcube.resourcemanagement.manager.io.exceptions;
/**
* Don't forget to comment!
*
* @author Manuele Simi (ISTI CNR)
*
*/
public class SerializableErrorCode implements ErrorCode {
private int id;
private String message;
public SerializableErrorCode() {}
/**
* @param id
* @param message
*/
public SerializableErrorCode(int id, String msg) {
this.id = id;
this.message = msg;
}
/* (non-Javadoc)
* @see org.gcube.resourcemanagement.manager.io.exceptions.ErrorCode#getId()
*/
@Override
public int getId() {
return this.id;
}
/* (non-Javadoc)
* @see org.gcube.resourcemanagement.manager.io.exceptions.ErrorCode#getMessage()
*/
@Override
public String getMessage() {
return this.message;
}
}

View File

@ -78,7 +78,7 @@
<groupId>org.gcube.information-system</groupId>
<artifactId>gcube-resources</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-context-client</artifactId>
@ -92,6 +92,12 @@
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>

View File

@ -1,8 +1,5 @@
package org.gcube.resourcemanagement.manager.webapp.context;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.gcube.resourcemanagement.manager.io.exceptions.CodeException;
@ -19,7 +16,7 @@ public class ResponseFromResourceRegistry {
private String message;
private List<? extends CodeException> exceptions = new ArrayList<>();
CodeException exception;
protected ResponseFromResourceRegistry() {}
@ -46,8 +43,8 @@ public class ResponseFromResourceRegistry {
* The {@link Exception} returned by the RR.
* @return the exception
*/
public Optional<? extends CodeException> getException() {
return Optional.ofNullable(success? null: this.exceptions.get(0));
public Optional<CodeException> getException() {
return Optional.ofNullable(success? null: this.exception);
}
/**
@ -55,10 +52,10 @@ public class ResponseFromResourceRegistry {
* @param e
* @return the response
*/
public static <E extends CodeException> ResponseFromResourceRegistry fromException(E e) {
public static ResponseFromResourceRegistry fromException(CodeException e) {
ResponseFromResourceRegistry response = new ResponseFromResourceRegistry();
response.success = false;
response.exceptions = Collections.singletonList(e);
response.exception = e;
return response;
}

View File

@ -10,7 +10,6 @@ import javax.ws.rs.core.Response.Status;
import org.gcube.informationsystem.model.entity.Context;
import static org.gcube.resourcemanagement.manager.io.rs.RMContextPath.*;
import org.gcube.resourcemanagement.manager.io.exceptions.CodeException;
import org.gcube.resourcemanagement.manager.webapp.ResourceInitializer;
import org.gcube.resourcemanagement.manager.webapp.context.CreateRequest;
import org.gcube.resourcemanagement.manager.webapp.context.ResponseFromResourceRegistry;
@ -34,7 +33,7 @@ public class RMContext {
*/
@POST
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public Response create(String json, @QueryParam(FORCE_RRURL_PARAM) String rrURL) throws CodeException {
public Response create(String json, @QueryParam(FORCE_RRURL_PARAM) String rrURL) {
logger.info("Requested to create context {} with json {}", Context.NAME, json);
logger.info("Force URL: " + rrURL);
Response response;

View File

@ -18,6 +18,7 @@ import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.impl.entity.ContextImpl;
import org.gcube.informationsystem.impl.utils.ISMapper;
import org.gcube.informationsystem.model.entity.Context;
import org.gcube.resourcemanagement.manager.io.exceptions.SerializableErrorCode;
import org.gcube.resourcemanagement.manager.io.rs.RMContextPath;
import org.gcube.resourcemanagement.manager.webapp.rs.RMContext;
import org.glassfish.jersey.server.ResourceConfig;
@ -110,12 +111,13 @@ public class RMContextTest extends JerseyTest {
Response create = target("context").queryParam(RMContextPath.FORCE_RRURL_PARAM, RR).request()
.post(Entity.entity(ISMapper.marshal(newContext), MediaType.APPLICATION_JSON + ";charset=UTF-8"));
SerializableErrorCode code = create.readEntity(SerializableErrorCode.class);
assertNotNull(create);
// TODO: to restore an expected success when create is fully
// functional
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
Status.CREATED.getStatusCode(), create.getStatus());
assertEquals("Context successfully created.", (String) create.readEntity(String.class));
Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus());
assertEquals("Context successfully created.", 3, code.getId());
} catch (JsonProcessingException e) {
assertFalse("Failed to marshal the context.", false);
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n</pattern>
</encoder>
</appender>
<logger name="org.gcube" level="INFO" />
<logger name="org.gcube.resourcemanagement" level="TRACE" />
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>