Example usage for org.apache.commons.httpclient HttpClient getParams

List of usage examples for org.apache.commons.httpclient HttpClient getParams

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpClient getParams.

Prototype

public HttpClientParams getParams() 

Source Link

Usage

From source file:com.predic8.membrane.interceptor.LoadBalancingInterceptorTest.java

@Test
public void testRoundRobinDispachingStrategy() throws Exception {
    balancingInterceptor.setDispatchingStrategy(roundRobinStrategy);

    HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    PostMethod vari = getPostMethod();/*from   www  . j a v  a 2  s. c  o m*/
    int status = client.executeMethod(vari);
    //System.out.println(new String(vari.getResponseBody()));

    assertEquals(200, status);
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(0, mockInterceptor2.getCount());

    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());

    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(2, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());

    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(2, mockInterceptor1.getCount());
    assertEquals(2, mockInterceptor2.getCount());
}

From source file:com.predic8.membrane.interceptor.LoadBalancingInterceptorTest.java

@Test
public void testFailOverOnStatus500() throws Exception {
    balancingInterceptor.setDispatchingStrategy(roundRobinStrategy);

    HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(0, mockInterceptor2.getCount());

    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());

    ((ServiceProxy) service1.getRuleManager().getRules().get(0)).getInterceptors().add(0,
            new AbstractInterceptor() {
                @Override//from   ww  w.  j  a va  2 s  .  com
                public Outcome handleRequest(Exchange exc) throws Exception {
                    exc.setResponse(Response.internalServerError().build());
                    return Outcome.ABORT;
                }
            });

    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(2, mockInterceptor2.getCount());

    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(3, mockInterceptor2.getCount());

}

From source file:net.neurowork.cenatic.centraldir.workers.XMLRestWorker.java

public static HttpClient getHttpClient() {
    HttpClient httpclient = new HttpClient();
    //      httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, HTTP_CONTEXT_CHARSET_ISO_8859_1);
    httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, HTTP_CONTEXT_CHARSET_UTF_8);

    return httpclient;
}

From source file:edu.ku.brc.ui.FeedBackSender.java

/**
 * @param item//from w ww. jav  a 2  s  .  com
 * @throws Exception
 */
