Commits

John Paulett committed 5d6a5c0

Fix casting issue on GetURL and register getURL in the ScriptApplicator.

Comments (0)

Files changed (2)

src/main/java/org/nrg/dcm/edit/ScriptApplicator.java

 import org.dcm4che2.io.DicomOutputStream;
 import org.nrg.dcm.edit.DicomUtils.DicomObjectException;
 import org.nrg.dcm.edit.fn.Format;
+import org.nrg.dcm.edit.fn.GetURL;
 import org.nrg.dcm.edit.fn.Lowercase;
 import org.nrg.dcm.edit.fn.Substring;
 import org.nrg.dcm.edit.gen.UIDGenerator;
 		    astParser.setFunction(Substring.name, new Substring());
 		    astParser.setFunction(Format.name, new Format());
 		    astParser.setFunction(Lowercase.name, new Lowercase());
+		    astParser.setFunction(GetURL.name, new GetURL());
 		    for (final Iterator mei = functions.entrySet().iterator(); mei.hasNext(); ) {
 			final Map.Entry me = (Map.Entry)mei.next();
 			logger.trace("adding function " + me);

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

 	public static final String name = "getURL";
 
 	public Value apply(List args) throws ScriptEvaluationException {
-		final URL url = getURL(args.get(0));
+		final Value url = (Value) args.get(0);
 		return new Value() {
 			public Set getVariables() {
 				return Collections.EMPTY_SET;
 			}
 
 			public String on(DicomObject o) throws ScriptEvaluationException {
-				return request(url);
+				return request(url.on(o));
 			}
 
 			public String on(Map m) throws ScriptEvaluationException {
-				return request(url);
+				return request(url.on(m));
 			}
 		};
 	}
 
-	private URL getURL(Object arg) throws ScriptEvaluationException {
+	private String request(final String url) 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);
+			String response = getHttpClient().request(new URL(url));
 			if (response == null) {
 				throw new ScriptEvaluationException("Empty response from external webservice, " + url);
 			}
 			return response.trim();
+		} catch (MalformedURLException e) {
+			throw new ScriptEvaluationException("Improper URL, " + url);
 		} catch (IOException e) {
 			throw new ScriptEvaluationException("Error connecting to external webservice, " + url, e);
 		}