001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/framework/util/NetWorker.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2006 by: 006 EXSE, Department of Geography, University of Bonn 007 http://www.giub.uni-bonn.de/deegree/ 008 lat/lon GmbH 009 http://www.lat-lon.de 010 011 This library is free software; you can redistribute it and/or 012 modify it under the terms of the GNU Lesser General Public 013 License as published by the Free Software Foundation; either 014 version 2.1 of the License, or (at your option) any later version. 015 016 This library is distributed in the hope that it will be useful, 017 but WITHOUT ANY WARRANTY; without even the implied warranty of 018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 019 Lesser General Public License for more details. 020 021 You should have received a copy of the GNU Lesser General Public 022 License along with this library; if not, write to the Free Software 023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 024 025 Contact: 026 027 Andreas Poth 028 lat/lon GmbH 029 Aennchenstr. 19 030 53115 Bonn 031 Germany 032 E-Mail: poth@lat-lon.de 033 034 Prof. Dr. Klaus Greve 035 Department of Geography 036 University of Bonn 037 Meckenheimer Allee 166 038 53115 Bonn 039 Germany 040 E-Mail: greve@giub.uni-bonn.de 041 042 043 ---------------------------------------------------------------------------*/ 044 package org.deegree.framework.util; 045 046 import java.io.ByteArrayOutputStream; 047 import java.io.IOException; 048 import java.io.InputStream; 049 import java.io.OutputStreamWriter; 050 import java.io.PrintWriter; 051 import java.net.URL; 052 import java.net.URLConnection; 053 054 import org.deegree.framework.log.ILogger; 055 import org.deegree.framework.log.LoggerFactory; 056 057 /** 058 * Performs a HTTP request using the service URL submitted to the constructor 059 * 060 * @version $Revision: 7935 $ 061 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 062 */ 063 public class NetWorker { 064 065 private static final ILogger LOG = LoggerFactory.getLogger( NetWorker.class ); 066 private static final int GET = 0; 067 private static final int POST = 1; 068 private String contentType = null; 069 private String request = null; 070 private URL url = null; 071 private int reqType = -1; 072 private String encoding = null; 073 074 /** 075 * constructor for initializing a HTTP GET connection with 076 * with default character encoding 077 * @param url URL to the net resource containing the URI 078 */ 079 public NetWorker( URL url ) { 080 081 this.reqType = GET; 082 this.url = url; 083 this.encoding = CharsetUtils.getSystemCharset(); 084 085 } 086 087 /** 088 * constructor for initializing a HTTP GET connection with a user defined 089 * encoding 090 * @param encoding desired character encoding 091 * @param url URL to the net resource containing the URI 092 */ 093 public NetWorker( String encoding, URL url ) { 094 this.reqType = GET; 095 this.url = url; 096 this.encoding = encoding; 097 098 } 099 100 /** 101 * constructor for initializing a HTTP POST connection with UTF-8 as 102 * character encoding 103 * @param url URL to the net resource (without URI parameters) 104 * @param request request that shall be posted to the net resource 105 */ 106 public NetWorker( URL url, String request ) { 107 108 this.reqType = POST; 109 this.url = url; 110 this.request = request; 111 this.encoding = "UTF-8"; 112 113 } 114 115 /** 116 * constructor for initializing a HTTP POST connection with a user defined 117 * encoding 118 * @param encoding desired character encoding 119 * @param url URL to the net resource (without URI parameters) 120 * @param request request that shall be posted to the net resource 121 */ 122 public NetWorker( String encoding, URL url, String request ) { 123 this.reqType = POST; 124 this.url = url; 125 this.request = request; 126 this.encoding = encoding; 127 128 } 129 130 /** 131 * @return the content type of the response from the connected net resource. 132 * this method shall be called after <tt>getInputStream</tt> or 133 * <tt>getDataAsByteArr</tt> has been called. 134 * 135 */ 136 public String getContentType() { 137 return contentType; 138 } 139 140 141 /** 142 * sends the request that have been passed to the constructor without 143 * expecting to receive a response. 144 * @throws IOException 145 */ 146 public void sendRequest() throws IOException { 147 148 LOG.logDebug( "Trying to send request " + request + " via POST with encoding " + encoding ); 149 150 // open connection to the requested host 151 URLConnection connection = url.openConnection(); 152 153 connection.setDoInput( false ); 154 155 // sets the content type of the request 156 connection.setRequestProperty( "Content-Type", "text/xml" ); 157 158 if ( ( reqType == POST ) && ( request != null ) ) { 159 connection.setDoOutput( true ); 160 161 // get connection stream 162 OutputStreamWriter osw = 163 new OutputStreamWriter( connection.getOutputStream(), encoding ); 164 PrintWriter os = new PrintWriter( osw ); 165 166 // write post request into stream 167 os.print( request ); 168 os.close(); 169 } else { 170 connection.setDoOutput( false ); 171 } 172 173 LOG.logDebug("Sent request."); 174 } 175 176 /** 177 * returns an <tt>InputStream</tt> from the et resource 178 * 179 * @return InputStream accessing the net resource 180 * 181 * @throws IOException 182 */ 183 public InputStream getInputStream() throws IOException { 184 185 // open connection to the requested host 186 URLConnection connection = url.openConnection(); 187 188 connection.setDoInput( true ); 189 190 // sets the content type of the request 191 connection.setRequestProperty( "Content-Type", "text/xml" ); 192 193 // provide result stream 194 InputStream is = null; 195 196 if ( ( reqType == POST ) && ( request != null ) ) { 197 connection.setDoOutput( true ); 198 199 // get connection stream 200 OutputStreamWriter osw = 201 new OutputStreamWriter( connection.getOutputStream(), encoding ); 202 PrintWriter os = new PrintWriter( osw ); 203 204 // write post request into stream 205 os.print( request ); 206 os.close(); 207 } else { 208 connection.setDoOutput( false ); 209 } 210 211 // reads the content type of the connected net resource 212 try { 213 contentType = connection.getHeaderField( "Content-Type" ); 214 } catch (Exception e) { 215 e.printStackTrace(); 216 } 217 218 // get result of the request 219 try { 220 is = connection.getInputStream(); 221 } catch ( Exception e ) { 222 e.printStackTrace (); 223 throw new IOException( "could not provide data: " + e ); 224 } 225 226 return is; 227 } 228 229 /** 230 * performs the request and returns the result as a byte array. 231 * 232 * @param expectedDataSize size a the data in bytes expected to be returned 233 * from the net resource. this value will be replaced if the resource 234 * is able to return the available data size. 235 * @return a byte array containing the content of the net resource 236 * 237 * @throws IOException 238 */ 239 public byte[] getDataAsByteArr( int expectedDataSize ) throws IOException { 240 241 InputStream is = getInputStream(); 242 243 if ( expectedDataSize <= 0 ) { 244 expectedDataSize = 10000; 245 } 246 247 ByteArrayOutputStream bos = new ByteArrayOutputStream( expectedDataSize ); 248 249 int v = 0; 250 251 // write result to a ByteArrayOutputStream 252 while ( ( v = is.read() ) > -1 ) { 253 bos.write( v ); 254 } 255 256 bos.flush(); 257 bos.close(); 258 259 is.close(); 260 261 // return result as byte array 262 return bos.toByteArray(); 263 } 264 265 /** 266 * Returns the original form of a <tt>URL</tt> as as <tt>String</tt>. 267 * Handles local filenames correctly, C:/foo is formatted as 268 * file:///C:/foo (and not as file:/C:/foo as returned by the toString () 269 * method of the <tt<URL</tt> object. 270 * <p> 271 * @param url <tt>URL</tt> to be converted 272 * @return <tt>String</tt> representation of the given <tt>URL</tt> 273 */ 274 public static synchronized String url2String( URL url ) { 275 String port = ""; 276 277 if ( url.getPort() != -1 ) { 278 port = ":" + url.getPort(); 279 } 280 281 String s = url.getProtocol() + "://" + url.getHost() + port + url.getPath(); 282 283 return s; 284 } 285 286 /** 287 * @param url 288 * @return true if a connection to the submitted <tt>URL</tt> can be opend 289 */ 290 public static synchronized boolean existsURL(URL url) { 291 try { 292 URLConnection con = url.openConnection(); 293 con.connect(); 294 con.getContentType(); 295 } catch (Exception e) { 296 return false; 297 } 298 return true; 299 } 300 301 302 }