Start to model the request/response interactions with the Resource Registry.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@160523 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Manuele Simi 2017-12-15 04:04:27 +00:00
parent 34bb1f57ff
commit ca5bc87382
8 changed files with 164 additions and 17 deletions

View File

@ -20,4 +20,4 @@ public class ResourceInitializer extends ResourceConfig {
packages(RMContext.class.getPackage().toString());
}
}
}

View File

@ -0,0 +1,34 @@
package org.gcube.resourcemanagement.manager.webapp.context;
import org.gcube.informationsystem.impl.utils.ISMapper;
import org.gcube.informationsystem.model.entity.Context;
/**
* Hold a context before sending it to the Resource Registry.
*
* @author Manuele Simi (ISTI-CNR)
*
*/
public class ContextHolder {
private final Context inputContext;
/**
* Create a new holder for the serialized context.
* @param serialization a serialization in the JSON formate
*/
public ContextHolder(String serialization) {
try {
this.inputContext = ISMapper.unmarshal(Context.class, serialization);
} catch (Exception e) {
throw new IllegalArgumentException("The context is syntactically not valid");
}
}
protected Context getContext() {
return inputContext;
}
}

View File

@ -0,0 +1,29 @@
package org.gcube.resourcemanagement.manager.webapp.context;
import org.gcube.informationsystem.model.entity.Context;
/**
* Actions that can be performed on the context.
*
* @author Manuele Simi (ISTI-CNR)
*
*/
public final class CreateRequest extends RequestToResourceRegistry {
private Context context;
private CreateRequest(Context context) {
this.context = context;
}
public static CreateRequest fromHolder(ContextHolder holder) {
return new CreateRequest(holder.getContext());
}
@Override
public ResponseFromResourceRegistry submit() {
return new ResponseFromResourceRegistry();
}
}

View File

@ -0,0 +1,30 @@
package org.gcube.resourcemanagement.manager.webapp.context;
/**
* Base request to an operation exposed by the Resource Registry.
*
* @author Manuele Simi (ISTI-CNR)
*
*/
public abstract class RequestToResourceRegistry {
protected String forceURL = "";
/**
* Forces to use a RR instance at the given url.
* @param forceURL
*/
public RequestToResourceRegistry forceURL(String forceURL) {
if (!forceURL.isEmpty())
this.forceURL = forceURL;
return this;
}
/**
* Submits the request to the RR.
* @return the response from the service.
*/
abstract public ResponseFromResourceRegistry submit();
}

View File

@ -0,0 +1,38 @@
package org.gcube.resourcemanagement.manager.webapp.context;
import java.util.Optional;
/**
* The response to a {@link RequestToResourceRegistry}.
*
* @author Manuele Simi (ISTI CNR)
*
*/
public class ResponseFromResourceRegistry {
private boolean success = false;
private String message;
protected ResponseFromResourceRegistry() {}
public boolean wasSuccessful() {return success;}
/**
* The message associated to the response
* @return the message
*/
public Optional<String> getMessage() {
return Optional.ofNullable(message);
}
/**
* The {@link Exception} returned by the RR.
* @return the exception
*/
@SuppressWarnings("unchecked")
public <E extends Exception> E getException() {return success? null: (E) new Exception();}
}

View File

@ -0,0 +1,12 @@
package org.gcube.resourcemanagement.manager.webapp.context;
/**
* Validate a context before operating on it.
* @author Manuele Simi (ISTI-CNR)
*
*/
public class Validator {
}

View File

@ -0,0 +1,8 @@
/**
*
* Logic to manage contexts within the webapp.
*
* @author Manuele Simi (ISTI-CNR)
*
*/
package org.gcube.resourcemanagement.manager.webapp.context;

View File

@ -1,24 +1,19 @@
package org.gcube.resourcemanagement.manager.webapp.rs;
import java.io.IOException;
import java.util.Objects;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.gcube.informationsystem.impl.utils.ISMapper;
import org.gcube.informationsystem.model.entity.Context;
import org.gcube.resourcemanagement.manager.io.rs.RMContextPath;
import org.gcube.resourcemanagement.manager.webapp.ResourceInitializer;
import org.gcube.resourcemanagement.manager.webapp.context.CreateRequest;
import org.gcube.resourcemanagement.manager.webapp.context.ContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
/**
* Methods for manipulating {@link Context}s.
*
@ -36,15 +31,16 @@ public class RMContext {
@POST
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public Response create(String json)
throws JsonParseException, JsonMappingException, IOException, IllegalArgumentException {
throws IllegalArgumentException {
logger.info("Requested to create context {} with json {}", Context.NAME, json);
Context inputContext = ISMapper.unmarshal(Context.class, json);
if (Objects.nonNull(inputContext.getParent()))
return Response.status(Status.BAD_REQUEST).build();
String response = "All good";
// TODO
return Response.status(Status.CREATED).entity(response).type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
.build();
Response response;
ContextHolder holder = new ContextHolder(json);
if (CreateRequest.fromHolder(holder).forceURL("").submit().wasSuccessful()) {
response = Response.status(Status.CREATED).entity("Context successfully created").type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
.build();
} else {
response = Response.status(Status.BAD_REQUEST).build();;
}
return response;
}
}