Example usage for org.apache.http.impl.auth BasicScheme authenticate

List of usage examples for org.apache.http.impl.auth BasicScheme authenticate

Introduction

In this page you can find the example usage for org.apache.http.impl.auth BasicScheme authenticate.

Prototype

@Deprecated
public static Header authenticate(final Credentials credentials, final String charset, final boolean proxy) 

Source Link

Document

Returns a basic Authorization header value for the given Credentials and charset.

Usage

From source file:org.openntf.xpt.agents.master.PasswordService.java

public ExecutionUserProperties checkPassword(String strUser, String strPW, String strURL) {
    ExecutionUserProperties eupRC = new ExecutionUserProperties();
    eupRC.setLoggedIn(false);/*from  ww  w . jav a  2s .  com*/
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();

        httpClient = (DefaultHttpClient) ClientSSLResistanceExtender.wrapClient(httpClient);
        httpClient.setRedirectStrategy(new DefaultRedirectStrategy());
        /*
         * String strNSFURL = strURL; String strRedirection = strNSFURL +
         * "/xsp/xpage.agent?loginCheck"; java.util.List<NameValuePair>
         * formparams = new ArrayList<NameValuePair>(); formparams.add(new
         * BasicNameValuePair("username", strUser)); formparams.add(new
         * BasicNameValuePair("password", strPW)); formparams.add(new
         * BasicNameValuePair("redirectto", strRedirection));
         * UrlEncodedFormEntity entity = new
         * UrlEncodedFormEntity(formparams, "UTF-8");
         * 
         * HttpPost postRequest = new HttpPost(strNSFURL + "?login");
         * postRequest.getParams().setParameter(ClientPNames.COOKIE_POLICY,
         * org
         * .apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY);
         * 
         * postRequest.setHeader("Content-Type",
         * "application/x-www-form-urlencoded");
         * postRequest.addHeader("accept", "application/json");
         * postRequest.setEntity(entity); HttpResponse hsr =
         * httpClient.execute(postRequest); for (Cookie ck :
         * httpClient.getCookieStore().getCookies()) { if
         * ("LtpaToken".equalsIgnoreCase(ck.getName())) { blRC = true; } if
         * ("DomAuthSessId".equalsIgnoreCase(ck.getName())) { blRC = true; }
         * }
         */
        String strNSFURL = strURL;
        String strRedirection = strNSFURL + "/xsp/xpage.agent?action=checkLogin";
        HttpGet getRequestINIT = new HttpGet(strNSFURL);

        HttpGet getRequest = new HttpGet(strRedirection);
        getRequest.addHeader(
                BasicScheme.authenticate(new UsernamePasswordCredentials(strUser, strPW), "UTF-8", false));
        getRequestINIT.addHeader(
                BasicScheme.authenticate(new UsernamePasswordCredentials(strUser, strPW), "UTF-8", false));
        HttpResponse hsrINTI = httpClient.execute(getRequestINIT);
        if (hsrINTI.getStatusLine().getStatusCode() == 200) {
            EntityUtils.consume(hsrINTI.getEntity());
            HttpResponse hsr = httpClient.execute(getRequest);
            JsonJavaObject json = (JsonJavaObject) JsonParser.fromJson(JsonJavaFactory.instanceEx,
                    EntityUtils.toString(hsr.getEntity()));

            if (json.getString("status").equalsIgnoreCase("ok")) {
                eupRC.setLoggedIn(true);
                eupRC.setUserName(json.getString("username"));
                eupRC.setAccessLevel(json.getInt("level"));
            } else {
                eupRC.setLoggedIn(false);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return eupRC;
}

From source file:uk.org.openeyes.diagnostics.HttpTransfer.java

/**
 * /*from  w  w  w  .ja  v  a2  s  .c  o m*/
 * @param resourceType
 * @param data
 * @param username
 * @param password
 * @return
 * @throws ConnectException 
 */
public int send(String resourceType, String data, String username, String password) throws ConnectException {
    int result = -1;
    String strURL = "http://" + host + ":" + port + "/api/" + resourceType + "?_format=xml&resource_type="
            + resourceType;
    HttpPost post = new HttpPost(strURL);
    try {
        data = data.replace("fhir:", "");
        StringEntity entity = new StringEntity(data);
        post.setEntity(entity);
        post.addHeader("Content-type", "text/xml");
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
        post.addHeader(BasicScheme.authenticate(creds, "US-ASCII", false));
        DefaultHttpClient httpclient = new DefaultHttpClient();

        CloseableHttpResponse httpResponse = httpclient.execute(post);
        result = httpResponse.getStatusLine().getStatusCode();
        HttpEntity entity2 = httpResponse.getEntity();

        StringWriter writer = new StringWriter();
        IOUtils.copy(entity2.getContent(), writer);
        this.response = writer.toString();
        EntityUtils.consume(entity2);
        Header[] headers = post.getHeaders("Location");
        if (headers.length > 0) {
            this.location = headers[0];
        }

    } catch (ConnectException e) {
        // TODO - binary exponential backoff algorithm  
        throw e;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        post.releaseConnection();
    }
    return result;
}

From source file:uk.org.openeyes.diagnostics.HttpTransfer.java

/**
 * // w  w w . j  a v  a 2  s .c o  m
 * @param resourceType
 * @param jsonType
 * @param requestParams
 * @param username
 * @param password
 * @return
 * @throws ConnectException 
 */
public int read(String resourceType, String jsonType, String requestParams, String username, String password)
        throws ConnectException {
    DefaultHttpClient http = new DefaultHttpClient();

    int result = -1;
    String strURL = "http://" + host + ":" + port + "/api/" + resourceType
            + "?resource_type=Patient&_format=xml";
    if (requestParams != null) {
        strURL += "&" + requestParams;
    }
    HttpGet get = new HttpGet(strURL);
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
    get.addHeader(BasicScheme.authenticate(creds, "US-ASCII", false));

    try {
        get.addHeader("Content-type", "text/xml");
        HttpClientBuilder builder = HttpClientBuilder.create();
        CloseableHttpClient httpclient = builder.build();

        CloseableHttpResponse httpResponse = httpclient.execute(get);
        result = httpResponse.getStatusLine().getStatusCode();
        HttpEntity entity2 = httpResponse.getEntity();
        StringWriter writer = new StringWriter();
        IOUtils.copy(entity2.getContent(), writer);
        this.response = writer.toString();
        EntityUtils.consume(entity2);
    } catch (ConnectException e) {
        // this happens when there's no server to connect to
        e.printStackTrace();
        throw e;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        get.releaseConnection();
    }
    return result;
}