diff --git a/pom.xml b/pom.xml index c911967..8648d6f 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ edu.pitt.dbmi.ccd causal-tetrad-rest-client - 0.0.1-SNAPSHOT + 0.0.1 jar causal-tetrad-rest-client @@ -17,6 +17,17 @@ 1.8 + + + ccd-releases + https://ccd3.vm.bridges.psc.edu/nexus/content/repositories/releases/ + + + ccd-snapshots + https://ccd3.vm.bridges.psc.edu/nexus/content/repositories/snapshots/ + + + ccd-releases diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/RestHttpsClient.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/RestHttpsClient.java index c22feba..caa5065 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/RestHttpsClient.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/RestHttpsClient.java @@ -16,13 +16,12 @@ import org.apache.http.ssl.SSLContextBuilder; /** - * + * * Aug 20, 2016 12:17:02 AM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ - public class RestHttpsClient { private final String email; @@ -33,64 +32,57 @@ public class RestHttpsClient { private final CloseableHttpClient httpClient; private final HttpClientContext localContext; - - public RestHttpsClient(String email, String password, String scheme, - String hostname, int port) throws Exception { - this.email = email; - this.password = password; - this.scheme = scheme; - this.hostname = hostname; - this.port = port; - this.httpClient = createHttpClient(); - this.localContext = establishCustomContext(httpClient); + + public RestHttpsClient(String email, String password, String scheme, String hostname, int port) throws Exception { + this.email = email; + this.password = password; + this.scheme = scheme; + this.hostname = hostname; + this.port = port; + this.httpClient = createHttpClient(); + this.localContext = establishCustomContext(httpClient); } private CloseableHttpClient createHttpClient() throws Exception { - // Trust own CA and all self-signed certs - SSLContextBuilder builder = new SSLContextBuilder(); - builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); + // Trust own CA and all self-signed certs + SSLContextBuilder builder = new SSLContextBuilder(); + builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); - SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( - builder.build(), - new String[] { "TLSv1.2" }, - null, - SSLConnectionSocketFactory.getDefaultHostnameVerifier()); + SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"TLSv1.2"}, + null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); - CredentialsProvider credsProvider = new BasicCredentialsProvider(); - credsProvider.setCredentials(new AuthScope(hostname, port), - new UsernamePasswordCredentials(email, password)); + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(new AuthScope(hostname, port), new UsernamePasswordCredentials(email, password)); - CloseableHttpClient httpClient = HttpClients.custom() - .setSSLSocketFactory(sslsf) - .setDefaultCredentialsProvider(credsProvider).build(); + CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf) + .setDefaultCredentialsProvider(credsProvider).build(); - return httpClient; + return httpClient; } - private HttpClientContext establishCustomContext( - CloseableHttpClient httpclient) throws Exception { - HttpHost target = new HttpHost(hostname, port, scheme); - - // Create AuthCache instance - AuthCache authCache = new BasicAuthCache(); - // Generate BASIC scheme object and add it to the local - // auth cache - BasicScheme basicAuth = new BasicScheme(); - authCache.put(target, basicAuth); - - // Add AuthCache to the execution context - HttpClientContext localContext = HttpClientContext.create(); - localContext.setAuthCache(authCache); - - return localContext; + private HttpClientContext establishCustomContext(CloseableHttpClient httpclient) throws Exception { + HttpHost target = new HttpHost(hostname, port, scheme); + + // Create AuthCache instance + AuthCache authCache = new BasicAuthCache(); + // Generate BASIC scheme object and add it to the local + // auth cache + BasicScheme basicAuth = new BasicScheme(); + authCache.put(target, basicAuth); + + // Add AuthCache to the execution context + HttpClientContext localContext = HttpClientContext.create(); + localContext.setAuthCache(authCache); + + return localContext; } public CloseableHttpClient getHttpClient() { - return httpClient; + return httpClient; } public HttpClientContext getLocalContext() { - return localContext; + return localContext; } public static void main(String[] args) throws Exception { diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/AlgorithmInfo.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/AlgorithmInfo.java index 2ed0723..5d7124a 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/AlgorithmInfo.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/AlgorithmInfo.java @@ -1,11 +1,11 @@ package edu.pitt.dbmi.ccd.rest.client.dto.algo; /** - * + * * Aug 24, 2016 6:09:21 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class AlgorithmInfo { @@ -18,27 +18,27 @@ public AlgorithmInfo() { } public int getId() { - return id; + return id; } public void setId(int id) { - this.id = id; + this.id = id; } public String getName() { - return name; + return name; } public void setName(String name) { - this.name = name; + this.name = name; } public String getDescription() { - return description; + return description; } public void setDescription(String description) { - this.description = description; + this.description = description; } } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/AlgorithmParamRequest.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/AlgorithmParamRequest.java index ae425f4..5a757bb 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/AlgorithmParamRequest.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/AlgorithmParamRequest.java @@ -1,63 +1,65 @@ package edu.pitt.dbmi.ccd.rest.client.dto.algo; +import java.util.List; import java.util.Map; /** - * + * * Aug 25, 2016 2:20:41 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class AlgorithmParamRequest { - private long datasetFileId; - - private Map dataValidation; - - private Map AlgorithmParameters; - - private Map jvmOptions; - - private Map hpcParameters; - - public long getDatasetFileId() { - return datasetFileId; - } - - public void setDatasetFileId(long dataFileId) { - this.datasetFileId = dataFileId; - } - - public Map getDataValidation() { - return dataValidation; - } - - public void setDataValidation(Map dataValidation) { - this.dataValidation = dataValidation; - } - - public Map getAlgorithmParameters() { - return AlgorithmParameters; - } - - public void setAlgorithmParameters(Map algorithmParameters) { - AlgorithmParameters = algorithmParameters; - } - - public Map getJvmOptions() { - return jvmOptions; - } - - public void setJvmOptions(Map jvmOptions) { - this.jvmOptions = jvmOptions; - } - - public Map getHpcParameters() { - return hpcParameters; - } - - public void setHpcParameters(Map hpcParameters) { - this.hpcParameters = hpcParameters; - } + private long datasetFileId; + + private Map dataValidation; + + private Map AlgorithmParameters; + + private Map jvmOptions; + + private List hpcParameters; + + public long getDatasetFileId() { + return datasetFileId; + } + + public void setDatasetFileId(long dataFileId) { + this.datasetFileId = dataFileId; + } + + public Map getDataValidation() { + return dataValidation; + } + + public void setDataValidation(Map dataValidation) { + this.dataValidation = dataValidation; + } + + public Map getAlgorithmParameters() { + return AlgorithmParameters; + } + + public void setAlgorithmParameters(Map algorithmParameters) { + AlgorithmParameters = algorithmParameters; + } + + public Map getJvmOptions() { + return jvmOptions; + } + + public void setJvmOptions(Map jvmOptions) { + this.jvmOptions = jvmOptions; + } + + public List getHpcParameters() { + return hpcParameters; + } + + public void setHpcParameters(List hpcParameters) { + this.hpcParameters = hpcParameters; + } + } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/HpcParameter.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/HpcParameter.java new file mode 100644 index 0000000..09ce7c5 --- /dev/null +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/HpcParameter.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2015 University of Pittsburgh. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package edu.pitt.dbmi.ccd.rest.client.dto.algo; + +/** + * + * Feb 21, 2017 12:24:39 PM + * + * @author Chirayu Kong Wongchokprasitti, PhD (chw20@pitt.edu) + * + */ +public class HpcParameter { + + private String key; + + private String value; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/JobInfo.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/JobInfo.java index 881a912..994b72e 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/JobInfo.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/JobInfo.java @@ -3,11 +3,11 @@ import java.util.Date; /** - * + * * Aug 31, 2016 12:48:31 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class JobInfo { @@ -18,11 +18,11 @@ public class JobInfo { private Date addedTime; private String resultFileName; - + private String resultJsonFileName; private String errorResultFileName; - + private int status; public Long getId() { diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/ResultFile.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/ResultFile.java index efaea54..52f5f1d 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/ResultFile.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/algo/ResultFile.java @@ -3,20 +3,20 @@ import java.util.Date; /** - * + * * Sep 26, 2016 1:35:27 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class ResultFile { - + private String name; - + private Date creationTime; - + private Date lastModifiedTime; - + private long fileSize; public String getName() { diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/ChunkUpload.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/ChunkUpload.java index 85468bd..3f02f78 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/ChunkUpload.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/ChunkUpload.java @@ -1,5 +1,6 @@ package edu.pitt.dbmi.ccd.rest.client.dto.data; +import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; import java.io.BufferedInputStream; import java.io.IOException; import java.net.URI; @@ -9,7 +10,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; - import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; import org.apache.http.HttpStatus; @@ -26,14 +26,12 @@ import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; -import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; - /** - * + * * Aug 22, 2016 7:47:32 PM - * - * @author Chirayu (Kong) Wongchokprasitti, PhD - * Optional: make it parallel with its child's Callable implementation + * + * @author Chirayu (Kong) Wongchokprasitti, PhD Optional: make it parallel with + * its child's Callable implementation */ public class ChunkUpload implements Runnable { @@ -58,200 +56,200 @@ public class ChunkUpload implements Runnable { private final Map fileUploadMap; public ChunkUpload(Path file, long chunkSize, URI uri, - CloseableHttpClient httpclient, JsonWebToken jsonWebToken, - Map fileUploadMap) { - this.file = file; - this.chunkSize = chunkSize; - this.uri = uri; - this.httpclient = httpclient; - this.jsonWebToken = jsonWebToken; - this.fileUploadMap = fileUploadMap; + CloseableHttpClient httpclient, JsonWebToken jsonWebToken, + Map fileUploadMap) { + this.file = file; + this.chunkSize = chunkSize; + this.uri = uri; + this.httpclient = httpclient; + this.jsonWebToken = jsonWebToken; + this.fileUploadMap = fileUploadMap; } public void run() { - progress = 0; - suspended = false; - stopped = false; - - try { - String fileName = this.file.getFileName().toString(); - long fileSize = Files.size(this.file); - long maxOffset = Math.max(Math.round(fileSize / this.chunkSize), 1); - long resumableChunkSize = this.chunkSize; - long resumableTotalSize = fileSize; - - long resumableTotalChunks = maxOffset; - this.resumableIdentifier = String.format("%d-%s", fileSize, - fileName.replaceAll("/[^0-9a-zA-Z_-]/img", "")); - String resumableFilename = fileName; - String resumableRelativePath = fileName; - String resumableType = Files.probeContentType(this.file); - if (resumableType == null || resumableType.length() == 0) { - resumableType = "Text"; - } - - try (BufferedInputStream inputStream = new BufferedInputStream( - Files.newInputStream(file))) { - - try { - for (long offset = 0; offset < maxOffset; offset++) { - - synchronized (this) { - while (suspended) { - wait(); - if (stopped) { - break; - } - } - if (stopped) { - break; - } - } - - long startByte = offset * this.chunkSize; - long endByte = Math.min(fileSize, (offset + 1) - * this.chunkSize); - if (fileSize - endByte < this.chunkSize) { - endByte = fileSize; - } - long resumableChunkNumber = offset + 1; - long resumableCurrentChunkSize = endByte - startByte; - - try { - List nameValuePairs = new ArrayList<>(); - nameValuePairs.add(new BasicNameValuePair( - "resumableType", resumableType)); - nameValuePairs.add(new BasicNameValuePair( - "resumableIdentifier", - this.resumableIdentifier)); - nameValuePairs.add(new BasicNameValuePair( - "resumableChunkNumber", Long - .toString(resumableChunkNumber))); - nameValuePairs.add(new BasicNameValuePair( - "resumableChunkSize", Long - .toString(resumableChunkSize))); - nameValuePairs.add(new BasicNameValuePair( - "resumableTotalSize", Long - .toString(resumableTotalSize))); - nameValuePairs.add(new BasicNameValuePair( - "resumableCurrentChunkSize", - Long.toString(resumableCurrentChunkSize))); - nameValuePairs.add(new BasicNameValuePair( - "resumableTotalChunks", Long - .toString(resumableTotalChunks))); - nameValuePairs.add(new BasicNameValuePair( - "resumableFilename", resumableFilename)); - nameValuePairs.add(new BasicNameValuePair( - "resumableRelativePath", - resumableRelativePath)); - - URIBuilder uriBuilder = new URIBuilder(this.uri) - .setParameters(nameValuePairs); - - URI uri = uriBuilder.build(); - HttpGet httpget = new HttpGet(uri); - httpget.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); - //System.out.println("Executing request " - // + httpget.getRequestLine()); - CloseableHttpResponse response = httpclient - .execute(httpget); - - int statusCode = response.getStatusLine() - .getStatusCode(); - - HttpEntity entity = response.getEntity(); - EntityUtils.consume(entity); - - if (statusCode == HttpStatus.SC_NOT_FOUND) { - int readSize = (int) resumableCurrentChunkSize; - byte[] byteChunkPart = new byte[readSize]; - inputStream.read(byteChunkPart, 0, readSize); - - MultipartEntityBuilder multiEnBuilder = MultipartEntityBuilder - .create(); - multiEnBuilder.addPart("file", - new ByteArrayBody(byteChunkPart, - fileName)); - multiEnBuilder - .addPart( - "resumableChunkNumber", - new StringBody( - Long.toString(resumableChunkNumber), - ContentType.TEXT_PLAIN)); - multiEnBuilder.addPart("resumableChunkSize", - new StringBody( - Long.toString(chunkSize), - ContentType.TEXT_PLAIN)); - multiEnBuilder - .addPart( - "resumableCurrentChunkSize", - new StringBody( - Long.toString(resumableCurrentChunkSize), - ContentType.TEXT_PLAIN)); - multiEnBuilder.addPart("resumableFilename", - new StringBody(resumableFilename, - ContentType.TEXT_PLAIN)); - multiEnBuilder.addPart("resumableIdentifier", - new StringBody( - this.resumableIdentifier, - ContentType.TEXT_PLAIN)); - multiEnBuilder.addPart("resumableRelativePath", - new StringBody(resumableRelativePath, - ContentType.TEXT_PLAIN)); - multiEnBuilder - .addPart( - "resumableTotalChunks", - new StringBody( - Long.toString(resumableTotalChunks), - ContentType.TEXT_PLAIN)); - multiEnBuilder.addPart( - "resumableTotalSize", - new StringBody(Long - .toString(resumableTotalSize), - ContentType.TEXT_PLAIN)); - multiEnBuilder.addPart("resumableType", - new StringBody(resumableType, - ContentType.TEXT_PLAIN)); - multiEnBuilder.addPart("resumableFilename", - new StringBody(resumableFilename, - ContentType.TEXT_PLAIN)); - multiEnBuilder.addPart("resumableRelativePath", - new StringBody(resumableRelativePath, - ContentType.TEXT_PLAIN)); - - HttpEntity httpEntity = multiEnBuilder.build(); - HttpPost httppost = new HttpPost(uri); - httppost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); - httppost.setEntity(httpEntity); - //System.out.println("Executing request " - // + httppost.getRequestLine()); - response = httpclient.execute(httppost); - statusCode = response.getStatusLine() - .getStatusCode(); - if (statusCode == HttpStatus.SC_OK) { - progress = (offset + 1.0) / maxOffset; - } - entity = response.getEntity(); - EntityUtils.consume(entity); - } - - } catch (URISyntaxException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - } catch (InterruptedException e) { - e.printStackTrace(System.err); - } - - } catch (IOException e) { - e.printStackTrace(System.err); - } - fileUploadMap.remove(this.file.toAbsolutePath().toString()); - } catch (IOException e) { - e.printStackTrace(System.err); - } + progress = 0; + suspended = false; + stopped = false; + + try { + String fileName = this.file.getFileName().toString(); + long fileSize = Files.size(this.file); + long maxOffset = Math.max(Math.round(fileSize / this.chunkSize), 1); + long resumableChunkSize = this.chunkSize; + long resumableTotalSize = fileSize; + + long resumableTotalChunks = maxOffset; + this.resumableIdentifier = String.format("%d-%s", fileSize, + fileName.replaceAll("/[^0-9a-zA-Z_-]/img", "")); + String resumableFilename = fileName; + String resumableRelativePath = fileName; + String resumableType = Files.probeContentType(this.file); + if (resumableType == null || resumableType.length() == 0) { + resumableType = "Text"; + } + + try (BufferedInputStream inputStream = new BufferedInputStream( + Files.newInputStream(file))) { + + try { + for (long offset = 0; offset < maxOffset; offset++) { + + synchronized (this) { + while (suspended) { + wait(); + if (stopped) { + break; + } + } + if (stopped) { + break; + } + } + + long startByte = offset * this.chunkSize; + long endByte = Math.min(fileSize, (offset + 1) + * this.chunkSize); + if (fileSize - endByte < this.chunkSize) { + endByte = fileSize; + } + long resumableChunkNumber = offset + 1; + long resumableCurrentChunkSize = endByte - startByte; + + try { + List nameValuePairs = new ArrayList<>(); + nameValuePairs.add(new BasicNameValuePair( + "resumableType", resumableType)); + nameValuePairs.add(new BasicNameValuePair( + "resumableIdentifier", + this.resumableIdentifier)); + nameValuePairs.add(new BasicNameValuePair( + "resumableChunkNumber", Long + .toString(resumableChunkNumber))); + nameValuePairs.add(new BasicNameValuePair( + "resumableChunkSize", Long + .toString(resumableChunkSize))); + nameValuePairs.add(new BasicNameValuePair( + "resumableTotalSize", Long + .toString(resumableTotalSize))); + nameValuePairs.add(new BasicNameValuePair( + "resumableCurrentChunkSize", + Long.toString(resumableCurrentChunkSize))); + nameValuePairs.add(new BasicNameValuePair( + "resumableTotalChunks", Long + .toString(resumableTotalChunks))); + nameValuePairs.add(new BasicNameValuePair( + "resumableFilename", resumableFilename)); + nameValuePairs.add(new BasicNameValuePair( + "resumableRelativePath", + resumableRelativePath)); + + URIBuilder uriBuilder = new URIBuilder(this.uri) + .setParameters(nameValuePairs); + + URI uri = uriBuilder.build(); + HttpGet httpget = new HttpGet(uri); + httpget.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); + //System.out.println("Executing request " + // + httpget.getRequestLine()); + CloseableHttpResponse response = httpclient + .execute(httpget); + + int statusCode = response.getStatusLine() + .getStatusCode(); + + HttpEntity entity = response.getEntity(); + EntityUtils.consume(entity); + + if (statusCode == HttpStatus.SC_NOT_FOUND) { + int readSize = (int) resumableCurrentChunkSize; + byte[] byteChunkPart = new byte[readSize]; + inputStream.read(byteChunkPart, 0, readSize); + + MultipartEntityBuilder multiEnBuilder = MultipartEntityBuilder + .create(); + multiEnBuilder.addPart("file", + new ByteArrayBody(byteChunkPart, + fileName)); + multiEnBuilder + .addPart( + "resumableChunkNumber", + new StringBody( + Long.toString(resumableChunkNumber), + ContentType.TEXT_PLAIN)); + multiEnBuilder.addPart("resumableChunkSize", + new StringBody( + Long.toString(chunkSize), + ContentType.TEXT_PLAIN)); + multiEnBuilder + .addPart( + "resumableCurrentChunkSize", + new StringBody( + Long.toString(resumableCurrentChunkSize), + ContentType.TEXT_PLAIN)); + multiEnBuilder.addPart("resumableFilename", + new StringBody(resumableFilename, + ContentType.TEXT_PLAIN)); + multiEnBuilder.addPart("resumableIdentifier", + new StringBody( + this.resumableIdentifier, + ContentType.TEXT_PLAIN)); + multiEnBuilder.addPart("resumableRelativePath", + new StringBody(resumableRelativePath, + ContentType.TEXT_PLAIN)); + multiEnBuilder + .addPart( + "resumableTotalChunks", + new StringBody( + Long.toString(resumableTotalChunks), + ContentType.TEXT_PLAIN)); + multiEnBuilder.addPart( + "resumableTotalSize", + new StringBody(Long + .toString(resumableTotalSize), + ContentType.TEXT_PLAIN)); + multiEnBuilder.addPart("resumableType", + new StringBody(resumableType, + ContentType.TEXT_PLAIN)); + multiEnBuilder.addPart("resumableFilename", + new StringBody(resumableFilename, + ContentType.TEXT_PLAIN)); + multiEnBuilder.addPart("resumableRelativePath", + new StringBody(resumableRelativePath, + ContentType.TEXT_PLAIN)); + + HttpEntity httpEntity = multiEnBuilder.build(); + HttpPost httppost = new HttpPost(uri); + httppost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); + httppost.setEntity(httpEntity); + //System.out.println("Executing request " + // + httppost.getRequestLine()); + response = httpclient.execute(httppost); + statusCode = response.getStatusLine() + .getStatusCode(); + if (statusCode == HttpStatus.SC_OK) { + progress = (offset + 1.0) / maxOffset; + } + entity = response.getEntity(); + EntityUtils.consume(entity); + } + + } catch (URISyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + } catch (InterruptedException e) { + e.printStackTrace(System.err); + } + + } catch (IOException e) { + e.printStackTrace(System.err); + } + fileUploadMap.remove(this.file.toAbsolutePath().toString()); + } catch (IOException e) { + e.printStackTrace(System.err); + } } public boolean isSuspended() { @@ -278,5 +276,5 @@ public void stop() { public double getProgress() { return progress; } - + } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/DataFile.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/DataFile.java index 0c03fc1..9c8fc7f 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/DataFile.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/DataFile.java @@ -3,11 +3,11 @@ import java.util.Date; /** - * + * * Aug 20, 2016 12:17:02 AM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class DataFile { @@ -20,15 +20,15 @@ public class DataFile { private Date lastModifiedTime; private long fileSize; - + private String md5checkSum; private DataFileSummary fileSummary; - public DataFile(){ - + public DataFile() { + } - + public Long getId() { return id; } @@ -84,5 +84,5 @@ public DataFileSummary getFileSummary() { public void setFileSummary(DataFileSummary fileSummary) { this.fileSummary = fileSummary; } - + } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/DataFileSummary.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/DataFileSummary.java index 7a4f423..e77c063 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/DataFileSummary.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/DataFileSummary.java @@ -1,11 +1,11 @@ - package edu.pitt.dbmi.ccd.rest.client.dto.data; +package edu.pitt.dbmi.ccd.rest.client.dto.data; /** - * + * * Aug 20, 2016 12:19:08 AM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class DataFileSummary { @@ -17,10 +17,10 @@ public class DataFileSummary { private Integer numOfColumns; - public DataFileSummary(){ - + public DataFileSummary() { + } - + public String getVariableType() { return variableType; } @@ -52,5 +52,5 @@ public Integer getNumOfColumns() { public void setNumOfColumns(Integer numOfColumns) { this.numOfColumns = numOfColumns; } - + } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/UploadStatus.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/UploadStatus.java index c9ffb8f..37b74cb 100755 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/UploadStatus.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/data/UploadStatus.java @@ -23,41 +23,41 @@ * Sep 22, 2015 12:59:11 PM * * @author Kevin V. Bui (kvb2@pitt.edu) - * + * * Dec 15, 2015 12:55:27 PM - * + * * @author Chirayu (Kong) Wongchokprasitti * */ public class UploadStatus { - private String id; + private String id; - private boolean paused; + private boolean paused; - /** - * @param id - * @param paused - */ - public UploadStatus(String id, boolean paused) { - this.id = id; - this.paused = paused; - } + /** + * @param id + * @param paused + */ + public UploadStatus(String id, boolean paused) { + this.id = id; + this.paused = paused; + } - public String getId() { - return id; - } + public String getId() { + return id; + } - public void setId(String id) { - this.id = id; - } + public void setId(String id) { + this.id = id; + } - public boolean isPaused() { - return paused; - } + public boolean isPaused() { + return paused; + } - public void setPaused(boolean paused) { - this.paused = paused; - } + public void setPaused(boolean paused) { + this.paused = paused; + } } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/user/JsonWebToken.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/user/JsonWebToken.java index a520318..b653e39 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/user/JsonWebToken.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/dto/user/JsonWebToken.java @@ -3,26 +3,26 @@ import java.util.Date; /** - * + * * Oct 18, 2016 3:45:11 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class JsonWebToken { - + private int userId; - + private String jwt; - + private Date issuedTime; - + private long lifetime; - + private Date expireTime; - + private String[] wallTime; - + public int getUserId() { return userId; } @@ -63,12 +63,12 @@ public void setExpireTime(Date expireTime) { this.expireTime = expireTime; } - public String[] getWallTime() { - return wallTime; - } + public String[] getWallTime() { + return wallTime; + } - public void setWallTime(String[] wallTime) { - this.wallTime = wallTime; - } + public void setWallTime(String[] wallTime) { + this.wallTime = wallTime; + } } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/AbstractRequestService.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/AbstractRequestService.java index 5190c7c..1252bda 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/AbstractRequestService.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/AbstractRequestService.java @@ -1,8 +1,9 @@ package edu.pitt.dbmi.ccd.rest.client.service; +import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; +import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; import java.io.IOException; import java.net.URI; - import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; import org.apache.http.client.ClientProtocolException; @@ -13,15 +14,12 @@ import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.client.CloseableHttpClient; -import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; -import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; - /** - * + * * Aug 24, 2016 6:14:52 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class AbstractRequestService implements RestRequestService { @@ -32,78 +30,79 @@ public class AbstractRequestService implements RestRequestService { protected final CloseableHttpClient httpClient; protected final HttpClientContext localContext; - public AbstractRequestService(RestHttpsClient restHttpsClient, - String scheme, String hostname, int port) { - this.httpClient = restHttpsClient.getHttpClient(); - this.localContext = restHttpsClient.getLocalContext(); - this.scheme = scheme; - this.hostname = hostname; - this.port = port; + public AbstractRequestService(RestHttpsClient restHttpsClient, String scheme, String hostname, int port) { + this.httpClient = restHttpsClient.getHttpClient(); + this.localContext = restHttpsClient.getLocalContext(); + this.scheme = scheme; + this.hostname = hostname; + this.port = port; } - - protected CloseableHttpResponse doGet(URI uri) throws ClientProtocolException, IOException{ - HttpGet httpget = new HttpGet(uri); - httpget.addHeader(HttpHeaders.ACCEPT, "application/json"); - System.out.println("Executing request " + httpget.getRequestLine()); - return httpClient.execute(httpget, localContext); + + protected CloseableHttpResponse doGet(URI uri) throws ClientProtocolException, IOException { + HttpGet httpget = new HttpGet(uri); + httpget.addHeader(HttpHeaders.ACCEPT, "application/json"); + System.out.println("Executing request " + httpget.getRequestLine()); + return httpClient.execute(httpget, localContext); } - - protected CloseableHttpResponse doGet(URI uri, JsonWebToken jsonWebToken) throws ClientProtocolException, IOException{ - HttpGet httpget = new HttpGet(uri); - httpget.addHeader(HttpHeaders.ACCEPT, "application/json"); - httpget.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); - System.out.println("Executing request " + httpget.getRequestLine()); - - CloseableHttpResponse response = httpClient.execute(httpget); - /*System.out.println("----------------------------------------"); - - Header[] header = response.getAllHeaders(); - for (int i = 0; i < header.length; i++) { - String name = header[i].getName(); - String value = header[i].getValue(); - System.out.println(name + ":" + value); - } - System.out.println("----------------------------------------");*/ - - return response; + + protected CloseableHttpResponse doGet(URI uri, JsonWebToken jsonWebToken) + throws ClientProtocolException, IOException { + HttpGet httpget = new HttpGet(uri); + httpget.addHeader(HttpHeaders.ACCEPT, "application/json"); + httpget.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); + System.out.println("Executing request " + httpget.getRequestLine()); + + CloseableHttpResponse response = httpClient.execute(httpget); + /* + * System.out.println("----------------------------------------"); + * + * Header[] header = response.getAllHeaders(); for (int i = 0; i < + * header.length; i++) { String name = header[i].getName(); String value + * = header[i].getValue(); System.out.println(name + ":" + value); } + * System.out.println("----------------------------------------"); + */ + + return response; } - protected CloseableHttpResponse doPost(URI uri, JsonWebToken jsonWebToken) throws ClientProtocolException, IOException{ - return doPost(uri, null, jsonWebToken); + protected CloseableHttpResponse doPost(URI uri, JsonWebToken jsonWebToken) + throws ClientProtocolException, IOException { + return doPost(uri, null, jsonWebToken); } - - protected CloseableHttpResponse doPost(URI uri, HttpEntity entity, JsonWebToken jsonWebToken) throws ClientProtocolException, IOException{ - HttpPost httppost = new HttpPost(uri); - httppost.addHeader(HttpHeaders.ACCEPT, "application/json"); - httppost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); - System.out.println("Executing request " + httppost.getRequestLine()); - if(entity != null){ - httppost.setEntity(entity); - } - - CloseableHttpResponse response = httpClient.execute(httppost); - /*System.out.println("----------------------------------------"); - - Header[] header = response.getAllHeaders(); - for (int i = 0; i < header.length; i++) { - String name = header[i].getName(); - String value = header[i].getValue(); - System.out.println(name + ":" + value); - } - System.out.println("----------------------------------------");*/ - - return response; + + protected CloseableHttpResponse doPost(URI uri, HttpEntity entity, JsonWebToken jsonWebToken) + throws ClientProtocolException, IOException { + HttpPost httppost = new HttpPost(uri); + httppost.addHeader(HttpHeaders.ACCEPT, "application/json"); + httppost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); + System.out.println("Executing request " + httppost.getRequestLine()); + if (entity != null) { + httppost.setEntity(entity); + } + + CloseableHttpResponse response = httpClient.execute(httppost); + /* + * System.out.println("----------------------------------------"); + * + * Header[] header = response.getAllHeaders(); for (int i = 0; i < + * header.length; i++) { String name = header[i].getName(); String value + * = header[i].getValue(); System.out.println(name + ":" + value); } + * System.out.println("----------------------------------------"); + */ + + return response; } - protected CloseableHttpResponse doDelete(URI uri, JsonWebToken jsonWebToken) throws ClientProtocolException, IOException{ - HttpDelete httpDelete = new HttpDelete(uri); - httpDelete.addHeader(HttpHeaders.ACCEPT, "application/json"); - httpDelete.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); - System.out.println("Executing request " + httpDelete.getRequestLine()); - - CloseableHttpResponse response = httpClient.execute(httpDelete); - - return response; + protected CloseableHttpResponse doDelete(URI uri, JsonWebToken jsonWebToken) + throws ClientProtocolException, IOException { + HttpDelete httpDelete = new HttpDelete(uri); + httpDelete.addHeader(HttpHeaders.ACCEPT, "application/json"); + httpDelete.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); + System.out.println("Executing request " + httpDelete.getRequestLine()); + + CloseableHttpResponse response = httpClient.execute(httpDelete); + + return response; } - + } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/RestRequestService.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/RestRequestService.java index 3ecf589..2bbc636 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/RestRequestService.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/RestRequestService.java @@ -1,22 +1,22 @@ package edu.pitt.dbmi.ccd.rest.client.service; /** - * + * * Aug 24, 2016 4:25:33 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public interface RestRequestService { public static final String REST_API = "ccd-api"; public static final String DATASET = "dataset"; - + public static final String PRIOR_KNOWLEDGE = "priorknowledge"; public static final String CHUNKUPLOAD = "chunkupload";//"upload/chunk"; public static final String SUMMARIZE = "summarize"; - + } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/algo/AbstractAlgorithmRequest.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/algo/AbstractAlgorithmRequest.java index 155cbf5..aa94f97 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/algo/AbstractAlgorithmRequest.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/algo/AbstractAlgorithmRequest.java @@ -1,20 +1,20 @@ package edu.pitt.dbmi.ccd.rest.client.service.algo; /** - * + * * Aug 25, 2016 4:30:37 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public interface AbstractAlgorithmRequest { public static final String ALGORITHMS = "algorithms"; public static final String FGES = "FGESc"; - + public static final String FGES_DISCRETE = "FGESd"; - + public static final String GFCI = "GFCIc"; public static final String GFCI_DISCRETE = "GFCId"; diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/algo/AlgorithmService.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/algo/AlgorithmService.java index c15cc4d..01136c7 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/algo/AlgorithmService.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/algo/AlgorithmService.java @@ -1,52 +1,50 @@ package edu.pitt.dbmi.ccd.rest.client.service.algo; +import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; +import edu.pitt.dbmi.ccd.rest.client.dto.algo.AlgorithmInfo; +import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; +import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; +import edu.pitt.dbmi.ccd.rest.client.util.JsonUtils; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Set; - import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.utils.URIBuilder; import org.apache.http.util.EntityUtils; -import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; -import edu.pitt.dbmi.ccd.rest.client.dto.algo.AlgorithmInfo; -import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; -import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; -import edu.pitt.dbmi.ccd.rest.client.util.JsonUtils; - /** - * + * * Aug 24, 2016 6:04:54 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class AlgorithmService extends AbstractRequestService implements AbstractAlgorithmRequest { public AlgorithmService(RestHttpsClient restHttpsClient, - String scheme, String hostname, int port) { - super(restHttpsClient, scheme, hostname, port); + String scheme, String hostname, int port) { + super(restHttpsClient, scheme, hostname, port); } public Set listAllAlgorithms(JsonWebToken jsonWebToken) throws URISyntaxException, - ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname) - .setScheme(scheme) - .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + ALGORITHMS) - .setPort(port); + ClientProtocolException, IOException { + URIBuilder uriBuilder = new URIBuilder().setHost(hostname) + .setScheme(scheme) + .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + ALGORITHMS) + .setPort(port); - URI uri = uriBuilder.build(); - CloseableHttpResponse response = doGet(uri, jsonWebToken); + URI uri = uriBuilder.build(); + CloseableHttpResponse response = doGet(uri, jsonWebToken); - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - EntityUtils.consume(entity); + EntityUtils.consume(entity); - return JsonUtils.parseJSONArrayToAlgorithmInfos(jsonResponse); + return JsonUtils.parseJSONArrayToAlgorithmInfos(jsonResponse); } - + } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/data/DataUploadService.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/data/DataUploadService.java index a0f9164..3e9726d 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/data/DataUploadService.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/data/DataUploadService.java @@ -1,5 +1,10 @@ package edu.pitt.dbmi.ccd.rest.client.service.data; +import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; +import edu.pitt.dbmi.ccd.rest.client.dto.data.ChunkUpload; +import edu.pitt.dbmi.ccd.rest.client.dto.data.UploadStatus; +import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; +import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Path; @@ -9,21 +14,14 @@ import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; - import org.apache.http.client.utils.URIBuilder; -import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; -import edu.pitt.dbmi.ccd.rest.client.dto.data.ChunkUpload; -import edu.pitt.dbmi.ccd.rest.client.dto.data.UploadStatus; -import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; -import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; - /** - * + * * Aug 23, 2016 7:10:32 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class DataUploadService extends AbstractRequestService { @@ -34,61 +32,61 @@ public class DataUploadService extends AbstractRequestService { private static final long chunkSize = 512 * 1024; public DataUploadService(RestHttpsClient restHttpsClient, - int simultaneousUpload, String scheme, - String hostname, int port) { - super(restHttpsClient, scheme, hostname, port); - this.fileUploadMap = new HashMap<>(); - this.executorService = Executors.newFixedThreadPool(simultaneousUpload); + int simultaneousUpload, String scheme, + String hostname, int port) { + super(restHttpsClient, scheme, hostname, port); + this.fileUploadMap = new HashMap<>(); + this.executorService = Executors.newFixedThreadPool(simultaneousUpload); } public synchronized List getJobsInQueue() { - List status = new LinkedList<>(); + List status = new LinkedList<>(); - for (String key : fileUploadMap.keySet()) { - status.add(new UploadStatus(key, fileUploadMap.get(key) - .isSuspended())); - } + for (String key : fileUploadMap.keySet()) { + status.add(new UploadStatus(key, fileUploadMap.get(key) + .isSuspended())); + } - return status; + return status; } public synchronized int getUploadJobStatus(String id) { - ChunkUpload chunkUpload = fileUploadMap.get(id); - if (chunkUpload == null) { - return 100; - } else { - return (int) (chunkUpload.getProgress() * 100); - } + ChunkUpload chunkUpload = fileUploadMap.get(id); + if (chunkUpload == null) { + return 100; + } else { + return (int) (chunkUpload.getProgress() * 100); + } } public synchronized boolean pauseUploadStatus(String id) { - ChunkUpload chunkUpload = fileUploadMap.get(id); - if (chunkUpload == null) { - return false; - } else { - chunkUpload.suspend(); - return true; - } + ChunkUpload chunkUpload = fileUploadMap.get(id); + if (chunkUpload == null) { + return false; + } else { + chunkUpload.suspend(); + return true; + } } public synchronized boolean startUpload(Path file, JsonWebToken jsonWebToken) - throws URISyntaxException { - String id = file.toAbsolutePath().toString(); - URIBuilder uriBuilder = new URIBuilder() - .setHost(hostname) - .setScheme(scheme) - .setPath( - "/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + - CHUNKUPLOAD).setPort(port); - - URI uri = uriBuilder.build(); - ChunkUpload chunkUpload = new ChunkUpload(file, chunkSize, uri, - httpClient, jsonWebToken, fileUploadMap); - fileUploadMap.put(id, chunkUpload); - executorService.execute(chunkUpload); - // executorService.shutdown(); - // executorService.awaitTermination(5, TimeUnit.MINUTES); - - return true; + throws URISyntaxException { + String id = file.toAbsolutePath().toString(); + URIBuilder uriBuilder = new URIBuilder() + .setHost(hostname) + .setScheme(scheme) + .setPath( + "/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + + CHUNKUPLOAD).setPort(port); + + URI uri = uriBuilder.build(); + ChunkUpload chunkUpload = new ChunkUpload(file, chunkSize, uri, + httpClient, jsonWebToken, fileUploadMap); + fileUploadMap.put(id, chunkUpload); + executorService.execute(chunkUpload); + // executorService.shutdown(); + // executorService.awaitTermination(5, TimeUnit.MINUTES); + + return true; } } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/data/RemoteDataFileService.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/data/RemoteDataFileService.java index 7d49521..3e0848f 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/data/RemoteDataFileService.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/data/RemoteDataFileService.java @@ -1,10 +1,14 @@ package edu.pitt.dbmi.ccd.rest.client.service.data; +import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; +import edu.pitt.dbmi.ccd.rest.client.dto.data.DataFile; +import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; +import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; +import edu.pitt.dbmi.ccd.rest.client.util.JsonUtils; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Set; - import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; @@ -14,45 +18,39 @@ import org.apache.http.util.EntityUtils; import org.json.JSONObject; -import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; -import edu.pitt.dbmi.ccd.rest.client.dto.data.DataFile; -import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; -import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; -import edu.pitt.dbmi.ccd.rest.client.util.JsonUtils; - /** - * + * * Aug 23, 2016 7:10:11 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class RemoteDataFileService extends AbstractRequestService { public RemoteDataFileService(RestHttpsClient restHttpsClient, - String scheme, String hostname, int port) { - super(restHttpsClient, scheme, hostname, port); + String scheme, String hostname, int port) { + super(restHttpsClient, scheme, hostname, port); } public Set retrieveDataFileInfo(JsonWebToken jsonWebToken) - throws URISyntaxException, ClientProtocolException, IOException { + throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname) - .setScheme(scheme) - .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + DATASET) - .setPort(port); + URIBuilder uriBuilder = new URIBuilder().setHost(hostname) + .setScheme(scheme) + .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + DATASET) + .setPort(port); - URI uri = uriBuilder.build(); - /*HttpGet httpget = new HttpGet(uri); + URI uri = uriBuilder.build(); + /*HttpGet httpget = new HttpGet(uri); httpget.addHeader(HttpHeaders.ACCEPT, "application/json"); httpget.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); System.out.println("Executing request " + httpget.getRequestLine());*/ - CloseableHttpResponse response = doGet(uri, jsonWebToken);//httpClient.execute(httpget); + CloseableHttpResponse response = doGet(uri, jsonWebToken);//httpClient.execute(httpget); - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - System.out.println(jsonResponse); - /*System.out.println("----------------------------------------"); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + System.out.println(jsonResponse); + /*System.out.println("----------------------------------------"); Header[] header = response.getAllHeaders(); for (int i = 0; i < header.length; i++) { @@ -62,69 +60,68 @@ public Set retrieveDataFileInfo(JsonWebToken jsonWebToken) } System.out.println("----------------------------------------");*/ - EntityUtils.consume(entity); + EntityUtils.consume(entity); - return JsonUtils.parseJSONArrayToDataFiles(jsonResponse); + return JsonUtils.parseJSONArrayToDataFiles(jsonResponse); } public DataFile summarizeDataFile(long id, String variableType, - String fileDelimiter, JsonWebToken jsonWebToken) throws URISyntaxException, - ClientProtocolException, IOException { - - URIBuilder uriBuilder = new URIBuilder() - .setHost(hostname) - .setScheme(scheme) - .setPath( - "/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + DATASET + "/" - + SUMMARIZE).setPort(port); - - URI uri = uriBuilder.build(); - /*HttpPost httppost = new HttpPost(uri); + String fileDelimiter, JsonWebToken jsonWebToken) throws URISyntaxException, + ClientProtocolException, IOException { + + URIBuilder uriBuilder = new URIBuilder() + .setHost(hostname) + .setScheme(scheme) + .setPath( + "/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + DATASET + "/" + + SUMMARIZE).setPort(port); + + URI uri = uriBuilder.build(); + /*HttpPost httppost = new HttpPost(uri); httppost.addHeader(HttpHeaders.ACCEPT, "application/json"); httppost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt());*/ - JSONObject jsonRequest = new JSONObject(); - jsonRequest.put("id", id); - jsonRequest.put("variableType", variableType); - jsonRequest.put("fileDelimiter", fileDelimiter); - String json = jsonRequest.toString(); - System.out.println(json); - StringEntity requestEntity = new StringEntity(json, - ContentType.APPLICATION_JSON); + JSONObject jsonRequest = new JSONObject(); + jsonRequest.put("id", id); + jsonRequest.put("variableType", variableType); + jsonRequest.put("fileDelimiter", fileDelimiter); + String json = jsonRequest.toString(); + System.out.println(json); + StringEntity requestEntity = new StringEntity(json, + ContentType.APPLICATION_JSON); - //httppost.setEntity(requestEntity); + //httppost.setEntity(requestEntity); + //System.out.println("Executing request " + httppost.getRequestLine()); + CloseableHttpResponse response = doPost(uri, requestEntity, jsonWebToken);//httpClient.execute(httppost); - //System.out.println("Executing request " + httppost.getRequestLine()); - CloseableHttpResponse response = doPost(uri, requestEntity, jsonWebToken);//httpClient.execute(httppost); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + System.out.println(jsonResponse); - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - System.out.println(jsonResponse); - - EntityUtils.consume(entity); + EntityUtils.consume(entity); - return JsonUtils.parseJSONObjectToDataFile(jsonResponse); + return JsonUtils.parseJSONObjectToDataFile(jsonResponse); } public Set retrievePriorKnowledgeFileInfo(JsonWebToken jsonWebToken) - throws URISyntaxException, ClientProtocolException, IOException { + throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname) - .setScheme(scheme) - .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + PRIOR_KNOWLEDGE) - .setPort(port); + URIBuilder uriBuilder = new URIBuilder().setHost(hostname) + .setScheme(scheme) + .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + PRIOR_KNOWLEDGE) + .setPort(port); - URI uri = uriBuilder.build(); - /*HttpGet httpget = new HttpGet(uri); + URI uri = uriBuilder.build(); + /*HttpGet httpget = new HttpGet(uri); httpget.addHeader(HttpHeaders.ACCEPT, "application/json"); httpget.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jsonWebToken.getJwt()); System.out.println("Executing request " + httpget.getRequestLine());*/ - CloseableHttpResponse response = doGet(uri, jsonWebToken);//httpClient.execute(httpget); + CloseableHttpResponse response = doGet(uri, jsonWebToken);//httpClient.execute(httpget); - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - System.out.println(jsonResponse); - /*System.out.println("----------------------------------------"); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + System.out.println(jsonResponse); + /*System.out.println("----------------------------------------"); Header[] header = response.getAllHeaders(); for (int i = 0; i < header.length; i++) { @@ -134,10 +131,9 @@ public Set retrievePriorKnowledgeFileInfo(JsonWebToken jsonWebToken) } System.out.println("----------------------------------------");*/ - EntityUtils.consume(entity); + EntityUtils.consume(entity); - return JsonUtils.parseJSONArrayToDataFiles(jsonResponse); + return JsonUtils.parseJSONArrayToDataFiles(jsonResponse); } - } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/jobqueue/AbstractJobQueueRequest.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/jobqueue/AbstractJobQueueRequest.java index 63339e5..f25b420 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/jobqueue/AbstractJobQueueRequest.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/jobqueue/AbstractJobQueueRequest.java @@ -1,14 +1,14 @@ package edu.pitt.dbmi.ccd.rest.client.service.jobqueue; /** - * + * * Sep 22, 2016 3:01:05 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public interface AbstractJobQueueRequest { public static final String JOBS = "jobs"; - + } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/jobqueue/JobQueueService.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/jobqueue/JobQueueService.java index 012b8fb..3a817a0 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/jobqueue/JobQueueService.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/jobqueue/JobQueueService.java @@ -1,10 +1,15 @@ package edu.pitt.dbmi.ccd.rest.client.service.jobqueue; +import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; +import edu.pitt.dbmi.ccd.rest.client.dto.algo.AlgorithmParamRequest; +import edu.pitt.dbmi.ccd.rest.client.dto.algo.JobInfo; +import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; +import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; +import edu.pitt.dbmi.ccd.rest.client.util.JsonUtils; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.List; - import org.apache.http.HttpEntity; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; @@ -15,105 +20,98 @@ import org.apache.http.util.EntityUtils; import org.json.JSONObject; -import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; -import edu.pitt.dbmi.ccd.rest.client.dto.algo.AlgorithmParamRequest; -import edu.pitt.dbmi.ccd.rest.client.dto.algo.JobInfo; -import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; -import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; -import edu.pitt.dbmi.ccd.rest.client.util.JsonUtils; - /** - * + * * Sep 22, 2016 2:58:48 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class JobQueueService extends AbstractRequestService implements AbstractJobQueueRequest { - public JobQueueService(RestHttpsClient restHttpsClient, String scheme, String hostname, int port) { - super(restHttpsClient, scheme, hostname, port); - } + public JobQueueService(RestHttpsClient restHttpsClient, String scheme, String hostname, int port) { + super(restHttpsClient, scheme, hostname, port); + } - public JobInfo addToRemoteQueue(String AlgorithmName, AlgorithmParamRequest algoParamRequest, - JsonWebToken jsonWebToken) throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) - .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + JOBS + "/" + AlgorithmName) - .setPort(port); + public JobInfo addToRemoteQueue(String AlgorithmName, AlgorithmParamRequest algoParamRequest, + JsonWebToken jsonWebToken) throws URISyntaxException, ClientProtocolException, IOException { + URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) + .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + JOBS + "/" + AlgorithmName) + .setPort(port); - URI uri = uriBuilder.build(); + URI uri = uriBuilder.build(); - JSONObject jsonRequest = new JSONObject(algoParamRequest); - String json = jsonRequest.toString(); - StringEntity requestEntity = new StringEntity(json, ContentType.APPLICATION_JSON); + JSONObject jsonRequest = new JSONObject(algoParamRequest); + String json = jsonRequest.toString(); + StringEntity requestEntity = new StringEntity(json, ContentType.APPLICATION_JSON); - CloseableHttpResponse response = doPost(uri, requestEntity, jsonWebToken); + CloseableHttpResponse response = doPost(uri, requestEntity, jsonWebToken); - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - System.out.println(jsonResponse); + System.out.println(jsonResponse); - return JsonUtils.parseJSONObjectToJobInfo(jsonResponse); - } + return JsonUtils.parseJSONObjectToJobInfo(jsonResponse); + } - public List getActiveJobs(JsonWebToken jsonWebToken) - throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) - .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + JOBS).setPort(port); + public List getActiveJobs(JsonWebToken jsonWebToken) + throws URISyntaxException, ClientProtocolException, IOException { + URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) + .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + JOBS).setPort(port); - URI uri = uriBuilder.build(); - CloseableHttpResponse response = doGet(uri, jsonWebToken); + URI uri = uriBuilder.build(); + CloseableHttpResponse response = doGet(uri, jsonWebToken); - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - System.out.println(jsonResponse); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + System.out.println(jsonResponse); - EntityUtils.consume(entity); + EntityUtils.consume(entity); - return JsonUtils.parseJSONArrayToJobInfos(jsonResponse); - } + return JsonUtils.parseJSONArrayToJobInfos(jsonResponse); + } - public JobInfo getJobStatus(long id, JsonWebToken jsonWebToken) - throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) - .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + JOBS + "/" + id).setPort(port); + public JobInfo getJobStatus(long id, JsonWebToken jsonWebToken) + throws URISyntaxException, ClientProtocolException, IOException { + URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) + .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + JOBS + "/" + id).setPort(port); - URI uri = uriBuilder.build(); - CloseableHttpResponse response = doGet(uri, jsonWebToken); + URI uri = uriBuilder.build(); + CloseableHttpResponse response = doGet(uri, jsonWebToken); - StatusLine sl = response.getStatusLine(); - if (sl.getStatusCode() == 404) { - return null; - } + StatusLine sl = response.getStatusLine(); + if (sl.getStatusCode() == 404) { + return null; + } - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - System.out.println(jsonResponse); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + System.out.println(jsonResponse); - EntityUtils.consume(entity); + EntityUtils.consume(entity); - return JsonUtils.parseJSONObjectToJobInfo(jsonResponse); - } + return JsonUtils.parseJSONObjectToJobInfo(jsonResponse); + } - public void requestJobKilled(long id, JsonWebToken jsonWebToken) - throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) - .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + JOBS + "/" + id).setPort(port); + public void requestJobKilled(long id, JsonWebToken jsonWebToken) + throws URISyntaxException, ClientProtocolException, IOException { + URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) + .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + JOBS + "/" + id).setPort(port); - URI uri = uriBuilder.build(); - CloseableHttpResponse response = doDelete(uri, jsonWebToken); + URI uri = uriBuilder.build(); + CloseableHttpResponse response = doDelete(uri, jsonWebToken); - StatusLine sl = response.getStatusLine(); - if (sl.getStatusCode() == 404) { - return; - } + StatusLine sl = response.getStatusLine(); + if (sl.getStatusCode() == 404) { + return; + } - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - System.out.println(jsonResponse); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + System.out.println(jsonResponse); - EntityUtils.consume(entity); - } + EntityUtils.consume(entity); + } } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/result/AbstractResultRequest.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/result/AbstractResultRequest.java index e725fd7..c6daf14 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/result/AbstractResultRequest.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/result/AbstractResultRequest.java @@ -1,18 +1,18 @@ package edu.pitt.dbmi.ccd.rest.client.service.result; /** - * + * * Sep 26, 2016 1:21:48 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public interface AbstractResultRequest { public static final String RESULTS = "results"; - + public static final String COMPARISONS = "comparisons"; - + public static final String COMPARE = "compare"; - + } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/result/ResultService.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/result/ResultService.java index ad0fd3a..8151b47 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/result/ResultService.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/result/ResultService.java @@ -1,122 +1,120 @@ package edu.pitt.dbmi.ccd.rest.client.service.result; +import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; +import edu.pitt.dbmi.ccd.rest.client.dto.algo.ResultFile; +import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; +import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; +import edu.pitt.dbmi.ccd.rest.client.util.JsonUtils; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Set; - import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.utils.URIBuilder; import org.apache.http.util.EntityUtils; -import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; -import edu.pitt.dbmi.ccd.rest.client.dto.algo.ResultFile; -import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; -import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; -import edu.pitt.dbmi.ccd.rest.client.util.JsonUtils; - /** - * + * * Sep 26, 2016 1:26:23 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class ResultService extends AbstractRequestService implements AbstractResultRequest { - public ResultService(RestHttpsClient restHttpsClient, String scheme, String hostname, int port) { - super(restHttpsClient, scheme, hostname, port); - } + public ResultService(RestHttpsClient restHttpsClient, String scheme, String hostname, int port) { + super(restHttpsClient, scheme, hostname, port); + } - public Set listAlgorithmResultFiles(JsonWebToken jsonWebToken) - throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) - .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + RESULTS).setPort(port); + public Set listAlgorithmResultFiles(JsonWebToken jsonWebToken) + throws URISyntaxException, ClientProtocolException, IOException { + URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) + .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + RESULTS).setPort(port); - URI uri = uriBuilder.build(); - CloseableHttpResponse response = doGet(uri, jsonWebToken); + URI uri = uriBuilder.build(); + CloseableHttpResponse response = doGet(uri, jsonWebToken); - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - // System.out.println(jsonResponse); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + // System.out.println(jsonResponse); - EntityUtils.consume(entity); + EntityUtils.consume(entity); - return JsonUtils.parseJSONArrayToResultFiles(jsonResponse); - } + return JsonUtils.parseJSONArrayToResultFiles(jsonResponse); + } - public String downloadAlgorithmResultFile(String fileName, JsonWebToken jsonWebToken) - throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) - .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + RESULTS + "/" + fileName) - .setPort(port); + public String downloadAlgorithmResultFile(String fileName, JsonWebToken jsonWebToken) + throws URISyntaxException, ClientProtocolException, IOException { + URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) + .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + RESULTS + "/" + fileName) + .setPort(port); - URI uri = uriBuilder.build(); - CloseableHttpResponse response = doGet(uri, jsonWebToken); + URI uri = uriBuilder.build(); + CloseableHttpResponse response = doGet(uri, jsonWebToken); - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - // System.out.println(jsonResponse); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + // System.out.println(jsonResponse); - EntityUtils.consume(entity); + EntityUtils.consume(entity); - return jsonResponse; - } + return jsonResponse; + } - public Set listAlgorithmResultComparisonFiles(JsonWebToken jsonWebToken) - throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) - .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + RESULTS + "/" + COMPARISONS) - .setPort(port); + public Set listAlgorithmResultComparisonFiles(JsonWebToken jsonWebToken) + throws URISyntaxException, ClientProtocolException, IOException { + URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme) + .setPath("/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + RESULTS + "/" + COMPARISONS) + .setPort(port); - URI uri = uriBuilder.build(); - CloseableHttpResponse response = doGet(uri, jsonWebToken); + URI uri = uriBuilder.build(); + CloseableHttpResponse response = doGet(uri, jsonWebToken); - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - // System.out.println(jsonResponse); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + // System.out.println(jsonResponse); - EntityUtils.consume(entity); + EntityUtils.consume(entity); - return JsonUtils.parseJSONArrayToResultFiles(jsonResponse); - } + return JsonUtils.parseJSONArrayToResultFiles(jsonResponse); + } - public String downloadAlgorithmResultsComparisonFile(String fileName, JsonWebToken jsonWebToken) - throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme).setPath( - "/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + RESULTS + "/" + COMPARISONS + "/" + fileName) - .setPort(port); + public String downloadAlgorithmResultsComparisonFile(String fileName, JsonWebToken jsonWebToken) + throws URISyntaxException, ClientProtocolException, IOException { + URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme).setPath( + "/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + RESULTS + "/" + COMPARISONS + "/" + fileName) + .setPort(port); - URI uri = uriBuilder.build(); - CloseableHttpResponse response = doGet(uri, jsonWebToken); + URI uri = uriBuilder.build(); + CloseableHttpResponse response = doGet(uri, jsonWebToken); - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - // System.out.println(jsonResponse); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + // System.out.println(jsonResponse); - EntityUtils.consume(entity); + EntityUtils.consume(entity); - return jsonResponse; - } + return jsonResponse; + } - public String compareAlgorithmResults(String fileNames, JsonWebToken jsonWebToken) - throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme).setPath( - "/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + RESULTS + "/" + COMPARE + "/" + fileNames) - .setPort(port); + public String compareAlgorithmResults(String fileNames, JsonWebToken jsonWebToken) + throws URISyntaxException, ClientProtocolException, IOException { + URIBuilder uriBuilder = new URIBuilder().setHost(hostname).setScheme(scheme).setPath( + "/" + REST_API + "/" + jsonWebToken.getUserId() + "/" + RESULTS + "/" + COMPARE + "/" + fileNames) + .setPort(port); - URI uri = uriBuilder.build(); - CloseableHttpResponse response = doGet(uri, jsonWebToken); + URI uri = uriBuilder.build(); + CloseableHttpResponse response = doGet(uri, jsonWebToken); - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - // System.out.println(jsonResponse); + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + // System.out.println(jsonResponse); - EntityUtils.consume(entity); + EntityUtils.consume(entity); - return jsonResponse; - } + return jsonResponse; + } } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/user/AbstractUserRequest.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/user/AbstractUserRequest.java index f17f38a..bf2112c 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/user/AbstractUserRequest.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/user/AbstractUserRequest.java @@ -1,14 +1,14 @@ package edu.pitt.dbmi.ccd.rest.client.service.user; /** - * + * * Oct 18, 2016 4:09:05 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public interface AbstractUserRequest { - + public static final String JWT = "jwt"; } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/user/UserService.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/user/UserService.java index d17c849..1947245 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/user/UserService.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/service/user/UserService.java @@ -1,51 +1,49 @@ package edu.pitt.dbmi.ccd.rest.client.service.user; +import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; +import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; +import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; +import edu.pitt.dbmi.ccd.rest.client.util.JsonUtils; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; - import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.utils.URIBuilder; import org.apache.http.util.EntityUtils; -import edu.pitt.dbmi.ccd.rest.client.RestHttpsClient; -import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; -import edu.pitt.dbmi.ccd.rest.client.service.AbstractRequestService; -import edu.pitt.dbmi.ccd.rest.client.util.JsonUtils; - /** - * + * * Oct 18, 2016 4:12:07 PM - * + * * @author Chirayu (Kong) Wongchokprasitti, PhD - * + * */ public class UserService extends AbstractRequestService implements - AbstractUserRequest { + AbstractUserRequest { public UserService(RestHttpsClient restHttpsClient, String scheme, - String hostname, int port) { - super(restHttpsClient, scheme, hostname, port); + String hostname, int port) { + super(restHttpsClient, scheme, hostname, port); } public JsonWebToken requestJWT() throws URISyntaxException, - ClientProtocolException, IOException { - URIBuilder uriBuilder = new URIBuilder().setHost(hostname) - .setScheme(scheme).setPath("/" + REST_API + "/" + JWT) - .setPort(port); - - URI uri = uriBuilder.build(); - - CloseableHttpResponse response = doGet(uri); - - HttpEntity entity = response.getEntity(); - String jsonResponse = EntityUtils.toString(entity, "UTF-8"); - - System.out.println(jsonResponse); - - return JsonUtils.parseJSONObjectToJsonWebToken(jsonResponse); + ClientProtocolException, IOException { + URIBuilder uriBuilder = new URIBuilder().setHost(hostname) + .setScheme(scheme).setPath("/" + REST_API + "/" + JWT) + .setPort(port); + + URI uri = uriBuilder.build(); + + CloseableHttpResponse response = doGet(uri); + + HttpEntity entity = response.getEntity(); + String jsonResponse = EntityUtils.toString(entity, "UTF-8"); + + System.out.println(jsonResponse); + + return JsonUtils.parseJSONObjectToJsonWebToken(jsonResponse); } } diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/util/JsonUtils.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/util/JsonUtils.java index 8ec3b50..3a5a6aa 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/util/JsonUtils.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/util/JsonUtils.java @@ -1,244 +1,240 @@ package edu.pitt.dbmi.ccd.rest.client.util; +import edu.pitt.dbmi.ccd.rest.client.dto.algo.AlgorithmInfo; +import edu.pitt.dbmi.ccd.rest.client.dto.algo.JobInfo; +import edu.pitt.dbmi.ccd.rest.client.dto.algo.ResultFile; +import edu.pitt.dbmi.ccd.rest.client.dto.data.DataFile; +import edu.pitt.dbmi.ccd.rest.client.dto.data.DataFileSummary; +import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; - import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; -import edu.pitt.dbmi.ccd.rest.client.dto.algo.AlgorithmInfo; -import edu.pitt.dbmi.ccd.rest.client.dto.algo.JobInfo; -import edu.pitt.dbmi.ccd.rest.client.dto.algo.ResultFile; -import edu.pitt.dbmi.ccd.rest.client.dto.data.DataFile; -import edu.pitt.dbmi.ccd.rest.client.dto.data.DataFileSummary; -import edu.pitt.dbmi.ccd.rest.client.dto.user.JsonWebToken; - /** - * + * * Aug 25, 2016 1:38:47 PM - * + * * @author Chirayu Kong Wongchokprasitti, PhD (chw20@pitt.edu) - * + * */ public class JsonUtils { - public static JsonWebToken parseJSONObjectToJsonWebToken(JSONObject jObj) { - JsonWebToken jsonWebToken = new JsonWebToken(); - - int userId = jObj.getInt("userId"); - String jwt = jObj.getString("jwt"); - long issuedTime = jObj.getLong("issuedTime"); - long lifetime = jObj.getLong("lifetime"); - long expireTime = jObj.getLong("expireTime"); - String[] wallTime = null; - - if(!jObj.isNull("wallTime")){ - JSONArray wallTimeArray = jObj.getJSONArray("wallTime"); - if(wallTimeArray != null && wallTimeArray.length() > 0){ - wallTime = new String[wallTimeArray.length()]; - for(int index=0;index parseJSONArrayToDataFiles(String jsonResponse) { - Set dataFiles = new HashSet<>(); - - JSONArray jArray = new JSONArray(jsonResponse); - for (int i = 0; i < jArray.length(); i++) { - dataFiles.add(parseJSONObjectToDataFile(jArray.getJSONObject(i))); - } - - return dataFiles; - } - - public static DataFile parseJSONObjectToDataFile(JSONObject jObj) { - long id = jObj.getLong("id"); - String name = jObj.get("name").toString(); - long creationTime = jObj.getLong("creationTime"); - long lastModifiedTime = jObj.getLong("lastModifiedTime"); - long fileSize = jObj.getLong("fileSize"); - String md5checkSum = jObj.getString("md5checkSum"); - - DataFile dataFile = new DataFile(); - dataFile.setId(id); - dataFile.setName(name); - dataFile.setCreationTime(new Date(creationTime)); - dataFile.setLastModifiedTime(new Date(lastModifiedTime)); - dataFile.setFileSize(fileSize); - dataFile.setMd5checkSum(md5checkSum); - - System.out.println("id: " + id); - System.out.println("name: " + name); - System.out.println("creationTime: " + dataFile.getCreationTime()); - System.out.println("lastModifiedTime: " + dataFile.getLastModifiedTime()); - System.out.println("fileSize: " + fileSize); - System.out.println("md5checkSum: " + md5checkSum); - - if (!jObj.isNull("fileSummary")) { - JSONObject fileSummary = jObj.getJSONObject("fileSummary"); - String variableType = fileSummary.get("variableType") instanceof String - ? fileSummary.getString("variableType") : null; - String fileDelimiter = fileSummary.get("fileDelimiter") instanceof String - ? fileSummary.getString("fileDelimiter") : null; - Integer numOfRows = fileSummary.get("numOfRows") instanceof Integer ? fileSummary.getInt("numOfRows") - : null; - Integer numOfColumns = fileSummary.get("numOfColumns") instanceof Integer - ? fileSummary.getInt("numOfColumns") : null; - - DataFileSummary dataFileSummary = new DataFileSummary(); - dataFileSummary.setVariableType(variableType); - dataFileSummary.setFileDelimiter(fileDelimiter); - dataFileSummary.setNumOfRows(numOfRows); - dataFileSummary.setNumOfColumns(numOfColumns); - dataFile.setFileSummary(dataFileSummary); - - System.out.println("variableType: " + variableType); - System.out.println("fileDelimiter: " + fileDelimiter); - System.out.println("numOfRows: " + numOfRows); - System.out.println("numOfColumns: " + numOfColumns); - } - - System.out.println("----------------------------------------"); - - return dataFile; - } - - public static DataFile parseJSONObjectToDataFile(String jsonResponse) { - return parseJSONObjectToDataFile(new JSONObject(jsonResponse)); - } - - public static Set parseJSONArrayToAlgorithmInfos(String jsonResponse) { - Set algoInfos = new HashSet<>(); - - JSONArray jArray = new JSONArray(jsonResponse); - for (int i = 0; i < jArray.length(); i++) { - algoInfos.add(parseJSONObjectToAlgorithmInfo(jArray.getJSONObject(i))); - } - - return algoInfos; - } - - public static AlgorithmInfo parseJSONObjectToAlgorithmInfo(JSONObject jObj) { - int id = jObj.getInt("id"); - String name = jObj.getString("name"); - String description = jObj.getString("description"); - - AlgorithmInfo algoInfo = new AlgorithmInfo(); - algoInfo.setId(id); - algoInfo.setName(name); - algoInfo.setDescription(description); - - System.out.println("Algorithm id: " + id); - System.out.println("Algorithm name: " + name); - System.out.println("Algorithm description: " + description); - System.out.println("----------------------------------------"); - - return algoInfo; - } - - public static JobInfo parseJSONObjectToJobInfo(String jsonResponse) { - JSONObject jObj = new JSONObject(jsonResponse); - return parseJSONObjectToJobInfo(jObj); - } - - public static JobInfo parseJSONObjectToJobInfo(JSONObject jObj) { - long id = jObj.getLong("id"); - String algorithmName = jObj.getString("algorithmName"); - int status = jObj.getInt("status"); - long addedTime = jObj.getLong("addedTime"); - String resultFileName = jObj.getString("resultFileName"); - String resultJsonFileName = jObj.getString("resultJsonFileName"); - String errorResultFileName = jObj.getString("errorResultFileName"); - - JobInfo jobInfo = new JobInfo(); - jobInfo.setId(id); - jobInfo.setAlgorithmName(algorithmName); - jobInfo.setStatus(status); - jobInfo.setAddedTime(new Date(addedTime)); - jobInfo.setResultFileName(resultFileName); - jobInfo.setResultJsonFileName(resultJsonFileName); - jobInfo.setErrorResultFileName(errorResultFileName); - - System.out.println("Job id: " + id); - System.out.println("Algorithm name: " + algorithmName); - System.out.println("Status: " + status); - System.out.println("Added date: " + jobInfo.getAddedTime()); - System.out.println("Result FileName: " + resultFileName); - System.out.println("Result Json FileName: " + resultJsonFileName); - System.out.println("Error Result FileName: " + errorResultFileName); - System.out.println("----------------------------------------"); - - return jobInfo; - } - - public static List parseJSONArrayToJobInfos(String jsonResponse) { - List jobInfos = new ArrayList(); - - JSONArray jArray = new JSONArray(jsonResponse); - for (int i = 0; i < jArray.length(); i++) { - jobInfos.add(parseJSONObjectToJobInfo(jArray.getJSONObject(i))); - } - - return jobInfos; - } - - public static Set parseJSONArrayToResultFiles(String jsonResponse) { - Set resultFiles = new HashSet<>(); - - JSONArray jArray = new JSONArray(jsonResponse); - for (int i = 0; i < jArray.length(); i++) { - resultFiles.add(parseJSONObjectToResultFile(jArray.getJSONObject(i))); - } - - return resultFiles; - } - - public static ResultFile parseJSONObjectToResultFile(JSONObject jObj) { - String name = jObj.get("name").toString(); - long creationTime = jObj.getLong("creationTime"); - long lastModifiedTime = jObj.getLong("lastModifiedTime"); - long fileSize = jObj.getLong("fileSize"); - - ResultFile resultFile = new ResultFile(); - resultFile.setName(name); - resultFile.setCreationTime(new Date(creationTime)); - resultFile.setLastModifiedTime(new Date(lastModifiedTime)); - resultFile.setFileSize(fileSize); - - System.out.println("name: " + name); - System.out.println("creationTime: " + resultFile.getCreationTime()); - System.out.println("lastModifiedTime: " + resultFile.getLastModifiedTime()); - System.out.println("fileSize: " + fileSize); - - System.out.println("----------------------------------------"); - - return resultFile; - } - - public static ResultFile parseJSONObjectToResultFile(String jsonResponse) { - return parseJSONObjectToResultFile(new JSONObject(jsonResponse)); - } + public static JsonWebToken parseJSONObjectToJsonWebToken(JSONObject jObj) { + JsonWebToken jsonWebToken = new JsonWebToken(); + + int userId = jObj.getInt("userId"); + String jwt = jObj.getString("jwt"); + long issuedTime = jObj.getLong("issuedTime"); + long lifetime = jObj.getLong("lifetime"); + long expireTime = jObj.getLong("expireTime"); + String[] wallTime = null; + + if (!jObj.isNull("wallTime")) { + JSONArray wallTimeArray = jObj.getJSONArray("wallTime"); + if (wallTimeArray != null && wallTimeArray.length() > 0) { + wallTime = new String[wallTimeArray.length()]; + for (int index = 0; index < wallTimeArray.length(); index++) { + Object obj = wallTimeArray.get(index); + wallTime[index] = obj.toString(); + } + } + } + + jsonWebToken.setUserId(userId); + jsonWebToken.setJwt(jwt); + jsonWebToken.setIssuedTime(new Date(issuedTime)); + jsonWebToken.setLifetime(lifetime); + jsonWebToken.setExpireTime(new Date(expireTime)); + jsonWebToken.setWallTime(wallTime); + + return jsonWebToken; + } + + public static JsonWebToken parseJSONObjectToJsonWebToken(String jsonResponse) { + return parseJSONObjectToJsonWebToken(new JSONObject(jsonResponse)); + } + + public static Set parseJSONArrayToDataFiles(String jsonResponse) { + Set dataFiles = new HashSet<>(); + + JSONArray jArray = new JSONArray(jsonResponse); + for (int i = 0; i < jArray.length(); i++) { + dataFiles.add(parseJSONObjectToDataFile(jArray.getJSONObject(i))); + } + + return dataFiles; + } + + public static DataFile parseJSONObjectToDataFile(JSONObject jObj) { + long id = jObj.getLong("id"); + String name = jObj.get("name").toString(); + long creationTime = jObj.getLong("creationTime"); + long lastModifiedTime = jObj.getLong("lastModifiedTime"); + long fileSize = jObj.getLong("fileSize"); + String md5checkSum = jObj.getString("md5checkSum"); + + DataFile dataFile = new DataFile(); + dataFile.setId(id); + dataFile.setName(name); + dataFile.setCreationTime(new Date(creationTime)); + dataFile.setLastModifiedTime(new Date(lastModifiedTime)); + dataFile.setFileSize(fileSize); + dataFile.setMd5checkSum(md5checkSum); + + System.out.println("id: " + id); + System.out.println("name: " + name); + System.out.println("creationTime: " + dataFile.getCreationTime()); + System.out.println("lastModifiedTime: " + dataFile.getLastModifiedTime()); + System.out.println("fileSize: " + fileSize); + System.out.println("md5checkSum: " + md5checkSum); + + if (!jObj.isNull("fileSummary")) { + JSONObject fileSummary = jObj.getJSONObject("fileSummary"); + String variableType = fileSummary.get("variableType") instanceof String + ? fileSummary.getString("variableType") : null; + String fileDelimiter = fileSummary.get("fileDelimiter") instanceof String + ? fileSummary.getString("fileDelimiter") : null; + Integer numOfRows = fileSummary.get("numOfRows") instanceof Integer ? fileSummary.getInt("numOfRows") + : null; + Integer numOfColumns = fileSummary.get("numOfColumns") instanceof Integer + ? fileSummary.getInt("numOfColumns") : null; + + DataFileSummary dataFileSummary = new DataFileSummary(); + dataFileSummary.setVariableType(variableType); + dataFileSummary.setFileDelimiter(fileDelimiter); + dataFileSummary.setNumOfRows(numOfRows); + dataFileSummary.setNumOfColumns(numOfColumns); + dataFile.setFileSummary(dataFileSummary); + + System.out.println("variableType: " + variableType); + System.out.println("fileDelimiter: " + fileDelimiter); + System.out.println("numOfRows: " + numOfRows); + System.out.println("numOfColumns: " + numOfColumns); + } + + System.out.println("----------------------------------------"); + + return dataFile; + } + + public static DataFile parseJSONObjectToDataFile(String jsonResponse) { + return parseJSONObjectToDataFile(new JSONObject(jsonResponse)); + } + + public static Set parseJSONArrayToAlgorithmInfos(String jsonResponse) { + Set algoInfos = new HashSet<>(); + + JSONArray jArray = new JSONArray(jsonResponse); + for (int i = 0; i < jArray.length(); i++) { + algoInfos.add(parseJSONObjectToAlgorithmInfo(jArray.getJSONObject(i))); + } + + return algoInfos; + } + + public static AlgorithmInfo parseJSONObjectToAlgorithmInfo(JSONObject jObj) { + int id = jObj.getInt("id"); + String name = jObj.getString("name"); + String description = jObj.getString("description"); + + AlgorithmInfo algoInfo = new AlgorithmInfo(); + algoInfo.setId(id); + algoInfo.setName(name); + algoInfo.setDescription(description); + + System.out.println("Algorithm id: " + id); + System.out.println("Algorithm name: " + name); + System.out.println("Algorithm description: " + description); + System.out.println("----------------------------------------"); + + return algoInfo; + } + + public static JobInfo parseJSONObjectToJobInfo(String jsonResponse) { + JSONObject jObj = new JSONObject(jsonResponse); + return parseJSONObjectToJobInfo(jObj); + } + + public static JobInfo parseJSONObjectToJobInfo(JSONObject jObj) { + long id = jObj.getLong("id"); + String algorithmName = jObj.getString("algorithmName"); + int status = jObj.getInt("status"); + long addedTime = jObj.getLong("addedTime"); + String resultFileName = jObj.getString("resultFileName"); + String resultJsonFileName = jObj.getString("resultJsonFileName"); + String errorResultFileName = jObj.getString("errorResultFileName"); + + JobInfo jobInfo = new JobInfo(); + jobInfo.setId(id); + jobInfo.setAlgorithmName(algorithmName); + jobInfo.setStatus(status); + jobInfo.setAddedTime(new Date(addedTime)); + jobInfo.setResultFileName(resultFileName); + jobInfo.setResultJsonFileName(resultJsonFileName); + jobInfo.setErrorResultFileName(errorResultFileName); + + System.out.println("Job id: " + id); + System.out.println("Algorithm name: " + algorithmName); + System.out.println("Status: " + status); + System.out.println("Added date: " + jobInfo.getAddedTime()); + System.out.println("Result FileName: " + resultFileName); + System.out.println("Result Json FileName: " + resultJsonFileName); + System.out.println("Error Result FileName: " + errorResultFileName); + System.out.println("----------------------------------------"); + + return jobInfo; + } + + public static List parseJSONArrayToJobInfos(String jsonResponse) { + List jobInfos = new ArrayList(); + + JSONArray jArray = new JSONArray(jsonResponse); + for (int i = 0; i < jArray.length(); i++) { + jobInfos.add(parseJSONObjectToJobInfo(jArray.getJSONObject(i))); + } + + return jobInfos; + } + + public static Set parseJSONArrayToResultFiles(String jsonResponse) { + Set resultFiles = new HashSet<>(); + + JSONArray jArray = new JSONArray(jsonResponse); + for (int i = 0; i < jArray.length(); i++) { + resultFiles.add(parseJSONObjectToResultFile(jArray.getJSONObject(i))); + } + + return resultFiles; + } + + public static ResultFile parseJSONObjectToResultFile(JSONObject jObj) { + String name = jObj.get("name").toString(); + long creationTime = jObj.getLong("creationTime"); + long lastModifiedTime = jObj.getLong("lastModifiedTime"); + long fileSize = jObj.getLong("fileSize"); + + ResultFile resultFile = new ResultFile(); + resultFile.setName(name); + resultFile.setCreationTime(new Date(creationTime)); + resultFile.setLastModifiedTime(new Date(lastModifiedTime)); + resultFile.setFileSize(fileSize); + + System.out.println("name: " + name); + System.out.println("creationTime: " + resultFile.getCreationTime()); + System.out.println("lastModifiedTime: " + resultFile.getLastModifiedTime()); + System.out.println("fileSize: " + fileSize); + + System.out.println("----------------------------------------"); + + return resultFile; + } + + public static ResultFile parseJSONObjectToResultFile(String jsonResponse) { + return parseJSONObjectToResultFile(new JSONObject(jsonResponse)); + } } diff --git a/src/main/resources/log4j2-test.properties b/src/main/resources/log4j2-test.properties new file mode 100644 index 0000000..cd7045f --- /dev/null +++ b/src/main/resources/log4j2-test.properties @@ -0,0 +1,14 @@ +status = error +dest = err +name = PropertiesConfig + +filter.threshold.type = ThresholdFilter +filter.threshold.level = off + +appender.console.type = Console +appender.console.name = STDOUT +appender.console.layout.type = PatternLayout +appender.console.layout.pattern = %m%n + +rootLogger.level = off +rootLogger.appenderRef.stdout.ref = STDOUT \ No newline at end of file diff --git a/src/test/java/edu/pitt/dbmi/ccd/rest/client/PSCClientTest.java b/src/test/java/edu/pitt/dbmi/ccd/rest/client/PSCClientTest.java index ccddf06..12ea1ec 100644 --- a/src/test/java/edu/pitt/dbmi/ccd/rest/client/PSCClientTest.java +++ b/src/test/java/edu/pitt/dbmi/ccd/rest/client/PSCClientTest.java @@ -1,14 +1,5 @@ package edu.pitt.dbmi.ccd.rest.client; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.junit.Ignore; - import edu.pitt.dbmi.ccd.rest.client.dto.algo.AlgorithmParamRequest; import edu.pitt.dbmi.ccd.rest.client.dto.algo.JobInfo; import edu.pitt.dbmi.ccd.rest.client.dto.algo.ResultFile; @@ -21,148 +12,155 @@ import edu.pitt.dbmi.ccd.rest.client.service.jobqueue.JobQueueService; import edu.pitt.dbmi.ccd.rest.client.service.result.ResultService; import edu.pitt.dbmi.ccd.rest.client.service.user.UserService; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.junit.Ignore; /** * Unit test for simple App. */ @Ignore public class PSCClientTest extends TestCase { + /** * Create the test case * - * @param testName - * name of the test case + * @param testName name of the test case */ public PSCClientTest(String testName) { - super(testName); + super(testName); } /** * @return the suite of tests being tested */ public static Test suite() { - return new TestSuite(PSCClientTest.class); + return new TestSuite(PSCClientTest.class); } public void testApp() throws Exception { - final String username = "chw20@pitt.edu"; - final String password = "kongman20"; - final String scheme = "https"; - final String hostname = "ccd2.vm.bridges.psc.edu"; - final int port = 443; - - RestHttpsClient restClient = new RestHttpsClient(username, password, - scheme, hostname, port); - - UserService userService = new UserService(restClient, scheme, hostname, - port); - // JWT token is valid for 1 hour - JsonWebToken jsonWebToken = userService.requestJWT(); - - DataUploadService dataUploadService = new DataUploadService(restClient, - 4, scheme, hostname, port); - Path file = Paths.get("/Users/kong/Documents/DBMI/tetrad/causal-cmd/test/data/diff_delim/sim_data_20vars_100cases.txt"); - dataUploadService.startUpload(file, jsonWebToken); - - RemoteDataFileService remoteDataService = new RemoteDataFileService( - restClient, scheme, hostname, port); - int progress; - while ((progress = dataUploadService.getUploadJobStatus(file - .toAbsolutePath().toString())) < 100) { - System.out.println("Upload Progress: " + progress + "%"); - Thread.sleep(500); - } - - Set dataFiles = remoteDataService - .retrieveDataFileInfo(jsonWebToken); - long id = -1; - for (DataFile dataFile : dataFiles) { - - System.out.println(dataFile.getName() + "\t" + dataFile.getFileSummary().getVariableType()); - - if (dataFile.getName().equalsIgnoreCase( - file.getFileName().toString())) { - id = dataFile.getId(); - String variableType = "continuous"; - String fileDelimiter = "tab"; - - remoteDataService.summarizeDataFile(id, variableType, - fileDelimiter, jsonWebToken); - } - } - - // Show a list of algorithms - AlgorithmService algorithmService = new AlgorithmService(restClient, - scheme, hostname, port); - - // Set algoInfos = - algorithmService.listAllAlgorithms(jsonWebToken); - - // Run GFCI continuous - String algorithmName = AbstractAlgorithmRequest.GFCI; - AlgorithmParamRequest paramRequest = new AlgorithmParamRequest(); - paramRequest.setDatasetFileId(id); - - Map DataValidation = new HashMap<>(); - DataValidation.put("skipNonzeroVariance", false); - DataValidation.put("skipUniqueVarName", false); - // DataValidation.put("skipCategoryLimit", false); - paramRequest.setDataValidation(DataValidation); - - Map AlgorithmParameters = new HashMap<>(); - AlgorithmParameters.put("alpha", 0.01); - AlgorithmParameters.put("maxDegree", 100); - AlgorithmParameters.put("penaltyDiscount", 4.0); - AlgorithmParameters.put("faithfulnessAssumed", true); - AlgorithmParameters.put("verbose", true); - paramRequest.setAlgorithmParameters(AlgorithmParameters); - - Map JvmOptions = new HashMap<>(); - JvmOptions.put("maxHeapSize", 100); - paramRequest.setJvmOptions(JvmOptions); - - JobQueueService jobQueueService = new JobQueueService(restClient, - scheme, hostname, port); - - // Submit a job - JobInfo jobInfo = jobQueueService.addToRemoteQueue(algorithmName, - paramRequest, jsonWebToken); - - // Get all active jobs - List jobInfos = jobQueueService.getActiveJobs(jsonWebToken); - for (JobInfo job : jobInfos) { - System.out.println("JobId: " + job.getId() + "\tStatus: " - + job.getStatus()); - } - - // Get job's status - JobInfo job; - while ((job = jobQueueService.getJobStatus(jobInfo.getId(), - jsonWebToken)) != null) { - Thread.sleep(50000); - } - - ResultService resultService = new ResultService(restClient, scheme, - hostname, port); - - // Get all results - Set resultFiles = resultService - .listAlgorithmResultFiles(jsonWebToken); - - Set resultComparisonFiles = resultService - .listAlgorithmResultComparisonFiles(jsonWebToken); - - // Download result - System.out.println("Result File: " - + resultService.downloadAlgorithmResultFile( - jobInfo.getResultFileName(), jsonWebToken)); - System.out.println("Json Result File: " - + resultService.downloadAlgorithmResultFile( - jobInfo.getResultJsonFileName(), jsonWebToken)); - - assertTrue(true); + final String username = "chw20@pitt.edu"; + final String password = "kongman20"; + final String scheme = "https"; + final String hostname = "ccd2.vm.bridges.psc.edu"; + final int port = 443; + + RestHttpsClient restClient = new RestHttpsClient(username, password, + scheme, hostname, port); + + UserService userService = new UserService(restClient, scheme, hostname, + port); + // JWT token is valid for 1 hour + JsonWebToken jsonWebToken = userService.requestJWT(); + + DataUploadService dataUploadService = new DataUploadService(restClient, + 4, scheme, hostname, port); + Path file = Paths.get("/Users/kong/Documents/DBMI/tetrad/causal-cmd/test/data/diff_delim/sim_data_20vars_100cases.txt"); + dataUploadService.startUpload(file, jsonWebToken); + + RemoteDataFileService remoteDataService = new RemoteDataFileService( + restClient, scheme, hostname, port); + int progress; + while ((progress = dataUploadService.getUploadJobStatus(file + .toAbsolutePath().toString())) < 100) { + System.out.println("Upload Progress: " + progress + "%"); + Thread.sleep(500); + } + + Set dataFiles = remoteDataService + .retrieveDataFileInfo(jsonWebToken); + long id = -1; + for (DataFile dataFile : dataFiles) { + + System.out.println(dataFile.getName() + "\t" + dataFile.getFileSummary().getVariableType()); + + if (dataFile.getName().equalsIgnoreCase( + file.getFileName().toString())) { + id = dataFile.getId(); + String variableType = "continuous"; + String fileDelimiter = "tab"; + + remoteDataService.summarizeDataFile(id, variableType, + fileDelimiter, jsonWebToken); + } + } + + // Show a list of algorithms + AlgorithmService algorithmService = new AlgorithmService(restClient, + scheme, hostname, port); + + // Set algoInfos = + algorithmService.listAllAlgorithms(jsonWebToken); + + // Run GFCI continuous + String algorithmName = AbstractAlgorithmRequest.GFCI; + AlgorithmParamRequest paramRequest = new AlgorithmParamRequest(); + paramRequest.setDatasetFileId(id); + + Map DataValidation = new HashMap<>(); + DataValidation.put("skipNonzeroVariance", false); + DataValidation.put("skipUniqueVarName", false); + // DataValidation.put("skipCategoryLimit", false); + paramRequest.setDataValidation(DataValidation); + + Map AlgorithmParameters = new HashMap<>(); + AlgorithmParameters.put("alpha", 0.01); + AlgorithmParameters.put("maxDegree", 100); + AlgorithmParameters.put("penaltyDiscount", 4.0); + AlgorithmParameters.put("faithfulnessAssumed", true); + AlgorithmParameters.put("verbose", true); + paramRequest.setAlgorithmParameters(AlgorithmParameters); + + Map JvmOptions = new HashMap<>(); + JvmOptions.put("maxHeapSize", 100); + paramRequest.setJvmOptions(JvmOptions); + + JobQueueService jobQueueService = new JobQueueService(restClient, + scheme, hostname, port); + + // Submit a job + JobInfo jobInfo = jobQueueService.addToRemoteQueue(algorithmName, + paramRequest, jsonWebToken); + + // Get all active jobs + List jobInfos = jobQueueService.getActiveJobs(jsonWebToken); + for (JobInfo job : jobInfos) { + System.out.println("JobId: " + job.getId() + "\tStatus: " + + job.getStatus()); + } + + // Get job's status + JobInfo job; + while ((job = jobQueueService.getJobStatus(jobInfo.getId(), + jsonWebToken)) != null) { + Thread.sleep(50000); + } + + ResultService resultService = new ResultService(restClient, scheme, + hostname, port); + + // Get all results + Set resultFiles = resultService + .listAlgorithmResultFiles(jsonWebToken); + + Set resultComparisonFiles = resultService + .listAlgorithmResultComparisonFiles(jsonWebToken); + + // Download result + System.out.println("Result File: " + + resultService.downloadAlgorithmResultFile( + jobInfo.getResultFileName(), jsonWebToken)); + System.out.println("Json Result File: " + + resultService.downloadAlgorithmResultFile( + jobInfo.getResultJsonFileName(), jsonWebToken)); + + assertTrue(true); } }