Commits

John Paulett committed d35f265

Add support for a GetURL anon language function, which retreives the content of a GET request to the URL
that is passed to the funtion. Connection code refactored out of UIDRequester.

Comments (0)

Files changed (5)

src/main/java/org/nrg/dcm/edit/fn/GetURL.java

+package org.nrg.dcm.edit.fn;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.dcm4che2.data.DicomObject;
+import org.nrg.dcm.edit.ScriptEvaluationException;
+import org.nrg.dcm.edit.ScriptFunction;
+import org.nrg.dcm.edit.Value;
+import org.nrg.util.HttpClient;
+import org.nrg.util.IHttpClient;
+
+public class GetURL implements ScriptFunction {
+	public static final String name = "getURL";
+
+	public Value apply(List args) throws ScriptEvaluationException {
+		final URL url = getURL(args.get(0));
+		return new Value() {
+			public Set getVariables() {
+				return Collections.EMPTY_SET;
+			}
+
+			public Set getTags() {
+				return Collections.EMPTY_SET;
+			}
+
+			public String on(DicomObject o) throws ScriptEvaluationException {
+				return request(url);
+			}
+
+			public String on(Map m) throws ScriptEvaluationException {
+				return request(url);
+			}
+		};
+	}
+
+	private URL getURL(Object arg) throws ScriptEvaluationException {
+		try {
+			return new URL((String) arg);
+		} catch (MalformedURLException e) {
+			throw new ScriptEvaluationException("Improper URL, " + arg);
+		}
+	}
+
+	private String request(final URL url) throws ScriptEvaluationException {
+		try {
+			String response = getHttpClient().request(url);
+			if (response == null) {
+				throw new ScriptEvaluationException("Empty response from external webservice, " + url);
+			}
+			return response.trim();
+		} catch (IOException e) {
+			throw new ScriptEvaluationException("Error connecting to external webservice, " + url, e);
+		}
+	}
+
+	// expose as protected so test case can override the implementation
+	protected IHttpClient getHttpClient() {
+		return new HttpClient();
+	}
+}

src/main/java/org/nrg/dcm/edit/gen/util/UIDRequester.java

  */
 package org.nrg.dcm.edit.gen.util;
 
-import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.HttpURLConnection;
 import java.net.URL;
 
 import org.apache.log4j.Logger;
 import org.json.JSONObject;
 import org.json.JSONTokener;
 import org.nrg.dcm.edit.DicomUtils;
+import org.nrg.util.HttpClient;
 
 
 /**
 
     public String getUID() throws IOException,JSONException {
 	if (null == uid) {
-	    final HttpURLConnection connection;
-	    try {
-		connection = (HttpURLConnection)server.openConnection();
-	    } catch (ClassCastException e) {
-		throw new IOException("unable to make HTTP/HTTPS connection to URL " + server);
-	    }
-	    if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
-		throw new IOException("request failed: " + connection.getResponseCode()
-			+ " " + connection.getResponseMessage());
-	    }
-
-	    final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
-	    final StringBuffer text = new StringBuffer();
-	    for (String line = reader.readLine(); null != line; line = reader.readLine()) {
-		text.append(line);
-	    }
-	    reader.close();
-	    connection.disconnect();
+		String text = new HttpClient().request(server);
 
 	    final JSONObject jso = new JSONObject(new JSONTokener(text.toString()));
 	    final String o = jso.getString(UID_KEY);

src/main/java/org/nrg/util/HttpClient.java

+package org.nrg.util;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+public class HttpClient implements IHttpClient {
+
+	public String request(final URL url) throws IOException {
+		final HttpURLConnection connection;
+		try {
+			connection = (HttpURLConnection) url.openConnection();
+		} catch (ClassCastException e) {
+			throw new IOException("unable to make HTTP/HTTPS connection to URL " + url);
+		}
+		if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
+			throw new IOException("request failed: " + connection.getResponseCode() + " " + connection.getResponseMessage());
+		}
+
+		final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+		final StringBuffer text = new StringBuffer();
+		for (String line = reader.readLine(); null != line; line = reader.readLine()) {
+			text.append(line);
+		}
+		reader.close();
+		connection.disconnect();
+		return text.toString();
+	}
+}

src/main/java/org/nrg/util/IHttpClient.java

+package org.nrg.util;
+
+import java.io.IOException;
+import java.net.URL;
+
+public interface IHttpClient {
+	public String request(URL url) throws IOException;
+}

src/test/java/org/nrg/dcm/edit/fn/GetURLTest.java

+package org.nrg.dcm.edit.fn;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.nrg.dcm.edit.Value;
+import org.nrg.util.IHttpClient;
+
+public class GetURLTest extends TestCase {
+	private FakeGetURL function;
+	private Map context;
+
+	protected void setUp() throws Exception {
+		function = new FakeGetURL();
+		context = new HashMap();
+	}
+
+	public void testApply() throws Exception {
+		function.response = "foobar";
+		Value value = function.apply(Arrays.asList(new String[] { "http://nrg.wustl.edu/something" }));
+		assertEquals("foobar", value.on(context));
+	}
+
+	public void testApplyExtraWhitespace() throws Exception {
+		function.response = "foobar \n\t";
+		Value value = function.apply(Arrays.asList(new String[] { "http://nrg.wustl.edu/something" }));
+		assertEquals("foobar", value.on(context));
+	}
+
+	private static class FakeGetURL extends GetURL {
+		String response;
+
+		protected IHttpClient getHttpClient() {
+			return new IHttpClient() {
+				public String request(URL url) throws IOException {
+					return response;
+				}
+			};
+		}
+	}
+}