Merge branch 'feature/20225' of gitea@code-repo.d4science.org:gCubeSystem/gcat.git into feature/20225

This commit is contained in:
Luca Frosini 2021-03-29 11:01:01 +02:00
commit e90829157a
1 changed files with 31 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package org.gcube.gcat.persistence.ckan; package org.gcube.gcat.persistence.ckan;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.util.HashMap; import java.util.HashMap;
@ -12,18 +13,18 @@ import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotFoundException; import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.gcat.utils.HTTPUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException; import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.NullNode; import org.gcube.com.fasterxml.jackson.databind.node.NullNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.gcat.utils.HTTPUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
@ -129,6 +130,7 @@ public abstract class CKAN {
} }
} }
/** /**
* Validate the CKAN response and return the * Validate the CKAN response and return the
* @param json * @param json
@ -241,8 +243,31 @@ public abstract class CKAN {
return gxhttpStringRequest; return gxhttpStringRequest;
} }
protected String getResultAsString(HttpURLConnection httpURLConnection) throws IOException {
int responseCode = httpURLConnection.getResponseCode();
if(responseCode >= Status.BAD_REQUEST.getStatusCode()) {
Status status = Status.fromStatusCode(responseCode);
InputStream inputStream = httpURLConnection.getErrorStream();
StringBuilder result = HTTPUtility.getStringBuilder(inputStream);
logger.trace(result.toString());
try {
JsonNode jsonNode = getAsJsonNode(result.toString());
JsonNode error = jsonNode.get(ERROR_KEY);
throw new WebApplicationException(getAsString(error), status);
}catch (WebApplicationException e) {
throw e;
}catch (Exception e) {
throw new WebApplicationException(result.toString(), status);
}
}
InputStream inputStream = httpURLConnection.getInputStream();
String ret = HTTPUtility.getStringBuilder(inputStream).toString();
logger.trace("Got Respose is {}", ret);
return ret;
}
protected String getResultAndValidate(HttpURLConnection httpURLConnection) throws IOException { protected String getResultAndValidate(HttpURLConnection httpURLConnection) throws IOException {
String ret = HTTPUtility.getResultAsString(httpURLConnection); String ret = getResultAsString(httpURLConnection);
logger.trace("Got Respose is {}", ret); logger.trace("Got Respose is {}", ret);
result = validateCKANResponse(ret); result = validateCKANResponse(ret);
if(result instanceof NullNode) { if(result instanceof NullNode) {