Switched to UriResolverManager

This commit is contained in:
Luca Frosini 2021-12-16 15:13:54 +01:00
parent 65aaefa8d2
commit a8732e2045
5 changed files with 23 additions and 60 deletions

View File

@ -9,9 +9,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Added support for moderation [#21342]
- Added support for IAM authz [#21628]
- Added items bulk delete/purge [#21685]
- Using UriResolverManager to get item URL in place of direct HTTP call [22549]
## [v2.0.0]
## [v2.0.0] [r5.2.0] - 2021-05-04
- Fixed retrieving of filename from content-disposition http header used to persist a resource [#21216]
- Fixed author and maintainer name and email [#21059] [#21189]
@ -20,14 +21,15 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Switched JSON management to gcube-jackson [#19735]
- Added support to publish an item organizations not matching the current context [#19365]
## [v1.4.5]
## [v1.4.5] [r5.1.0] - 2021-03-31
- Removed 'owner_org' field from result when reading an item [#20919]
- Dirty patched item validator [#20965]
- Improved error message return with message got from CKAN [#19516]
## [v1.4.4]
## [v1.4.4] [r5.0.0] - 2021-02-24
- Added count method for Item collection [#20627]
- Added count method for Organization, Group and Profile collection [#20629]

View File

@ -94,7 +94,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.gcube.portlets.user</groupId>
<artifactId>uri-resolver-manager</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>

View File

@ -665,8 +665,7 @@ public class CKANPackage extends CKAN implements Moderated {
protected String addItemURLViaResolver(JsonNode jsonNode) {
// Adding Item URL via Resolver
URIResolver uriResolver = new URIResolver();
String catalogueItemURL = uriResolver.getCatalogueItemURL(name);
String catalogueItemURL = URIResolver.getCatalogueItemURL(name);
addExtraField(jsonNode, EXTRAS_ITEM_URL_KEY, catalogueItemURL);
return catalogueItemURL;
}

View File

@ -1,24 +1,14 @@
package org.gcube.gcat.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.gcat.persistence.ckan.CKANInstance;
import org.gcube.portlets.user.uriresolvermanager.UriResolverManager;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
public class URIResolver {
public abstract class URIResolver {
private static final String CATALOGUE_CONTEXT = "gcube_scope";
private static final String ENTITY_TYPE = "entity_context";
@ -26,47 +16,16 @@ public class URIResolver {
private static final String DATASET = "dataset";
protected ObjectMapper mapper;
public URIResolver() {
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;
}
public String getCatalogueItemURL(String name) {
public static String getCatalogueItemURL(String name) {
try {
String uriResolverURL = CKANInstance.getInstance().getUriResolverURL();
ObjectNode requestContent = mapper.createObjectNode();
String context = SecretManager.instance.get().getContext();
requestContent.put(CATALOGUE_CONTEXT, context);
requestContent.put(ENTITY_TYPE, DATASET);
requestContent.put(ENTITY_NAME, name);
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(uriResolverURL);
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
gxhttpStringRequest.isExternalCall(true);
String body = mapper.writeValueAsString(requestContent);
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(body);
if(httpURLConnection.getResponseCode() != 200) {
throw new InternalServerErrorException("Unable to get Item URL via URI Resolver");
}
String url = getStringBuilder(httpURLConnection.getInputStream()).toString();
UriResolverManager uriResolverManager = new UriResolverManager("CTLG");
Map<String, String> params = new HashMap<>();
params.put(CATALOGUE_CONTEXT, context);
params.put(ENTITY_TYPE, DATASET);
params.put(ENTITY_NAME, name);
String url = uriResolverManager.getLink(params, false);
return url;
} catch(WebApplicationException e) {
throw e;

View File

@ -11,8 +11,7 @@ public class URIResolverTest extends ContextTest {
@Test
public void getURL() {
URIResolver uriResolver = new URIResolver();
String catalogueItemURL = uriResolver.getCatalogueItemURL("my_first_restful_transaction_model");
String catalogueItemURL = URIResolver.getCatalogueItemURL("my_first_restful_transaction_model");
logger.debug("Item URL is {}", catalogueItemURL);
}