1 16 17 package org.apache.commons.latka.http; 18 19 import java.net.URL ; 20 import java.util.LinkedList ; 21 22 import org.apache.commons.httpclient.HttpState; 23 import org.apache.commons.httpclient.Cookie; 24 25 36 public class SessionImpl implements Session { 37 38 44 protected LinkedList _urls = new LinkedList (); 45 46 51 protected HttpState _state = new HttpState(); 52 53 57 71 public Request createRequest(URL url, int httpMethod, String version) { 72 return createRequest(null, url, httpMethod, version, true, null); 74 } 75 76 94 public Request createRequest(String label, URL url, int httpMethod, 95 String version, boolean followRedirects) { 96 return createRequest(label, url, httpMethod, version, followRedirects, null); 98 } 99 100 116 public Request createRequest(URL url, int httpMethod, Proxy proxy, String version) { 117 return createRequest(null, url, httpMethod, version, true, proxy); 119 } 120 121 139 public Request createRequest(String label, URL url, int httpMethod, 140 String version, boolean followRedirects, Proxy proxy) { 141 142 RequestImpl request = new RequestImpl(label, url, httpMethod, _state, 143 this, followRedirects); 144 request.setProxy(proxy); 145 request.setVersion(version); 146 147 URL referer = getReferer(); 148 149 if (referer != null) { 150 request.addHeader("Referer", referer.toString()); 151 } 152 153 return request; 154 155 } 156 157 163 protected void setReferer(URL url) { 164 _urls.add(url); 165 } 166 167 176 protected URL getReferer() { 177 if (_urls.size() > 0) { 178 return (URL ) _urls.getLast(); 179 } 180 181 return null; 182 } 183 184 193 public void addCookie(String domain, String path, 194 String name, String value) { 195 196 Cookie cookie = new Cookie(domain, name, value); 197 cookie.setPath(path); 198 199 _state.addCookie(cookie); 200 } 201 202 209 public String getCookieValue(String name) { 210 String value = null; 211 boolean done = false; 212 213 Cookie[] cookies = _state.getCookies(); 214 215 for (int i = 0; i < cookies.length && !done; i++) { 216 if (cookies[i].getName().equals(name)) { 217 value = cookies[i].getValue(); 218 done = true; 219 } 220 } 221 222 return value; 223 } 224 225 } 226 | Popular Tags |