145 lines
5.1 KiB
Java
145 lines
5.1 KiB
Java
package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
|
|
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.ManageProductBean;
|
|
import org.json.simple.JSONObject;
|
|
import org.json.simple.parser.JSONParser;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse;
|
|
import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpPost;
|
|
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity;
|
|
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient;
|
|
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
/**
|
|
* Endpoint for sending update records information to GRSF KB
|
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
|
*/
|
|
public class GRSFNotificationServices {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(GRSFNotificationServices.class);
|
|
private static final String SERVICE_POST_METHOD= "/service/updater/post";
|
|
|
|
// request post fields
|
|
private static final String CATALOGUE_ID = "catalog_id";
|
|
private static final String KB_ID = "record_id";
|
|
private static final String PRODUCT_TYPE = "type";
|
|
private static final String STATUS = "status";
|
|
private static final String ANNOTATION = "annotation_msg";
|
|
private static final String ERROR = "error";
|
|
private static final int STATUS_SUCCESS = 200;
|
|
|
|
/**
|
|
* Discover the service endpoint and return its url
|
|
* @param context
|
|
* @return
|
|
*/
|
|
public static String discoverEndPoint(String context){
|
|
// TODO discover the instance in this scope
|
|
return "http://62.217.127.124:8080/grsf-services-updater";
|
|
}
|
|
|
|
/**
|
|
* Send an update for this bean
|
|
* @param baseUrl
|
|
* @param bean
|
|
* @param username
|
|
* @param catalogue
|
|
* @return true on success, false otherwise
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public static String updateCatalogueRecord(String serviceUrl, ManageProductBean bean, DataCatalogue catalogue, String username){
|
|
|
|
if(serviceUrl == null)
|
|
throw new IllegalArgumentException("Service url cannot be null");
|
|
|
|
if(bean == null)
|
|
throw new IllegalArgumentException("Product bean to manage cannot be null");
|
|
|
|
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){
|
|
|
|
JSONObject obj = new JSONObject();
|
|
obj.put(CATALOGUE_ID, bean.getCatalogueIdentifier());
|
|
obj.put(KB_ID, bean.getKnowledgeBaseIdentifier());
|
|
obj.put(PRODUCT_TYPE, bean.getProductType().toLowerCase());
|
|
obj.put(STATUS, bean.getNewStatus().toLowerCase());
|
|
|
|
String annotation = bean.getAnnotation();
|
|
if(annotation != null)
|
|
obj.put(ANNOTATION, annotation);
|
|
|
|
logger.debug("Update request looks like " + obj.toJSONString());
|
|
|
|
HttpPost request = new HttpPost(serviceUrl + SERVICE_POST_METHOD);
|
|
request.setHeader("Accept", "application/json");
|
|
request.setHeader("Content-type", "application/json");
|
|
StringEntity params = new StringEntity(obj.toJSONString());
|
|
request.setEntity(params);
|
|
HttpResponse response = httpClient.execute(request);
|
|
|
|
logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
|
|
|
|
String result = convertStreamToString(response.getEntity().getContent());
|
|
JSONParser parser = new JSONParser();
|
|
JSONObject parsedJSON = (JSONObject)parser.parse(result);
|
|
|
|
if(response.getStatusLine().getStatusCode() > STATUS_SUCCESS)
|
|
throw new IllegalArgumentException(
|
|
"Error while performing the update request: " + response.getStatusLine().getReasonPhrase() +
|
|
"and error in the result bean is " + parsedJSON.get(ERROR));
|
|
|
|
// patch the catalogue product
|
|
Map<String, List<String>> statusMap = new HashMap<String, List<String>>();
|
|
statusMap.put("Status", Arrays.asList(bean.getNewStatus()));
|
|
boolean updated = catalogue.patchProductCustomFields(bean.getCatalogueIdentifier(), catalogue.getApiKeyFromUsername(username), statusMap);
|
|
|
|
logger.info("Was the catalogue product updated too? " + updated);
|
|
|
|
}catch(Exception e){
|
|
logger.error("Unable to update this record", e);
|
|
return e.getMessage();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Convert an input stream to a string
|
|
* @param is
|
|
* @return the json string inside
|
|
*/
|
|
private static String convertStreamToString(InputStream is) {
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
String line = null;
|
|
try {
|
|
while ((line = reader.readLine()) != null) {
|
|
sb.append(line + "\n");
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
try {
|
|
is.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
}
|