View Javadoc
1 package org.neuclear.xml.soap; 2 3 /*** 4 * @author pelleb 5 * @version $Revision: 1.1 $ 6 */ 7 8 /* 9 * The NeuClear Project and it's libraries are 10 * (c) 2002-2004 Antilles Software Ventures SA 11 * For more information see: http://neuclear.org 12 * 13 * This library is free software; you can redistribute it and/or 14 * modify it under the terms of the GNU Lesser General Public 15 * License as published by the Free Software Foundation; either 16 * version 2.1 of the License, or (at your option) any later version. 17 * 18 * This library is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 * Lesser General Public License for more details. 22 * 23 * You should have received a copy of the GNU Lesser General Public 24 * License along with this library; if not, write to the Free Software 25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 26 */ 27 28 import org.dom4j.Document; 29 import org.dom4j.DocumentException; 30 import org.dom4j.Element; 31 import org.dom4j.io.SAXReader; 32 import org.neuclear.commons.NeuClearException; 33 34 import java.io.*; 35 import java.net.HttpURLConnection; 36 import java.net.MalformedURLException; 37 import java.net.URL; 38 import java.net.URLConnection; 39 40 public final class RESTTools { 41 private RESTTools() { 42 } 43 44 45 public static Element restRequestElement(final String endpoint, final Element request) throws NeuClearException { 46 try { 47 return restRequestElement(new URL(endpoint), request); 48 } catch (MalformedURLException e) { 49 throw new NeuClearException(e); 50 } 51 } 52 53 public static InputStream restRequest(final String endpoint, final String request) throws NeuClearException { 54 try { 55 return restRequest(new URL(endpoint), request); 56 } catch (MalformedURLException e) { 57 throw new NeuClearException(e); 58 } 59 } 60 61 public static Element restRequestElement(final URL endpoint, final Element request) throws NeuClearException { 62 try { 63 return restRequestElement(endpoint.openConnection(), request); 64 } catch (IOException e) { 65 throw new NeuClearException(e); 66 } 67 68 } 69 70 public static InputStream restRequest(final URL endpoint, final String request) throws NeuClearException { 71 try { 72 return restRequest(endpoint.openConnection(), request); 73 } catch (IOException e) { 74 throw new NeuClearException(e); 75 } 76 } 77 78 public static InputStream restRequest(final URLConnection conn, final Element request) throws NeuClearException { 79 return restRequest(conn, request.asXML()); 80 } 81 82 public static InputStream restRequest(final URLConnection conn, final String request) throws NeuClearException { 83 try { 84 //Set Headers 85 conn.setDoOutput(true); 86 conn.setDoInput(true); 87 if (conn instanceof HttpURLConnection) { 88 ((HttpURLConnection) conn).setRequestMethod("POST"); 89 ((HttpURLConnection) conn).setRequestProperty("Content-type", "text/xml"); 90 } 91 final OutputStream out = conn.getOutputStream(); 92 out.write(request.getBytes()); 93 out.close(); 94 95 return conn.getInputStream(); 96 // final InputStream stream = conn.getInputStream(); 97 // final ByteArrayOutputStream bos=new ByteArrayOutputStream(); 98 // byte b[]=new byte[1024]; 99 // int c=stream.read(b); 100 // while(c>=0){ 101 // bos.write(b,0,c); 102 // c=stream.read(b); 103 // } 104 // final byte[] bytes = bos.toByteArray(); 105 // System.out.println(new String(bytes)); 106 // return new ByteArrayInputStream(bytes); 107 } catch (IOException e) { 108 throw new NeuClearException(e); 109 } 110 } 111 112 113 public static Element restRequestElement(final URLConnection conn, final Element request) throws NeuClearException, IOException { 114 try { 115 final BufferedReader in = new BufferedReader(new InputStreamReader(restRequest(conn, request))); 116 final SAXReader reader = new SAXReader(); 117 final Document document = reader.read(in); 118 in.close(); 119 return document.getRootElement(); 120 } catch (DocumentException e) { 121 throw new NeuClearException(e); 122 } 123 } 124 125 126 }

This page was automatically generated by Maven