protected void send(final FeedBackSenderItem item) throws Exception {
    if (item != null) {
        // check the website for the info about the latest version
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setParameter("http.useragent", getClass().getName()); //$NON-NLS-1$

        PostMethod postMethod = new PostMethod(getSenderURL());

        // get the POST parameters (which includes usage stats, if we're allowed to send them)
        NameValuePair[] postParams = createPostParameters(item);
        postMethod.setRequestBody(postParams);

        // connect to the server
        try {
            httpClient.executeMethod(postMethod);

            // get the server response
            String responseString = postMethod.getResponseBodyAsString();

            if (StringUtils.isNotEmpty(responseString)) {
                System.err.println(responseString);
            }

        } catch (java.net.UnknownHostException uex) {
            log.error(uex.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.snaker.DownloadManager.java

public void prepare(Task task) {
    HttpClient client = new HttpClient(connectionManager);
    client.getParams().setParameter("http.protocol.single-cookie-header", true);
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    Proxy p = task.getProxy();/*from   ww  w . j a v  a 2 s .co  m*/
    if (p != null) {
        client.getHostConfiguration().setProxy(p.getHost(), p.getPort());
    }
    clients.put(task.getId(), client);
}

From source file:de.thorstenberger.examServer.ws.remoteusermanager.ShibbolethAuthenticationProvider.java

private boolean authenticateUser(final URL url, final String login, final String pwd) throws IOException {
    final HttpClient client = new HttpClient();
    client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    client.getState().setCredentials(new AuthScope(url.getHost(), AuthScope.ANY_PORT, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials(login, pwd));

    final GetMethod authget = new GetMethod(url.toString());

    authget.setDoAuthentication(true);//from   w ww  . j ava2  s .c  om

    String response = null;
    try {
        final int status = client.executeMethod(authget);

        // check if successful
        if (status == 200) {
            response = authget.getResponseBodyAsString();

            return isValidAuthentication(extractSAMLResponse(response));
        }
    } finally {
        // release any connection resources used by the method
        authget.releaseConnection();
    }

    return false;
}

From source file:edu.unc.lib.dl.admin.controller.MODSController.java

/**
 * Retrieves the MD_DESCRIPTIVE datastream, containing MODS, for this item if one is present. If it is not present,
 * then returns a blank MODS document./*from  ww  w  .j av a 2 s.  c o m*/
 *
 * @param idPrefix
 * @param id
 * @return
 */
@RequestMapping(value = "{pid}/mods", method = RequestMethod.GET)
public @ResponseBody String getMods(@PathVariable("pid") String pid) {
    String mods = null;
    String dataUrl = swordUrl + "em/" + pid + "/" + ContentModelHelper.Datastream.MD_DESCRIPTIVE;

    HttpClient client = HttpClientUtil.getAuthenticatedClient(dataUrl, swordUsername, swordPassword);
    client.getParams().setAuthenticationPreemptive(true);
    GetMethod method = new GetMethod(dataUrl);

    // Pass the users groups along with the request
    AccessGroupSet groups = GroupsThreadStore.getGroups();
    method.addRequestHeader(HttpClientUtil.FORWARDED_GROUPS_HEADER, groups.joinAccessGroups(";"));

    try {
        client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            try {
                mods = method.getResponseBodyAsString();
            } catch (IOException e) {
                log.info("Problem uploading MODS for " + pid + ": " + e.getMessage());
            } finally {
                method.releaseConnection();
            }
        } else {
            if (method.getStatusCode() == HttpStatus.SC_BAD_REQUEST
                    || method.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                // Ensure that the object actually exists
                PID existingPID = tripleStoreQueryService.verify(new PID(pid));
                if (existingPID != null) {
                    // MODS doesn't exist, so pass back an empty record.
                    mods = "<mods:mods xmlns:mods=\"http://www.loc.gov/mods/v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></mods:mods>";
                } else {
                    throw new Exception(
                            "Unable to retrieve MODS.  Object " + pid + " does not exist in the repository.");
                }
            } else {
                throw new Exception("Failure to retrieve fedora content due to response of: "
                        + method.getStatusLine().toString() + "\nPath was: " + method.getURI().getURI());
            }
        }
    } catch (Exception e) {
        log.error("Error while attempting to stream Fedora content for " + pid, e);
    }
    return mods;
}

From source file:com.zimbra.qa.unittest.TestCollectConfigServletsAccess.java

/**
 * Verify that delegated admin canNOT access servlet at /service/collectconfig/
 * @throws Exception/*from  w  w w  . j ava  2  s. c  o  m*/
 */
@Test
public void testConfigDelegatedAdmin() throws Exception {
    ZAuthToken at = TestUtil.getAdminSoapTransport(TEST_ADMIN_NAME, PASSWORD).getAuthToken();
    URI servletURI = new URI(getConfigServletUrl());
    HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
    HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    restClient.setState(initialState);
    restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    GetMethod get = new GetMethod(servletURI.toString());
    int statusCode = HttpClientUtil.executeMethod(restClient, get);
    assertEquals("This request should NOT succeed. Getting status code " + statusCode,
            HttpStatus.SC_UNAUTHORIZED, statusCode);
}

From source file:com.zimbra.qa.unittest.TestCollectConfigServletsAccess.java

/**
 * Verify that delegated admin canNOT access servlet at /service/collectldapconfig/
 * @throws Exception/*from   w  w w. j av a2s .co m*/
 */
@Test
public void testLDAPConfigDelegatedAdmin() throws Exception {
    ZAuthToken at = TestUtil.getAdminSoapTransport(TEST_ADMIN_NAME, PASSWORD).getAuthToken();
    URI servletURI = new URI(getLDAPConfigServletUrl());
    HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
    HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    restClient.setState(initialState);
    restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    GetMethod get = new GetMethod(servletURI.toString());
    int statusCode = HttpClientUtil.executeMethod(restClient, get);
    assertEquals("This request should NOT succeed. Getting status code " + statusCode,
            HttpStatus.SC_UNAUTHORIZED, statusCode);
}

From source file:com.atlassian.jira.web.action.setup.SetupProductBundleHelper.java

private HttpClient prepareClient() {
    final HttpClient httpClient = new HttpClient();

    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
    httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    httpClient.getState().addCookies(getCookies());

    return httpClient;
}