Example usage for org.apache.commons.httpclient Cookie toExternalForm

List of usage examples for org.apache.commons.httpclient Cookie toExternalForm

Introduction

In this page you can find the example usage for org.apache.commons.httpclient Cookie toExternalForm.

Prototype

public String toExternalForm() 

Source Link

Usage

From source file:com.tasktop.c2c.server.common.service.tests.ajp.AjpProxyWebTest.java

@Test
public void testProxyHandlesCookies() throws Exception {
    final String ajpBaseUri = String.format("ajp://localhost:%s", container.getAjpPort());
    Payload payload = new Payload(HttpServletResponse.SC_OK, "some content\none two three\n\nfour");
    payload.getResponseHeaders().put("foo", "bar");
    payload.getSessionVariables().put("s1", "v1");
    TestServlet.setResponsePayload(payload);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/test");
    request.setQueryString("a=b");
    request.setParameter("a", new String[] { "b" });
    request.addHeader("c", "d ef");
    MockHttpServletResponse response = new MockHttpServletResponse();
    proxy.proxyRequest(ajpBaseUri + "/foo", request, response);

    Request firstRequest = null;/*w w w.java 2 s.c o  m*/
    for (int i = 0; i < 100; i++) {
        firstRequest = TestServlet.getLastRequest();

        // If our request is not yet there, then pause and retry shortly - proxying is an async process, and this
        // request was sometimes coming back as null which was causing test failures on the first assert below.
        if (firstRequest == null) {
            Thread.sleep(10);
        } else {
            // Got our request, so break now.
            break;
        }
    }

    Assert.assertTrue(firstRequest.isNewSession());
    Assert.assertEquals("v1", firstRequest.getSessionAttributes().get("s1"));

    List<org.apache.commons.httpclient.Cookie> cookies = new ArrayList<org.apache.commons.httpclient.Cookie>();
    for (String headerName : response.getHeaderNames()) {
        if (headerName.equalsIgnoreCase("set-cookie") || headerName.equalsIgnoreCase("set-cookie2")) {
            cookies.addAll(Arrays.asList(new RFC2965Spec().parse("localhost", container.getPort(), "/", false,
                    response.getHeader(headerName).toString())));
        }
    }
    Assert.assertEquals(1, cookies.size());
    Cookie cookie = cookies.get(0);
    Assert.assertEquals("almp.JSESSIONID", cookie.getName());

    MockHttpServletRequest request2 = new MockHttpServletRequest();
    request2.setMethod("GET");
    request2.setRequestURI("/test");
    request2.addHeader("Cookie", cookie.toExternalForm());
    MockHttpServletResponse response2 = new MockHttpServletResponse();

    payload = new Payload(HttpServletResponse.SC_OK, "test");
    TestServlet.setResponsePayload(payload);

    proxy.proxyRequest(ajpBaseUri + "/foo", request2, response2);

    Request secondRequest = TestServlet.getLastRequest();
    Assert.assertFalse(secondRequest.isNewSession());
    Assert.assertEquals(firstRequest.getSessionId(), secondRequest.getSessionId());
    Assert.assertEquals("v1", secondRequest.getSessionAttributes().get("s1"));
}

From source file:com.intuit.tank.httpclient3.TankHttpClient3.java

