Created
May 22, 2014 08:45
-
-
Save davidraviv/4d16b7e3590e59c5d3fa to your computer and use it in GitHub Desktop.
Simple HTTP sender using java.net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.x.proxy.http; | |
import java.io.Serializable; | |
import java.util.Map; | |
/** | |
* Created by davidraviv on 7/5/14. | |
*/ | |
public class HTTPMessage implements Serializable { | |
public String url; | |
public Map<String, String> headers; | |
public Map<String, String> wmParams; | |
public Map<String, String> params; | |
public String payload; | |
public int responseCode = -1; | |
public String method; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.x.proxy | |
import com.x.proxy.http.HTTPMessage | |
import scala.collection.JavaConversions._ | |
import xml.{XML, Elem} | |
import java.net.URL | |
import java.io.{InputStream, OutputStream} | |
case class HTTPSender(urlStr: String) { | |
val url = new URL(urlStr) | |
/** | |
* Submits a message to the service endpoint. | |
* | |
* @param message the message to submit | |
* @return response message containing the response | |
*/ | |
def apply(message: HTTPMessage): HTTPMessage = { | |
val header = message.headers | |
val payload = message.payload.getBytes(Const.UTF8) | |
val method = message.method | |
import java.net.HttpURLConnection | |
val con = url.openConnection.asInstanceOf[HttpURLConnection] | |
con.setDoInput(true) | |
con.setDoOutput(true) | |
con.setUseCaches(false) | |
con.setRequestMethod(method) | |
for (h <- header) { | |
con.setRequestProperty(h._1, h._2) | |
} | |
val out = con.getOutputStream | |
try { | |
out.write(payload) | |
out.flush | |
} finally { | |
out.close | |
} | |
val responseHeader = parseResponseHeaders(con, 1) | |
val responseCode = con.getResponseCode | |
val in = | |
if (responseCode == 200) | |
con.getInputStream | |
else con.getErrorStream | |
try { | |
// val response = new Message("response from target", message.actionClassName, Const.EMPTY_STRING, responseHeader, null, null, slurp(in), responseCode, Const.EMPTY_STRING, Const.EMPTY_STRING, Const.EMPTY_STRING, -1) | |
val response = new HTTPMessage | |
response.headers = responseHeader | |
response.method = message.method | |
response.payload = slurp(in) | |
response.responseCode = responseCode | |
response | |
} finally { | |
if (in != null) in.close | |
} | |
} | |
private def slurp(in: java.io.InputStream): String = { | |
if (in == null) { | |
Const.EMPTY_STRING | |
} else { | |
val out = new java.io.ByteArrayOutputStream() | |
try { | |
copyStream(in, out) | |
new String(out.toByteArray(), Const.UTF8) | |
} finally { | |
out.close() | |
} | |
} | |
} | |
private def parseResponseHeaders(con: java.net.URLConnection, n: Int): Map[String, String] = { | |
val key = con.getHeaderFieldKey(n) | |
if (key == null) | |
Map.empty | |
else | |
parseResponseHeaders(con, n + 1) ++ Map(key -> con.getHeaderField(key)) | |
} | |
private def copyStream(istream: InputStream, ostream: OutputStream): Unit = { | |
var bytes = new Array[Byte](1024) | |
var len = -1 | |
while ( { | |
len = istream.read(bytes, 0, 1024); | |
len != -1 | |
}) | |
ostream.write(bytes, 0, len) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment