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(String paramString, int paramInt) 

Source Link

Usage

From source file:BasicAuthenticationForJSPPage.java

public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");

    HostConfiguration host = client.getHostConfiguration();
    host.setHost(new URI("http://localhost:8080", true));

    Credentials credentials = new UsernamePasswordCredentials("tomcat", "tomcat");
    AuthScope authScope = new AuthScope(host.getHost(), host.getPort());
    HttpState state = client.getState();
    state.setCredentials(authScope, credentials);

    GetMethod method = new GetMethod("/commons/chapter01/protected.jsp");
    try {//from  w  w  w  .  ja  v  a2 s.c o  m
        client.executeMethod(host, method);
        System.err.println(method.getStatusLine());
        System.err.println(method.getResponseBodyAsString());
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:at.gv.egovernment.moa.id.commons.utils.HttpClientWithProxySupport.java

public static HttpClient getHttpClient() {
    HttpClient client = new HttpClient();

    String host = System.getProperty("http.proxyHost"); //$NON-NLS-1$
    String port = System.getProperty("http.proxyPort"); //$NON-NLS-1$
    if (MiscUtil.isNotEmpty(host) && MiscUtil.isNotEmpty(port)) {
        int p = Integer.parseInt(port);
        client.getHostConfiguration().setProxy(host, p);
        Logger.info("Initial HTTPClient with proxy usage. " + "ProxyHost=" + host + " ProxyPort=" + port);

        String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$
        String pass = System.getProperty("http.proxyPassword"); //$NON-NLS-1$
        if (MiscUtil.isNotEmpty(user) && pass != null) {
            client.getState().setProxyCredentials(new AuthScope(host, p),
                    new UsernamePasswordCredentials(user, pass));

        }/*from   w w  w.j ava 2 s. com*/
    }
    return client;
}

From source file:eu.impact_project.iif.t2.client.Helper.java

/**
 * Creates an http client that uses basic authentication. The credentials
 * will be sent preemptively with each request.
 * //from  www .ja va 2  s .c o  m
 * @param domain
 *            Authentication domain
 * @param user
 *            User name for basic authentication
 * @param password
 *            Password for basic authentication
 * @return Created client object
 */
public static HttpClient createAuthenticatingClient(String domain, String user, String password) {
    HttpClient client = new HttpClient();

    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(new AuthScope(domain, -1),
            new UsernamePasswordCredentials(user, password));
    return client;
}

From source file:com.zimbra.common.httpclient.HttpClientUtil.java

public static int executeMethod(HttpClient client, HttpMethod method, HttpState state)
        throws HttpException, IOException {
    ProxyHostConfiguration proxyConfig = HttpProxyConfig.getProxyConfig(client.getHostConfiguration(),
            method.getURI().toString());
    if (proxyConfig != null && proxyConfig.getUsername() != null && proxyConfig.getPassword() != null) {
        if (state == null) {
            state = client.getState();/*from  www  .  j a  v  a 2 s  .  c  o  m*/
            if (state == null) {
                state = new HttpState();
            }
        }
        state.setProxyCredentials(new AuthScope(proxyConfig.getHost(), proxyConfig.getPort()),
                new UsernamePasswordCredentials(proxyConfig.getUsername(), proxyConfig.getPassword()));
    }
    return client.executeMethod(proxyConfig, method, state);
}

From source file:com.jackbe.mapreduce.RESTClient.java

public RESTClient() {
    client = new HttpClient();
    client.getState().setCredentials(new AuthScope(host, Integer.parseInt(port)),
            new UsernamePasswordCredentials(user, password));

    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
    client.setParams(params);//  ww  w. j ava2 s  . c  om
    client.getParams().setAuthenticationPreemptive(true);
    //throw new RuntimeException("TRYING TO CREATE EMMLRestRunner. BAD!");
}

From source file:eu.impact_project.iif.t2.client.InfoGenerator.java

/**
 * Gets information about a workflow./*w ww  .jav  a  2s.c  o m*/
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    HttpSession session = request.getSession(true);
    String wfId = request.getParameter("id");
    String urlString = "http://www.myexperiment.org/workflow.xml?id=" + wfId;

    String user = (String) session.getAttribute("user");
    String password = (String) session.getAttribute("password");

    // prepare httpclient for basic authentication
    HttpClient client = new HttpClient();
    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(new AuthScope("www.myexperiment.org", 80),
            new UsernamePasswordCredentials(user, password));

    GetMethod get = new GetMethod(urlString);
    get.setDoAuthentication(true);

    client.executeMethod(get);
    InputStream responseBody = get.getResponseBodyAsStream();

    try {

        SAXBuilder builder = new SAXBuilder();

        Document doc = builder.build(responseBody);

        WorkflowDetails details = new WorkflowDetails();

        Element root = doc.getRootElement();
        details.setTitle(root.getChild("title").getTextTrim());
        details.setDescription(root.getChild("description").getTextTrim());
        details.setImageUrl(root.getChild("preview").getTextTrim());

        session.setAttribute("wfDetails", details);

    } catch (JDOMException e) {
        e.printStackTrace();
    }

    RequestDispatcher rd = getServletContext().getRequestDispatcher("/info.jsp");
    rd.forward(request, response);
}

