org.apache.commons.httpclient.cookie.CookieSpec Java Examples

The following examples show how to use org.apache.commons.httpclient.cookie.CookieSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpMethodBase.java    From http4e with Apache License 2.0 6 votes vote down vote up
/**
 * This method is invoked immediately after 
 * {@link #readResponseHeaders(HttpState,HttpConnection)} and can be overridden by
 * sub-classes in order to provide custom response headers processing.

 * <p>
 * This implementation will handle the <tt>Set-Cookie</tt> and
 * <tt>Set-Cookie2</tt> headers, if any, adding the relevant cookies to
 * the given {@link HttpState}.
 * </p>
 *
 * @param state the {@link HttpState state} information associated with this method
 * @param conn the {@link HttpConnection connection} used to execute
 *        this HTTP method
 *
 * @see #readResponse
 * @see #readResponseHeaders
 */
protected void processResponseHeaders(HttpState state,
    HttpConnection conn) {
    LOG.trace("enter HttpMethodBase.processResponseHeaders(HttpState, "
        + "HttpConnection)");

    CookieSpec parser = getCookieSpec(state);

    // process set-cookie headers
    Header[] headers = getResponseHeaderGroup().getHeaders("set-cookie");
    processCookieHeaders(parser, headers, state, conn);

    // see if the cookie spec supports cookie versioning.
    if (parser instanceof CookieVersionSupport) {
        CookieVersionSupport versupport = (CookieVersionSupport) parser;
        if (versupport.getVersion() > 0) {
            // process set-cookie2 headers.
            // Cookie2 will replace equivalent Cookie instances
            headers = getResponseHeaderGroup().getHeaders("set-cookie2");
            processCookieHeaders(parser, headers, state, conn);
        }
    }
}
 
