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

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

Introduction

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

Prototype

AuthScope ANY

To view the source code for org.apache.commons.httpclient.auth AuthScope ANY.

Click Source Link

Usage

From source file:Correct.java

public static void main(String[] args) {

    String URLL = "";
    HttpClient client = new HttpClient();
    HttpMethod method = new PostMethod(URLL);
    method.setDoAuthentication(true);//from  w  w  w . j a v  a2  s.  c  o m
    HostConfiguration hostConfig = client.getHostConfiguration();
    hostConfig.setHost("172.29.38.8");
    hostConfig.setProxy("172.29.90.4", 8);
    //   NTCredentials proxyCredentials = new NTCredentials("", "", "", "");
    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", ""));
    ////        try {
    ////            URL url = new URL("");
    ////            HttpURLConnection urls = (HttpURLConnection) url.openConnection();
    ////            
    ////           
    ////        } catch (MalformedURLException ex) {
    ////            Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex);
    ////        } catch (IOException ex) {
    ////            Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex);
    ////        }
    try {
        // send the transaction
        client.executeMethod(hostConfig, method);
        StatusLine status = method.getStatusLine();

        if (status != null && method.getStatusCode() == 200) {

            System.out.println(method.getResponseBodyAsString() + "\n Status code : " + status);

        } else {

            System.err.println(method.getStatusLine() + "\n: Posting Failed !");
        }

    } catch (IOException ioe) {

        ioe.printStackTrace();

    }
    method.releaseConnection();

}

From source file:TrivialApp.java

public static void main(String[] args) {
    if ((args.length != 1) && (args.length != 3)) {
        printUsage();/*from w w w.  ja va 2  s  .co m*/
        System.exit(-1);
    }

    Credentials creds = null;
    if (args.length >= 3) {
        creds = new UsernamePasswordCredentials(args[1], args[2]);
    }

    //create a singular HttpClient object
    HttpClient client = new HttpClient();

    //establish a connection within 5 seconds
    client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

    //set the default credentials
    if (creds != null) {
        client.getState().setCredentials(AuthScope.ANY, creds);
    }

    String url = args[0];
    HttpMethod method = null;

    //create a method object
    method = new GetMethod(url);
    method.setFollowRedirects(true);
    //} catch (MalformedURLException murle) {
    //    System.out.println("<url> argument '" + url
    //            + "' is not a valid URL");
    //    System.exit(-2);
    //}

    //execute the method
    String responseBody = null;
    try {
        client.executeMethod(method);
        responseBody = method.getResponseBodyAsString();
    } catch (HttpException he) {
        System.err.println("Http error connecting to '" + url + "'");
        System.err.println(he.getMessage());
        System.exit(-4);
    } catch (IOException ioe) {
        System.err.println("Unable to connect to '" + url + "'");
        System.exit(-3);
    }

    //write out the request headers
    System.out.println("*** Request ***");
    System.out.println("Request Path: " + method.getPath());
    System.out.println("Request Query: " + method.getQueryString());
    Header[] requestHeaders = method.getRequestHeaders();
    for (int i = 0; i < requestHeaders.length; i++) {
        System.out.print(requestHeaders[i]);
    }

    //write out the response headers
    System.out.println("*** Response ***");
    System.out.println("Status Line: " + method.getStatusLine());
    Header[] responseHeaders = method.getResponseHeaders();
    for (int i = 0; i < responseHeaders.length; i++) {
        System.out.print(responseHeaders[i]);
    }

    //write out the response body
    System.out.println("*** Response Body ***");
    System.out.println(responseBody);

    //clean up the connection resources
    method.releaseConnection();

    System.exit(0);
}

From source file:com.bbytes.zorba.messaging.rabbitmq.utils.HttpClientFactoryBean.java

public HttpClientFactoryBean(HttpClient httpClient, Credentials credentials) {
    this.httpClient = httpClient;
    httpClient.getState().setCredentials(AuthScope.ANY, credentials);
}

From source file:com.cloudbees.api.HttpClientHelper.java

public static HttpClient createClient(BeesClientConfiguration beesClientConfiguration) {
    HttpClient client = new HttpClient();
    String proxyHost = beesClientConfiguration.getProxyHost();
    if (proxyHost != null) {
        int proxyPort = beesClientConfiguration.getProxyPort();

        client.getHostConfiguration().setProxy(proxyHost, proxyPort);

        //if there are proxy credentials available, set those too
        Credentials proxyCredentials = null;
        String proxyUser = beesClientConfiguration.getProxyUser();
        String proxyPassword = beesClientConfiguration.getProxyPassword();
        if (proxyUser != null || proxyPassword != null)
            proxyCredentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
        if (proxyCredentials != null)
            client.getState().setProxyCredentials(AuthScope.ANY, proxyCredentials);
    }//from  w  w  w  . j a  va2 s  .  co  m

    return client;
}

From source file:au.org.ala.spatial.util.UploadSpatialResource.java

public static String loadResource(String url, String extra, String username, String password,
        String resourcepath) {/*from w w  w. j ava  2 s  . c  o  m*/
    String output = "";

    HttpClient client = new HttpClient();
    client.setConnectionTimeout(10000);
    client.setTimeout(60000);

    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

    File input = new File(resourcepath);

    PutMethod put = new PutMethod(url);
    put.setDoAuthentication(true);

    //put.addRequestHeader("Content-type", "application/zip");

    // Request content will be retrieved directly 
    // from the input stream 
    RequestEntity entity = new FileRequestEntity(input, "application/zip");
    put.setRequestEntity(entity);

    // Execute the request 
    try {
        int result = client.executeMethod(put);

        output = result + ": " + put.getResponseBodyAsString();

    } catch (Exception e) {
        e.printStackTrace(System.out);
        output = "0: " + e.getMessage();
    } finally {
        // Release current connection to the connection pool once you are done 
        put.releaseConnection();
    }

    return output;

}

From source file:net.sf.ufsc.http.HttpSession.java

/**
 * @param uri/*from ww w.j  a  v  a 2s .com*/
 */
public HttpSession(URI uri) {
    super(uri);

    if (uri.getUserInfo() != null) {
        UserInfo userInfo = new UserInfo(uri);

        this.client.getState().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(userInfo.getUser(), userInfo.getPassword()));
    }
}

From source file:com.tw.go.plugin.material.artifactrepository.yum.exec.HttpConnectionChecker.java

public void checkConnection(String url, Credentials credentials) {
    HttpClient client = getHttpClient();
    if (credentials.isComplete()) {
        org.apache.commons.httpclient.Credentials usernamePasswordCredentials = new UsernamePasswordCredentials(
                credentials.getUser(), credentials.getPassword());
        client.getState().setCredentials(AuthScope.ANY, usernamePasswordCredentials);
    }//from w  w w .  j  a v  a  2  s .  c om
    GetMethod method = getGetMethod(url);
    method.setFollowRedirects(false);
    try {
        int returnCode = client.executeMethod(method);
        if (returnCode != HttpStatus.SC_OK) {
            throw new RuntimeException(method.getStatusLine().toString());
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

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;// ww w.j a v a 2 s  . c om
    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.acc.test.UserWebServiceTest.java

@Before
public void before() {
    LOG.setLevel(Level.DEBUG);//from  ww w.j ava  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.gs.jrpip.client.AuthenticatedUrl.java

public void setCredentialsOnClient(HttpClient client) {
    if (this.credentials != null) {
        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(AuthScope.ANY, this.credentials);
    }/*from  w  ww . j  a  v  a2s . c o  m*/

    if (this.cookies != null && this.cookies.length > 0) {
        client.getState().addCookies(this.cookies);
    }
}