Skip to content

Commit

Permalink
Started work on accepting octet streams
Browse files Browse the repository at this point in the history
Made the first pass at getting the services to accept octet stream
input.

Signed-off-by: Robert Smith <smithrw@ornl.gov>
  • Loading branch information
SmithRWORNL committed Feb 14, 2019
1 parent a88ad38 commit f5bf378
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 15 deletions.
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.eavp.micro.main.java;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.NumberFormat;
Expand All @@ -36,12 +37,15 @@ public class FileParsingService {

@POST
@Path("/csv/csvgrid/fulljson")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Produces(MediaType.TEXT_PLAIN)
public String plot(@QueryParam("filename") String name, String input) {
public String plot(@QueryParam("filename") String name, InputStream inputStream) {

// The grid being constructed
CSVGrid grid = new CSVGrid();

//Convert the input into a string
String input = InputStreamUtils.readStringStream(inputStream);

if (input == null) {
// TODO Read local file
Expand Down
@@ -0,0 +1,61 @@
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Robert Smith
*******************************************************************************/
package org.eclipse.eavp.micro.main.java;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
* Utility functions for dealing with input streams
*
* @author Robert Smith
*
*/
public class InputStreamUtils {

/**
* Read a full text stream from the input stream
*
* @param stream The stream to read.
* @return The string formed from the stream.
*/
public static String readStringStream(InputStream stream) {

//Buffer to hold chunks of the stream
char[] buffer = new char[1024];

//String builder that will hold the contents
StringBuilder builder = new StringBuilder();

try {

//Create a reader for the stream
InputStreamReader reader = new InputStreamReader(stream, "UTF-8");

//Loop until the stream is exhausted, adding all content to the builder
int size = reader.read(buffer, 0, 1024);

while(size >= 0) {
builder.append(buffer, 0, size);
size = reader.read(buffer, 0, 1024);
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return builder.toString();
}
}
Expand Up @@ -90,7 +90,7 @@ public VisualizationService() {
@GET
@Produces(MediaType.TEXT_HTML)
public String getSelectService(@QueryParam("filename") String name, @QueryParam("filepath") String path,
String input) {
InputStream input) {
return selectService(name, path, input);
}

Expand All @@ -108,10 +108,10 @@ public String getSelectService(@QueryParam("filename") String name, @QueryParam(
* detected service used to visualize the given file,
**/
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Produces(MediaType.TEXT_HTML)
public String postSelectService(@QueryParam("filename") String name, @QueryParam("filepath") String path,
String input) {
InputStream input) {
return selectService(name, path, input);
}

Expand All @@ -131,7 +131,7 @@ public String postSelectService(@QueryParam("filename") String name, @QueryParam
* message if the file could not be visualized.
*/
public String selectService(@QueryParam("filename") String name, @QueryParam("filepath") String path,
String input) {
InputStream input) {

// csv, dat, and txt files should be handled by the plotting service.
if (name.endsWith("csv") || name.endsWith(".dat") || name.endsWith(".txt")) {
Expand All @@ -158,7 +158,7 @@ public String selectService(@QueryParam("filename") String name, @QueryParam("fi
@Path("/qclimax")
@Produces(MediaType.TEXT_HTML)
public String selectServiceQClimax(@QueryParam("filename") String name, @QueryParam("filepath") String path,
String input) {
InputStream input) {

// csv, dat, and txt files should be handled by the plotting service.
if (name.endsWith("csv") || name.endsWith(".dat") || name.endsWith(".txt")) {
Expand Down Expand Up @@ -198,10 +198,12 @@ public String selectServiceQClimax(@QueryParam("filename") String name, @QueryPa
*/
@POST
@Path("/plot/{type}")
@Consumes(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Produces(MediaType.TEXT_HTML)
public String plot(@QueryParam("filename") String name, @QueryParam("filepath") String path,
@QueryParam("showlinefalse") String showLineFalse, @QueryParam("showmarkersfalse") String showMarkersFalse, @PathParam("type") String type, String input) {
@QueryParam("showlinefalse") String showLineFalse, @QueryParam("showmarkersfalse") String showMarkersFalse, @PathParam("type") String type, InputStream inputStream) {

String input = InputStreamUtils.readStringStream(inputStream);

String gridJSON = getPlotJSON(name, path, input);

Expand Down
Expand Up @@ -13,6 +13,8 @@

import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;

import org.eclipse.eavp.micro.main.java.FileParsingService;
import org.junit.Test;

Expand All @@ -34,27 +36,27 @@ public void testPlot() {
FileParsingService service = new FileParsingService();

//Test reading a csv file's contents
String json = service.plot("test.csv", "1,2,3\n4,5,6\n7,8,9");
String json = service.plot("test.csv", new ByteArrayInputStream("1,2,3\n4,5,6\n7,8,9".getBytes()));
assertTrue(json.contains("\"columns\":[[1.0,4.0,7.0],[2.0,5.0,8.0],[3.0,6.0,9.0]],\"columnNames\":[\"Column 1\",\"Column 2\",\"Column 3\"],\"rows\":[[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]],\"rowNames\":[\"Row 1\",\"Row 2\",\"Row 3\"]"));

//Test read a non csv files contents
json = service.plot("test.dat", "1 2 3\n4 5 6\n7 8 9");
json = service.plot("test.dat", new ByteArrayInputStream("1 2 3\n4 5 6\n7 8 9".getBytes()));
assertTrue(json.contains("\"columns\":[[1.0,4.0,7.0],[2.0,5.0,8.0],[3.0,6.0,9.0]],\"columnNames\":[\"Column 1\",\"Column 2\",\"Column 3\"],\"rows\":[[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]],\"rowNames\":[\"Row 1\",\"Row 2\",\"Row 3\"]"));

//Check that the service can handle NaNs by replacing them with 0
json = service.plot("test.csv", "1,2,3\n4,5,6\n7,8,NaN");
json = service.plot("test.csv", new ByteArrayInputStream("1,2,3\n4,5,6\n7,8,NaN".getBytes()));
assertTrue(json.contains("\"columns\":[[1.0,4.0,7.0],[2.0,5.0,8.0],[3.0,6.0,0.0]],\"columnNames\":[\"Column 1\",\"Column 2\",\"Column 3\"],\"rows\":[[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,0.0]],\"rowNames\":[\"Row 1\",\"Row 2\",\"Row 3\"]"));

//Test a file with column names
json = service.plot("test.csv", "col1,col2,col3\n1,2,3\n4,5,6\n7,8,9");
json = service.plot("test.csv", new ByteArrayInputStream("col1,col2,col3\n1,2,3\n4,5,6\n7,8,9".getBytes()));
assertTrue(json.contains("\"columnNames\":[\"col1\",\"col2\",\"col3\"]"));

//Test a file with row names
json = service.plot("test.csv", "row1,1,2,3\nrow2,4,5,6\nrow3,7,8,9");
json = service.plot("test.csv", new ByteArrayInputStream("row1,1,2,3\nrow2,4,5,6\nrow3,7,8,9".getBytes()));
assertTrue(json.contains("\"rowNames\":[\"row1\",\"row2\",\"row3\"]"));

//Test a file with column and row names
json = service.plot("test.csv", ",col1,col2,col3\nrow1,1,2,3\nrow2,4,5,6\nrow3,7,8,9");
json = service.plot("test.csv", new ByteArrayInputStream(",col1,col2,col3\nrow1,1,2,3\nrow2,4,5,6\nrow3,7,8,9".getBytes()));
assertTrue(json.contains("\"columnNames\":[\"col1\",\"col2\",\"col3\"],\"rows\":[[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]],\"rowNames\":[\"row1\",\"row2\",\"row3\"]"));
}
}

0 comments on commit f5bf378

Please sign in to comment.