Example #2
Source File: HttpMethodBase.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * This method is invoked immediately after 
 * {@link #readResponseHeaders(HttpState,HttpConnection)} and can be overridden by
 * sub-classes in order to provide custom response headers processing.

 * <p>
 * This implementation will handle the <tt>Set-Cookie</tt> and
 * <tt>Set-Cookie2</tt> headers, if any, adding the relevant cookies to
 * the given {@link HttpState}.
 * </p>
 *
 * @param state the {@link HttpState state} information associated with this method
 * @param conn the {@link HttpConnection connection} used to execute
 *        this HTTP method
 *
 * @see #readResponse
 * @see #readResponseHeaders
 */
protected void processResponseHeaders(HttpState state,
    HttpConnection conn) {
    LOG.trace("enter HttpMethodBase.processResponseHeaders(HttpState, "
        + "HttpConnection)");

    CookieSpec parser = getCookieSpec(state);

    // process set-cookie headers
    Header[] headers = getResponseHeaderGroup().getHeaders("set-cookie");
    processCookieHeaders(parser, headers, state, conn);

    // see if the cookie spec supports cookie versioning.
    if (parser instanceof CookieVersionSupport) {
        CookieVersionSupport versupport = (CookieVersionSupport) parser;
        if (versupport.getVersion() > 0) {
            // process set-cookie2 headers.
            // Cookie2 will replace equivalent Cookie instances
            headers = getResponseHeaderGroup().getHeaders("set-cookie2");
            processCookieHeaders(parser, headers, state, conn);
        }
    }
}
 
Example #3
Source File: HttpState.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns an array of {@link Cookie cookies} in this HTTP 
 * state that match the given request parameters.
 * 
 * @param domain the request domain
 * @param port the request port
 * @param path the request path
 * @param secure <code>true</code> when using HTTPS
 * 
 * @return an array of {@link Cookie cookies}.
 * 
 * @see #getCookies()
 * 
 * @deprecated use CookieSpec#match(String, int, String, boolean, Cookie)
 */
public synchronized Cookie[] getCookies(
    String domain, 
    int port, 
    String path, 
    boolean secure
) {
    LOG.trace("enter HttpState.getCookies(String, int, String, boolean)");

    CookieSpec matcher = CookiePolicy.getDefaultSpec();
    ArrayList list = new ArrayList(cookies.size());
    for (int i = 0, m = cookies.size(); i < m; i++) {
        Cookie cookie = (Cookie) (cookies.get(i));
        if (matcher.match(domain, port, path, secure, cookie)) {
            list.add(cookie);
        }
    }
    return (Cookie[]) (list.toArray(new Cookie[list.size()]));
}
 
Example #4
Source File: HttpMethodBase.java    From http4e with Apache License 2.0 5 votes vote down vote up
/** 
 * Returns the actual cookie policy
 * 
 * @param state HTTP state. TODO: to be removed in the future
 * 
 * @return cookie spec
 */
private CookieSpec getCookieSpec(final HttpState state) {
    if (this.cookiespec == null) {
        int i = state.getCookiePolicy();
        if (i == -1) {
            this.cookiespec = CookiePolicy.getCookieSpec(this.params.getCookiePolicy());
        } else {
            this.cookiespec = CookiePolicy.getSpecByPolicy(i);
        }
        this.cookiespec.setValidDateFormats(
                (Collection)this.params.getParameter(HttpMethodParams.DATE_PATTERNS));
    }
    return this.cookiespec;
}
 
Example #5
Source File: Cookie.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Return a textual representation of the cookie.
 * 
 * @return string.
 */
public String toExternalForm() {
    CookieSpec spec = null;
    if (getVersion() > 0) {
        spec = CookiePolicy.getDefaultSpec(); 
    } else {
        spec = CookiePolicy.getCookieSpec(CookiePolicy.NETSCAPE); 
    }
    return spec.formatCookie(this); 
}
 
Example #6
Source File: Cookie.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * <p>Compares two cookies to determine order for cookie header.</p>
 * <p>Most specific should be first. </p>
 * <p>This method is implemented so a cookie can be used as a comparator for
 * a SortedSet of cookies. Specifically it's used above in the 
 * createCookieHeader method.</p>
 * @param o1 The first object to be compared
 * @param o2 The second object to be compared
 * @return See {@link java.util.Comparator#compare(Object,Object)}
 */
public int compare(Object o1, Object o2) {
    LOG.trace("enter Cookie.compare(Object, Object)");

    if (!(o1 instanceof Cookie)) {
        throw new ClassCastException(o1.getClass().getName());
    }
    if (!(o2 instanceof Cookie)) {
        throw new ClassCastException(o2.getClass().getName());
    }
    Cookie c1 = (Cookie) o1;
    Cookie c2 = (Cookie) o2;
    if (c1.getPath() == null && c2.getPath() == null) {
        return 0;
    } else if (c1.getPath() == null) {
        // null is assumed to be "/"
        if (c2.getPath().equals(CookieSpec.PATH_DELIM)) {
            return 0;
        } else {
            return -1;
        }
    } else if (c2.getPath() == null) {
        // null is assumed to be "/"
        if (c1.getPath().equals(CookieSpec.PATH_DELIM)) {
            return 0;
        } else {
            return 1;
        }
    } else {
        return c1.getPath().compareTo(c2.getPath());
    }
}
 
Example #7
Source File: HttpMethodBase.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** 
 * Returns the actual cookie policy
 * 
 * @param state HTTP state. TODO: to be removed in the future
 * 
 * @return cookie spec
 */
private CookieSpec getCookieSpec(final HttpState state) {
    if (this.cookiespec == null) {
        int i = state.getCookiePolicy();
        if (i == -1) {
            this.cookiespec = CookiePolicy.getCookieSpec(this.params.getCookiePolicy());
        } else {
            this.cookiespec = CookiePolicy.getSpecByPolicy(i);
        }
        this.cookiespec.setValidDateFormats(
                (Collection)this.params.getParameter(HttpMethodParams.DATE_PATTERNS));
    }
    return this.cookiespec;
}
 
Example #8
Source File: RestConsumer.java    From RestServices with Apache License 2.0 5 votes vote down vote up
public static List<Cookie> getResponseCookiesFromRequestResult(IContext context, RequestResult requestResult) throws MalformedURLException {
	if (requestResult == null)
		throw new IllegalArgumentException("No request result provided");
	
	List<Cookie> res = new ArrayList<Cookie>();
	JSONObject headers = new JSONObject(requestResult.get_ResponseHeaders());
	
	URL requestUrl = new URL(requestResult.getRequestUrl());
	CookieSpec spec = CookiePolicy.getDefaultSpec();
	
	if (headers.has("Set-Cookie")) {
		JSONArray cookies = headers.getJSONArray("Set-Cookie");
		for(int i = 0; i < cookies.length(); i++) {
			try {
				org.apache.commons.httpclient.Cookie[] innercookies = spec.parse(requestUrl.getHost(), requestUrl.getPort(), requestUrl.getPath(), "https".equals(requestUrl.getProtocol()), cookies.getString(i));
				for(org.apache.commons.httpclient.Cookie innercookie : innercookies) {
					Cookie cookie = new Cookie(context);
					cookie.setName(innercookie.getName());
					cookie.setValue(innercookie.getValue());
					cookie.setDomain(innercookie.getDomain());
					cookie.setPath(innercookie.getPath());
					cookie.setMaxAgeSeconds(innercookie.getExpiryDate() == null ? -1 : Math.round((innercookie.getExpiryDate().getTime() - System.currentTimeMillis()) / 1000L));
					res.add(cookie);
				}
			} catch (Exception e) {
				RestServices.LOGCONSUME.warn("Failed to parse cookie: " + e.getMessage(), e);
			}
		}
	}
	
	return res;
}