|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| HttpClientConnectionHelper.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ====================================================================
|
|
| 3 |
*
|
|
| 4 |
* The Apache Software License, Version 1.1
|
|
| 5 |
*
|
|
| 6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
| 7 |
* reserved.
|
|
| 8 |
*
|
|
| 9 |
* Redistribution and use in source and binary forms, with or without
|
|
| 10 |
* modification, are permitted provided that the following conditions
|
|
| 11 |
* are met:
|
|
| 12 |
*
|
|
| 13 |
* 1. Redistributions of source code must retain the above copyright
|
|
| 14 |
* notice, this list of conditions and the following disclaimer.
|
|
| 15 |
*
|
|
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
| 17 |
* notice, this list of conditions and the following disclaimer in
|
|
| 18 |
* the documentation and/or other materials provided with the
|
|
| 19 |
* distribution.
|
|
| 20 |
*
|
|
| 21 |
* 3. The end-user documentation included with the redistribution, if
|
|
| 22 |
* any, must include the following acknowlegement:
|
|
| 23 |
* "This product includes software developed by the
|
|
| 24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
| 25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
| 26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
| 27 |
*
|
|
| 28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
| 29 |
* Foundation" must not be used to endorse or promote products
|
|
| 30 |
* derived from this software without prior written permission. For
|
|
| 31 |
* written permission, please contact apache@apache.org.
|
|
| 32 |
*
|
|
| 33 |
* 5. Products derived from this software may not be called "Apache"
|
|
| 34 |
* nor may "Apache" appear in their names without prior written
|
|
| 35 |
* permission of the Apache Group.
|
|
| 36 |
*
|
|
| 37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
| 38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
| 39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
| 40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
| 41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
| 42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
| 43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
| 44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
| 45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
| 46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
| 47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
| 48 |
* SUCH DAMAGE.
|
|
| 49 |
* ====================================================================
|
|
| 50 |
*
|
|
| 51 |
* This software consists of voluntary contributions made by many
|
|
| 52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
| 53 |
* information on the Apache Software Foundation, please see
|
|
| 54 |
* <http://www.apache.org/>.
|
|
| 55 |
*
|
|
| 56 |
*/
|
|
| 57 |
package org.apache.cactus.client.connector.http;
|
|
| 58 |
|
|
| 59 |
import java.io.IOException;
|
|
| 60 |
|
|
| 61 |
import java.net.HttpURLConnection;
|
|
| 62 |
import java.net.URL;
|
|
| 63 |
|
|
| 64 |
import java.util.ArrayList;
|
|
| 65 |
import java.util.Enumeration;
|
|
| 66 |
import java.util.List;
|
|
| 67 |
|
|
| 68 |
import org.apache.cactus.WebRequest;
|
|
| 69 |
import org.apache.cactus.client.authentication.Authentication;
|
|
| 70 |
import org.apache.cactus.configuration.Configuration;
|
|
| 71 |
import org.apache.cactus.util.CookieUtil;
|
|
| 72 |
import org.apache.cactus.util.UrlUtil;
|
|
| 73 |
import org.apache.commons.httpclient.HostConfiguration;
|
|
| 74 |
import org.apache.commons.httpclient.HttpClient;
|
|
| 75 |
import org.apache.commons.httpclient.HttpState;
|
|
| 76 |
import org.apache.commons.httpclient.HttpMethod;
|
|
| 77 |
import org.apache.commons.httpclient.NameValuePair;
|
|
| 78 |
import org.apache.commons.httpclient.methods.GetMethod;
|
|
| 79 |
import org.apache.commons.httpclient.methods.PostMethod;
|
|
| 80 |
import org.apache.commons.httpclient.protocol.Protocol;
|
|
| 81 |
|
|
| 82 |
/**
|
|
| 83 |
* Implementation of <code>ConnectionHelper</code> using Jakarta Commons
|
|
| 84 |
* HttpClient.
|
|
| 85 |
*
|
|
| 86 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
| 87 |
*
|
|
| 88 |
* @version $Id: HttpClientConnectionHelper.java,v 1.9 2003/06/27 09:14:43 cmlenz Exp $
|
|
| 89 |
*/
|
|
| 90 |
public class HttpClientConnectionHelper implements ConnectionHelper |
|
| 91 |
{
|
|
| 92 |
/**
|
|
| 93 |
* The <code>HttpMethod</code> used to connect to the HTTP server. It is
|
|
| 94 |
* either a <code>GetMethod</code> or a <code>PostMethod</code>.
|
|
| 95 |
*/
|
|
| 96 |
private HttpMethod method;
|
|
| 97 |
|
|
| 98 |
/**
|
|
| 99 |
* The URL that will be used for the HTTP connection.
|
|
| 100 |
*/
|
|
| 101 |
private String url;
|
|
| 102 |
|
|
| 103 |
/**
|
|
| 104 |
* @param theURL the URL that will be used for the HTTP connection.
|
|
| 105 |
*/
|
|
| 106 | 0 |
public HttpClientConnectionHelper(String theURL)
|
| 107 |
{
|
|
| 108 | 0 |
this.url = theURL;
|
| 109 |
} |
|
| 110 |
|
|
| 111 |
/**
|
|
| 112 |
* @see ConnectionHelper#connect(WebRequest, Configuration)
|
|
| 113 |
*/
|
|
| 114 | 0 |
public HttpURLConnection connect(WebRequest theRequest,
|
| 115 |
Configuration theConfiguration) throws Throwable
|
|
| 116 |
{
|
|
| 117 | 0 |
URL url = new URL(this.url); |
| 118 |
|
|
| 119 |
// Add Authentication headers, if necessary. This is the first
|
|
| 120 |
// step to allow authentication to add extra headers, HTTP parameters,
|
|
| 121 |
// etc.
|
|
| 122 | 0 |
Authentication authentication = theRequest.getAuthentication(); |
| 123 |
|
|
| 124 | 0 |
if (authentication != null) |
| 125 |
{
|
|
| 126 | 0 |
authentication.configure(theRequest, theConfiguration); |
| 127 |
} |
|
| 128 |
|
|
| 129 |
// Add the parameters that need to be passed as part of the URL
|
|
| 130 | 0 |
url = HttpUtil.addHttpGetParameters(theRequest, url); |
| 131 |
|
|
| 132 |
// Choose the method that we will use to post data :
|
|
| 133 |
// - If at least one parameter is to be sent in the request body, then
|
|
| 134 |
// we are doing a POST.
|
|
| 135 |
// - If user data has been specified, then we are doing a POST
|
|
| 136 | 0 |
if (theRequest.getParameterNamesPost().hasMoreElements()
|
| 137 |
|| (theRequest.getUserData() != null))
|
|
| 138 |
{
|
|
| 139 | 0 |
this.method = new PostMethod(); |
| 140 |
} |
|
| 141 |
else
|
|
| 142 |
{
|
|
| 143 | 0 |
this.method = new GetMethod(); |
| 144 |
} |
|
| 145 |
|
|
| 146 | 0 |
this.method.setFollowRedirects(false); |
| 147 | 0 |
this.method.setPath(UrlUtil.getPath(url));
|
| 148 | 0 |
this.method.setQueryString(UrlUtil.getQuery(url));
|
| 149 |
|
|
| 150 |
// Sets the content type
|
|
| 151 | 0 |
this.method.setRequestHeader("Content-type", |
| 152 |
theRequest.getContentType()); |
|
| 153 |
|
|
| 154 |
// Add the other header fields
|
|
| 155 | 0 |
addHeaders(theRequest); |
| 156 |
|
|
| 157 |
// Add the POST parameters if no user data has been specified (user data
|
|
| 158 |
// overried post parameters)
|
|
| 159 | 0 |
if (theRequest.getUserData() != null) |
| 160 |
{
|
|
| 161 | 0 |
addUserData(theRequest); |
| 162 |
} |
|
| 163 |
else
|
|
| 164 |
{
|
|
| 165 | 0 |
addHttpPostParameters(theRequest); |
| 166 |
} |
|
| 167 |
|
|
| 168 |
// Add the cookies
|
|
| 169 | 0 |
HttpState state = CookieUtil.createHttpState(theRequest, url); |
| 170 |
|
|
| 171 |
// Open the connection and get the result
|
|
| 172 | 0 |
HttpClient client = new HttpClient();
|
| 173 | 0 |
HostConfiguration hostConfiguration = new HostConfiguration();
|
| 174 | 0 |
hostConfiguration.setHost(url.getHost(), url.getPort(), |
| 175 |
Protocol.getProtocol(url.getProtocol())); |
|
| 176 | 0 |
client.setState(state); |
| 177 | 0 |
client.executeMethod(hostConfiguration, this.method);
|
| 178 |
|
|
| 179 |
// Wrap the HttpClient method in a java.net.HttpURLConnection object
|
|
| 180 | 0 |
return new org.apache.commons.httpclient.util.HttpURLConnection( |
| 181 |
this.method, url);
|
|
| 182 |
} |
|
| 183 |
|
|
| 184 |
/**
|
|
| 185 |
* Add the HTTP parameters that need to be passed in the request body.
|
|
| 186 |
*
|
|
| 187 |
* @param theRequest the request containing all data to pass to the server
|
|
| 188 |
* redirector.
|
|
| 189 |
*/
|
|
| 190 | 0 |
private void addHttpPostParameters(WebRequest theRequest) |
| 191 |
{
|
|
| 192 |
// If no parameters, then exit
|
|
| 193 | 0 |
if (!theRequest.getParameterNamesPost().hasMoreElements())
|
| 194 |
{
|
|
| 195 | 0 |
return;
|
| 196 |
} |
|
| 197 |
|
|
| 198 | 0 |
Enumeration keys = theRequest.getParameterNamesPost(); |
| 199 | 0 |
List parameters = new ArrayList();
|
| 200 | 0 |
while (keys.hasMoreElements())
|
| 201 |
{
|
|
| 202 | 0 |
String key = (String) keys.nextElement(); |
| 203 | 0 |
String[] values = theRequest.getParameterValuesPost(key); |
| 204 | 0 |
for (int i = 0; i < values.length; i++) |
| 205 |
{
|
|
| 206 | 0 |
parameters.add(new NameValuePair(key, values[i]));
|
| 207 |
} |
|
| 208 |
} |
|
| 209 | 0 |
((PostMethod) this.method).setRequestBody(
|
| 210 |
(NameValuePair[]) parameters.toArray( |
|
| 211 |
new NameValuePair[parameters.size()]));
|
|
| 212 |
} |
|
| 213 |
|
|
| 214 |
/**
|
|
| 215 |
* Add the Headers to the request.
|
|
| 216 |
*
|
|
| 217 |
* @param theRequest the request containing all data to pass to the server
|
|
| 218 |
* redirector.
|
|
| 219 |
*/
|
|
| 220 | 0 |
private void addHeaders(WebRequest theRequest) |
| 221 |
{
|
|
| 222 | 0 |
Enumeration keys = theRequest.getHeaderNames(); |
| 223 |
|
|
| 224 | 0 |
while (keys.hasMoreElements())
|
| 225 |
{
|
|
| 226 | 0 |
String key = (String) keys.nextElement(); |
| 227 | 0 |
String[] values = theRequest.getHeaderValues(key); |
| 228 |
|
|
| 229 | 0 |
StringBuffer fullHeaderValue = new StringBuffer(values[0]);
|
| 230 |
|
|
| 231 | 0 |
for (int i = 1; i < values.length; i++) |
| 232 |
{
|
|
| 233 | 0 |
fullHeaderValue.append("," + values[i]);
|
| 234 |
} |
|
| 235 |
|
|
| 236 | 0 |
this.method.addRequestHeader(key, fullHeaderValue.toString());
|
| 237 |
} |
|
| 238 |
} |
|
| 239 |
|
|
| 240 |
/**
|
|
| 241 |
* Add user data in the request body.
|
|
| 242 |
*
|
|
| 243 |
* @param theRequest the request containing all data to pass to the server
|
|
| 244 |
* redirector.
|
|
| 245 |
* @exception IOException if we fail to read the user data
|
|
| 246 |
*/
|
|
| 247 | 0 |
private void addUserData(WebRequest theRequest) throws IOException |
| 248 |
{
|
|
| 249 |
// If no user data, then exit
|
|
| 250 | 0 |
if (theRequest.getUserData() == null) |
| 251 |
{
|
|
| 252 | 0 |
return;
|
| 253 |
} |
|
| 254 |
|
|
| 255 | 0 |
((PostMethod) this.method).setRequestBody(theRequest.getUserData());
|
| 256 |
} |
|
| 257 |
} |
|
| 258 |
|
|
||||||||||