2018-12-04 12:06:22 +01:00
|
|
|
package org.gcube.gcat.utils;
|
|
|
|
|
2019-02-26 14:58:06 +01:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2019-01-10 12:29:47 +01:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
2019-02-26 14:58:06 +01:00
|
|
|
import javax.ws.rs.InternalServerErrorException;
|
2018-12-04 12:06:22 +01:00
|
|
|
import javax.ws.rs.WebApplicationException;
|
|
|
|
|
2019-01-10 12:29:47 +01:00
|
|
|
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
|
2018-12-04 12:06:22 +01:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.server.utils.url.EntityContext;
|
|
|
|
import org.gcube.gcat.persistence.ckan.CKAN;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
|
|
|
|
public class URIResolver {
|
|
|
|
|
|
|
|
private static final String CATALOGUE_CONTEXT = "gcube_scope";
|
|
|
|
private static final String ENTITY_TYPE = "entity_context";
|
|
|
|
private static final String ENTITY_NAME = "entity_name";
|
|
|
|
private static final String CATALOGUE_PLAIN_URL = "clear_url";
|
|
|
|
|
2019-02-26 14:58:06 +01:00
|
|
|
protected ObjectMapper mapper;
|
2018-12-04 12:06:22 +01:00
|
|
|
|
|
|
|
public URIResolver() {
|
2019-02-26 14:58:06 +01:00
|
|
|
this.mapper = new ObjectMapper();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected StringBuilder getStringBuilder(InputStream inputStream) throws IOException {
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
result.append(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2018-12-04 12:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getCatalogueItemURL(String name) {
|
|
|
|
try {
|
|
|
|
DataCatalogue dataCatalogue = CKAN.getCatalogue();
|
|
|
|
String uriResolverURL = dataCatalogue.getUriResolverUrl();
|
|
|
|
|
2019-02-26 14:58:06 +01:00
|
|
|
ObjectNode requestContent = mapper.createObjectNode();
|
2018-12-04 12:06:22 +01:00
|
|
|
requestContent.put(CATALOGUE_CONTEXT, ContextUtility.getCurrentContext());
|
|
|
|
requestContent.put(ENTITY_TYPE, EntityContext.PRODUCT.toString());
|
|
|
|
requestContent.put(ENTITY_NAME, name);
|
2019-02-26 14:58:06 +01:00
|
|
|
requestContent.put(CATALOGUE_PLAIN_URL, String.valueOf(true));
|
2018-12-04 12:06:22 +01:00
|
|
|
|
2019-01-10 12:29:47 +01:00
|
|
|
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(uriResolverURL);
|
2019-02-26 14:58:06 +01:00
|
|
|
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
|
|
|
|
// gxhttpStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
|
|
|
gxhttpStringRequest.isExternalCall(true);
|
|
|
|
String body = mapper.writeValueAsString(requestContent);
|
|
|
|
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(body);
|
2019-01-10 12:29:47 +01:00
|
|
|
|
2019-02-26 14:58:06 +01:00
|
|
|
if(httpURLConnection.getResponseCode()!=200) {
|
|
|
|
throw new InternalServerErrorException("Unable to get Item URL via URI Resolver");
|
|
|
|
}
|
2018-12-04 12:06:22 +01:00
|
|
|
|
2019-02-26 14:58:06 +01:00
|
|
|
String url = getStringBuilder(httpURLConnection.getInputStream()).toString();
|
2019-01-10 12:29:47 +01:00
|
|
|
|
2019-02-26 14:58:06 +01:00
|
|
|
return url;
|
|
|
|
} catch(WebApplicationException e) {
|
|
|
|
throw e;
|
2018-12-04 12:06:22 +01:00
|
|
|
} catch(Exception e) {
|
|
|
|
throw new WebApplicationException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|