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

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

Introduction

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

Prototype

public HttpState getState()

Source Link

Usage

From source file:net.sf.taverna.t2.uiexts.bioswr.ui.worker.CheckCredentialsWorker.java

@Override
protected Boolean doInBackground() throws Exception {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

    GetMethod get = new GetMethod(uri.toString());
    get.setDoAuthentication(true);/*from   www  .ja va2  s. c o  m*/

    try {
        final int status = client.executeMethod(get);
        return status != HttpURLConnection.HTTP_UNAUTHORIZED;
    } finally {
        get.releaseConnection();
    }

    //        HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
    //        connection.setAllowUserInteraction(false);
    //        connection.setRequestProperty ("Authorization", authorization);
    //        connection.connect();
    //        return connection.getResponseCode() != HttpURLConnection.HTTP_UNAUTHORIZED;
}

From source file:com.bt.aloha.batchtest.v2.RemoteTomcatStackRunner.java

private String getResponse(String uri) {
    HttpClient httpClient = new HttpClient();
    httpClient.getState().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(getUsername(), getPassword()));
    HttpMethod getMethod = new GetMethod(uri);
    getMethod.setDoAuthentication(true);

    try {/*from w  w w . j ava2  s. com*/
        int rc = httpClient.executeMethod(getMethod);
        LOG.debug(rc);
        if (rc != 200)
            throw new RuntimeException(String.format("bad http response from Tomcat: %d", rc));
        return getMethod.getResponseBodyAsString();
    } catch (HttpException e) {
        LOG.warn(e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        LOG.warn(e);
        throw new RuntimeException(e);
    }
}

From source file:liveplugin.toolwindow.addplugin.git.jetbrains.plugins.github.api.GithubApiUtil.java

@NotNull
private static HttpClient getHttpClient(@Nullable GithubAuthData.BasicAuth basicAuth, boolean useProxy) {
    int timeout = GithubSettings.getInstance().getConnectionTimeout();
    final HttpClient client = new HttpClient();
    HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
    params.setConnectionTimeout(timeout); //set connection timeout (how long it takes to connect to remote host)
    params.setSoTimeout(timeout); //set socket timeout (how long it takes to retrieve data from remote host)

    client.getParams().setContentCharset("UTF-8");
    // Configure proxySettings if it is required
    final HttpConfigurable proxySettings = HttpConfigurable.getInstance();
    if (useProxy && proxySettings.USE_HTTP_PROXY && !StringUtil.isEmptyOrSpaces(proxySettings.PROXY_HOST)) {
        client.getHostConfiguration().setProxy(proxySettings.PROXY_HOST, proxySettings.PROXY_PORT);
        if (proxySettings.PROXY_AUTHENTICATION) {
            client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
                    proxySettings.PROXY_LOGIN, proxySettings.getPlainProxyPassword()));
        }/*ww w . j  av a 2  s . c  o  m*/
    }
    if (basicAuth != null) {
        client.getParams().setCredentialCharset("UTF-8");
        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(basicAuth.getLogin(), basicAuth.getPassword()));
    }
    return client;
}

From source file:com.honnix.cheater.validator.CookieValidator.java

public boolean isLoginValid(HttpClient httpClient, HttpMethodBase method) {
    Cookie[] cookies = httpClient.getState().getCookies();
    boolean hasDSID = hasDSID(cookies);
    boolean hasDSFirstAccess = hasDSFirstAccess(cookies);
    boolean hasDSLastAccess = hasDSLastAccess(cookies);

    return hasDSID & hasDSFirstAccess & hasDSLastAccess;
}

From source file:fr.jayasoft.ivy.url.HttpClientDownloader.java

private GetMethod doGet(URL url) throws IOException, HttpException {
    HttpClient client = new HttpClient();

    if (useAuthentication()) {
        client.getState().setCredentials(_realm, _host, new UsernamePasswordCredentials(_userName, _passwd));
    }/*from ww  w.j av a  2  s.  c om*/

    GetMethod get = new GetMethod(url.toExternalForm());
    get.setDoAuthentication(useAuthentication());
    client.executeMethod(get);
    return get;
}

From source file:com.tasktop.c2c.server.common.service.tests.http.MultiUserClientHttpRequestFactoryTest.java

@Test
public void testAuthCredentialsChanged() throws URISyntaxException, IOException {
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("foo", "123"));

    URI uri = new URI("http://localhost/test");

    ClientHttpRequest request = requestFactory.createRequest(uri, HttpMethod.GET);
    HttpClient client = getHttpClient(request);
    assertNotNull(client);//w w w.  j a  v a 2 s  .com
    client.getState().addCookie(new Cookie("test.com", "sessionid", "123"));
    assertEquals(1, client.getState().getCookies().length);

    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken("foo", "1234"));
    ClientHttpRequest request2 = requestFactory.createRequest(uri, HttpMethod.GET);
    HttpClient client2 = getHttpClient(request);

    assertSame(client, client2);
    assertEquals(0, client.getState().getCookies().length);
}

