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