1 /** 2 * Copyright 2014 Internet2 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 /* 17 * $Header: /home/hagleyj/i2mi/grouper-misc/grouperClient/src/ext/edu/internet2/middleware/grouperClientExt/org/apache/commons/httpclient/util/HttpURLConnection.java,v 1.1 2008-11-30 10:57:27 mchyzer Exp $ 18 * $Revision: 1.1 $ 19 * $Date: 2008-11-30 10:57:27 $ 20 * 21 * ==================================================================== 22 * 23 * Licensed to the Apache Software Foundation (ASF) under one or more 24 * contributor license agreements. See the NOTICE file distributed with 25 * this work for additional information regarding copyright ownership. 26 * The ASF licenses this file to You under the Apache License, Version 2.0 27 * (the "License"); you may not use this file except in compliance with 28 * the License. You may obtain a copy of the License at 29 * 30 * http://www.apache.org/licenses/LICENSE-2.0 31 * 32 * Unless required by applicable law or agreed to in writing, software 33 * distributed under the License is distributed on an "AS IS" BASIS, 34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 * See the License for the specific language governing permissions and 36 * limitations under the License. 37 * ==================================================================== 38 * 39 * This software consists of voluntary contributions made by many 40 * individuals on behalf of the Apache Software Foundation. For more 41 * information on the Apache Software Foundation, please see 42 * <http://www.apache.org/>. 43 * 44 */ 45 46 package edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.util; 47 48 import java.io.IOException; 49 import java.io.InputStream; 50 import java.io.OutputStream; 51 import java.net.ProtocolException; 52 import java.net.URL; 53 import java.security.Permission; 54 55 import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.Header; 56 import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.HttpMethod; 57 import edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log; 58 import edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.LogFactory; 59 60 /** 61 * Provides a <code>HttpURLConnection</code> wrapper around HttpClient's 62 * <code>HttpMethod</code>. This allows existing code to easily switch to 63 * HttpClieht without breaking existing interfaces using the JDK 64 * <code>HttpURLConnection</code>. 65 * 66 * Note 1: The current implementations wraps only a connected 67 * <code>HttpMethod</code>, ie a method that has alreayd been used to connect 68 * to an HTTP server. 69 * 70 * Note 2: It is a best try effort as different version of the JDK have 71 * different behaviours for <code>HttpURLConnection</code> (And I'm not even 72 * including the numerous <code>HttpURLConnection</code> bugs!). 73 * 74 * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 75 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 76 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 77 * 78 * @since 2.0 79 * 80 * @version $Id: HttpURLConnection.java,v 1.1 2008-11-30 10:57:27 mchyzer Exp $ 81 */ 82 public class HttpURLConnection extends java.net.HttpURLConnection { 83 84 // -------------------------------------------------------- Class Variables 85 86 /** Log object for this class. */ 87 private static final Log LOG = LogFactory.getLog(HttpURLConnection.class); 88 89 90 // ----------------------------------------------------- Instance Variables 91 92 /** 93 * The <code>HttpMethod</code> object that was used to connect to the 94 * HTTP server. It contains all the returned data. 95 */ 96 private HttpMethod method; 97 98 /** 99 * The URL to which we are connected 100 */ 101 private URL url; 102 103 104 105 // ----------------------------------------------------------- Constructors 106 107 /** 108 * Creates an <code>HttpURLConnection</code> from a <code>HttpMethod</code>. 109 * 110 * @param method the theMethod that was used to connect to the HTTP 111 * server and which contains the returned data. 112 * @param url the URL to which we are connected (includes query string) 113 */ 114 public HttpURLConnection(HttpMethod method, URL url) { 115 super(url); 116 this.method = method; 117 this.url = url; 118 } 119 120 /** 121 * Create an instance. 122 * @param url The URL. 123 * @see java.net.HttpURLConnection#HttpURLConnection(URL) 124 */ 125 protected HttpURLConnection(URL url) { 126 super(url); 127 throw new RuntimeException("An HTTP URL connection can only be " 128 + "constructed from a HttpMethod class"); 129 } 130 131 132 // --------------------------------------------------------- Public Methods 133 134 /** 135 * Gets an input stream for the HttpMethod response body. 136 * @throws IOException If an IO problem occurs. 137 * @return The input stream. 138 * @see java.net.HttpURLConnection#getInputStream() 139 * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.HttpMethod#getResponseBodyAsStream() 140 */ 141 public InputStream getInputStream() throws IOException { 142 LOG.trace("enter HttpURLConnection.getInputStream()"); 143 return this.method.getResponseBodyAsStream(); 144 } 145 146 /** 147 * Not yet implemented. 148 * Return the error stream. 149 * @see java.net.HttpURLConnection#getErrorStream() 150 */ 151 public InputStream getErrorStream() { 152 LOG.trace("enter HttpURLConnection.getErrorStream()"); 153 throw new RuntimeException("Not implemented yet"); 154 } 155 156 /** 157 * Not yet implemented. 158 * @see java.net.HttpURLConnection#disconnect() 159 */ 160 public void disconnect() { 161 LOG.trace("enter HttpURLConnection.disconnect()"); 162 throw new RuntimeException("Not implemented yet"); 163 } 164 165 /** 166 * Not available: the data must have already been retrieved. 167 * @throws IOException If an IO problem occurs. 168 * @see java.net.HttpURLConnection#connect() 169 */ 170 public void connect() throws IOException { 171 LOG.trace("enter HttpURLConnection.connect()"); 172 throw new RuntimeException("This class can only be used with already" 173 + "retrieved data"); 174 } 175 176 /** 177 * Not yet implemented. 178 * @return true if we are using a proxy. 179 * @see java.net.HttpURLConnection#usingProxy() 180 */ 181 public boolean usingProxy() { 182 LOG.trace("enter HttpURLConnection.usingProxy()"); 183 throw new RuntimeException("Not implemented yet"); 184 } 185 186 /** 187 * Return the request method. 188 * @return The request method. 189 * @see java.net.HttpURLConnection#getRequestMethod() 190 * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.HttpMethod#getName() 191 */ 192 public String getRequestMethod() { 193 LOG.trace("enter HttpURLConnection.getRequestMethod()"); 194 return this.method.getName(); 195 } 196 197 /** 198 * Return the response code. 199 * @return The response code. 200 * @throws IOException If an IO problem occurs. 201 * @see java.net.HttpURLConnection#getResponseCode() 202 * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.HttpMethod#getStatusCode() 203 */ 204 public int getResponseCode() throws IOException { 205 LOG.trace("enter HttpURLConnection.getResponseCode()"); 206 return this.method.getStatusCode(); 207 } 208 209 /** 210 * Return the response message 211 * @return The response message 212 * @throws IOException If an IO problem occurs. 213 * @see java.net.HttpURLConnection#getResponseMessage() 214 * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.HttpMethod#getStatusText() 215 */ 216 public String getResponseMessage() throws IOException { 217 LOG.trace("enter HttpURLConnection.getResponseMessage()"); 218 return this.method.getStatusText(); 219 } 220 221 /** 222 * Return the header field 223 * @param name the name of the header 224 * @return the header field. 225 * @see java.net.HttpURLConnection#getHeaderField(String) 226 * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.HttpMethod#getResponseHeaders() 227 */ 228 public String getHeaderField(String name) { 229 LOG.trace("enter HttpURLConnection.getHeaderField(String)"); 230 // Note: Return the last matching header in the Header[] array, as in 231 // the JDK implementation. 232 Header[] headers = this.method.getResponseHeaders(); 233 for (int i = headers.length - 1; i >= 0; i--) { 234 if (headers[i].getName().equalsIgnoreCase(name)) { 235 return headers[i].getValue(); 236 } 237 } 238 239 return null; 240 } 241 242 /** 243 * Return the header field key 244 * @param keyPosition The key position 245 * @return The header field key. 246 * @see java.net.HttpURLConnection#getHeaderFieldKey(int) 247 * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.HttpMethod#getResponseHeaders() 248 */ 249 public String getHeaderFieldKey(int keyPosition) { 250 LOG.trace("enter HttpURLConnection.getHeaderFieldKey(int)"); 251 252 // Note: HttpClient does not consider the returned Status Line as 253 // a response header. However, getHeaderFieldKey(0) is supposed to 254 // return null. Hence the special case below ... 255 256 if (keyPosition == 0) { 257 return null; 258 } 259 260 // Note: HttpClient does not currently keep headers in the same order 261 // that they are read from the HTTP server. 262 263 Header[] headers = this.method.getResponseHeaders(); 264 if (keyPosition < 0 || keyPosition > headers.length) { 265 return null; 266 } 267 268 return headers[keyPosition - 1].getName(); 269 } 270 271 /** 272 * Return the header field at the specified position 273 * @param position The position 274 * @return The header field. 275 * @see java.net.HttpURLConnection#getHeaderField(int) 276 * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.HttpMethod#getResponseHeaders() 277 */ 278 public String getHeaderField(int position) { 279 LOG.trace("enter HttpURLConnection.getHeaderField(int)"); 280 281 // Note: HttpClient does not consider the returned Status Line as 282 // a response header. However, getHeaderField(0) is supposed to 283 // return the status line. Hence the special case below ... 284 285 if (position == 0) { 286 return this.method.getStatusLine().toString(); 287 } 288 289 // Note: HttpClient does not currently keep headers in the same order 290 // that they are read from the HTTP server. 291 292 Header[] headers = this.method.getResponseHeaders(); 293 if (position < 0 || position > headers.length) { 294 return null; 295 } 296 297 return headers[position - 1].getValue(); 298 } 299 300 /** 301 * Return the URL 302 * @return The URL. 303 * @see java.net.HttpURLConnection#getURL() 304 */ 305 public URL getURL() { 306 LOG.trace("enter HttpURLConnection.getURL()"); 307 return this.url; 308 } 309 310 // Note: We don't implement the following methods so that they default to 311 // the JDK implementation. They will all call 312 // <code>getHeaderField(String)</code> which we have overridden. 313 314 // java.net.HttpURLConnection#getHeaderFieldDate(String, long) 315 // java.net.HttpURLConnection#getContentLength() 316 // java.net.HttpURLConnection#getContentType() 317 // java.net.HttpURLConnection#getContentEncoding() 318 // java.net.HttpURLConnection#getDate() 319 // java.net.HttpURLConnection#getHeaderFieldInt(String, int) 320 // java.net.HttpURLConnection#getExpiration() 321 // java.net.HttpURLConnection#getLastModified() 322 323 /** 324 * Not available: the data must have already been retrieved. 325 */ 326 public void setInstanceFollowRedirects(boolean isFollowingRedirects) { 327 LOG.trace("enter HttpURLConnection.setInstanceFollowRedirects(boolean)"); 328 throw new RuntimeException("This class can only be used with already" 329 + "retrieved data"); 330 } 331 332 /** 333 * Not yet implemented. 334 */ 335 public boolean getInstanceFollowRedirects() { 336 LOG.trace("enter HttpURLConnection.getInstanceFollowRedirects()"); 337 throw new RuntimeException("Not implemented yet"); 338 } 339 340 /** 341 * Not available: the data must have already been retrieved. 342 * @see java.net.HttpURLConnection#setRequestMethod(String) 343 */ 344 public void setRequestMethod(String method) throws ProtocolException { 345 LOG.trace("enter HttpURLConnection.setRequestMethod(String)"); 346 throw new RuntimeException("This class can only be used with already" 347 + "retrieved data"); 348 } 349 350 /** 351 * Not yet implemented. 352 * @see java.net.HttpURLConnection#getPermission() 353 */ 354 public Permission getPermission() throws IOException { 355 LOG.trace("enter HttpURLConnection.getPermission()"); 356 throw new RuntimeException("Not implemented yet"); 357 } 358 359 /** 360 * Not yet implemented. 361 * @see java.net.HttpURLConnection#getContent() 362 */ 363 public Object getContent() throws IOException { 364 LOG.trace("enter HttpURLConnection.getContent()"); 365 throw new RuntimeException("Not implemented yet"); 366 } 367 368 /** 369 * Not yet implemented. 370 */ 371 public Object getContent(Class[] classes) throws IOException { 372 LOG.trace("enter HttpURLConnection.getContent(Class[])"); 373 throw new RuntimeException("Not implemented yet"); 374 } 375 376 /** 377 * @see java.net.HttpURLConnection#getOutputStream() 378 */ 379 public OutputStream getOutputStream() throws IOException { 380 LOG.trace("enter HttpURLConnection.getOutputStream()"); 381 throw new RuntimeException("This class can only be used with already" 382 + "retrieved data"); 383 } 384 385 /** 386 * Not available: the data must have already been retrieved. 387 * @see java.net.HttpURLConnection#setDoInput(boolean) 388 */ 389 public void setDoInput(boolean isInput) { 390 LOG.trace("enter HttpURLConnection.setDoInput()"); 391 throw new RuntimeException("This class can only be used with already" 392 + "retrieved data"); 393 } 394 395 /** 396 * Not yet implemented. 397 * @see java.net.HttpURLConnection#getDoInput() 398 */ 399 public boolean getDoInput() { 400 LOG.trace("enter HttpURLConnection.getDoInput()"); 401 throw new RuntimeException("Not implemented yet"); 402 } 403 404 /** 405 * Not available: the data must have already been retrieved. 406 * @see java.net.HttpURLConnection#setDoOutput(boolean) 407 */ 408 public void setDoOutput(boolean isOutput) { 409 LOG.trace("enter HttpURLConnection.setDoOutput()"); 410 throw new RuntimeException("This class can only be used with already" 411 + "retrieved data"); 412 } 413 414 /** 415 * Not yet implemented. 416 * @see java.net.HttpURLConnection#getDoOutput() 417 */ 418 public boolean getDoOutput() { 419 LOG.trace("enter HttpURLConnection.getDoOutput()"); 420 throw new RuntimeException("Not implemented yet"); 421 } 422 423 /** 424 * Not available: the data must have already been retrieved. 425 * @see java.net.HttpURLConnection#setAllowUserInteraction(boolean) 426 */ 427 public void setAllowUserInteraction(boolean isAllowInteraction) { 428 LOG.trace("enter HttpURLConnection.setAllowUserInteraction(boolean)"); 429 throw new RuntimeException("This class can only be used with already" 430 + "retrieved data"); 431 } 432 433 /** 434 * Not yet implemented. 435 * @see java.net.HttpURLConnection#getAllowUserInteraction() 436 */ 437 public boolean getAllowUserInteraction() { 438 LOG.trace("enter HttpURLConnection.getAllowUserInteraction()"); 439 throw new RuntimeException("Not implemented yet"); 440 } 441 442 /** 443 * Not available: the data must have already been retrieved. 444 * @see java.net.HttpURLConnection#setUseCaches(boolean) 445 */ 446 public void setUseCaches(boolean isUsingCaches) { 447 LOG.trace("enter HttpURLConnection.setUseCaches(boolean)"); 448 throw new RuntimeException("This class can only be used with already" 449 + "retrieved data"); 450 } 451 452 /** 453 * Not yet implemented. 454 * @see java.net.HttpURLConnection#getUseCaches() 455 */ 456 public boolean getUseCaches() { 457 LOG.trace("enter HttpURLConnection.getUseCaches()"); 458 throw new RuntimeException("Not implemented yet"); 459 } 460 461 /** 462 * Not available: the data must have already been retrieved. 463 * @see java.net.HttpURLConnection#setIfModifiedSince(long) 464 */ 465 public void setIfModifiedSince(long modificationDate) { 466 LOG.trace("enter HttpURLConnection.setIfModifiedSince(long)"); 467 throw new RuntimeException("This class can only be used with already" 468 + "retrieved data"); 469 } 470 471 /** 472 * Not yet implemented. 473 * @see java.net.HttpURLConnection#getIfModifiedSince() 474 */ 475 public long getIfModifiedSince() { 476 LOG.trace("enter HttpURLConnection.getIfmodifiedSince()"); 477 throw new RuntimeException("Not implemented yet"); 478 } 479 480 /** 481 * Not available: the data must have already been retrieved. 482 * @see java.net.HttpURLConnection#getDefaultUseCaches() 483 */ 484 public boolean getDefaultUseCaches() { 485 LOG.trace("enter HttpURLConnection.getDefaultUseCaches()"); 486 throw new RuntimeException("Not implemented yet"); 487 } 488 489 /** 490 * Not available: the data must have already been retrieved. 491 * @see java.net.HttpURLConnection#setDefaultUseCaches(boolean) 492 */ 493 public void setDefaultUseCaches(boolean isUsingCaches) { 494 LOG.trace("enter HttpURLConnection.setDefaultUseCaches(boolean)"); 495 throw new RuntimeException("This class can only be used with already" 496 + "retrieved data"); 497 } 498 499 /** 500 * Not available: the data must have already been retrieved. 501 * @see java.net.HttpURLConnection#setRequestProperty(String,String) 502 */ 503 public void setRequestProperty(String key, String value) { 504 LOG.trace("enter HttpURLConnection.setRequestProperty()"); 505 throw new RuntimeException("This class can only be used with already" 506 + "retrieved data"); 507 } 508 509 /** 510 * Not yet implemented. 511 * @see java.net.HttpURLConnection#getRequestProperty(String) 512 */ 513 public String getRequestProperty(String key) { 514 LOG.trace("enter HttpURLConnection.getRequestProperty()"); 515 throw new RuntimeException("Not implemented yet"); 516 } 517 518 } 519