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,11 +35,10 @@ 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";
|
||||
|
||||
|
||||
public enum HTTPMETHOD {
|
||||
HEAD, GET, POST, PUT, DELETE;
|
||||
|
||||
|
@ -48,25 +47,24 @@ public class HTTPCall {
|
|||
return this.name();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static final String PATH_SEPARATOR = "/";
|
||||
public static final String PARAM_STARTER = "?";
|
||||
public static final String PARAM_EQUALS = "=";
|
||||
public static final String PARAM_SEPARATOR = "&";
|
||||
public static final String UTF8 = "UTF-8";
|
||||
|
||||
|
||||
protected final String address;
|
||||
protected final String userAgent;
|
||||
|
||||
|
||||
public HTTPCall(String address, String userAgent) {
|
||||
this.address = address;
|
||||
this.userAgent = userAgent;
|
||||
}
|
||||
|
||||
protected String getParametersDataString(
|
||||
Map<String, ? extends Object> parameters)
|
||||
|
||||
protected String getParametersDataString(Map<String, ? extends Object> parameters)
|
||||
throws UnsupportedEncodingException {
|
||||
|
||||
|
||||
if (parameters == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -88,112 +86,101 @@ public class HTTPCall {
|
|||
}
|
||||
|
||||
protected URL getURL(String address, String path, String urlParameters) throws MalformedURLException {
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
return getConnection(url, method, body);
|
||||
}
|
||||
|
||||
|
||||
protected HttpURLConnection getConnection(URL url, HTTPMETHOD method, String body) throws Exception {
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
connection.setDoOutput(true);
|
||||
|
||||
connection.setRequestProperty("Content-type", APPLICATION_JSON_CHARSET_UTF_8);
|
||||
connection.setRequestProperty("User-Agent", userAgent);
|
||||
|
||||
connection.setRequestMethod(method.toString());
|
||||
|
||||
|
||||
if (body != null
|
||||
&& (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) {
|
||||
|
||||
DataOutputStream wr = new DataOutputStream(
|
||||
connection.getOutputStream());
|
||||
wr.writeBytes(body);
|
||||
|
||||
if (body != null && (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) {
|
||||
|
||||
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
|
||||
wr.write(body.getBytes("UTF-8"));
|
||||
wr.flush();
|
||||
wr.close();
|
||||
}
|
||||
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
int responseCode = connection.getResponseCode();
|
||||
String responseMessage = connection.getResponseMessage();
|
||||
logger.trace("{} {} : {} - {}",
|
||||
method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
|
||||
responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
|
||||
responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
|
||||
|
||||
logger.trace("{} {} : {} - {}", method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM
|
||||
|| responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
|
||||
|
||||
URL redirectURL = getURL(connection.getHeaderField("Location"));
|
||||
|
||||
|
||||
logger.trace("{} is going to be redirect to {}", url.toString(), redirectURL.toString());
|
||||
|
||||
|
||||
connection = getConnection(redirectURL, method, body);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
protected <C> ERNotFoundException getElementNotFoundException(Class<C> clz)
|
||||
throws ERNotFoundException, ResourceRegistryException {
|
||||
String error = String.format("Requested %s instance was not found", clz.getSimpleName());
|
||||
|
@ -206,7 +193,7 @@ public class HTTPCall {
|
|||
}
|
||||
return new ERNotFoundException(error);
|
||||
}
|
||||
|
||||
|
||||
protected <C> ERAvailableInAnotherContextException getElementAvailableInAnotherContextException(Class<C> clz) {
|
||||
String error = String.format("Requested %s instance was not found", clz.getSimpleName());
|
||||
if (Resource.class.isAssignableFrom(clz)) {
|
||||
|
@ -218,81 +205,79 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
public <C> C call(Class<C> clz, String path, HTTPMETHOD method, String body) throws Exception {
|
||||
return call(clz, path, method, null, body);
|
||||
}
|
||||
|
||||
|
||||
@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);
|
||||
|
||||
|
||||
HttpURLConnection connection = getConnection(path, urlParameters, method, body);
|
||||
|
||||
|
||||
try {
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
int responseCode = connection.getResponseCode();
|
||||
String responseMessage = connection.getResponseMessage();
|
||||
logger.info("{} {} : {} - {}",
|
||||
method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
if(method == HTTPMETHOD.HEAD){
|
||||
if(responseCode == HttpURLConnection.HTTP_NO_CONTENT){
|
||||
logger.info("{} {} : {} - {}", method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
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();
|
||||
StringBuilder result = getStringBuilder(inputStream);
|
||||
|
||||
|
||||
String res = result.toString();
|
||||
|
||||
|
||||
ResourceRegistryException rre = null;
|
||||
try {
|
||||
rre = ExceptionMapper.unmarshal(ResourceRegistryException.class, res);
|
||||
}catch (Exception e) {
|
||||
rre = ExceptionMapper.unmarshal(ResourceRegistryException.class, res);
|
||||
} catch (Exception e) {
|
||||
rre = new ResourceRegistryException(responseMessage);
|
||||
}
|
||||
|
||||
|
||||
throw rre;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
StringBuilder result = getStringBuilder(connection.getInputStream());
|
||||
|
||||
|
||||
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