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

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

Introduction

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

Prototype

public String toString() 

Source Link

Usage

From source file:com.djimenez.tuenti.example.FormLoginDemo.java

/**
 * @param initcookies//w  w  w  . j  av a  2s .c o m
 */
private static void checkCookies(final Cookie[] initcookies) {
    if (initcookies.length == 0) {
        System.out.println("None");
    } else {
        for (final Cookie initcookie : initcookies) {
            System.out.println("- " + initcookie.toString());
        }
    }
}

From source file:com.zimbra.qa.unittest.prov.soap.SoapDebugListener.java

@Override
public void sendSoapMessage(PostMethod postMethod, Element envelope, HttpState httpState) {
    if (level == Level.OFF) {
        return;//from   w w w . j ava 2s . co m
    }

    System.out.println();
    System.out.println("=== Request ===");

    if (Level.needsHeader(level)) {
        try {
            URI uri = postMethod.getURI();
            System.out.println(uri.toString());
        } catch (URIException e) {
            e.printStackTrace();
        }

        // headers
        Header[] headers = postMethod.getRequestHeaders();
        for (Header header : headers) {
            System.out.println(header.toString().trim()); // trim the ending crlf
        }
        System.out.println();

        //cookies
        if (httpState != null) {
            Cookie[] cookies = httpState.getCookies();
            for (Cookie cookie : cookies) {
                System.out.println("Cookie: " + cookie.toString());
            }
        }
        System.out.println();
    }

    if (Level.needsBody(level)) {
        System.out.println(envelope.prettyPrint());
    }
}

From source file:com.zimbra.common.util.SoapDebugListener.java

@Override
public void sendSoapMessage(PostMethod postMethod, Element envelope, HttpState httpState) {
    if (level == Level.OFF) {
        return;//from w  ww.  j  av  a  2  s .co m
    }

    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) {
            printer.println(header.toString().trim()); // trim the ending crlf
        }
        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());
    }
}

From source file:com.celamanzi.liferay.portlets.rails286.OnlineClient.java

protected void debugCookies(Cookie[] cookies) {
    log.debug("Cookie inspector found " + cookies.length + " cookies ------v");
    for (Cookie cookie : cookies)
        log.debug(cookie.toString() + ", domain=" + cookie.getDomain() + ", path=" + cookie.getPath()
                + ", max-age=" + cookie.getExpiryDate() + ", secure=" + cookie.getSecure());
    log.debug("----------------------------");
}

From source file:com.cerema.cloud2.lib.common.OwnCloudClient.java

public String getCookiesString() {
    Cookie[] cookies = getState().getCookies();
    String cookiesString = "";
    for (Cookie cookie : cookies) {
        cookiesString = cookiesString + cookie.toString() + ";";

        // logCookie(cookie);
    }//from  www .  ja v a 2  s.c  o  m

    return cookiesString;

}

From source file:com.ihelpoo.app.api.ApiClient.java

/**
 * post// w  w w .  jav  a  2  s .  c om
 *
 * @param url
 * @param params
 * @param files
 * @throws AppException
 */
private static InputStream _post(AppContext appContext, String url, Map<String, Object> params,
        Map<String, File> files) throws AppException {
    //System.out.println("post_url==> "+url);
    String cookie = getCookie(appContext);
    String userAgent = getUserAgent(appContext);

    HttpClient httpClient = null;
    PostMethod httpPost = null;

    //post???
    int length = (params == null ? 0 : params.size()) + (files == null ? 0 : files.size());
    Part[] parts = new Part[length];
    int i = 0;
    if (params != null)
        for (String name : params.keySet()) {
            parts[i++] = new StringPart(name, String.valueOf(params.get(name)), UTF_8);
        }
    if (files != null)
        for (String file : files.keySet()) {
            try {
                parts[i++] = new FilePart(file, files.get(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            //System.out.println("post_key_file==> "+file);
        }

    String responseBody = "";
    int time = 0;
    do {
        try {
            httpClient = getHttpClient();
            httpPost = getHttpPost(url, cookie, userAgent);
            httpPost.setRequestEntity(new MultipartRequestEntity(parts, httpPost.getParams()));
            int statusCode = httpClient.executeMethod(httpPost);
            if (statusCode != HttpStatus.SC_OK) {
                throw AppException.http(statusCode);
            } else if (statusCode == HttpStatus.SC_OK) {
                Cookie[] cookies = httpClient.getState().getCookies();
                String tmpcookies = "";
                for (Cookie ck : cookies) {
                    tmpcookies += ck.toString() + ";";
                }
                //?cookie
                if (appContext != null && tmpcookies != "") {
                    appContext.setProperty("cookie", tmpcookies);
                    appCookie = tmpcookies;
                }
            }
            responseBody = httpPost.getResponseBodyAsString();
            //              System.out.println("XMLDATA=====>"+responseBody);
            break;
        } catch (HttpException e) {
            time++;
            if (time < RETRY_TIME) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                }
                continue;
            }
            // ?????
            e.printStackTrace();
            throw AppException.http(e);
        } catch (IOException e) {
            time++;
            if (time < RETRY_TIME) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                }
                continue;
            }
            // ?
            e.printStackTrace();
            throw AppException.network(e);
        } finally {
            // 
            httpPost.releaseConnection();
            httpClient = null;
        }
    } while (time < RETRY_TIME);

    responseBody = responseBody.replaceAll("\\p{Cntrl}", "");
    //      if(responseBody.contains("result") && responseBody.contains("errorCode") && appContext.containsProperty("user.uid")){//FIXME
    //         try {
    //            Result res = Result.parse(new ByteArrayInputStream(responseBody.getBytes()));
    //            if(res.getErrorCode() == 0){
    //               appContext.logout();
    //               appContext.getUnLoginHandler().sendEmptyMessage(1);
    //            }
    //         } catch (Exception e) {
    //            e.printStackTrace();
    //         }
    //      }
    return new ByteArrayInputStream(responseBody.getBytes());
}

