use of it.unibo.arces.wot.sepa.commons.request.RegistrationRequest in project SEPA by arces-wot.
the class SPARQL11SEProtocol method executeSPARQL11SEPrimitive.
protected Response executeSPARQL11SEPrimitive(SPARQL11SEPrimitive op, Object request) {
// Create the HTTPS request
URI uri;
String path = null;
int port = 0;
// Headers and body
String contentType = null;
ByteArrayEntity body = null;
String accept = null;
String authorization = null;
switch(op) {
case SUBSCRIBE:
SubscribeRequest subscribe = (SubscribeRequest) request;
return wsClient.subscribe(subscribe.getSPARQL());
case UNSUBSCRIBE:
UnsubscribeRequest unsubscribe = (UnsubscribeRequest) request;
return wsClient.unsubscribe(unsubscribe.getSubscribeUUID());
// }
default:
break;
}
switch(op) {
case REGISTER:
path = properties.getRegisterPath();
port = properties.getHttpsPort();
accept = "application/json";
contentType = "application/json";
String identity = (String) request;
try {
body = new ByteArrayEntity(new RegistrationRequest(identity).toString().getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
return new ErrorResponse(500, e.getMessage());
}
break;
case REQUESTTOKEN:
String basic;
try {
basic = properties.getBasicAuthorization();
} catch (SEPASecurityException e2) {
return new ErrorResponse(500, e2.getMessage());
}
if (basic == null)
return new ErrorResponse(0, 401, "Basic authorization in null. Register first");
path = properties.getTokenRequestPath();
port = properties.getHttpsPort();
authorization = "Basic " + basic;
contentType = "application/json";
accept = "application/json";
break;
case SECUREUPDATE:
path = properties.getSecurePath() + properties.getUpdatePath();
port = properties.getHttpsPort();
accept = "text/plain";
contentType = "application/x-www-form-urlencoded";
try {
authorization = "Bearer " + properties.getAccessToken();
} catch (SEPASecurityException e2) {
return new ErrorResponse(500, e2.getMessage());
}
String encodedContent;
try {
encodedContent = URLEncoder.encode(((UpdateRequest) request).getSPARQL(), "UTF-8");
} catch (UnsupportedEncodingException e) {
return new ErrorResponse(500, e.getMessage());
}
body = new ByteArrayEntity(("update=" + encodedContent).getBytes());
body.setContentType(contentType);
break;
case SECUREQUERY:
path = properties.getSecurePath() + properties.getQueryPath();
port = properties.getHttpsPort();
accept = "application/sparql-results+json";
contentType = "application/sparql-query";
try {
authorization = "Bearer " + properties.getAccessToken();
} catch (SEPASecurityException e2) {
return new ErrorResponse(500, e2.getMessage());
}
try {
body = new ByteArrayEntity(((QueryRequest) request).getSPARQL().getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
return new ErrorResponse(500, e.getMessage());
}
break;
default:
break;
}
// POST request
try {
uri = new URI("https", null, properties.getHost(), port, path, null, null);
} catch (URISyntaxException e1) {
return new ErrorResponse(500, e1.getMessage());
}
HttpUriRequest httpRequest = new HttpPost(uri);
if (contentType != null)
httpRequest.setHeader("Content-Type", contentType);
if (accept != null)
httpRequest.setHeader("Accept", accept);
if (authorization != null)
httpRequest.setHeader("Authorization", authorization);
if (body != null)
((HttpPost) httpRequest).setEntity(body);
logger.debug("Request: " + httpRequest);
// HTTP request execution
CloseableHttpClient httpclient;
try {
httpclient = new SSLSecurityManager("TLSv1", "sepa.jks", "sepa2017", "sepa2017").getSSLHttpClient();
} catch (KeyManagementException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException e) {
return new ErrorResponse(500, e.getMessage());
}
CloseableHttpResponse response = null;
String jsonResponse = null;
try {
long timing = System.nanoTime();
try {
response = httpclient.execute(httpRequest);
} catch (IOException e) {
return new ErrorResponse(500, e.getMessage());
}
timing = System.nanoTime() - timing;
logger.debug("Response: " + response);
if (op.equals(SPARQL11SEPrimitive.REGISTER))
logger.debug("REGISTER " + timing / 1000000 + " ms");
else if (op.equals(SPARQL11SEPrimitive.REQUESTTOKEN))
logger.debug("TOKEN " + timing / 1000000 + " ms");
else if (op.equals(SPARQL11SEPrimitive.SECUREQUERY))
logger.debug("SECURE_QUERY " + timing / 1000000 + " ms");
else if (op.equals(SPARQL11SEPrimitive.SECUREUPDATE))
logger.debug("SECURE_UPDATE " + timing / 1000000 + " ms");
HttpEntity entity = response.getEntity();
try {
jsonResponse = EntityUtils.toString(entity, Charset.forName("UTF-8"));
} catch (ParseException | IOException e) {
return new ErrorResponse(500, e.getMessage());
}
try {
EntityUtils.consume(entity);
} catch (IOException e) {
return new ErrorResponse(500, e.getMessage());
}
} finally {
try {
response.close();
} catch (IOException e) {
return new ErrorResponse(500, e.getMessage());
}
}
// Parsing the response
try {
return parseSPARQL11SEResponse(jsonResponse, op);
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
return new ErrorResponse(500, e.getMessage());
}
}
Aggregations