1 16 package com.google.gwt.http.client; 17 18 import com.google.gwt.core.client.GWT; 19 import com.google.gwt.core.client.JavaScriptObject; 20 import com.google.gwt.user.client.impl.HTTPRequestImpl; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 48 public class RequestBuilder { 49 52 public static final class Method { 53 private final String name; 54 55 private Method(String name) { 56 this.name = name; 57 } 58 59 public String toString() { 60 return name; 61 } 62 } 63 64 67 public static final Method GET = new Method("GET"); 68 69 72 public static final Method POST = new Method("POST"); 73 74 private static final HTTPRequestImpl httpRequest = (HTTPRequestImpl) GWT.create(HTTPRequestImpl.class); 75 76 80 private Map headers; 81 82 85 private String httpMethod; 86 87 90 private String password; 91 92 95 private int timeoutMillis; 96 97 100 private String url; 101 102 105 private String user; 106 107 118 public RequestBuilder(Method httpMethod, String url) { 119 this((httpMethod == null) ? null : httpMethod.toString(), url); 120 } 121 122 139 protected RequestBuilder(String httpMethod, String url) { 140 141 StringValidator.throwIfEmptyOrNull("httpMethod", httpMethod); 142 StringValidator.throwIfEmptyOrNull("url", url); 143 144 this.httpMethod = httpMethod; 145 this.url = url; 146 } 147 148 158 public Request sendRequest(String requestData, RequestCallback callback) 159 throws RequestException { 160 JavaScriptObject xmlHttpRequest = httpRequest.createXmlHTTPRequest(); 161 162 String openError = XMLHTTPRequest.open(xmlHttpRequest, httpMethod, url, 163 true, user, password); 164 if (openError != null) { 165 throw new RequestPermissionException(url); 166 } 167 168 setHeaders(xmlHttpRequest); 169 170 Request request = new Request(xmlHttpRequest, timeoutMillis, callback); 171 172 String sendError = XMLHTTPRequest.send(xmlHttpRequest, request, 173 requestData, callback); 174 if (sendError != null) { 175 throw new RequestException(sendError); 176 } 177 178 return request; 179 } 180 181 192 public void setHeader(String header, String value) { 193 StringValidator.throwIfEmptyOrNull("header", header); 194 StringValidator.throwIfEmptyOrNull("value", value); 195 196 if (headers == null) { 197 headers = new HashMap (); 198 } 199 200 headers.put(header, value); 201 } 202 203 212 public void setPassword(String password) { 213 StringValidator.throwIfEmptyOrNull("password", password); 214 215 this.password = password; 216 } 217 218 233 public void setTimeoutMillis(int timeoutMillis) { 234 if (timeoutMillis < 0) { 235 throw new IllegalArgumentException ("Timeouts cannot be negative"); 236 } 237 238 this.timeoutMillis = timeoutMillis; 239 } 240 241 248 public void setUser(String user) { 249 StringValidator.throwIfEmptyOrNull("user", user); 250 251 this.user = user; 252 } 253 254 260 private void setHeaders(JavaScriptObject xmlHttpRequest) 261 throws RequestException { 262 if (headers != null && headers.size() > 0) { 263 Set entrySet = headers.entrySet(); 264 Iterator iter = entrySet.iterator(); 265 while (iter.hasNext()) { 266 Map.Entry header = (Map.Entry ) iter.next(); 267 String errorMessage = XMLHTTPRequest.setRequestHeader(xmlHttpRequest, 268 (String ) header.getKey(), (String ) header.getValue()); 269 if (errorMessage != null) { 270 throw new RequestException(errorMessage); 271 } 272 } 273 } else { 274 XMLHTTPRequest.setRequestHeader(xmlHttpRequest, "Content-Type", 275 "text/plain; charset=utf-8"); 276 } 277 } 278 } 279 | Popular Tags |