package org.gcube.informationsystem.publisher.stubs.registry; import java.io.BufferedOutputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import javax.net.ssl.HttpsURLConnection; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.publisher.stubs.registry.faults.CreateException; import org.gcube.informationsystem.publisher.stubs.registry.faults.InvalidResourceException; import org.gcube.informationsystem.publisher.stubs.registry.faults.RemoveException; import org.gcube.informationsystem.publisher.stubs.registry.faults.ResourceDoesNotExistException; import org.gcube.informationsystem.publisher.stubs.registry.faults.ResourceNotAcceptedException; import org.gcube.informationsystem.publisher.stubs.registry.faults.UpdateException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CollectorStubs implements RegistryStub { private static Logger log = LoggerFactory.getLogger(CollectorStubs.class); public static final String TOKEN_HEADER_ENTRY = "gcube-token"; public static final String SCOPE_HEADER_ENTRY = "gcube-scope"; private String endopoint; public CollectorStubs(String endopoint) { super(); this.endopoint = endopoint; } @Override public void create(String profile, String type) throws InvalidResourceException, ResourceNotAcceptedException, CreateException { StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type); try { URL url = new URL(callUrl.toString()); HttpURLConnection connection = makeRequest(url, "POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-type", "text/xml"); try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){ os.write(profile.getBytes()); } if (connection.getResponseCode()!=200) throw new CreateException("error creating resource "+connection.getResponseCode()); }catch (Exception e) { log.error("error on create",e); throw new CreateException(e.getMessage()); } } @Override public void update(String id, String type, String profile) throws InvalidResourceException, ResourceNotAcceptedException, UpdateException { StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type).append("/").append(id); try { URL url = new URL(callUrl.toString()); HttpURLConnection connection = makeRequest(url, "PUT"); connection.setDoOutput(true); connection.setRequestProperty("Content-type", "text/xml"); try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){ os.write(profile.getBytes()); } if (connection.getResponseCode()!=200) throw new UpdateException("error updating resource "+connection.getResponseCode()); }catch (Exception e) { log.error("error on update",e); throw new UpdateException(e.getMessage()); } } @Override public void remove(String id, String type) throws ResourceDoesNotExistException, RemoveException { StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type).append("/").append(id); try { URL url = new URL(callUrl.toString()); HttpURLConnection connection = makeRequest(url, "DELETE"); connection.setDoInput(true); if (connection.getResponseCode()!=200){ log.info("response code is not 200"); if (connection.getResponseCode()==404) throw new ResourceDoesNotExistException("resource with id "+id+" not found"); else throw new RemoveException("generic error removing resource "+id); } }catch (ResourceDoesNotExistException | RemoveException e) { throw e; }catch (Exception e) { throw new RemoveException("connection error removing resource"); } } private HttpURLConnection makeRequest(URL url, String method) throws Exception{ HttpURLConnection connection; if (url.toString().startsWith("https://")) connection = (HttpsURLConnection)url.openConnection(); else connection = (HttpURLConnection)url.openConnection(); if (ScopeProvider.instance.get()!=null) connection.setRequestProperty(SCOPE_HEADER_ENTRY,ScopeProvider.instance.get()); else if (SecurityTokenProvider.instance.get()!=null) connection.setRequestProperty(TOKEN_HEADER_ENTRY,SecurityTokenProvider.instance.get()); else throw new RuntimeException("Collector requires authorization (via token or scope)"); connection.setRequestMethod(method); return connection; } }