From source file:com.bbva.arq.devops.ae.mirrorgate.jenkins.plugin.utils.RestCall.java

public MirrorGateResponse makeRestCallPost(String url, String jsonString, String user, String password) {

    MirrorGateResponse response;/*from  w  w  w. j  a  v  a 2 s .  c o m*/
    PostMethod post = new PostMethod(url);
    try {
        HttpClient client = getHttpClient();

        if (user != null && password != null) {
            client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
            post.setDoAuthentication(true);
        }

        StringRequestEntity requestEntity = new StringRequestEntity(jsonString, "application/json", "UTF-8");
        post.setRequestEntity(requestEntity);
        int responseCode = client.executeMethod(post);
        String responseString = post.getResponseBodyAsStream() != null
                ? getResponseString(post.getResponseBodyAsStream())
                : "";
        response = new MirrorGateResponse(responseCode, responseString);
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, "MirrorGate: Error posting to MirrorGate", e);
        response = new MirrorGateResponse(HttpStatus.SC_BAD_REQUEST, "");
    } finally {
        post.releaseConnection();
    }
    return response;
}

From source file:com.liferay.portlet.rss.util.RSSWebCacheItem.java

public Object convert(String key) throws WebCacheException {
    SyndFeed feed = null;//from www . jav  a 2s.  c  o m

    try {

        // com.liferay.portal.kernel.util.HttpUtil will break the connection
        // if it spends more than 5 seconds looking up a location. However,
        // German umlauts do not get encoded correctly. This may be a bug
        // with commons-httpclient or with how FeedParser uses
        // java.io.Reader.

        // Use http://xml.newsisfree.com/feeds/29/629.xml and
        // http://test.domosoft.com/up/RSS to test if German umlauts show
        // up correctly.

        /*Reader reader = new StringReader(
           new String(HttpUtil.URLtoByteArray(_url)));
                
        channel = FeedParser.parse(builder, reader);*/

        HttpImpl httpImpl = (HttpImpl) HttpUtil.getHttp();

        HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(_url);

        HttpClient httpClient = httpImpl.getClient(hostConfiguration);

        httpImpl.proxifyState(httpClient.getState(), hostConfiguration);

        HttpClientParams httpClientParams = httpClient.getParams();

        httpClientParams.setConnectionManagerTimeout(PropsValues.RSS_CONNECTION_TIMEOUT);
        httpClientParams.setSoTimeout(PropsValues.RSS_CONNECTION_TIMEOUT);

        GetMethod getMethod = new GetMethod(_url);

        httpClient.executeMethod(hostConfiguration, getMethod);

        SyndFeedInput input = new SyndFeedInput();

        feed = input.build(new XmlReader(getMethod.getResponseBodyAsStream()));
    } catch (Exception e) {
        throw new WebCacheException(_url + " " + e.toString());
    }

    return feed;
}

From source file:com.bbva.arq.devops.ae.mirrorgate.jenkins.plugin.utils.RestCall.java

public MirrorGateResponse makeRestCallGet(String url, String user, String password) {
    MirrorGateResponse response;//w ww  . j  a  va 2 s .c o m
    GetMethod get = new GetMethod(url);
    try {
        HttpClient client = getHttpClient();

        if (user != null && password != null) {
            client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
            get.setDoAuthentication(true);
        }

        get.getParams().setContentCharset("UTF-8");
        int responseCode = client.executeMethod(get);
        String responseString = get.getResponseBodyAsStream() != null
                ? getResponseString(get.getResponseBodyAsStream())
                : "";
        response = new MirrorGateResponse(responseCode, responseString);
    } catch (HttpException e) {
        LOGGER.log(Level.WARNING, "Error connecting to MirrorGate", e);
        response = new MirrorGateResponse(HttpStatus.SC_BAD_REQUEST, "");
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, "Error connecting to MirrorGate", e);
        response = new MirrorGateResponse(HttpStatus.SC_BAD_REQUEST, "");
    } finally {
        get.releaseConnection();
    }
    return response;
}

From source file:com.yahoo.flowetl.services.http.BaseHttpGenerator.java

@Override
public Pair<HttpClient, HttpMethod> generate(HttpParams in) {
    HttpClient c = new HttpClient();
    c.getState().clear();
    c.getHttpConnectionManager().setParams(getManagerParams(in));
    c.setParams(getClientParams(in));//from  w w  w  . ja v a  2  s  .c  om
    HttpMethod toCall = null;
    if (in instanceof HttpService.PostHttpParams) {
        // post??
        toCall = getPostMethod(in);
    } else {
        // otherwise get (support more later??)
        toCall = getGetMethod(in);
    }
    applyCommonProperties(in, toCall);
    return new Pair<HttpClient, HttpMethod>(c, toCall);
}