package org.gcube.portlets.user.uriresolvermanager.resolvers; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; import org.gcube.portlets.user.uriresolvermanager.entity.GenericResolver; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * The Class CatalogueResolverCallBuilder. * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * * Nov 5, 2021 */ public class CatalogueResolverCallBuilder extends GenericResolver { private static final int _60SEC = 60000; public static final Logger LOG = LoggerFactory.getLogger(CatalogueResolverCallBuilder.class); /** * Instantiates a new catalogue resolver wrapper. * * @param resourceName the resource name * @param entryName the entry name */ public CatalogueResolverCallBuilder(String resourceName, String entryName) { super(resourceName, entryName); } /** * Gets the link. * * @param baseURI the base URI * @param parameters the parameters * @return the link * @throws Exception the exception */ @Override public String getLink(String baseURI, Map parameters) throws Exception { LOG.debug("called getLink: " + baseURI + " parameters: "+parameters); HttpURLConnection con = null; String theResponse = null; try { URL urlObj = new URL(baseURI); con = (HttpURLConnection) urlObj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); // con.setRequestProperty("Accept", "application/json"); con.setDoOutput(true); con.setReadTimeout(_60SEC); con.setConnectTimeout(_60SEC); JSONObject jObj = new org.json.JSONObject(); for (String key : parameters.keySet()) { jObj.put(key, parameters.get(key)); } String toJSON = jObj.toString(); LOG.info("Submitting JSON: " + toJSON); Integer code = null; try { OutputStream os = con.getOutputStream(); os.write(toJSON.getBytes("UTF-8")); os.close(); code = con.getResponseCode(); theResponse = readResponse(con.getInputStream()); if (!((200 <= code) && (code <= 208))) { throw new Exception("CatalogueResolver returned code: " + code + ". Response is: " + theResponse); } } catch (IOException e) { theResponse = readResponse(con.getInputStream()); LOG.error("CatalogueResolver returned code: " + code + ". Response is: " + theResponse); throw e; } } catch (Exception e) { LOG.error(CatalogueResolverCallBuilder.class.getSimpleName() + " error: ", e); throw e; } finally { try { if (con != null) con.disconnect(); } catch (Exception e) { // silent } } LOG.info("Got Link: " + theResponse); return theResponse; } /** * Read response. * * @param ris the ris * @return the string * @throws IOException Signals that an I/O exception has occurred. */ private String readResponse(InputStream ris) throws IOException { // Receive the response from the server InputStream in = new BufferedInputStream(ris); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder result = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { result.append(line); } return result.toString(); } }