List of usage examples for org.apache.commons.httpclient Cookie setPath
public void setPath(String paramString)
From source file:dslab.crawler.pack.CrawlerPack.java
/** * Return a Cookie array/*w w w . j a v a 2 s . c o m*/ * and auto importing domain and path when domain was empty. * * @param uri required Apache Common VFS supported file systems and response JSON format content. * @return Cookie[] */ Cookie[] getCookies(String uri) { if (null == cookies || 0 == cookies.size()) return null; for (Cookie cookie : cookies) { if ("".equals(cookie.getDomain())) { String domain = uri.replaceAll("^.*:\\/\\/([^\\/]+)[\\/]?.*$", "$1"); // System.out.println(domain); cookie.setDomain(domain); cookie.setPath("/"); cookie.setExpiryDate(null); cookie.setSecure(false); } } return cookies.toArray(new Cookie[cookies.size()]); }
From source file:com.github.abola.crawler.CrawlerPack.java
/** * Return a Cookie array// ww w .j a v a2 s .c o m * and auto importing domain and path when domain was empty. * * @param uri required Apache Common VFS supported file systems and response JSON format content. * @return Cookie[] */ Cookie[] getCookies(String uri) { if (null == cookies || 0 == cookies.size()) return null; for (Cookie cookie : cookies) { if ("".equals(cookie.getDomain())) { String domain = uri.replaceAll("^.*:\\/\\/([^\\/]+)[\\/]?.*$", "$1"); cookie.setDomain(domain); cookie.setPath("/"); cookie.setExpiryDate(null); cookie.setSecure(false); } } return cookies.toArray(new Cookie[cookies.size()]); }
From source file:com.hp.alm.ali.rest.client.AliRestClient.java
private void addTenantCookie(Cookie ssoCookie) { if (ssoCookie != null) { Cookie tenant_id_cookie = new Cookie(ssoCookie.getDomain(), "TENANT_ID_COOKIE", "0"); tenant_id_cookie.setDomainAttributeSpecified(true); tenant_id_cookie.setPath(ssoCookie.getPath()); tenant_id_cookie.setPathAttributeSpecified(true); httpClient.getState().addCookie(tenant_id_cookie); }/*from w ww . j a v a2s . c om*/ }
From source file:flex.messaging.services.http.proxy.ResponseFilter.java
protected void copyCookiesFromEndpoint(ProxyContext context) { HttpServletResponse clientResponse = FlexContext.getHttpResponse(); if (clientResponse != null) { Cookie[] cookies = context.getHttpClient().getState().getCookies(); // We need to filter out the request cookies, we don't need to send back to the client Set requestCookies = context.getRequestCookies(); for (int i = 0; i < cookies.length; i++) { if (requestCookies != null && requestCookies.contains(cookies[i]) && cookies[i].getExpiryDate() == null) { // It means it is a request cookie and nothing changed, we need to skip it continue; }/* w ww .ja va2 s . c om*/ // Process the cookie; String domain = cookies[i].getDomain(); String path = cookies[i].getPath(); String name = cookies[i].getName(); String value = cookies[i].getValue(); String clientName = ResponseUtil.getCookieName(context, path, name, domain); if (Log.isInfo()) { String str = "-- Cookie in response: domain = '" + domain + "', path = '" + path + "', client name = '" + clientName + "', endpoint name = '" + name + "', value = '" + value; Log.getLogger(HTTPProxyService.LOG_CATEGORY).debug(str); } javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie(clientName, value); Date expiry = cookies[i].getExpiryDate(); if (expiry != null) { int maxAge = (int) ((expiry.getTime() - System.currentTimeMillis()) / 1000); cookie.setMaxAge(maxAge); } cookie.setSecure(cookies[i].getSecure()); cookie.setPath("/"); clientResponse.addCookie(cookie); } } }
From source file:nl.nn.adapterframework.util.SsoUtil.java
public static void addSsoCredential(HttpMethod method, HttpState state, String defaultForwardHost) { try {/*from w w w .j a v a 2 s .c o m*/ String name = SsoUtil.getSsoTokenName(); String value = SsoUtil.getSsoToken(); if (StringUtils.isEmpty(value)) { if (log.isDebugEnabled()) log.debug("no value for SsoCredential [" + name + "]"); } else { if (log.isDebugEnabled()) log.debug("constructing SsoCredentialCookie [" + name + "]"); Cookie ssoCookie = new Cookie(); ssoCookie.setName(name); ssoCookie.setValue(value); String forwardHost; try { URI uri = method.getURI(); forwardHost = uri.getHost(); if (StringUtils.isEmpty(forwardHost)) { if (log.isDebugEnabled()) log.debug("did not find host from URI [" + uri.getURI() + "], will use default [" + defaultForwardHost + "] for SSO credential cookie"); forwardHost = defaultForwardHost; } } catch (Throwable t) { log.warn("could not extract host from URI", t); forwardHost = defaultForwardHost; } ssoCookie.setDomain(forwardHost); // path must have a value, otherwise cookie is not appended to request ssoCookie.setPath("/"); if (log.isDebugEnabled()) log.debug("set SSOcookie attributes: domain [" + ssoCookie.getDomain() + "] path [" + ssoCookie.getPath() + "]"); state.addCookie(ssoCookie); } } catch (Exception e) { log.warn("could not obtain SsoToken: " + e.getMessage()); } }
From source file:org.apache.cactus.WebResponse.java
/** * @return the cookies returned by the server *//*from w ww . j a va2 s . co m*/ public Cookie[] getCookies() { Cookie[] returnCookies = null; // There can be several headers named "Set-Cookie", so loop through // all the headers, looking for cookies String headerName = this.connection.getHeaderFieldKey(0); String headerValue = this.connection.getHeaderField(0); Vector cookieVector = new Vector(); CookieSpec cookieSpec = CookiePolicy.getDefaultSpec(); for (int i = 1; (headerName != null) || (headerValue != null); i++) { LOGGER.debug("Header name = [" + headerName + "]"); LOGGER.debug("Header value = [" + headerValue + "]"); if ((headerName != null) && (headerName.toLowerCase().equals("set-cookie") || headerName.toLowerCase().equals("set-cookie2"))) { // Parse the cookie definition org.apache.commons.httpclient.Cookie[] cookies; try { cookies = cookieSpec.parse( CookieUtil.getCookieDomain(getWebRequest(), getConnection().getURL().getHost()), CookieUtil.getCookiePort(getWebRequest(), getConnection().getURL().getPort()), CookieUtil.getCookiePath(getWebRequest(), getConnection().getURL().getFile()), false, new Header(headerName, headerValue)); } catch (HttpException e) { throw new ChainedRuntimeException("Error parsing cookies", e); } // Transform the HttpClient cookies into Cactus cookies and // add them to the cookieVector vector for (int j = 0; j < cookies.length; j++) { Cookie cookie = new Cookie(cookies[j].getDomain(), cookies[j].getName(), cookies[j].getValue()); cookie.setComment(cookies[j].getComment()); cookie.setExpiryDate(cookies[j].getExpiryDate()); cookie.setPath(cookies[j].getPath()); cookie.setSecure(cookies[j].getSecure()); cookieVector.addElement(cookie); } } headerName = this.connection.getHeaderFieldKey(i); headerValue = this.connection.getHeaderField(i); } returnCookies = new Cookie[cookieVector.size()]; cookieVector.copyInto(returnCookies); return returnCookies; }
From source file:org.mule.transport.http.CookieWrapper.java
public Cookie createCookie() throws ParseException { Cookie cookie = new Cookie(); cookie.setName(getName());//w w w . ja v a2 s . co m cookie.setValue(getValue()); cookie.setDomain(domain); cookie.setPath(path); if (expiryDate != null) { cookie.setExpiryDate(formatExpiryDate(expiryDate)); } if (maxAge != null && expiryDate == null) { cookie.setExpiryDate(new Date(System.currentTimeMillis() + Integer.valueOf(maxAge) * 1000L)); } if (secure != null) { cookie.setSecure(Boolean.valueOf(secure)); } if (version != null) { cookie.setVersion(Integer.valueOf(version)); } return cookie; }
From source file:org.wso2.appserver.integration.tests.session.persistence.WSAS2060SessionPersistenceTestCase.java
@Test(groups = "wso2.as", description = "Check if Session Webapp is available") public void testSessionWebappAvailable() throws Exception { GetMethod httpGet = null;/*from www .java 2 s.co m*/ GetMethod httpGetFabIcon = null; boolean isSecondSession = false; try { httpGet = new GetMethod(endpoint); int statusCode = httpClient.executeMethod(httpGet); ++sessionCount; assertTrue(HttpStatus.SC_OK == statusCode, "Session example webapp is not available for Tenant: " + userMode); HttpClient httpClientFabIcon = new HttpClient(); httpGetFabIcon = new GetMethod(SERVER_URL + "/" + "favicon.ico"); statusCode = httpClientFabIcon.executeMethod(httpGetFabIcon); Cookie cookie = null; if (HttpStatus.SC_METHOD_NOT_ALLOWED == statusCode) { Cookie[] cookiesFabIcon = httpClientFabIcon.getState().getCookies(); if (cookiesFabIcon.length > 0) { cookie = cookiesFabIcon[0]; isSecondSession = true; } } if (!isSecondSession) { cookie = new Cookie(); cookie.setDomain(asServer.getInstance().getHosts().get("default")); cookie.setPath("/"); cookie.setName("JSESSIONID"); cookie.setValue("C0FCA53EBF7F09D24E2B430847214934"); } httpClient.getState().addCookie(cookie); } finally { if (httpGet != null) { httpGet.releaseConnection(); } if (httpGetFabIcon != null) { httpGetFabIcon.releaseConnection(); } } }
From source file:org.zaproxy.zap.network.ZapCookieSpecUnitTest.java
@Test public void shouldBeValidEvenIfCookiePathIsDifferentThanOrigin() throws MalformedCookieException { // Given/*from www . j a va 2 s. c om*/ CookieSpec cookieSpec = createCookieSpec(); Cookie cookie = new Cookie(HOST, "name", "value"); cookie.setPath("/other/path/"); // When cookieSpec.validate(HOST, PORT, PATH, SECURE, cookie); // Then = No exception. }