Example usage for org.apache.commons.httpclient.auth AuthScope AuthScope

List of usage examples for org.apache.commons.httpclient.auth AuthScope AuthScope

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.auth AuthScope AuthScope.

Prototype

public AuthScope(AuthScope paramAuthScope) 

Source Link

Usage

From source file:com.acc.test.UserWebServiceTest.java

@Before
public void before() {
    LOG.setLevel(Level.DEBUG);/* w  ww  .j  a  v a 2s  .  c  om*/
    final HttpClient client = new HttpClient();
    final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(TestConstants.USERNAME,
            TestConstants.PASSWORD);
    client.getState().setCredentials(new AuthScope(AuthScope.ANY), credentials);

    final CommonsClientHttpRequestFactory commons = new CommonsClientHttpRequestFactory(client);

    template = new RestTemplate(commons);
}

From source file:com.acc.test.ProductWebServiceTest.java

@Before
public void before() {
    LOG.setLevel(Level.DEBUG);/*ww w  .  ja v a2  s . c  o m*/
    final HttpClient client = new HttpClient();
    final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(USER, PASSWD);
    client.getState().setCredentials(new AuthScope(AuthScope.ANY), credentials);

    final CommonsClientHttpRequestFactory commons = new CommonsClientHttpRequestFactory(client);

    final HttpMessageConverter<?> jsonConverter = new org.springframework.http.converter.xml.MarshallingHttpMessageConverter(
            new org.springframework.oxm.xstream.XStreamMarshaller());
    ((org.springframework.http.converter.xml.MarshallingHttpMessageConverter) jsonConverter)
            .setSupportedMediaTypes(Arrays.asList(org.springframework.http.MediaType.APPLICATION_JSON));

    final HttpMessageConverter<?> xmlConverter = new org.springframework.http.converter.xml.MarshallingHttpMessageConverter(
            new org.springframework.oxm.xstream.XStreamMarshaller());
    ((org.springframework.http.converter.xml.MarshallingHttpMessageConverter) jsonConverter)
            .setSupportedMediaTypes(Arrays.asList(org.springframework.http.MediaType.APPLICATION_XML));

    converters = new ArrayList<HttpMessageConverter<?>>();
    converters.add(jsonConverter);
    converters.add(xmlConverter);

    template = new RestTemplate(commons);
    template.setMessageConverters(converters);

}

From source file:com.exalead.io.failover.FailoverHttpClient.java

public int executeMethod(HttpMethod method, int timeout, int retries) throws HttpException, IOException {
    if (manager.hosts.size() == 0) {
        logger.error("Could not execute method without any host.");
        throw new HttpException("Trying to execute methods without host");
    }//from  w w  w .  j a v  a2 s  .c  om
    // Fake config, the underlying manager manages all
    HostConfiguration config = new HostConfiguration();

    /* Set method parameters */
    method.getParams().setSoTimeout(timeout);
    /* DO NOT retry magically the method */
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(0, false));

    if (manager.getCredentials() != null) {
        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(new AuthScope(AuthScope.ANY), manager.getCredentials());
    }

    IOException fail = null;
    for (int i = 1; i <= retries; ++i) {
        try {
            return client.executeMethod(config, method);
        } catch (IOException e) {
            logger.warn("Failed to execute method - try " + i + "/" + retries);
            fail = e;
            continue;
        }
    }
    logger.warn("exception in executeMethod: " + fail.getMessage());
    throw fail;
}

From source file:com.twinsoft.convertigo.engine.ProxyManager.java

public void setAnonymAuth(HttpState httpState) {
    // Setting anonym authentication for proxy
    if ((!this.proxyServer.equals("")) && (!this.proxyUser.equals(""))) {
        httpState.setProxyCredentials(new AuthScope(AuthScope.ANY), new Credentials() {
        });//from  w ww. j a  v a  2s.  com

        Engine.logProxyManager.debug("(ProxyManager) Proxy credentials: anonym");
    }
}

From source file:org.apache.servicemix.http.security.HttpSecurityTest.java

