1 22 package org.jboss.test.util.web; 23 24 import java.net.URL ; 25 import java.net.HttpURLConnection ; 26 import java.io.IOException ; 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 45 public class HttpUtils 46 { 47 private static Logger log = Logger.getLogger(HttpUtils.class); 48 private static String baseURL = "http://jduke:theduke@localhost:" + Integer.getInteger("web.port", 8080) + "/"; 49 private static String 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 getBaseURL() 60 { 61 return baseURL; 62 } 63 public static String getBaseURL(String username, String password) 64 { 65 String url = "http://"+username+":"+password+"@localhost:" 66 + Integer.getInteger("web.port", 8080) + "/"; 67 return url; 68 } 69 public static String getBaseURLNoAuth() 70 { 71 return baseURLNoAuth; 72 } 73 74 80 public static HttpMethodBase accessURL(URL url) throws Exception 81 { 82 return accessURL(url, "JBossTest Servlets", HttpURLConnection.HTTP_OK); 83 } 84 92 public static HttpMethodBase accessURL(URL url, String realm, 93 int expectedHttpCode) 94 throws Exception 95 { 96 return accessURL(url, realm, expectedHttpCode, null); 97 } 98 public static HttpMethodBase accessURL(URL url, String realm, 99 int expectedHttpCode, int type) 100 throws Exception 101 { 102 return accessURL(url, realm, expectedHttpCode, null, type); 103 } 104 public static HttpMethodBase accessURL(URL url, String realm, 105 int expectedHttpCode, Header[] hdrs) 106 throws Exception 107 { 108 return accessURL(url, realm, expectedHttpCode, hdrs, GET); 109 } 110 public static HttpMethodBase accessURL(URL url, String realm, 111 int expectedHttpCode, Header[] hdrs, int type) 112 throws Exception 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 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 response = request.getStatusText(); 131 log.debug("responseCode="+responseCode+", response="+response); 132 String content = request.getResponseBodyAsString(); 133 log.debug(content); 134 if( responseCode != expectedHttpCode ) 136 { 137 throw new IOException ("Expected reply code:"+expectedHttpCode 138 +", actual="+responseCode); 139 } 140 } 141 catch(IOException e) 142 { 143 throw e; 144 } 145 return request; 146 } 147 148 public static HttpMethodBase createMethod(URL 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 |