From source file:org.frameworkset.spi.remote.http.HttpReqeust.java

/**
 * post//w  w  w  .  ja  v  a  2 s .  co m
 * 
 * @param url
 * @param params
 * @param files
 * @throws AppException
 */
public static String httpPostforString(String url, String cookie, String userAgent, Map<String, Object> params,
        Map<String, File> files) throws Exception {
    // System.out.println("post_url==> "+url);
    // String cookie = getCookie(appContext);
    // String userAgent = getUserAgent(appContext);

    HttpClient httpClient = null;
    PostMethod httpPost = null;
    Part[] parts = null;
    NameValuePair[] paramPair = null;
    if (files != null) {
        // post???
        int length = (params == null ? 0 : params.size()) + (files == null ? 0 : files.size());
        parts = new Part[length];
        int i = 0;
        if (params != null) {
            Iterator<Entry<String, Object>> it = params.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, Object> entry = it.next();
                parts[i++] = new StringPart(entry.getKey(), String.valueOf(entry.getValue()), UTF_8);
                // System.out.println("post_key==> "+name+"    value==>"+String.valueOf(params.get(name)));
            }
        }
        if (files != null) {
            Iterator<Entry<String, File>> it = files.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, File> entry = it.next();
                try {
                    parts[i++] = new FilePart(entry.getKey(), entry.getValue());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                // System.out.println("post_key_file==> "+file);
            }
        }
    } else if (params != null && params.size() > 0) {
        paramPair = new NameValuePair[params.size()];
        Iterator<Entry<String, Object>> it = params.entrySet().iterator();
        NameValuePair paramPair_ = null;
        for (int i = 0; it.hasNext(); i++) {
            Entry<String, Object> entry = it.next();
            paramPair_ = new NameValuePair();
            paramPair_.setName(entry.getKey());
            paramPair_.setValue(String.valueOf(entry.getValue()));
            paramPair[i] = paramPair_;
        }
    }

    String responseBody = "";
    int time = 0;
    do {
        try {
            httpClient = getHttpClient();
            httpPost = getHttpPost(url, cookie, userAgent);
            if (files != null) {
                httpPost.setRequestEntity(new MultipartRequestEntity(parts, httpPost.getParams()));
            } else {
                httpPost.addParameters(paramPair);

            }

            int statusCode = httpClient.executeMethod(httpPost);
            if (statusCode != HttpStatus.SC_OK) {
                throw new HttpRuntimeException("" + statusCode);
            } else if (statusCode == HttpStatus.SC_OK) {
                Cookie[] cookies = httpClient.getState().getCookies();
                String tmpcookies = "";
                for (Cookie ck : cookies) {
                    tmpcookies += ck.toString() + ";";
                }
                // //?cookie
                // if(appContext != null && tmpcookies != ""){
                // appContext.setProperty("cookie", tmpcookies);
                // appCookie = tmpcookies;
                // }
            }
            responseBody = httpPost.getResponseBodyAsString();
            // System.out.println("XMLDATA=====>"+responseBody);
            break;
        } catch (HttpException e) {
            time++;
            if (time < RETRY_TIME) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                }
                continue;
            }
            // ?????
            throw new HttpRuntimeException("", e);
        } catch (IOException e) {
            time++;
            if (time < RETRY_TIME) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                }
                continue;
            }
            // ?
            throw new HttpRuntimeException("", e);
        } finally {
            // 
            httpPost.releaseConnection();
            httpClient = null;
        }
    } while (time < RETRY_TIME);
    return responseBody;
    // responseBody = responseBody.replaceAll("\\p{Cntrl}", "");
    // if(responseBody.contains("result") &&
    // responseBody.contains("errorCode") &&
    // appContext.containsProperty("user.uid")){
    // try {
    // Result res = Result.parse(new
    // ByteArrayInputStream(responseBody.getBytes()));
    // if(res.getErrorCode() == 0){
    // appContext.Logout();
    // appContext.getUnLoginHandler().sendEmptyMessage(1);
    // }
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // }
    // return new ByteArrayInputStream(responseBody.getBytes());
}

