Introduce the validation step for the request. Handle exceptions. More structure on the response.
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@160614 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
7f5de4c5af
commit
ac8d0cf465
|
@ -24,9 +24,20 @@ public final class CreateRequest extends RequestToResourceRegistry {
|
|||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.gcube.resourcemanagement.manager.webapp.context.RequestToResourceRegistry#validate()
|
||||
*/
|
||||
@Override
|
||||
public ResponseFromResourceRegistry submit() {
|
||||
return new ResponseFromResourceRegistry();
|
||||
public CreateRequest validate() throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.gcube.resourcemanagement.manager.webapp.context.RequestToResourceRegistry#send()
|
||||
*/
|
||||
@Override
|
||||
protected ResponseFromResourceRegistry send() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,7 +11,8 @@ public abstract class RequestToResourceRegistry {
|
|||
protected String forceURL = "";
|
||||
|
||||
/**
|
||||
* Forces to use a RR instance at the given url.
|
||||
* Forces to use a RR instance at the given url.
|
||||
*
|
||||
* @param forceURL
|
||||
*/
|
||||
public RequestToResourceRegistry forceURL(String forceURL) {
|
||||
|
@ -20,11 +21,29 @@ public abstract class RequestToResourceRegistry {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the request.
|
||||
*
|
||||
* @return the response from the service.
|
||||
*/
|
||||
abstract public RequestToResourceRegistry validate() throws Exception;
|
||||
|
||||
public ResponseFromResourceRegistry submit() {
|
||||
try {
|
||||
this.validate();
|
||||
} catch (Exception e) {
|
||||
ResponseFromResourceRegistry response = ResponseFromResourceRegistry.fromException(e).setMessage(
|
||||
"Failed to validate the request. The request was not submitted to the Resource Registry.");
|
||||
return response;
|
||||
}
|
||||
return this.send();
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits the request to the RR.
|
||||
*
|
||||
* @return the response from the service.
|
||||
*/
|
||||
abstract public ResponseFromResourceRegistry submit();
|
||||
abstract protected ResponseFromResourceRegistry send();
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package org.gcube.resourcemanagement.manager.webapp.context;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
|
@ -14,25 +17,47 @@ public class ResponseFromResourceRegistry {
|
|||
|
||||
private String message;
|
||||
|
||||
private List<? extends Exception> exceptions = new ArrayList<>();
|
||||
|
||||
protected ResponseFromResourceRegistry() {}
|
||||
|
||||
public boolean wasSuccessful() {return success;}
|
||||
|
||||
/**
|
||||
* The message associated to the response
|
||||
* Gets the message associated to the response
|
||||
* @return the message
|
||||
*/
|
||||
public Optional<String> getMessage() {
|
||||
return Optional.ofNullable(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the message for the response.
|
||||
* @param message the message
|
||||
*/
|
||||
public ResponseFromResourceRegistry setMessage(String message) {
|
||||
this.message = message;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@link Exception} returned by the RR.
|
||||
* @return the exception
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <E extends Exception> Optional<E> getException() {
|
||||
return Optional.ofNullable(success? null: (E) new Exception());
|
||||
public Optional<? extends Exception> getException() {
|
||||
return Optional.ofNullable(success? null: this.exceptions.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a response from an exception.
|
||||
* @param e
|
||||
* @return the response
|
||||
*/
|
||||
public static <E extends Exception> ResponseFromResourceRegistry fromException(E e) {
|
||||
ResponseFromResourceRegistry response = new ResponseFromResourceRegistry();
|
||||
response.success = false;
|
||||
response.exceptions = Collections.singletonList(e);
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -30,8 +30,7 @@ public class RMContext {
|
|||
*/
|
||||
@POST
|
||||
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
||||
public Response create(String json)
|
||||
throws IllegalArgumentException {
|
||||
public Response create(String json) {
|
||||
logger.info("Requested to create context {} with json {}", Context.NAME, json);
|
||||
Response response;
|
||||
ContextHolder holder = new ContextHolder(json);
|
||||
|
|
Loading…
Reference in New Issue