diff --git a/README.md b/README.md index 75a4c01..abd755e 100755 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ This project depends on the below major projects. * Apache Velocity * Apache Log4j2 * Mashape Unirest +* SparkJava diff --git a/modules/repl-server/src/main/java/edu/emory/bmi/datarepl/ds_mgmt/tcia/TCIAClientImpl.java b/modules/repl-server/src/main/java/edu/emory/bmi/datarepl/ds_mgmt/tcia/TCIAClientImpl.java index b40dfb0..6ae12c6 100755 --- a/modules/repl-server/src/main/java/edu/emory/bmi/datarepl/ds_mgmt/tcia/TCIAClientImpl.java +++ b/modules/repl-server/src/main/java/edu/emory/bmi/datarepl/ds_mgmt/tcia/TCIAClientImpl.java @@ -1,6 +1,7 @@ package edu.emory.bmi.datarepl.ds_mgmt.tcia; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; @@ -293,6 +294,38 @@ public String getPatient(String collection, OutputFormat format) } } + /** + * Downloads the images in a given series + * @param seriesInstanceUID the inst + * @throws TCIAClientException + * @throws IOException + */ + public void downloadImagesOfSeries(String seriesInstanceUID) throws TCIAClientException, IOException { + ITCIAClient.ImageResult imageResult = getImage(seriesInstanceUID); + saveTo(imageResult.getRawData(), seriesInstanceUID + ".zip", "."); + } + + + private static void saveTo(InputStream in, String name, String directory) throws IOException { + FileOutputStream fos = new FileOutputStream(directory + "/" + name); + byte[] buffer = new byte[4096]; + int read = -1; + int sum = 0; + while ((read = in.read(buffer)) > 0) { + fos.write(buffer, 0, read); + long mseconds = System.currentTimeMillis(); + sum += read; + + if (mseconds % 10 == 0) { + System.out.println(String.format("Bytes Written %s", sum)); + } + } + + fos.close(); + in.close(); + } + + public ImageResult getImage(String seriesInstanceUID) throws edu.emory.bmi.datarepl.ds_mgmt.tcia.TCIAClientException { try { @@ -310,8 +343,6 @@ public ImageResult getImage(String seriesInstanceUID) // create a new HttpGet request HttpGet request = new HttpGet(uri); -// add api_key to the header - request.setHeader(API_KEY_FIELD, apiKey); long startTime = System.currentTimeMillis(); HttpResponse response = httpClient.execute(request); long diff = System.currentTimeMillis() - startTime; diff --git a/modules/repl-server/src/test/java/edu/emory/bmi/datarepl/test/TestTCIAClient.java b/modules/repl-server/src/test/java/edu/emory/bmi/datarepl/test/TestTCIAClient.java index f82b381..2098166 100755 --- a/modules/repl-server/src/test/java/edu/emory/bmi/datarepl/test/TestTCIAClient.java +++ b/modules/repl-server/src/test/java/edu/emory/bmi/datarepl/test/TestTCIAClient.java @@ -53,24 +53,17 @@ public void testGetCollectionValues() { * Method : GetImage * Description : Returns images in a zip file */ - @Test public void testGetImage() { - - // create TCIA Client by passing API-Key and baseUrl in the constructor - ITCIAClient client = TCIAClientImpl.getTCIAClientImpl(); + TCIAClientImpl client = TCIAClientImpl.getTCIAClientImpl(); String seriesInstanceUID = "1.3.6.1.4.1.14519.5.2.1.7695.4001.306204232344341694648035234440"; try { - // Make the RESTfull call . Response comes back as InputStream. - ITCIAClient.ImageResult imageResult = client.getImage(seriesInstanceUID); - saveTo(imageResult.getRawData(), seriesInstanceUID + ".zip", "."); - + client.downloadImagesOfSeries(seriesInstanceUID); } catch (TCIAClientException e) { - fail(e.getMessage()); // request failed - } catch (Exception e) { - fail(e.getMessage()); // request failed + fail("TCIA Client Exception in downloading the images. " + e.getMessage()); + } catch (IOException e) { + fail("IOException in downloading the images. " + e.getMessage()); } - } @@ -238,24 +231,6 @@ public static void saveTo(InputStream in, String name, String directory, int est in.close(); } - public static void saveTo(InputStream in, String name, String directory) throws IOException { - FileOutputStream fos = new FileOutputStream(directory + "/" + name); - byte[] buffer = new byte[4096]; - int read = -1; - int sum = 0; - while ((read = in.read(buffer)) > 0) { - fos.write(buffer, 0, read); - long mseconds = System.currentTimeMillis(); - sum += read; - - if (mseconds % 10 == 0) { - System.out.println(String.format("Bytes Written %s", sum)); - } - } - - fos.close(); - in.close(); - } public static String toString(InputStream in) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream();