Search in sources :

Example 71 with Cookie

use of org.apache.commons.httpclient.Cookie in project jaxrs-api by eclipse-ee4j.

the class AdaptiveHttpRequest method createCookie.

/*
   * private methods
   * ========================================================================
   */
private void createCookie(String cookieHeader) {
    String cookieLine = cookieHeader.substring(cookieHeader.indexOf(':') + 1).trim();
    StringTokenizer st = new StringTokenizer(cookieLine, " ;");
    Cookie cookie = new Cookie();
    cookie.setVersion(1);
    getState();
    if (cookieLine.indexOf("$Version") == -1) {
        cookie.setVersion(0);
        _method.getParams().setCookiePolicy(CookiePolicy.NETSCAPE);
    }
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if (token.charAt(0) != '$' && !token.startsWith("Domain") && !token.startsWith("Path")) {
            cookie.setName(token.substring(0, token.indexOf('=')));
            cookie.setValue(token.substring(token.indexOf('=') + 1));
        } else if (token.indexOf("Domain") > -1) {
            cookie.setDomainAttributeSpecified(true);
            cookie.setDomain(token.substring(token.indexOf('=') + 1));
        } else if (token.indexOf("Path") > -1) {
            cookie.setPathAttributeSpecified(true);
            cookie.setPath(token.substring(token.indexOf('=') + 1));
        }
    }
    _state.addCookie(cookie);
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) StringTokenizer(java.util.StringTokenizer)

Example 72 with Cookie

use of org.apache.commons.httpclient.Cookie in project zm-mailbox by Zimbra.

the class SoapDebugListener method sendSoapMessage.

@Override
public void sendSoapMessage(PostMethod postMethod, Element envelope, HttpState httpState) {
    if (level == Level.OFF) {
        return;
    }
    printer.println();
    printer.println("=== Request ===");
    if (Level.needsHeader(level)) {
        try {
            URI uri = postMethod.getURI();
            printer.println(uri.toString());
        } catch (URIException e) {
            e.printStackTrace();
        }
        // headers
        Header[] headers = postMethod.getRequestHeaders();
        for (Header header : headers) {
            // trim the ending crlf
            printer.println(header.toString().trim());
        }
        printer.println();
        //cookies
        if (httpState != null) {
            Cookie[] cookies = httpState.getCookies();
            for (Cookie cookie : cookies) {
                printer.println("Cookie: " + cookie.toString());
            }
        }
        printer.println();
    }
    if (Level.needsBody(level)) {
        printer.println(envelope.prettyPrint());
    }
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) URIException(org.apache.commons.httpclient.URIException) Header(org.apache.commons.httpclient.Header) URI(org.apache.commons.httpclient.URI)

Example 73 with Cookie

use of org.apache.commons.httpclient.Cookie in project zm-mailbox by Zimbra.

the class ZimbraServlet method proxyServletRequest.