private void sendRequest(BaseRequest request, @Nonnull HttpMethod method, String requestBody) {
    String uri = null;//from   w w w .j  av  a 2 s.  c  om
    long waitTime = 0L;

    try {
        uri = method.getURI().toString();
        logger.debug(request.getLogUtil().getLogMessage(
                "About to " + method.getName() + " request to " + uri + " with requestBody  " + requestBody,
                LogEventType.Informational));
        List<String> cookies = new ArrayList<String>();
        if (httpclient != null && httpclient.getState() != null && httpclient.getState().getCookies() != null) {
            for (Cookie cookie : httpclient.getState().getCookies()) {
                cookies.add("REQUEST COOKIE: " + cookie.toExternalForm() + " (domain=" + cookie.getDomain()
                        + " : path=" + cookie.getPath() + ")");
            }
        }
        request.logRequest(uri, requestBody, method.getName(), request.getHeaderInformation(), cookies, false);
        setHeaders(request, method, request.getHeaderInformation());
        long startTime = System.currentTimeMillis();
        request.setTimestamp(new Date(startTime));
        httpclient.executeMethod(method);

        // read response body
        byte[] responseBody = new byte[0];
        // check for no content headers
        if (method.getStatusCode() != 203 && method.getStatusCode() != 202 && method.getStatusCode() != 204) {
            try {
                InputStream httpInputStream = method.getResponseBodyAsStream();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int curByte = httpInputStream.read();
                while (curByte >= 0) {
                    out.write(curByte);
                    curByte = httpInputStream.read();
                }
                responseBody = out.toByteArray();
            } catch (Exception e) {
                logger.warn("could not get response body: " + e);
            }
        }
        long endTime = System.currentTimeMillis();
        processResponse(responseBody, startTime, endTime, request, method.getStatusText(),
                method.getStatusCode(), method.getResponseHeaders(), httpclient.getState());
        waitTime = endTime - startTime;
    } catch (Exception ex) {
        logger.error(request.getLogUtil().getLogMessage(
                "Could not do " + method.getName() + " to url " + uri + " |  error: " + ex.toString(),
                LogEventType.IO), ex);
        throw new RuntimeException(ex);
    } finally {
        try {
            method.releaseConnection();
        } catch (Exception e) {
            logger.warn("Could not release connection: " + e, e);
        }
        if (method.getName().equalsIgnoreCase("post")
                && request.getLogUtil().getAgentConfig().getLogPostResponse()) {
            logger.info(request.getLogUtil()
                    .getLogMessage("Response from POST to " + request.getRequestUrl() + " got status code "
                            + request.getResponse().getHttpCode() + " BODY { " + request.getResponse().getBody()
                            + " }", LogEventType.Informational));
        }
    }
    if (waitTime != 0) {
        doWaitDueToLongResponse(request, waitTime, uri);
    }
}

From source file:com.intuit.tank.http.BaseRequest.java

@SuppressWarnings("rawtypes")
protected void logRequest(String url, String body, String method, HashMap<String, String> headerInformation,
        HttpClient httpclient, boolean force) {
    try {/* www  .  ja  v  a 2s . c  om*/
        StringBuilder sb = new StringBuilder();

        sb.append("REQUEST URL: " + method + " " + url).append(NEWLINE);
        // Header Information
        for (Map.Entry mapEntry : headerInformation.entrySet()) {
            sb.append("REQUEST HEADER: " + (String) mapEntry.getKey() + " = " + (String) mapEntry.getValue())
                    .append(NEWLINE);
        }
        // Cookies Information
        if (httpclient != null && httpclient.getState() != null && httpclient.getState().getCookies() != null) {
            for (Cookie cookie : httpclient.getState().getCookies()) {
                sb.append("REQUEST COOKIE: " + cookie.toExternalForm() + " (domain=" + cookie.getDomain()
                        + " : path=" + cookie.getPath() + ")").append(NEWLINE);
            }
        }
        if (null != body) {
            sb.append("REQUEST SIZE: " + body.getBytes().length).append(NEWLINE);
            sb.append("REQUEST BODY: " + body).append(NEWLINE);
        }
        this.logMsg = sb.toString();
        if (APITestHarness.getInstance().isDebug()) {
            System.out.println("******** REQUEST *********");
            System.out.println(this.logMsg);
        }
        logger.debug("******** REQUEST *********");
        logger.debug(this.logMsg);

    } catch (Exception ex) {
        logger.error("Unable to log request", ex);
    }
}

From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java

