KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > util > web > HttpUtils


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.util.web;
23
24 import java.net.URL JavaDoc;
25 import java.net.HttpURLConnection JavaDoc;
26 import java.io.IOException JavaDoc;
27 import org.apache.commons.httpclient.HttpClient;
28 import org.apache.commons.httpclient.UsernamePasswordCredentials;
29 import org.apache.commons.httpclient.HttpMethodBase;
30 import org.apache.commons.httpclient.Header;
31 import org.apache.commons.httpclient.methods.GetMethod;
32 import org.apache.commons.httpclient.methods.PostMethod;
33 import org.apache.commons.httpclient.methods.HeadMethod;
34 import org.apache.commons.httpclient.methods.OptionsMethod;
35 import org.apache.commons.httpclient.methods.PutMethod;
36 import org.apache.commons.httpclient.methods.DeleteMethod;
37 import org.apache.commons.httpclient.methods.TraceMethod;
38 import org.jboss.logging.Logger;
39
40 /** Utilities for client http requests
41  *
42  * @author Scott.Stark@jboss.org
43  * @version $Revision: 37406 $
44  */

45 public class HttpUtils
46 {
47    private static Logger log = Logger.getLogger(HttpUtils.class);
48    private static String JavaDoc baseURL = "http://jduke:theduke@localhost:" + Integer.getInteger("web.port", 8080) + "/";
49    private static String JavaDoc baseURLNoAuth = "http://localhost:" + Integer.getInteger("web.port", 8080) + "/";
50
51    public static final int GET = 1;
52    public static final int POST = 2;
53    public static final int HEAD = 3;
54    public static final int OPTIONS = 4;
55    public static final int PUT = 5;
56    public static final int DELETE = 6;
57    public static final int TRACE = 7;
58
59    public static String JavaDoc getBaseURL()
60    {
61       return baseURL;
62    }
63    public static String JavaDoc getBaseURL(String JavaDoc username, String JavaDoc password)
64    {
65       String JavaDoc url = "http://"+username+":"+password+"@localhost:"
66          + Integer.getInteger("web.port", 8080) + "/";
67       return url;
68    }
69    public static String JavaDoc getBaseURLNoAuth()
70    {
71       return baseURLNoAuth;
72    }
73
74    /** Perform a get on the indicated URL and assert an HTTP_OK response code
75     *
76     * @param url
77     * @return The commons HttpClient used to perform the get
78     * @throws Exception on any failure
79     */

80    public static HttpMethodBase accessURL(URL JavaDoc url) throws Exception JavaDoc
81    {
82       return accessURL(url, "JBossTest Servlets", HttpURLConnection.HTTP_OK);
83    }
84    /** Perform a get on the indicated URL and assert that the response code
85     * matches the expectedHttpCode argument.
86     *
87     * @param url
88     * @param expectedHttpCode the http response code expected
89     * @return The commons HttpClient used to perform the get
90     * @throws Exception on any failure
91     */

92    public static HttpMethodBase accessURL(URL JavaDoc url, String JavaDoc realm,
93       int expectedHttpCode)
94       throws Exception JavaDoc
95    {
96       return accessURL(url, realm, expectedHttpCode, null);
97    }
98    public static HttpMethodBase accessURL(URL JavaDoc url, String JavaDoc realm,
99       int expectedHttpCode, int type)
100       throws Exception JavaDoc
101    {
102       return accessURL(url, realm, expectedHttpCode, null, type);
103    }
104    public static HttpMethodBase accessURL(URL JavaDoc url, String JavaDoc realm,
105       int expectedHttpCode, Header[] hdrs)
106       throws Exception JavaDoc
107    {
108       return accessURL(url, realm, expectedHttpCode, hdrs, GET);
109    }
110    public static HttpMethodBase accessURL(URL JavaDoc url, String JavaDoc realm,
111       int expectedHttpCode, Header[] hdrs, int type)
112       throws Exception JavaDoc
113    {
114       HttpClient httpConn = new HttpClient();
115       HttpMethodBase request = createMethod(url, type);
116       int hdrCount = hdrs != null ? hdrs.length : 0;
117       for(int n = 0; n < hdrCount; n ++)
118          request.addRequestHeader(hdrs[n]);
119       try
120       {
121          log.debug("Connecting to: "+url);
122          String JavaDoc userInfo = url.getUserInfo();
123          if( userInfo != null )
124          {
125             UsernamePasswordCredentials auth = new UsernamePasswordCredentials(userInfo);
126             httpConn.getState().setCredentials(realm, url.getHost(), auth);
127          }
128          log.debug("RequestURI: "+request.getURI());
129          int responseCode = httpConn.executeMethod(request);
130          String JavaDoc response = request.getStatusText();
131          log.debug("responseCode="+responseCode+", response="+response);
132          String JavaDoc content = request.getResponseBodyAsString();
133          log.debug(content);
134          // Validate that we are seeing the requested response code
135
if( responseCode != expectedHttpCode )
136          {
137             throw new IOException JavaDoc("Expected reply code:"+expectedHttpCode
138                +", actual="+responseCode);
139          }
140       }
141       catch(IOException JavaDoc e)
142       {
143          throw e;
144       }
145       return request;
146    }
147
148    public static HttpMethodBase createMethod(URL JavaDoc url, int type)
149    {
150       HttpMethodBase request = null;
151       switch( type )
152       {
153          case GET:
154             request = new GetMethod(url.toString());
155             break;
156          case POST:
157             request = new PostMethod(url.toString());
158             break;
159          case HEAD:
160             request = new HeadMethod(url.toString());
161             break;
162          case OPTIONS:
163             request = new OptionsMethod(url.toString());
164             break;
165          case PUT:
166             request = new PutMethod(url.toString());
167             break;
168          case DELETE:
169             request = new DeleteMethod(url.toString());
170             break;
171          case TRACE:
172             request = new TraceMethod(url.toString());
173             break;
174       }
175       return request;
176    }
177 }
178
Popular Tags