123 lines
4.1 KiB
Java
123 lines
4.1 KiB
Java
package org.gcube.portal.social_networking_client_library.utils;
|
|
|
|
import javax.ws.rs.WebApplicationException;
|
|
import javax.ws.rs.client.Client;
|
|
import javax.ws.rs.client.ClientBuilder;
|
|
import javax.ws.rs.client.Entity;
|
|
import javax.ws.rs.client.WebTarget;
|
|
import javax.ws.rs.core.GenericType;
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
import org.gcube.portal.socialnetworking.model.output.ResponseBean;
|
|
import org.glassfish.jersey.client.ClientConfig;
|
|
import org.glassfish.jersey.jackson.JacksonFeature;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
/**
|
|
* Http client with post and get methods (jersey is used)
|
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
|
*/
|
|
public class HttpClient{
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(HttpClient.class);
|
|
|
|
/**
|
|
* Executes a get request
|
|
* @param genericType
|
|
* @param genericType
|
|
* @param genericType
|
|
* @param returnType
|
|
* @param requestUrl
|
|
* @param token
|
|
* @return
|
|
*/
|
|
public static <R> R get(GenericType<ResponseBean<R>> returnType, String requestUrl, String token){
|
|
|
|
logger.debug("Executing get request at url " + requestUrl + " and token is " + maskedToken(token));
|
|
|
|
ClientConfig cc = new ClientConfig().register(new JacksonFeature());//.register(new JacksonJsonProvider(getMapper()));
|
|
Client client = ClientBuilder.newClient(cc);
|
|
WebTarget webResourceGet = client.target(requestUrl).queryParam("gcube-token", token);
|
|
ResponseBean<R> response = webResourceGet.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).get(returnType);
|
|
|
|
if(response == null)
|
|
throw new WebApplicationException("Response returned by the service is null");
|
|
|
|
if (response.getMessage() != null && !response.getMessage().isEmpty())
|
|
throw new WebApplicationException(
|
|
"Error message is " + response.getMessage());
|
|
|
|
return response.getResult();
|
|
}
|
|
|
|
// public static ObjectMapper getMapper(){
|
|
//
|
|
// ObjectMapper mapper = new ObjectMapper();
|
|
// mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
// mapper.addMixIn(WorkspaceMessage.class, MessageOutputBeanMixIn.class);
|
|
// mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
|
|
// return mapper;
|
|
// }
|
|
|
|
/**
|
|
* Executes a post request
|
|
* @param returnType
|
|
* @param requestUrl
|
|
* @param token
|
|
* @param body
|
|
* @return
|
|
* @throws JsonProcessingException
|
|
*/
|
|
public static <R> R post(GenericType<ResponseBean<R>> returnType, String requestUrl, String token, Object obj){
|
|
|
|
logger.debug("Executing post request at url " + requestUrl + " and token is " + maskedToken(token));
|
|
|
|
ClientConfig cc = new ClientConfig().register(new JacksonFeature());
|
|
Client client = ClientBuilder.newClient(cc);
|
|
WebTarget webResourceGet = client.target(requestUrl).queryParam("gcube-token", token);
|
|
//
|
|
// String asJson = null;
|
|
// try{
|
|
// ObjectMapper mapper = new ObjectMapper();
|
|
// mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
// mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
|
|
// mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
|
|
// asJson = mapper.writeValueAsString(obj);
|
|
// logger.debug("Request body looks like\n"+asJson);
|
|
// }catch(Exception e){
|
|
// logger.error("Failed to parse data", e);
|
|
// }
|
|
|
|
logger.debug("Entity looks like " + Entity.json(obj));
|
|
|
|
ResponseBean<R> response = webResourceGet.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(Entity.json(obj), returnType);
|
|
|
|
if(response == null)
|
|
throw new WebApplicationException("Response returned by the service is null");
|
|
|
|
if (response.getMessage() != null && !response.getMessage().isEmpty())
|
|
throw new WebApplicationException(
|
|
"Error message is " + response.getMessage());
|
|
|
|
return response.getResult();
|
|
}
|
|
|
|
/**
|
|
* Avoid to show the token
|
|
* @param token
|
|
* @return
|
|
*/
|
|
private static String maskedToken(String token){
|
|
|
|
if(token == null)
|
|
return "";
|
|
else
|
|
return token.substring(0, 5) + "**************************";
|
|
|
|
}
|
|
|
|
}
|