protected void testAuthenticate(final String username, final String password) throws Exception {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(new AuthScope(AuthScope.ANY),
            new UsernamePasswordCredentials(username, password));

    PostMethod method = new PostMethod("http://localhost:8192/Service/");
    try {/*from   w  ww.  j  a  va 2 s.co  m*/
        method.setDoAuthentication(true);
        method.setRequestEntity(new StringRequestEntity("<hello>world</hello>"));
        int state = client.executeMethod(method);
        FileUtil.copyInputStream(method.getResponseBodyAsStream(), System.err);
        if (state != HttpServletResponse.SC_OK && state != HttpServletResponse.SC_ACCEPTED) {
            throw new IllegalStateException("Http status: " + state);
        }
    } finally {
        method.releaseConnection();
    }
}

From source file:org.eclipse.smila.connectivity.framework.crawler.web.http.Http.java

/**
 * Loads HTTP client configuration for this web site.
 *//*  ww  w  . j  ava  2 s .  c o m*/
private void configureClient() {
    final HttpConnectionManagerParams params = s_connectionManager.getParams();
    if (_timeout != 0) {
        params.setConnectionTimeout(_timeout);
        params.setSoTimeout(_timeout);
    } else {
        params.setConnectionTimeout(_connectTimeout);
        params.setSoTimeout(_readTimeout);
    }
    params.setSendBufferSize(BUFFER_SIZE);
    params.setReceiveBufferSize(BUFFER_SIZE);
    final HostConfiguration hostConf = s_client.getHostConfiguration();
    final List<Header> headers = new ArrayList<Header>();
    // prefer English
    headers.add(new Header("Accept-Language", "en-us,en-gb,en;q=0.7,*;q=0.3"));
    // prefer UTF-8
    headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7"));
    // prefer understandable formats
    headers.add(new Header("Accept",
            "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8"));
    // accept GZIP content
    headers.add(new Header("Accept-Encoding", "x-gzip, gzip"));
    final String[] webSiteHeaders = getConf().get(HttpProperties.HEADERS).split(SEMICOLON);
    for (String header : webSiteHeaders) {
        final String[] headerInformation = header.split(COLON);
        if (headerInformation.length > 2) {
            headers.add(new Header(headerInformation[0].trim(), headerInformation[1].trim()));
        }
    }
    hostConf.getParams().setParameter("http.default-headers", headers);
    if (_useProxy) {
        hostConf.setProxy(_proxyHost, _proxyPort);
        if (_proxyLogin.length() > 0) {
            final Credentials proxyCreds = new UsernamePasswordCredentials(_proxyLogin, _proxyPassword);
            s_client.getState().setProxyCredentials(new AuthScope(AuthScope.ANY), proxyCreds);
        }
    }
    final List<Rfc2617Authentication> httpAuthentications = _authentication.getRfc2617Authentications();

    for (Rfc2617Authentication auth : httpAuthentications) {
        s_client.getState().setCredentials(
                new AuthScope(auth.getHost(), Integer.valueOf(auth.getPort()), auth.getRealm()),
                new UsernamePasswordCredentials(auth.getLogin(), auth.getPassword()));
    }

    final SslCertificateAuthentication sslAuth = _authentication.getSslCertificateAuthentication();
    if (sslAuth != null) {
        try {
            final URL truststoreURL = new File(sslAuth.getTruststoreUrl()).toURL();
            final URL keystoreURL = new File(sslAuth.getKeystoreUrl()).toURL();

            final ProtocolSocketFactory sslFactory = new AuthSSLProtocolSocketFactory(keystoreURL,
                    sslAuth.getKeystorePassword(), truststoreURL, sslAuth.getTruststorePassword());
            _https = new Protocol(sslAuth.getProtocolName(), sslFactory, Integer.valueOf(sslAuth.getPort()));
            Protocol.registerProtocol(sslAuth.getProtocolName(), _https);
        } catch (MalformedURLException exception) {
            LOG.error("unable to bind https protocol" + exception.toString());
        }
    }
}