Revision: 4671 http://sourceforge.net/p/vexi/code/4671 Author: mkpg2 Date: 2014-03-07 18:09:57 +0000 (Fri, 07 Mar 2014) Log Message: ----------- Switch to jre_http by default.
Added Paths: ----------- branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/JreHTTP.java Copied: branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/JreHTTP.java (from rev 4670, branches/vexi3/org.vexi-library.net/src/main/jpp/org/ibex/net/JreHTTP.jpp) =================================================================== --- branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/JreHTTP.java (rev 0) +++ branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/JreHTTP.java 2014-03-07 18:09:57 UTC (rev 4671) @@ -0,0 +1,122 @@ + +package org.ibex.net; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.ibex.util.IOUtil; +import org.ibex.util.Logger; + +public class JreHTTP implements HTTP { + + static synchronized JreHTTP create(Logger logger, String url) throws IOException{ + if (url.indexOf("://") == -1) + throw new IOException("URLs must contain a ://"); + return new JreHTTP(logger, url); + } + + final Logger logger; + final String url; + boolean ssl; + + + public JreHTTP(Logger logger, String url) throws IOException { + this.logger = logger; + this.url = url; + if (url.startsWith("https:")) { + ssl = true; + } else if (!url.startsWith("http:")) { + + throw new IOException("HTTP only supports http/https urls"); + } + } + + public HTTPResponse GET() throws IOException { + HttpURLConnection connection = null; + try { + //Create connection + connection = newConnection(); + connection.setRequestMethod("GET"); + connection.setUseCaches (false); + connection.setDoInput(true); + connection.setDoOutput(false); + + //Get Response + return readResponse(connection); + } catch (IOException e) { + throw e; + } finally{ +// if(connection!=null){ +// connection.disconnect(); +// } + } + } + + public HTTPResponse POST(String contentType, byte[] content) throws IOException { + HttpURLConnection connection = null; + try { + //Create connection + connection = newConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", + contentType); + + connection.setRequestProperty("Content-Length", "" + Integer.toString(content.length)); + connection.setRequestProperty("Content-Language", "en-US"); + + connection.setUseCaches (false); + connection.setDoInput(true); + connection.setDoOutput(true); + + //Send request + OutputStream wr = connection.getOutputStream(); + wr.write(content); + wr.flush (); + wr.close (); + + //Get Response + return readResponse(connection); + } catch (IOException e) { + throw e; + } finally{ +// if(connection!=null){ +// connection.disconnect(); +// } + } + } + + private HttpURLConnection newConnection() throws IOException{ + URL url = new URL(this.url); + return (HttpURLConnection)url.openConnection(); + } + + public HTTPResponse readResponse(HttpURLConnection connection) throws IOException{ + InputStream is = connection.getInputStream(); + int statusCode = connection.getResponseCode(); + if(statusCode>=400){ + byte[] bytes = IOUtil.toByteArray(is); // HACK doesn't seem to work unless we read this here + HTTPEntityInfo info = new HTTPEntityInfo((int)bytes.length,"",connection.getContentType()); + throw new HTTPErrorResponse(connection.getResponseMessage(), statusCode+"", bytes, info); + }else{ + + String lastmod = connection.getHeaderField("Last-Modified"); + String lengthStr = connection.getHeaderField("Content-Length"); + String contentTypeR = connection.getHeaderField("Content-Type"); + int length; + if(lengthStr!=null){ + length = Integer.parseInt(lengthStr); + }else{ + length = -1; + } + HTTPEntityInfo info = new HTTPEntityInfo(length, lastmod, contentTypeR); + return new HTTPResponse(info, is);//new ByteArrayInputStream(bytes)); + } + } + +} + + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce. With Perforce, you get hassle-free workflows. Merge that actually works. Faster operations. Version large binaries. Built-in WAN optimization and the freedom to use Git, Perforce or both. Make the move to Perforce. http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn