1 20 package org.apache.cactus.internal.client.connector.http; 21 22 import java.io.IOException ; 23 24 import java.net.HttpURLConnection ; 25 import java.net.URL ; 26 27 import java.util.ArrayList ; 28 import java.util.Enumeration ; 29 import java.util.List ; 30 31 import org.apache.cactus.WebRequest; 32 import org.apache.cactus.client.authentication.Authentication; 33 import org.apache.cactus.internal.configuration.Configuration; 34 import org.apache.cactus.internal.util.CookieUtil; 35 import org.apache.cactus.internal.util.UrlUtil; 36 import org.apache.commons.httpclient.HostConfiguration; 37 import org.apache.commons.httpclient.HttpClient; 38 import org.apache.commons.httpclient.HttpState; 39 import org.apache.commons.httpclient.HttpMethod; 40 import org.apache.commons.httpclient.NameValuePair; 41 import org.apache.commons.httpclient.methods.GetMethod; 42 import org.apache.commons.httpclient.methods.PostMethod; 43 import org.apache.commons.httpclient.protocol.Protocol; 44 45 51 public class HttpClientConnectionHelper implements ConnectionHelper 52 { 53 57 private HttpMethod method; 58 59 62 private String url; 63 64 67 public HttpClientConnectionHelper(String theURL) 68 { 69 this.url = theURL; 70 } 71 72 75 public HttpURLConnection connect(WebRequest theRequest, 76 Configuration theConfiguration) throws Throwable 77 { 78 URL url = new URL (this.url); 79 80 HttpState state = new HttpState(); 81 82 if (theRequest.getParameterNamesPost().hasMoreElements() 87 || (theRequest.getUserData() != null)) 88 { 89 this.method = new PostMethod(); 90 } 91 else 92 { 93 this.method = new GetMethod(); 94 } 95 96 Authentication authentication = theRequest.getAuthentication(); 100 101 if (authentication != null) 102 { 103 authentication.configure(state, this.method, theRequest, 104 theConfiguration); 105 } 106 107 url = HttpUtil.addHttpGetParameters(theRequest, url); 109 110 this.method.setFollowRedirects(false); 111 this.method.setPath(UrlUtil.getPath(url)); 112 this.method.setQueryString(UrlUtil.getQuery(url)); 113 114 this.method.setRequestHeader("Content-type", 116 theRequest.getContentType()); 117 118 addHeaders(theRequest); 120 121 if (theRequest.getUserData() != null) 124 { 125 addUserData(theRequest); 126 } 127 else 128 { 129 addHttpPostParameters(theRequest); 130 } 131 132 state.addCookies(CookieUtil.createHttpClientCookies(theRequest, 134 url)); 135 136 HttpClient client = new HttpClient(); 138 HostConfiguration hostConfiguration = new HostConfiguration(); 139 hostConfiguration.setHost(url.getHost(), url.getPort(), 140 Protocol.getProtocol(url.getProtocol())); 141 client.setState(state); 142 client.executeMethod(hostConfiguration, this.method); 143 144 return new org.apache.commons.httpclient.util.HttpURLConnection( 146 this.method, url); 147 } 148 149 155 private void addHttpPostParameters(WebRequest theRequest) 156 { 157 if (!theRequest.getParameterNamesPost().hasMoreElements()) 159 { 160 return; 161 } 162 163 Enumeration keys = theRequest.getParameterNamesPost(); 164 List parameters = new ArrayList (); 165 while (keys.hasMoreElements()) 166 { 167 String key = (String ) keys.nextElement(); 168 String [] values = theRequest.getParameterValuesPost(key); 169 for (int i = 0; i < values.length; i++) 170 { 171 parameters.add(new NameValuePair(key, values[i])); 172 } 173 } 174 ((PostMethod) this.method).setRequestBody( 175 (NameValuePair[]) parameters.toArray( 176 new NameValuePair[parameters.size()])); 177 } 178 179 185 private void addHeaders(WebRequest theRequest) 186 { 187 Enumeration keys = theRequest.getHeaderNames(); 188 189 while (keys.hasMoreElements()) 190 { 191 String key = (String ) keys.nextElement(); 192 String [] values = theRequest.getHeaderValues(key); 193 194 StringBuffer fullHeaderValue = new StringBuffer (values[0]); 195 196 for (int i = 1; i < values.length; i++) 197 { 198 fullHeaderValue.append("," + values[i]); 199 } 200 201 this.method.addRequestHeader(key, fullHeaderValue.toString()); 202 } 203 } 204 205 212 private void addUserData(WebRequest theRequest) throws IOException 213 { 214 if (theRequest.getUserData() == null) 216 { 217 return; 218 } 219 220 ((PostMethod) this.method).setRequestBody(theRequest.getUserData()); 221 } 222 } 223 | Popular Tags |