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