Example usage for org.apache.http.auth UsernamePasswordCredentials toString

List of usage examples for org.apache.http.auth UsernamePasswordCredentials toString

Introduction

In this page you can find the example usage for org.apache.http.auth UsernamePasswordCredentials toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:com.serena.rlc.provider.schedule.client.ScheduleWaiter.java

@Override
public void run() {
    logger.debug("ScheduleWaiter is sleeping for " + waitTime + " milliseconds, until " + endDate);
    try {/*w w w .  j ava2s. co m*/
        Thread.sleep(waitTime);
    } catch (InterruptedException ex) {
        logger.error("ScheduleWaiter was interrupted: " + ex.getLocalizedMessage());
    } catch (CancellationException ex) {
        logger.error("ScheduleWaiter thread has been cancelled: ", ex.getLocalizedMessage());
    }
    synchronized (executionId) {
        logger.debug("ScheduleWaiter has finished sleeping at " + new Date());

        try {
            String uri = callbackUrl + executionId + "/COMPLETED";
            logger.info("Start executing RLC PUT request to url=\"{}\"", uri);
            DefaultHttpClient rlcParams = new DefaultHttpClient();
            HttpPut put = new HttpPut(uri);
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(callbackUsername,
                    callbackPassword);
            put.addHeader(BasicScheme.authenticate(credentials, "US-ASCII", false));
            //put.addHeader("Content-Type", "application/x-www-form-urlencoded");
            //put.addHeader("Accept", "application/json");
            logger.info(credentials.toString());
            HttpResponse response = rlcParams.execute(put);
            if (response.getStatusLine().getStatusCode() != 200) {
                logger.error("HTTP Status Code: " + response.getStatusLine().getStatusCode());
            }
        } catch (IOException ex) {
            logger.error(ex.getLocalizedMessage());
        }

        executionId.notify();
    }

}

From source file:com.serena.rlc.provider.servicenow.client.ApprovalWaiter.java

synchronized void notifyRLC(String status) {
    try {//from w  ww  .  j  av a2 s .  co  m
        String uri = callbackUrl + executionId + "/" + status;
        logger.debug("Start executing RLC PUT request to url=\"{}\"", uri);
        DefaultHttpClient rlcParams = new DefaultHttpClient();
        HttpPut put = new HttpPut(uri);
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(callbackUsername,
                callbackPassword);
        put.addHeader(BasicScheme.authenticate(credentials, "US-ASCII", false));
        //put.addHeader("Content-Type", "application/x-www-form-urlencoded");
        //put.addHeader("Accept", "application/json");
        logger.info(credentials.toString());
        HttpResponse response = rlcParams.execute(put);
        if (response.getStatusLine().getStatusCode() != 200) {
            logger.error("HTTP Status Code: " + response.getStatusLine().getStatusCode());
        }
    } catch (IOException ex) {
        logger.error(ex.getLocalizedMessage());
    }
}

From source file:org.ocsinventoryng.android.actions.OCSProtocol.java

public String sendmethod(File pFile, String server, boolean gziped) throws OCSProtocolException {
    OCSLog ocslog = OCSLog.getInstance();
    OCSSettings ocssettings = OCSSettings.getInstance();
    ocslog.debug("Start send method");
    String retour;//w w  w.  jav a2  s  . c  om

    HttpPost httppost;

    try {
        httppost = new HttpPost(server);
    } catch (IllegalArgumentException e) {
        ocslog.error(e.getMessage());
        throw new OCSProtocolException("Incorect serveur URL");
    }

    File fileToPost;
    if (gziped) {
        ocslog.debug("Start compression");
        fileToPost = ocsfile.getGzipedFile(pFile);
        if (fileToPost == null) {
            throw new OCSProtocolException("Error during temp file creation");
        }
        ocslog.debug("Compression done");
    } else {
        fileToPost = pFile;
    }

    FileEntity fileEntity = new FileEntity(fileToPost, "text/plain; charset=\"UTF-8\"");
    httppost.setEntity(fileEntity);
    httppost.setHeader("User-Agent", http_agent);
    if (gziped) {
        httppost.setHeader("Content-Encoding", "gzip");
    }

    DefaultHttpClient httpClient = getNewHttpClient(OCSSettings.getInstance().isSSLStrict());

    if (ocssettings.isProxy()) {
        ocslog.debug("Use proxy : " + ocssettings.getProxyAdr());
        HttpHost proxy = new HttpHost(ocssettings.getProxyAdr(), ocssettings.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    if (ocssettings.isAuth()) {
        ocslog.debug("Use AUTH : " + ocssettings.getLogin() + "/*****");
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(ocssettings.getLogin(),
                ocssettings.getPasswd());
        ocslog.debug(creds.toString());
        httpClient.getCredentialsProvider()
                .setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds);
    }

    ocslog.debug("Call : " + server);
    HttpResponse localHttpResponse;
    try {
        localHttpResponse = httpClient.execute(httppost);
        ocslog.debug("Message sent");
    } catch (ClientProtocolException e) {
        ocslog.error("ClientProtocolException" + e.getMessage());
        throw new OCSProtocolException(e.getMessage());
    } catch (IOException e) {
        String msg = appCtx.getString(R.string.err_cant_connect) + " " + e.getMessage();
        ocslog.error(msg);
        throw new OCSProtocolException(msg);
    }

    try {
        int httpCode = localHttpResponse.getStatusLine().getStatusCode();
        ocslog.debug("Response status code : " + String.valueOf(httpCode));
        if (httpCode == 200) {
            if (gziped) {
                InputStream is = localHttpResponse.getEntity().getContent();
                GZIPInputStream gzis = new GZIPInputStream(is);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buff = new byte[128];
                int n;
                while ((n = gzis.read(buff, 0, buff.length)) > 0) {
                    baos.write(buff, 0, n);
                }
                retour = baos.toString();
            } else {
                retour = EntityUtils.toString(localHttpResponse.getEntity());
            }
        } else if (httpCode == 400) {
            throw new OCSProtocolException("Error http 400 may be wrong agent version");
        } else {
            ocslog.error("***Server communication error: ");
            throw new OCSProtocolException("Http communication error code " + String.valueOf(httpCode));
        }
        ocslog.debug("Finnish send method");
    } catch (IOException localIOException) {
        String msg = localIOException.getMessage();
        ocslog.error(msg);
        throw new OCSProtocolException(msg);
    }
    return retour;
}