diff options
Diffstat (limited to 'org.eclipse.osee.ote.master.rest.client/src')
8 files changed, 282 insertions, 0 deletions
diff --git a/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/OTEMasterServer.java b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/OTEMasterServer.java new file mode 100644 index 000000000..3ef9077bc --- /dev/null +++ b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/OTEMasterServer.java @@ -0,0 +1,12 @@ +package org.eclipse.osee.ote.master.rest.client; + +import java.net.URI; +import java.util.concurrent.Future; + +import org.eclipse.osee.ote.master.rest.model.OTEServer; + +public interface OTEMasterServer { + Future<OTEMasterServerAvailableNodes> getAvailableServers(URI uri); + Future<OTEMasterServerResult> addServer(URI uri, OTEServer server); + Future<OTEMasterServerResult> removeServer(URI uri, OTEServer server); +} diff --git a/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/OTEMasterServerAvailableNodes.java b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/OTEMasterServerAvailableNodes.java new file mode 100644 index 000000000..f69f2eea1 --- /dev/null +++ b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/OTEMasterServerAvailableNodes.java @@ -0,0 +1,27 @@ +package org.eclipse.osee.ote.master.rest.client; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.osee.ote.master.rest.model.OTEServer; + +public class OTEMasterServerAvailableNodes extends OTEMasterServerResult { + + private List<OTEServer> oteServers; + + public OTEMasterServerAvailableNodes(){ + oteServers = new ArrayList<>(); + } + + public List<OTEServer> getServers(){ + return oteServers; + } + + public void setServers(OTEServer[] servers) { + oteServers.clear(); + for(OTEServer server:servers){ + oteServers.add(server); + } + } + +} diff --git a/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/OTEMasterServerResult.java b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/OTEMasterServerResult.java new file mode 100644 index 000000000..df14451b7 --- /dev/null +++ b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/OTEMasterServerResult.java @@ -0,0 +1,21 @@ +package org.eclipse.osee.ote.master.rest.client; + +public class OTEMasterServerResult { + + private Throwable th = null; + private boolean success = true; + + public Throwable getThrowable() { + return th; + } + public void setThrowable(Throwable th) { + this.th = th; + } + public boolean isSuccess() { + return success; + } + public void setSuccess(boolean success) { + this.success = success; + } + +} diff --git a/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/AddServer.java b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/AddServer.java new file mode 100644 index 000000000..afba07488 --- /dev/null +++ b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/AddServer.java @@ -0,0 +1,50 @@ +package org.eclipse.osee.ote.master.rest.client.internal; + +import java.net.HttpURLConnection; +import java.net.URI; +import java.util.concurrent.Callable; +import java.util.logging.Level; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.UriBuilder; + +import org.eclipse.osee.framework.logging.OseeLog; +import org.eclipse.osee.jaxrs.client.JaxRsClient; +import org.eclipse.osee.jaxrs.client.JaxRsWebTarget; +import org.eclipse.osee.ote.master.rest.client.OTEMasterServerResult; +import org.eclipse.osee.ote.master.rest.model.OTEServer; + +public class AddServer implements Callable<OTEMasterServerResult> { + + private final JaxRsClient webClientProvider; + private final OTEServer server; + private final URI uri; + + public AddServer(JaxRsClient webClientProvider, URI uri, OTEServer server) { + this.webClientProvider = webClientProvider; + this.uri = uri; + this.server = server; + } + + @Override + public OTEMasterServerResult call() throws Exception { + OTEMasterServerResult result = new OTEMasterServerResult(); + try { + URI targetUri = + UriBuilder.fromUri(uri).path(OTEMasterServerImpl.CONTEXT_NAME).path(OTEMasterServerImpl.CONTEXT_SERVERS).build(); + + if(HttpUtil.canConnect(targetUri)){ + JaxRsWebTarget target = webClientProvider.target(targetUri); + javax.ws.rs.client.Invocation.Builder builder = target.request(MediaType.APPLICATION_XML); + builder.post(Entity.xml(server)); + } else { + result.setSuccess(false); + } + } catch (Throwable th) { + result.setSuccess(false); + // result.setThrowable(th); + } + return result; + } +} diff --git a/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/GetAvailableServers.java b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/GetAvailableServers.java new file mode 100644 index 000000000..57bc86f23 --- /dev/null +++ b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/GetAvailableServers.java @@ -0,0 +1,39 @@ +package org.eclipse.osee.ote.master.rest.client.internal; + +import java.net.URI; +import java.util.concurrent.Callable; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.UriBuilder; +import org.eclipse.osee.jaxrs.client.JaxRsClient; +import org.eclipse.osee.ote.master.rest.client.OTEMasterServerAvailableNodes; +import org.eclipse.osee.ote.master.rest.model.OTEServer; + +public class GetAvailableServers implements Callable<OTEMasterServerAvailableNodes> { + + private final JaxRsClient webClientProvider; + private final URI uri; + + public GetAvailableServers(JaxRsClient webClientProvider, URI uri) { + this.webClientProvider = webClientProvider; + this.uri = uri; + } + + @Override + public OTEMasterServerAvailableNodes call() throws Exception { + URI targetUri = + UriBuilder.fromUri(uri).path(OTEMasterServerImpl.CONTEXT_NAME).path(OTEMasterServerImpl.CONTEXT_SERVERS).build(); + + OTEMasterServerAvailableNodes result = new OTEMasterServerAvailableNodes(); + try { + OTEServer[] servers = + webClientProvider.target(targetUri).request(MediaType.APPLICATION_XML).get(OTEServer[].class); + result.setServers(servers); + result.setSuccess(true); + } catch (Throwable th) { + result.setSuccess(false); + result.setThrowable(th); + } + return result; + } + +} diff --git a/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/HttpUtil.java b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/HttpUtil.java new file mode 100644 index 000000000..1f7edc382 --- /dev/null +++ b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/HttpUtil.java @@ -0,0 +1,24 @@ +package org.eclipse.osee.ote.master.rest.client.internal; + +import java.net.HttpURLConnection; +import java.net.URI; +import java.util.logging.Level; + +import org.eclipse.osee.framework.logging.OseeLog; + +public class HttpUtil { + + public static boolean canConnect(URI targetUri) { + try{ + HttpURLConnection connection = (HttpURLConnection)targetUri.toURL().openConnection(); + connection.setRequestMethod("HEAD"); + int responseCode = connection.getResponseCode(); + if(responseCode == 200){ + return true; + } + } catch (Throwable th){ + OseeLog.log(HttpUtil.class, Level.INFO, th); + } + return false; + } +} diff --git a/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/OTEMasterServerImpl.java b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/OTEMasterServerImpl.java new file mode 100644 index 000000000..2a5e334b0 --- /dev/null +++ b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/OTEMasterServerImpl.java @@ -0,0 +1,63 @@ +package org.eclipse.osee.ote.master.rest.client.internal; + +import java.net.URI; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ThreadFactory; + +import org.eclipse.osee.jaxrs.client.JaxRsClient; +import org.eclipse.osee.ote.master.rest.client.OTEMasterServer; +import org.eclipse.osee.ote.master.rest.client.OTEMasterServerAvailableNodes; +import org.eclipse.osee.ote.master.rest.client.OTEMasterServerResult; +import org.eclipse.osee.ote.master.rest.model.OTEServer; + +public class OTEMasterServerImpl implements OTEMasterServer { + + static final String CONTEXT_NAME = "otemaster"; + static final String CONTEXT_SERVERS = "servers"; + + private volatile JaxRsClient client; + private ExecutorService executor; + + public void start(Map<String, Object> props) { + executor = Executors.newCachedThreadPool(new ThreadFactory() { + @Override + public Thread newThread(Runnable arg0) { + Thread th = new Thread(arg0); + th.setName("OTE Master Client " + th.getId()); + th.setDaemon(false); + return th; + } + }); + update(props); + } + + public void stop() { + if (executor != null) { + executor.shutdown(); + } + client = null; + } + + public void update(Map<String, Object> props) { + client = JaxRsClient.newBuilder().properties(props).build(); + } + + @Override + public Future<OTEMasterServerAvailableNodes> getAvailableServers(URI uri) { + return executor.submit(new GetAvailableServers(client, uri)); + } + + @Override + public Future<OTEMasterServerResult> addServer(URI uri, OTEServer server) { + return executor.submit(new AddServer(client, uri, server)); + } + + @Override + public Future<OTEMasterServerResult> removeServer(URI uri, OTEServer server) { + return executor.submit(new RemoveServer(client, uri, server)); + } + +} diff --git a/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/RemoveServer.java b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/RemoveServer.java new file mode 100644 index 000000000..b91f43fd1 --- /dev/null +++ b/org.eclipse.osee.ote.master.rest.client/src/org/eclipse/osee/ote/master/rest/client/internal/RemoveServer.java @@ -0,0 +1,46 @@ +package org.eclipse.osee.ote.master.rest.client.internal; + +import java.net.HttpURLConnection; +import java.net.URI; +import java.util.concurrent.Callable; +import java.util.logging.Level; + +import javax.ws.rs.core.UriBuilder; + +import org.eclipse.osee.framework.logging.OseeLog; +import org.eclipse.osee.jaxrs.client.JaxRsClient; +import org.eclipse.osee.ote.master.rest.client.OTEMasterServerResult; +import org.eclipse.osee.ote.master.rest.model.OTEServer; + +public class RemoveServer implements Callable<OTEMasterServerResult> { + + private final JaxRsClient webClientProvider; + private final OTEServer server; + private final URI uri; + + public RemoveServer(JaxRsClient webClientProvider, URI uri, OTEServer server) { + this.webClientProvider = webClientProvider; + this.uri = uri; + this.server = server; + } + + @Override + public OTEMasterServerResult call() throws Exception { + OTEMasterServerResult result = new OTEMasterServerResult(); + try { + URI mainTarget = + UriBuilder.fromUri(uri).path(OTEMasterServerImpl.CONTEXT_NAME).path(OTEMasterServerImpl.CONTEXT_SERVERS).build(); + URI targetUri = + UriBuilder.fromUri(uri).path(OTEMasterServerImpl.CONTEXT_NAME).path(OTEMasterServerImpl.CONTEXT_SERVERS).path(server.getUUID().toString()).build(); + if(HttpUtil.canConnect(mainTarget)){ + webClientProvider.target(targetUri).request().delete(); + } else { + result.setSuccess(false); + } + } catch (Throwable th) { + result.setSuccess(false); + result.setThrowable(th); + } + return result; + } +} |