From source file:org.mule.transport.http.CookieWrapperTestCase.java

@Test
public void testCookieWrapperOnlyRequiredAttributes() throws ParseException {
    cookieWrapper.setName("test");
    cookieWrapper.setValue("test");

    mockParse();/*from   w w  w .java2s.  co m*/

    cookieWrapper.parse(mockMuleMessage, mockExpressionManager);
    Cookie cookie = cookieWrapper.createCookie();

    assertEquals("test=test", cookie.toString());
}

From source file:uk.ac.open.kmi.iserve.rest.sal.test.ServiceResourceTest.java

/**
 * Test method for AddService./*w  ww  .ja v a2 s .co m*/
 */
@Test
public final void testAddService() throws IOException {
    // Clean the whole thing before testing
    try {
        manager.clearRegistry();
    } catch (SalException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

    // Add all the test collections
    log.info("Uploading services");
    log.info("Test collection: " + testFolder);

    log.info("Trying without logging in");
    given().log().all(). // Log requests
            redirects().follow(true). // Follow redirects introduced by Shiro
            multiPart("file", msmTtlTcFiles[0], MediaType.TEXT_TURTLE.getMediaType()). // Submit file
            expect().log().all(). // Log responses
            response().statusCode(302). // We should get a 302 that takes us to the login page
            when().post(REGISTRY_SERVICES_PATH);

    log.info("Correctly redirected");

    // Now log into Shiro and try again
    log.info("Now logging in");
    performLogin(false);
    Cookie cookie = webClient.getCookieManager().getCookie("JSESSIONID");
    log.info("Cookie data: " + cookie.toString());

    // Test oriented towards individuals.
    // To be updated with application oriented logging.

    log.info("Uploading services");
    for (int i = 0; i < MAX_DOCS && i < msmTtlTcFiles.length; i++) {
        // rest assured
        given().log().all(). // Log requests
                sessionId(cookie.getValue()). // Keep the session along
                redirects().follow(true). // Follow redirects introduced by Shiro
                multiPart("file", msmTtlTcFiles[i], MediaType.TEXT_TURTLE.getMediaType()). // Submit file
                expect().log().all(). // Log responses
                response().statusCode(201). // We should get a 201 created (if we have the rights)
                when().post(REGISTRY_SERVICES_PATH);
    }
}

From source file:uk.ac.open.kmi.iserve.rest.sal.test.ServiceResourceTest.java

@Test
public void testDeleteService() throws Exception {

    String relativeUri;/*from   ww w. j a  va 2  s  .  com*/
    Set<URI> existingServices = manager.getServiceManager().listServices();

    log.info("Trying to delete prior to logging");
    URI testUri = existingServices.iterator().next();
    relativeUri = URI.create(SERVICES_URI).relativize(testUri).toASCIIString();
    log.info("Trying to delete service id: " + testUri);
    given().log().all().expect().response().statusCode(405).when().delete(REGISTRY_SERVICES_PATH + relativeUri);

    // Delete all without logging
    log.info("Deleting services");
    // Try to delete endpoint
    given().log().all().redirects().follow(true).expect().log().all().response().statusCode(302).when()
            .delete(REGISTRY_SERVICES_PATH);

    // Now login and run the rest of the tests
    log.info("Now logging in");
    performLogin(false);
    Cookie cookie = webClient.getCookieManager().getCookie("JSESSIONID");
    log.info("Cookie data: " + cookie.toString());

    // Try to delete non existing svcs (directly at the graph level)
    for (int i = 0; i < 10; i++) {
        given().log().all().sessionId(cookie.getValue()).redirects().follow(true).expect().log().all()
                .response().statusCode(404).when().delete(REGISTRY_SERVICES_PATH + i);
    }

    // Try to delete non existing svcs
    for (int i = 0; i < 10; i++) {
        given().log().all().sessionId(cookie.getValue()).redirects().follow(true).expect().log().all()
                .response().statusCode(404).when().delete(REGISTRY_SERVICES_PATH + i + "/serviceName");
    }

    // Try to delete 10 services using their entire URIs
    Iterator<URI> iter = existingServices.iterator();
    for (int i = 0; i < 10 && iter.hasNext(); i++) {
        URI uri = iter.next();
        relativeUri = URI.create(SERVICES_URI).relativize(uri).toASCIIString();
        log.info("Deleting service id: " + uri);
        given().log().all().sessionId(cookie.getValue()).redirects().follow(true).expect().log().all()
                .response().statusCode(200).when().delete(REGISTRY_SERVICES_PATH + relativeUri);
    }

    // Now try to delete the whole endpoint (i.e., clear)
    given().log().all().sessionId(cookie.getValue()).redirects().follow(true).expect().log().all().response()
            .statusCode(200).when().delete(REGISTRY_SERVICES_PATH);

}