Refs #9966: Issues with json serialization for the registry
Task-Url: https://support.d4science.org/issues/9966 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-api@158162 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
d7281a3548
commit
8b922c0f3a
|
@ -35,8 +35,7 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
public class HTTPCall {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(HTTPCall.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(HTTPCall.class);
|
||||
|
||||
public static final String APPLICATION_JSON_CHARSET_UTF_8 = "application/json;charset=UTF-8";
|
||||
|
||||
|
@ -63,8 +62,7 @@ public class HTTPCall {
|
|||
this.userAgent = userAgent;
|
||||
}
|
||||
|
||||
protected String getParametersDataString(
|
||||
Map<String, ? extends Object> parameters)
|
||||
protected String getParametersDataString(Map<String, ? extends Object> parameters)
|
||||
throws UnsupportedEncodingException {
|
||||
|
||||
if (parameters == null) {
|
||||
|
@ -92,19 +90,19 @@ public class HTTPCall {
|
|||
StringWriter stringWriter = new StringWriter();
|
||||
stringWriter.append(address);
|
||||
|
||||
if(address.endsWith(PATH_SEPARATOR)){
|
||||
if(path.startsWith(PATH_SEPARATOR)){
|
||||
if (address.endsWith(PATH_SEPARATOR)) {
|
||||
if (path.startsWith(PATH_SEPARATOR)) {
|
||||
path = path.substring(1);
|
||||
}
|
||||
}else{
|
||||
if(!path.startsWith(PATH_SEPARATOR)){
|
||||
} else {
|
||||
if (!path.startsWith(PATH_SEPARATOR)) {
|
||||
stringWriter.append(PARAM_SEPARATOR);
|
||||
}
|
||||
}
|
||||
|
||||
stringWriter.append(path);
|
||||
|
||||
if(urlParameters!=null){
|
||||
if (urlParameters != null) {
|
||||
stringWriter.append(PARAM_STARTER);
|
||||
stringWriter.append(urlParameters);
|
||||
}
|
||||
|
@ -112,16 +110,14 @@ public class HTTPCall {
|
|||
return getURL(stringWriter.toString());
|
||||
}
|
||||
|
||||
|
||||
protected URL getURL(String urlString) throws MalformedURLException{
|
||||
protected URL getURL(String urlString) throws MalformedURLException {
|
||||
URL url = new URL(urlString);
|
||||
if(url.getProtocol().compareTo("https")==0){
|
||||
if (url.getProtocol().compareTo("https") == 0) {
|
||||
url = new URL(url.getProtocol(), url.getHost(), url.getDefaultPort(), url.getFile());
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
|
||||
protected HttpURLConnection getConnection(String path, String urlParameters, HTTPMETHOD method, String body)
|
||||
throws Exception {
|
||||
URL url = getURL(address, path, urlParameters);
|
||||
|
@ -133,11 +129,9 @@ public class HTTPCall {
|
|||
|
||||
if (SecurityTokenProvider.instance.get() == null) {
|
||||
if (ScopeProvider.instance.get() == null) {
|
||||
throw new RuntimeException(
|
||||
"Null Token and Scope. Please set your token first.");
|
||||
throw new RuntimeException("Null Token and Scope. Please set your token first.");
|
||||
}
|
||||
connection.setRequestProperty("gcube-scope",
|
||||
ScopeProvider.instance.get());
|
||||
connection.setRequestProperty("gcube-scope", ScopeProvider.instance.get());
|
||||
} else {
|
||||
connection.setRequestProperty(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY,
|
||||
SecurityTokenProvider.instance.get());
|
||||
|
@ -150,26 +144,20 @@ public class HTTPCall {
|
|||
|
||||
connection.setRequestMethod(method.toString());
|
||||
|
||||
if (body != null && (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) {
|
||||
|
||||
if (body != null
|
||||
&& (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) {
|
||||
|
||||
DataOutputStream wr = new DataOutputStream(
|
||||
connection.getOutputStream());
|
||||
wr.writeBytes(body);
|
||||
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
|
||||
wr.write(body.getBytes("UTF-8"));
|
||||
wr.flush();
|
||||
wr.close();
|
||||
}
|
||||
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
String responseMessage = connection.getResponseMessage();
|
||||
logger.trace("{} {} : {} - {}",
|
||||
method, connection.getURL(), responseCode, responseMessage);
|
||||
logger.trace("{} {} : {} - {}", method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
|
||||
responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
|
||||
responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
|
||||
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM
|
||||
|| responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
|
||||
|
||||
URL redirectURL = getURL(connection.getHeaderField("Location"));
|
||||
|
||||
|
@ -181,10 +169,9 @@ public class HTTPCall {
|
|||
return connection;
|
||||
}
|
||||
|
||||
protected StringBuilder getStringBuilder(InputStream inputStream) throws IOException{
|
||||
protected StringBuilder getStringBuilder(InputStream inputStream) throws IOException {
|
||||
StringBuilder result = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(inputStream))) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
result.append(line);
|
||||
|
@ -219,12 +206,12 @@ public class HTTPCall {
|
|||
return new ERAvailableInAnotherContextException(error);
|
||||
}
|
||||
|
||||
|
||||
public <C> C call(Class<C> clz, String path, HTTPMETHOD method) throws Exception {
|
||||
return call(clz, path, method, null, null);
|
||||
}
|
||||
|
||||
public <C> C call(Class<C> clz, String path, HTTPMETHOD method, Map<String, ? extends Object> parameters) throws Exception {
|
||||
public <C> C call(Class<C> clz, String path, HTTPMETHOD method, Map<String, ? extends Object> parameters)
|
||||
throws Exception {
|
||||
return call(clz, path, method, parameters, null);
|
||||
}
|
||||
|
||||
|
@ -233,7 +220,8 @@ public class HTTPCall {
|
|||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <C> C call(Class<C> clz, String path, HTTPMETHOD method, Map<String, ? extends Object> parameters, String body) throws Exception {
|
||||
protected <C> C call(Class<C> clz, String path, HTTPMETHOD method, Map<String, ? extends Object> parameters,
|
||||
String body) throws Exception {
|
||||
|
||||
String urlParameters = getParametersDataString(parameters);
|
||||
|
||||
|
@ -243,22 +231,20 @@ public class HTTPCall {
|
|||
|
||||
int responseCode = connection.getResponseCode();
|
||||
String responseMessage = connection.getResponseMessage();
|
||||
logger.info("{} {} : {} - {}",
|
||||
method, connection.getURL(), responseCode, responseMessage);
|
||||
logger.info("{} {} : {} - {}", method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
if(method == HTTPMETHOD.HEAD){
|
||||
if(responseCode == HttpURLConnection.HTTP_NO_CONTENT){
|
||||
if (method == HTTPMETHOD.HEAD) {
|
||||
if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
|
||||
return null;
|
||||
}
|
||||
if(responseCode == HttpURLConnection.HTTP_NOT_FOUND){
|
||||
if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
|
||||
throw getElementNotFoundException(clz);
|
||||
}
|
||||
if(responseCode == HttpURLConnection.HTTP_FORBIDDEN){
|
||||
if (responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
|
||||
throw getElementAvailableInAnotherContextException(clz);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
|
||||
|
||||
InputStream inputStream = connection.getErrorStream();
|
||||
|
@ -269,7 +255,7 @@ public class HTTPCall {
|
|||
ResourceRegistryException rre = null;
|
||||
try {
|
||||
rre = ExceptionMapper.unmarshal(ResourceRegistryException.class, res);
|
||||
}catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
rre = new ResourceRegistryException(responseMessage);
|
||||
}
|
||||
|
||||
|
@ -282,17 +268,16 @@ public class HTTPCall {
|
|||
String res = result.toString();
|
||||
logger.trace("Server returned content : {}", res);
|
||||
|
||||
if(Boolean.class.isAssignableFrom(clz)){
|
||||
return (C) ((Boolean) Boolean.valueOf(res)) ;
|
||||
}else if(ISManageable.class.isAssignableFrom(clz)){
|
||||
if (Boolean.class.isAssignableFrom(clz)) {
|
||||
return (C) ((Boolean) Boolean.valueOf(res));
|
||||
} else if (ISManageable.class.isAssignableFrom(clz)) {
|
||||
return (C) ISMapper.unmarshal((Class<ISManageable>) clz, res);
|
||||
}
|
||||
|
||||
return (C) res;
|
||||
}finally {
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue