Created prototype implementation of LB2PC proxy
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/infrastructure-tests@165205 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
a8bf4f14e3
commit
d28b8595f8
7
pom.xml
7
pom.xml
|
@ -62,6 +62,13 @@
|
|||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Document Store Lib -->
|
||||
<dependency>
|
||||
|
|
|
@ -2,18 +2,10 @@ package org.gcube.lb2pc;
|
|||
|
||||
public class HttpRequest {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private final Proxy ok;
|
||||
|
||||
/**
|
||||
* @param proxy
|
||||
*/
|
||||
HttpRequest(Proxy proxy) {
|
||||
ok = proxy;
|
||||
public HttpRequest() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public String getHeader(String string) {
|
||||
return null;
|
||||
}
|
||||
|
@ -26,13 +18,26 @@ public class HttpRequest {
|
|||
return null;
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
String contentType = "application/json";
|
||||
if(this.getHeader("Content-Type")!=null) {
|
||||
contentType = this.getHeader("Content-Type");
|
||||
} else if(this.getHeader("Accept")!=null) {
|
||||
contentType = selectBestContenType(this.getHeader("Accept"));
|
||||
}
|
||||
return contentType;
|
||||
}
|
||||
|
||||
protected String getTransactionURI(HttpRequest request) {
|
||||
return request.getHeader("X-Transaction-URI");
|
||||
}
|
||||
|
||||
private String selectBestContenType(String header) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,118 +1,196 @@
|
|||
package org.gcube.lb2pc;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class Proxy {
|
||||
|
||||
public HttpResponse service(HttpRequest request) throws Exception {
|
||||
public HttpResponse onRequest(HttpRequest request) throws Exception {
|
||||
|
||||
HttpMethod httpMethod = request.getMethod();
|
||||
|
||||
if(httpMethod == HttpMethod.OPTIONS) {
|
||||
sendTransactionServerList();
|
||||
switch(httpMethod) {
|
||||
case OPTIONS:
|
||||
return sendSupportedTransactionServiceList(request);
|
||||
|
||||
case POST:
|
||||
case HEAD:
|
||||
case GET:
|
||||
case PUT:
|
||||
case DELETE:
|
||||
String transactionURI = request.getTransactionURI(request);
|
||||
|
||||
if(transactionURI!=null) {
|
||||
// Transactional Client
|
||||
return manageTransactionAction(request, transactionURI);
|
||||
}else {
|
||||
// Non-Transactional Client
|
||||
return createMiniTransaction(request);
|
||||
}
|
||||
|
||||
default:
|
||||
return sendErrorResponseToClient(HttpStatusCode._405, "Unsupported Operation");
|
||||
|
||||
}
|
||||
|
||||
String transactionURI = request.getHeader("X-Transaction-URI");
|
||||
String requestURI = request.getRequestURI();
|
||||
String contentType = "application/json";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected HttpResponse manageTransactionAction(HttpRequest request, String transactionURI) throws Exception {
|
||||
|
||||
if(transactionURI!=null) {
|
||||
// Transactional Client
|
||||
|
||||
if(request.getHeader("Accept")!=null) {
|
||||
contentType = request.getHeader("Accept");
|
||||
}
|
||||
HttpMethod httpMethod = request.getMethod();
|
||||
String requestURI = request.getRequestURI();
|
||||
String contentType = request.getContentType();
|
||||
String lockURI = request.getHeader("X-Lock-URI");
|
||||
|
||||
if(lockURI!=null) {
|
||||
// Client already own a lock, verifying and upgrading if needed
|
||||
verifyAndUpgradeLock(httpMethod, contentType, lockURI, transactionURI, requestURI);
|
||||
|
||||
String shadowResourceURI = getShadowResourceURI(transactionURI, requestURI);
|
||||
logRequest(shadowResourceURI, contentType, httpMethod, null);
|
||||
|
||||
HttpResponse actionResponse = forwardAction(request);
|
||||
|
||||
String lockURI = request.getHeader("X-Lock-URI");
|
||||
|
||||
if(lockURI!=null) {
|
||||
// Client already own a lock, verifying and upgrading if needed
|
||||
verifyAndUpgradeLock(httpMethod, contentType, lockURI, transactionURI, requestURI);
|
||||
|
||||
String shadowResourceURI = ShadowResource.getShadowResourceURI(transactionURI, requestURI);
|
||||
logRequest(shadowResourceURI, contentType, httpMethod, null);
|
||||
|
||||
HttpResponse actionResponse = forwardAction(request);
|
||||
|
||||
actionResponse.setHeader("X-Lock-URI", lockURI);
|
||||
actionResponse.setHeader("X-Transaction-URI", transactionURI);
|
||||
return replyToClient(actionResponse);
|
||||
|
||||
}else {
|
||||
// Client does not own a lock
|
||||
|
||||
if(httpMethod != HttpMethod.POST && httpMethod != HttpMethod.PUT &&
|
||||
httpMethod != HttpMethod.GET && httpMethod != HttpMethod.HEAD) {
|
||||
// The first action on a resource must be a GET or a CREATE
|
||||
sendErrorResponseToClient(HttpStatusCode._403, "A resource must be read before to be able to modify or delete it.");
|
||||
}
|
||||
|
||||
HttpResponse actionResponse = null;
|
||||
|
||||
Lock lock = createLock(httpMethod, transactionURI, requestURI);
|
||||
lockURI = lock.getLockURI();
|
||||
|
||||
String resource = null;
|
||||
String shadowResourceURI= null;
|
||||
|
||||
switch(httpMethod) {
|
||||
case PUT:
|
||||
|
||||
HttpResponse r = createHttpRequest(HttpMethod.HEAD, null, null, requestURI);
|
||||
if(r.getHttpStatusCode() == HttpStatusCode._204 || (r.getHttpStatusCode().getCode()>=200 && r.getHttpStatusCode().getCode()<300)) {
|
||||
lock.delete();
|
||||
sendErrorResponseToClient(HttpStatusCode._403, "A resource must be read before to be able to modify or delete it.");
|
||||
}else {
|
||||
// Create with PUT
|
||||
shadowResourceURI = createShadowResource(transactionURI, requestURI, lockURI, contentType, null);
|
||||
logRequest(shadowResourceURI, contentType, httpMethod, request.getContent());
|
||||
actionResponse = forwardAction(request);
|
||||
}
|
||||
break;
|
||||
|
||||
case POST:
|
||||
HttpResponse getResponse = getResourceOnEffectiveService(requestURI);
|
||||
resource = getResponse.getContent();
|
||||
|
||||
shadowResourceURI = createShadowResource(transactionURI, requestURI, lockURI, contentType, resource);
|
||||
logRequest(shadowResourceURI, contentType, httpMethod, request.getContent());
|
||||
|
||||
actionResponse = forwardAction(request);
|
||||
|
||||
String locationURI = actionResponse.getHeader("Location");
|
||||
logRequest(shadowResourceURI, contentType, httpMethod, null, locationURI);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
HttpResponse response = getResourceOnEffectiveService(requestURI);
|
||||
resource = response.getContent();
|
||||
|
||||
shadowResourceURI = createShadowResource(transactionURI, requestURI, lockURI, contentType, resource);
|
||||
|
||||
// We don't need any log
|
||||
// logRequest(shadowResourceURI, contentType, httpMethod, requestURI, request.getContent());
|
||||
|
||||
actionResponse = forwardAction(request);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
actionResponse.setHeader("X-Lock-URI", lockURI);
|
||||
|
||||
actionResponse.setHeader("X-Transaction-URI", transactionURI);
|
||||
|
||||
return replyToClient(actionResponse);
|
||||
}
|
||||
|
||||
actionResponse.setHeader("X-Lock-URI", lockURI);
|
||||
return createResponse(actionResponse);
|
||||
|
||||
}else {
|
||||
// Non-Transactional Client
|
||||
return createMiniTransaction();
|
||||
// Client does not own a lock
|
||||
|
||||
if(httpMethod != HttpMethod.POST && httpMethod != HttpMethod.PUT &&
|
||||
httpMethod != HttpMethod.GET && httpMethod != HttpMethod.HEAD) {
|
||||
// The first action on a resource must be a GET or a CREATE
|
||||
sendErrorResponseToClient(HttpStatusCode._403, "A resource must be read before to be able to modify or delete it.");
|
||||
}
|
||||
|
||||
HttpResponse actionResponse = null;
|
||||
|
||||
Lock lock = createLock(httpMethod, transactionURI, requestURI);
|
||||
lockURI = lock.getLockURI();
|
||||
|
||||
String resource = null;
|
||||
String shadowResourceURI= null;
|
||||
|
||||
switch(httpMethod) {
|
||||
case PUT:
|
||||
|
||||
HttpResponse r = createHttpRequest(HttpMethod.HEAD, null, null, requestURI);
|
||||
if(r.getHttpStatusCode() == HttpStatusCode._204 || (r.getHttpStatusCode().getCode()>=200 && r.getHttpStatusCode().getCode()<300)) {
|
||||
lock.delete();
|
||||
sendErrorResponseToClient(HttpStatusCode._403, "A resource must be read before to be able to modify or delete it.");
|
||||
}else {
|
||||
// Create with PUT
|
||||
shadowResourceURI = createShadowResource(transactionURI, requestURI, lockURI, contentType, null);
|
||||
logRequest(shadowResourceURI, contentType, httpMethod, request.getContent());
|
||||
actionResponse = forwardAction(request);
|
||||
}
|
||||
break;
|
||||
|
||||
case POST:
|
||||
HttpResponse getResponse = getResourceOnEffectiveService(requestURI);
|
||||
resource = getResponse.getContent();
|
||||
|
||||
shadowResourceURI = createShadowResource(transactionURI, requestURI, lockURI, contentType, resource);
|
||||
logRequest(shadowResourceURI, contentType, httpMethod, request.getContent());
|
||||
|
||||
actionResponse = forwardAction(request);
|
||||
|
||||
String locationURI = actionResponse.getHeader("Location");
|
||||
logRequest(shadowResourceURI, contentType, httpMethod, null, locationURI);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
HttpResponse response = getResourceOnEffectiveService(requestURI);
|
||||
resource = response.getContent();
|
||||
|
||||
shadowResourceURI = createShadowResource(transactionURI, requestURI, lockURI, contentType, resource);
|
||||
|
||||
// Optimization We don't need any log here
|
||||
// logRequest(shadowResourceURI, contentType, httpMethod, requestURI, request.getContent());
|
||||
|
||||
actionResponse = forwardAction(request);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
actionResponse.setHeader("X-Lock-URI", lockURI);
|
||||
|
||||
actionResponse.setHeader("X-Transaction-URI", transactionURI);
|
||||
|
||||
return createResponse(actionResponse);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String getShadowResourceURI(String transactionURI, String requestURI) throws MalformedURLException {
|
||||
return ShadowResource.getShadowResourceURI(transactionURI, requestURI);
|
||||
}
|
||||
|
||||
|
||||
protected HttpResponse createMiniTransaction(HttpRequest request) throws Exception {
|
||||
String contentType = request.getContentType();
|
||||
|
||||
String transactionURI = createTransaction(contentType);
|
||||
|
||||
HttpResponse actionResponse = manageTransactionAction(request, transactionURI);
|
||||
|
||||
commitTransation(transactionURI);
|
||||
|
||||
return actionResponse;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void commitTransation(String transactionURI) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
protected String createTransaction(String contentType) {
|
||||
HttpResponse createTransactionResponse = createHttpRequest(HttpMethod.POST, contentType, getOneTransactionServiceURI());
|
||||
if(createTransactionResponse.getHttpStatusCode() != HttpStatusCode._201) {
|
||||
sendErrorResponseToClient(HttpStatusCode._503, "Please try later");
|
||||
}
|
||||
return createTransactionResponse.getHeader("Location");
|
||||
}
|
||||
|
||||
|
||||
private String getOneTransactionServiceURI() {
|
||||
List<String> list = getTransactionServiceURIs();
|
||||
Random random = new Random();
|
||||
return list.get(random.nextInt(list.size()));
|
||||
}
|
||||
|
||||
private List<String> getTransactionServiceURIs() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private HttpResponse sendSupportedTransactionServiceList(HttpRequest request) {
|
||||
String contentType = request.getContentType();
|
||||
List<String> list = getTransactionServiceURIs();
|
||||
// TODO create response with Transaction Services
|
||||
return null;
|
||||
}
|
||||
|
||||
private static HttpResponse createResponse(HttpResponse actionResponse) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static HttpResponse forwardAction(HttpRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private HttpResponse getResourceOnEffectiveService(String resourceURI) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void logRequest(String shadowResourceURI, String contentType, HttpMethod httpMethod, String resource)
|
||||
|
@ -124,28 +202,6 @@ public class Proxy {
|
|||
Log log = new Log(httpMethod, resource, locationURI);
|
||||
log.create(shadowResourceURI, contentType);
|
||||
}
|
||||
|
||||
private HttpResponse createMiniTransaction() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
private void sendTransactionServerList() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
private static HttpResponse replyToClient(HttpResponse actionResponse) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static HttpResponse forwardAction(HttpRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private HttpResponse getResourceOnEffectiveService(String resourceURI) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private String createShadowResource(String transactionURI, String requestURI, String lockURI, String contentType,
|
||||
String resource) throws MalformedURLException {
|
||||
|
@ -187,8 +243,8 @@ public class Proxy {
|
|||
|
||||
}
|
||||
|
||||
public static void sendErrorResponseToClient(HttpStatusCode name, String string) {
|
||||
return;
|
||||
public static HttpResponse sendErrorResponseToClient(HttpStatusCode name, String string) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue