Fixing APIs

This commit is contained in:
Luca Frosini 2024-03-26 18:16:55 +01:00
parent b8083f456c
commit 0a31aeb229
3 changed files with 59 additions and 27 deletions

View File

@ -0,0 +1,54 @@
package org.gcube.grsf.publisher.rest;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriInfo;
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
import org.gcube.gcat.api.GCatConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
/**
* @author Luca Frosini (ISTI - CNR)
*/
@RequestHeaders ({
@RequestHeader( name = "Authorization", description = "Bearer token, see <a href=\"https://dev.d4science.org/how-to-access-resources\">https://dev.d4science.org/how-to-access-resources</a>")
})
public class BaseREST {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Context
protected HttpHeaders httpHeaders;
@Context
protected UriInfo uriInfo;
protected static final String LOCATION_HEADER = "Location";
protected void setCalledMethod(String method) {
CalledMethodProvider.instance.set(method);
logger.info("{}", uriInfo.getAbsolutePath());
}
protected ResponseBuilder addLocation(ResponseBuilder responseBuilder, String id) {
return responseBuilder.header(LOCATION_HEADER,
String.format("%s/%s", uriInfo.getAbsolutePath().toString(), id));
}
protected String createCountJson(int count) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("{\"");
stringBuilder.append(GCatConstants.COUNT_KEY);
stringBuilder.append("\":");
stringBuilder.append(count);
stringBuilder.append("}");
return stringBuilder.toString();
}
}

View File

@ -10,7 +10,6 @@ import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.grsf.publisher.ckan.record.Record;
import org.slf4j.Logger;
@ -25,8 +24,9 @@ import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
@RequestHeaders ({
@RequestHeader( name = "Authorization", description = "Bearer token, see <a href=\"https://dev.d4science.org/how-to-access-resources\">https://dev.d4science.org/how-to-access-resources</a>")
})
public class BaseRESTAPIs<R extends Record> {
public class BaseRESTAPIs<R extends Record> extends BaseREST {
@SuppressWarnings("unused")
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Context
@ -49,19 +49,7 @@ public class BaseRESTAPIs<R extends Record> {
this.ID_PARAMETER = recordID;
this.reference = reference;
}
protected void setCalledMethod(String method) {
CalledMethodProvider.instance.set(method);
logger.info("{}", uriInfo.getAbsolutePath());
}
protected ResponseBuilder addLocation(ResponseBuilder responseBuilder, String id) {
return responseBuilder.header(LOCATION_HEADER,
String.format("%s/%s", uriInfo.getAbsolutePath().toString(), id));
}
protected R getInstance() {
try {
R record = reference.newInstance();
@ -160,16 +148,6 @@ public class BaseRESTAPIs<R extends Record> {
return delete(id, true);
}
protected String createCountJson(int count) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("{\"");
stringBuilder.append(GCatConstants.COUNT_KEY);
stringBuilder.append("\":");
stringBuilder.append(count);
stringBuilder.append("}");
return stringBuilder.toString();
}
protected String resultAsJsonAPI(String data) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("{ \"data\":");

View File

@ -26,15 +26,15 @@ import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
//import org.gcube.common.authorization.control.annotations.AuthorizationControl;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.gcat.annotation.PURGE;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.gcat.api.roles.Role;
import org.gcube.gcat.configuration.CatalogueConfigurationFactory;
import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration;
import org.gcube.gcat.persistence.ckan.CKANUser;
import org.gcube.gcat.persistence.ckan.cache.CKANUserCache;
import org.gcube.gcat.rest.BaseREST;
import org.gcube.grsf.publisher.annotation.PATCH;
import org.gcube.grsf.publisher.annotation.PURGE;
import org.gcube.grsf.publisher.rest.BaseREST;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;