public static void proxyServletRequest(HttpServletRequest req, HttpServletResponse resp, HttpMethod method, HttpState state) throws IOException, ServiceException {
    // create an HTTP client with the same cookies
    javax.servlet.http.Cookie[] cookies = req.getCookies();
    String hostname = method.getURI().getHost();
    boolean hasZMAuth = hasZimbraAuthCookie(state);
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals(ZimbraCookie.COOKIE_ZM_AUTH_TOKEN) && hasZMAuth)
                continue;
            state.addCookie(new Cookie(hostname, cookies[i].getName(), cookies[i].getValue(), "/", null, false));
        }
    }
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    if (state != null)
        client.setState(state);
    int hopcount = 0;
    for (Enumeration<?> enm = req.getHeaderNames(); enm.hasMoreElements(); ) {
        String hname = (String) enm.nextElement(), hlc = hname.toLowerCase();
        if (hlc.equals("x-zimbra-hopcount"))
            try {
                hopcount = Math.max(Integer.parseInt(req.getHeader(hname)), 0);
            } catch (NumberFormatException e) {
            }
        else if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.equals("authorization"))
            method.addRequestHeader(hname, req.getHeader(hname));
    }
    if (hopcount >= MAX_PROXY_HOPCOUNT)
        throw ServiceException.TOO_MANY_HOPS(HttpUtil.getFullRequestURL(req));
    method.addRequestHeader("X-Zimbra-Hopcount", Integer.toString(hopcount + 1));
    if (method.getRequestHeader("X-Zimbra-Orig-Url") == null)
        method.addRequestHeader("X-Zimbra-Orig-Url", req.getRequestURL().toString());
    String ua = req.getHeader("User-Agent");
    if (ua != null)
        method.setRequestHeader("User-Agent", ua);
    // dispatch the request and copy over the results
    int statusCode = -1;
    for (int retryCount = 3; statusCode == -1 && retryCount > 0; retryCount--) {
        statusCode = HttpClientUtil.executeMethod(client, method);
    }
    if (statusCode == -1) {
        resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "retry limit reached");
        return;
    } else if (statusCode >= 300) {
        resp.sendError(statusCode, method.getStatusText());
        return;
    }
    Header[] headers = method.getResponseHeaders();
    for (int i = 0; i < headers.length; i++) {
        String hname = headers[i].getName(), hlc = hname.toLowerCase();
        if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.startsWith("www-"))
            resp.addHeader(hname, headers[i].getValue());
    }
    InputStream responseStream = method.getResponseBodyAsStream();
    if (responseStream == null || resp.getOutputStream() == null)
        return;
    ByteUtil.copy(method.getResponseBodyAsStream(), false, resp.getOutputStream(), false);
}
Also used : ZimbraCookie(com.zimbra.common.util.ZimbraCookie) Cookie(org.apache.commons.httpclient.Cookie) Header(org.apache.commons.httpclient.Header) InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 74 with Cookie

use of org.apache.commons.httpclient.Cookie in project iaf by ibissource.

the class SsoUtil method addSsoCredential.

public static void addSsoCredential(HttpMethod method, HttpState state, String defaultForwardHost) {
    try {
        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());
    }
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) URI(org.apache.commons.httpclient.URI) WSSecurityException(com.ibm.websphere.security.WSSecurityException) CredentialDestroyedException(com.ibm.websphere.security.auth.CredentialDestroyedException) CredentialExpiredException(javax.security.auth.login.CredentialExpiredException)

Example 75 with Cookie

use of org.apache.commons.httpclient.Cookie in project ecf by eclipse.

the class PHPBBCookies method detectCookies.

public static Map<String, String> detectCookies(Cookie[] cookies) {
    Map<String, String> detected = new HashMap<String, String>();
    for (Cookie cookie : cookies) {
        if (PAT_SESS_ID.matcher(cookie.getValue()).matches()) {
            // detected session id
            detected.put(KEY_SESS_ID, cookie.getValue());
        } else {
            // try to detect user id
            try {
                String value = URLDecoder.decode(cookie.getValue(), "UTF-8");
                Map<String, String> map = PHPUtil.deserializeStringArray(value);
                if (map != null && map.containsKey("userid")) {
                    detected.put(KEY_USER_ID, map.get("userid"));
                }
            } catch (UnsupportedEncodingException e) {
            // do nothing
            }
        }
    }
    return detected;
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

Cookie (org.apache.commons.httpclient.Cookie)136 Header (org.apache.commons.httpclient.Header)69 NameValuePair (org.apache.commons.httpclient.NameValuePair)13 CookieSpec (org.apache.commons.httpclient.cookie.CookieSpec)8 Test (org.junit.jupiter.api.Test)8 HttpState (org.apache.commons.httpclient.HttpState)5 HashMap (java.util.HashMap)4 HttpException (org.apache.commons.httpclient.HttpException)4 URI (org.apache.commons.httpclient.URI)3 ApiException (org.zaproxy.zap.extension.api.ApiException)3 ZimbraCookie (com.zimbra.common.util.ZimbraCookie)2 IOException (java.io.IOException)2 Date (java.util.Date)2 StringTokenizer (java.util.StringTokenizer)2 JSONException (net.sf.json.JSONException)2 JSONObject (net.sf.json.JSONObject)2 HttpClient (org.apache.commons.httpclient.HttpClient)2 URIException (org.apache.commons.httpclient.URIException)2 WSSecurityException (com.ibm.websphere.security.WSSecurityException)1 CredentialDestroyedException (com.ibm.websphere.security.auth.CredentialDestroyedException)1