KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > dd > web > util > 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.ejb3.test.dd.web.util;
23
24 import java.net.URL JavaDoc;
25 import java.net.HttpURLConnection JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.lang.System JavaDoc;
28 import org.apache.commons.httpclient.HttpClient;
29 import org.apache.commons.httpclient.UsernamePasswordCredentials;
30 import org.apache.commons.httpclient.HttpMethodBase;
31 import org.apache.commons.httpclient.Header;
32 import org.apache.commons.httpclient.methods.GetMethod;
33 import org.apache.commons.httpclient.methods.PostMethod;
34 import org.apache.commons.httpclient.methods.HeadMethod;
35 import org.apache.commons.httpclient.methods.OptionsMethod;
36 import org.apache.commons.httpclient.methods.PutMethod;
37 import org.apache.commons.httpclient.methods.DeleteMethod;
38 import org.apache.commons.httpclient.methods.TraceMethod;
39 import org.jboss.logging.Logger;
40
41 /** Utilities for client http requests
42  *
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 37459 $
45  */

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

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

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