/**
 * Load cookies from a file before the first fetch.
 * <p>/*from  ww  w  .j  a va  2  s. c  o  m*/
 * The file is a text file in the Netscape's 'cookies.txt' file format.<br>
 * Example entry of cookies.txt file:<br>
 * <br>
 * www.archive.org FALSE / FALSE 1074567117 details-visit texts-cralond<br>
 * <br>
 * Each line has 7 tab-separated fields:<br>
 * <li>1. DOMAIN: The domain that created and have access to the cookie
 * value.
 * <li>2. FLAG: A TRUE or FALSE value indicating if hosts within the given
 * domain can access the cookie value.
 * <li>3. PATH: The path within the domain that the cookie value is valid
 * for.
 * <li>4. SECURE: A TRUE or FALSE value indicating if to use a secure
 * connection to access the cookie value.
 * <li>5. EXPIRATION: The expiration time of the cookie value (unix style.)
 * <li>6. NAME: The name of the cookie value
 * <li>7. VALUE: The cookie value
 *
 * @param cookiesFile file in the Netscape's 'cookies.txt' format.
 */
public void loadCookies(String cookiesFile) {
    // Do nothing if cookiesFile is not specified.
    if (cookiesFile == null || cookiesFile.length() <= 0) {
        return;
    }
    RandomAccessFile raf = null;
    try {
        raf = new RandomAccessFile(cookiesFile, "r");
        String[] cookieParts;
        String line;
        Cookie cookie = null;
        while ((line = raf.readLine()) != null) {
            // Line that starts with # is commented line, therefore skip it.
            if (!line.startsWith("#")) {
                cookieParts = line.split("\\t");
                if (cookieParts.length == 7) {
                    // Create cookie with not expiration date (-1 value).
                    // TODO: add this as an option.
                    cookie = new Cookie(cookieParts[0], cookieParts[5], cookieParts[6], cookieParts[2], -1,
                            Boolean.valueOf(cookieParts[3]).booleanValue());

                    if (cookieParts[1].toLowerCase().equals("true")) {
                        cookie.setDomainAttributeSpecified(true);
                    } else {
                        cookie.setDomainAttributeSpecified(false);
                    }
                    this.http.getState().addCookie(cookie);
                    logger.fine("Adding cookie: " + cookie.toExternalForm());
                }
            }
        }
    } catch (FileNotFoundException e) {
        // We should probably throw FatalConfigurationException.
        System.out.println("Could not find file: " + cookiesFile + " (Element: " + ATTR_LOAD_COOKIES + ")");

    } catch (IOException e) {
        // We should probably throw FatalConfigurationException.
        e.printStackTrace();
    } finally {
        try {
            if (raf != null) {
                raf.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

/**
 * Load cookies from a file before the first fetch.
 * <p>/* w ww .  ja v  a 2 s.com*/
 * The file is a text file in the Netscape's 'cookies.txt' file format.<br>
 * Example entry of cookies.txt file:<br>
 * <br>
 * www.archive.org FALSE / FALSE 1074567117 details-visit texts-cralond<br>
 * <br>
 * Each line has 7 tab-separated fields:<br>
 * <li>1. DOMAIN: The domain that created and have access to the cookie
 * value.
 * <li>2. FLAG: A TRUE or FALSE value indicating if hosts within the given
 * domain can access the cookie value.
 * <li>3. PATH: The path within the domain that the cookie value is valid
 * for.
 * <li>4. SECURE: A TRUE or FALSE value indicating if to use a secure
 * connection to access the cookie value.
 * <li>5. EXPIRATION: The expiration time of the cookie value (unix style.)
 * <li>6. NAME: The name of the cookie value
 * <li>7. VALUE: The cookie value
 *
 * @param cookiesFile file in the Netscape's 'cookies.txt' format.
 */
public void loadCookies(String cookiesFile) {
    // Do nothing if cookiesFile is not specified.
    if (cookiesFile == null || cookiesFile.length() <= 0) {
        return;
    }
    RandomAccessFile raf = null;
    try {
        raf = new RandomAccessFile(cookiesFile, "r");
        String[] cookieParts;
        String line;
        Cookie cookie = null;
        while ((line = raf.readLine()) != null) {
            // Line that starts with # is commented line, therefore skip it.
            if (!line.startsWith("#")) {
                cookieParts = line.split("\\t");
                if (cookieParts.length == 7) {
                    // Create cookie with not expiration date (-1 value).
                    // TODO: add this as an option.
                    cookie = new Cookie(cookieParts[0], cookieParts[5], cookieParts[6], cookieParts[2], -1,
                            Boolean.valueOf(cookieParts[3]).booleanValue());

                    if (cookieParts[1].toLowerCase().equals("true")) {
                        cookie.setDomainAttributeSpecified(true);
                    } else {
                        cookie.setDomainAttributeSpecified(false);
                    }
                    HttpClient http = this.getClient();
                    http.getState().addCookie(cookie);
                    logger.debug("Adding cookie: " + cookie.toExternalForm());
                }
            }
        }
    } catch (FileNotFoundException e) {
        // We should probably throw FatalConfigurationException.
        System.out.println("Could not find file: " + cookiesFile + " (Element: " + ATTR_LOAD_COOKIES + ")");

    } catch (IOException e) {
        // We should probably throw FatalConfigurationException.
        e.printStackTrace();
    } finally {
        try {
            if (raf != null) {
                raf.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.archive.modules.fetcher.AbstractCookieStorage.java

/**
 * Load cookies. The input is text in the Netscape's 'cookies.txt' file
 * format. Example entry of cookies.txt file:
 * <p>/*from   w w w.j a v  a 2  s. c o m*/
 * www.archive.org FALSE / FALSE 1311699995 details-visit texts-cralond
 * </p>
 * <p>
 * Each line has 7 tab-separated fields:
 * </p>
 * <ol>
 * <li>DOMAIN: The domain that created and have access to the cookie value.</li>
 * <li>FLAG: A TRUE or FALSE value indicating if hosts within the given
 * domain can access the cookie value.</li>
 * <li>PATH: The path within the domain that the cookie value is valid for.</li>
 * <li>SECURE: A TRUE or FALSE value indicating if to use a secure
 * connection to access the cookie value.</li>
 * <li>EXPIRATION: The expiration time of the cookie value, or -1 for no
 * expiration</li>
 * <li>NAME: The name of the cookie value</li>
 * <li>VALUE: The cookie value</li>
 * </ol>
 * 
 * @param reader
 *            input
 * @param cookiesFile
 *            file in the Netscape's 'cookies.txt' format.
 */
public static void loadCookies(Reader reader, SortedMap<String, Cookie> cookies) {
    BufferedReader br = new BufferedReader(reader);
    try {
        String line;
        int lineNo = 1;
        while ((line = br.readLine()) != null) {
            if (!line.matches("\\s*(?:#.*)?")) { // skip blank links and comments
                String[] tokens = line.split("\\t");
                if (tokens.length == 7) {
                    long epochSeconds = Long.parseLong(tokens[4]);
                    Date expirationDate = (epochSeconds >= 0 ? new Date(epochSeconds * 1000) : null);
                    Cookie cookie = new Cookie(tokens[0], tokens[5], tokens[6], tokens[2], expirationDate,
                            Boolean.valueOf(tokens[3]).booleanValue());
                    cookie.setDomainAttributeSpecified(Boolean.valueOf(tokens[1]).booleanValue());

                    LOGGER.fine("Adding cookie: domain " + cookie.getDomain() + " cookie "
                            + cookie.toExternalForm());
                    cookies.put(cookie.getSortKey(), cookie);
                } else {
                    LOGGER.warning(
                            "cookies input line " + lineNo + " invalid, expected 7 tab-delimited tokens");
                }
            }

            lineNo++;
        }
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, e.getMessage(), e);
    }
}