Changeset 661
- Timestamp:
- 05/21/07 13:56:13 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
org.jalcedo.client/trunk/org.jalcedo.client.rest/src/org/jalcedo/client/rest/connect/HttpClientConnection.java
r656 r661 14 14 import java.io.OutputStream; 15 15 import java.util.ArrayList; 16 import java.util.HashMap; 16 17 import java.util.Iterator; 17 18 import java.util.List; 18 19 import java.util.Map; 19 20 21 import org.apache.commons.httpclient.Header; 20 22 import org.apache.commons.httpclient.HttpClient; 21 23 import org.apache.commons.httpclient.HttpException; 22 24 import org.apache.commons.httpclient.HttpMethod; 25 import org.apache.commons.httpclient.HttpStatus; 23 26 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 24 27 import org.apache.commons.httpclient.NameValuePair; 28 import org.apache.commons.httpclient.URI; 29 import org.apache.commons.httpclient.URIException; 25 30 import org.apache.commons.httpclient.methods.DeleteMethod; 26 31 import org.apache.commons.httpclient.methods.EntityEnclosingMethod; … … 42 47 43 48 protected final Log logger = LogFactory.getLog(this.getClass()); 49 50 private boolean followRedirects = true; 51 52 /** 53 * 54 * @return 55 */ 56 public synchronized boolean isFollowRedirects() { 57 return followRedirects; 58 } 59 60 /** 61 * 62 * @param followRedirects 63 */ 64 public synchronized void setFollowRedirects(boolean followRedirects) { 65 this.followRedirects = followRedirects; 66 } 44 67 45 68 /* … … 70 93 try { 71 94 GetMethod method = new GetMethod(url); 95 if (this.isFollowRedirects()) { 96 method.setFollowRedirects(true); 97 } 72 98 return doMethod(queryParams, method); 73 99 } catch (IOException e) { … … 90 116 this.execute(method); 91 117 this.checkHttpResponse(method); 118 if (this.isFollowRedirects() && this.needRedirect(method)) { 119 URI redirectUri = this.extractRedirectURI(method); 120 return this.get(redirectUri.toString(), new HashMap<String, String>()); 121 } 92 122 return method.getResponseBodyAsString(); 123 } 124 125 /** 126 * 127 * @param method 128 * @return 129 */ 130 private URI extractRedirectURI(HttpMethod method) { 131 URI redirectUri = null; 132 Header header = method.getRequestHeader("location"); 133 try { 134 redirectUri = new URI(header.getValue(), true); 135 if (!redirectUri.isRelativeURI()) { 136 redirectUri = new URI(method.getURI(), redirectUri); 137 } 138 } catch (URIException e) { 139 // TODO Auto-generated catch block 140 throw new RuntimeException(e); 141 } catch (NullPointerException e) { 142 // TODO Auto-generated catch block 143 throw new RuntimeException(e); 144 } 145 return redirectUri; 146 } 147 148 /** 149 * this http method needs redirect. 150 * @param method 151 * @return 152 */ 153 private boolean needRedirect(HttpMethod method) { 154 switch (method.getStatusCode()) { 155 case HttpStatus.SC_MOVED_PERMANENTLY: 156 case HttpStatus.SC_MOVED_TEMPORARILY: 157 case HttpStatus.SC_SEE_OTHER: 158 case HttpStatus.SC_TEMPORARY_REDIRECT: 159 return true; 160 default: 161 return false; 162 } 93 163 } 94 164