From source file:com.collabnet.svnedge.net.CommonsHTTPProxySender.java

@Override
protected HostConfiguration getHostConfiguration(HttpClient client, MessageContext context, URL url) {
    client.getHostConfiguration().setProxy(proxyHost, proxyPort);
    if (this.proxyUser != null) {
        client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort),
                new UsernamePasswordCredentials(proxyUser, proxyPassword));
    }//from  w w  w. j ava 2  s .c  o m

    setupHttpClient(client, url.toString());
    // This needs to be set to 1.0 otherwise errors
    client.getHostConfiguration().getParams().setParameter(HttpClientParams.PROTOCOL_VERSION,
            HttpVersion.HTTP_1_0);
    httpClient = client;
    return client.getHostConfiguration();
}

From source file:com.antelink.sourcesquare.query.RestClient.java

protected RestTemplate getTemplate(String baseDomain) {
    HttpClient client = new HttpClient();

    // Managing HTTP proxy - if any
    String proxyHost = System.getProperty("http.proxyHost");
    String proxyPort = System.getProperty("http.proxyPort");
    if (proxyHost != null && proxyPort != null) {
        client.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort));
    }/*from  www. ja va  2s.  com*/

    // Managing HTTP proxy authentication - if any
    String proxyUser = System.getProperty("http.proxyUser");
    String proxyPassword = System.getProperty("http.proxyPassword");
    AuthScope auth;
    if (proxyHost != null && proxyUser != null && proxyPassword != null) {
        auth = new AuthScope(proxyHost, Integer.parseInt(proxyPort));
        client.getState().setProxyCredentials(auth, new UsernamePasswordCredentials(proxyUser, proxyPassword));
    } else {
        auth = new AuthScope(baseDomain, AuthScope.ANY_PORT);
        client.getState().setCredentials(auth, null);
    }

    CommonsClientHttpRequestFactory commons = new CommonsClientHttpRequestFactory(client) {
        @Override
        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            ClientHttpRequest createRequest = super.createRequest(uri, httpMethod);
            createRequest.getHeaders().add("User-Agent", "SourceSquare");
            return createRequest;
        }
    };
    return new RestTemplate(commons);
}

From source file:com.jackbe.mapreduce.EMMLRestRunner.java

public EMMLRestRunner() {
    client = new HttpClient();
    client.getState().setCredentials(new AuthScope(host, Integer.parseInt(port)),
            new UsernamePasswordCredentials(user, password));

    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
    client.setParams(params);//  w w w  . jav  a  2  s.co m
    client.getParams().setAuthenticationPreemptive(true);
    throw new RuntimeException("TRYING TO CREATE EMMLRestRunner. BAD!");
}

From source file:com.example.listsync.WebDavRepository.java

private void init() {
    client = new HttpClient();
    client.getState().setCredentials(new AuthScope(config.getAdress(), config.getPort()),
            new UsernamePasswordCredentials(config.getUsername(), config.getPassword()));
}