From b365bf48c38fd4d6d82a31ecdd9ede8a8e831c14 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 21 Mar 2014 16:32:51 +0000 Subject: [PATCH] * support setting request properties for post requests (required for PushBullet support in AMC script) --- .../sourceforge/filebot/cli/ScriptShell.lib.groovy | 6 +++--- source/net/sourceforge/filebot/web/WebRequest.java | 14 +++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy index ed89bfc3..9629e7a4 100644 --- a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy +++ b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy @@ -84,9 +84,9 @@ String.metaClass.getHtml = { new XmlParser(new org.cyberneko.html.parsers.SAXPar String.metaClass.getXml = { new XmlParser().parseText(delegate) } URL.metaClass.get = { delegate.getText() } -URL.metaClass.post = { Map parameters -> post(delegate.openConnection(), parameters) } -URL.metaClass.post = { byte[] data, contentType = 'application/octet-stream' -> post(delegate.openConnection(), data, contentType) } -URL.metaClass.post = { String text, contentType = 'text/plain', csn = 'utf-8' -> delegate.post(text.getBytes(csn), contentType) } +URL.metaClass.post = { Map parameters, requestParameters = null -> post(delegate, parameters, requestParameters) } +URL.metaClass.post = { byte[] data, contentType = 'application/octet-stream', requestParameters = null -> post(delegate, data, contentType, requestParameters) } +URL.metaClass.post = { String text, contentType = 'text/plain', csn = 'utf-8', requestParameters = null -> post(delegate, text.getBytes(csn), contentType, requestParameters) } ByteBuffer.metaClass.saveAs = { f -> f = f as File; f = f.absoluteFile; f.parentFile.mkdirs(); writeFile(delegate.duplicate(), f); f } URL.metaClass.saveAs = { f -> fetch(delegate).saveAs(f) } diff --git a/source/net/sourceforge/filebot/web/WebRequest.java b/source/net/sourceforge/filebot/web/WebRequest.java index f7d65d9b..637bfd04 100644 --- a/source/net/sourceforge/filebot/web/WebRequest.java +++ b/source/net/sourceforge/filebot/web/WebRequest.java @@ -173,16 +173,24 @@ public final class WebRequest { return buffer.getByteBuffer(); } - public static ByteBuffer post(HttpURLConnection connection, Map parameters) throws IOException { - return post(connection, encodeParameters(parameters, true).getBytes("UTF-8"), "application/x-www-form-urlencoded"); + public static ByteBuffer post(URL url, Map parameters, Map requestParameters) throws IOException { + return post(url, encodeParameters(parameters, true).getBytes("UTF-8"), "application/x-www-form-urlencoded", requestParameters); } - public static ByteBuffer post(HttpURLConnection connection, byte[] postData, String contentType) throws IOException { + public static ByteBuffer post(URL url, byte[] postData, String contentType, Map requestParameters) throws IOException { + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.addRequestProperty("Content-Length", String.valueOf(postData.length)); connection.addRequestProperty("Content-Type", contentType); connection.setRequestMethod("POST"); connection.setDoOutput(true); + if (requestParameters != null) { + for (Entry parameter : requestParameters.entrySet()) { + connection.addRequestProperty(parameter.getKey(), parameter.getValue()); + } + } + // write post data OutputStream out = connection.getOutputStream(); out.write(postData);