Example usage for org.apache.http.message BasicHttpRequest addHeader

List of usage examples for org.apache.http.message BasicHttpRequest addHeader

Introduction

In this page you can find the example usage for org.apache.http.message BasicHttpRequest addHeader.

Prototype

public void addHeader(String str, String str2) 

Source Link

Usage

From source file:gr.wavesoft.webng.io.web.WebStreams.java

public static HttpResponse httpGET(URL url, HashMap<String, String> headers) throws IOException {
    try {// ww  w  .j  a  va  2s  .c o  m

        // WebRequest connection
        ClientConnectionRequest connRequest = connectionManager.requestConnection(
                new HttpRoute(new HttpHost(url.getHost(), url.getPort(), url.getProtocol())), null);

        ManagedClientConnection conn = connRequest.getConnection(10, TimeUnit.SECONDS);
        try {

            // Prepare request
            BasicHttpRequest request = new BasicHttpRequest("GET", url.getPath());

            // Setup headers
            if (headers != null) {
                for (String k : headers.keySet()) {
                    request.addHeader(k, headers.get(k));
                }
            }

            // Send request
            conn.sendRequestHeader(request);

            // Fetch response
            HttpResponse response = conn.receiveResponseHeader();
            conn.receiveResponseEntity(response);

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                BasicManagedEntity managedEntity = new BasicManagedEntity(entity, conn, true);
                // Replace entity
                response.setEntity(managedEntity);
            }

            // Do something useful with the response
            // The connection will be released automatically 
            // as soon as the response content has been consumed
            return response;

        } catch (IOException ex) {
            // Abort connection upon an I/O error.
            conn.abortConnection();
            throw ex;
        }

    } catch (HttpException ex) {
        throw new IOException("HTTP Exception occured", ex);
    } catch (InterruptedException ex) {
        throw new IOException("InterruptedException", ex);
    } catch (ConnectionPoolTimeoutException ex) {
        throw new IOException("ConnectionPoolTimeoutException", ex);
    }

}

From source file:io.uploader.drive.drive.largefile.DriveResumableUpload.java

public String getFileId() throws IOException {
    logger.info("Querying file id of completed upload...");
    CloseableHttpClient httpclient = null;
    CloseableHttpResponse response = null;
    try {//from   w ww  . ja va  2s  .  c  om
        httpclient = getHttpClient();
        BasicHttpRequest httpreq = new BasicHttpRequest("PUT", location);
        httpreq.addHeader("Authorization", auth.getAuthHeader());
        httpreq.addHeader("Content-Length", "0");
        httpreq.addHeader("Content-Range", "bytes */" + getFileSizeString());
        response = httpclient.execute(URIUtils.extractHost(uri), httpreq);
        BufferedHttpEntity entity = new BufferedHttpEntity(response.getEntity());
        EntityUtils.consume(response.getEntity());
        String retSrc = EntityUtils.toString(entity);
        if (useOldApi) {
            // Old API will return XML!
            JSONObject result = XML.toJSONObject(retSrc);
            return result.getJSONObject("entry").getString("gd:resourceId").replace("file:", "");
        } else {
            JSONObject result = new JSONObject(retSrc);
            return result.getString("id");
        }
    } finally {
        if (response != null) {
            response.close();
        }
        if (httpclient != null) {
            httpclient.close();
        }
    }
}

From source file:io.uploader.drive.drive.largefile.DriveResumableUpload.java

public long getCurrentByte() throws IOException {
    logger.info("Querying status of resumable upload...");

    CloseableHttpClient httpclient = null;
    CloseableHttpResponse response = null;
    long lastbyte = -1;
    try {//w  ww . j  av  a2 s.c  o  m
        httpclient = getHttpClient();
        BasicHttpRequest httpreq = new BasicHttpRequest("PUT", location);
        httpreq.addHeader("Authorization", auth.getAuthHeader());
        httpreq.addHeader("Content-Length", "0");
        httpreq.addHeader("Content-Range", "bytes */" + getFileSizeString());
        //logger.info(httpreq.toString());
        response = httpclient.execute(URIUtils.extractHost(uri), httpreq);
        @SuppressWarnings("unused")
        BufferedHttpEntity entity = new BufferedHttpEntity(response.getEntity());
        EntityUtils.consume(response.getEntity());
        if (response.getStatusLine().getStatusCode() == 200
                || response.getStatusLine().getStatusCode() == 201) {
            lastbyte = fileSize;
        }
        if (response.getStatusLine().getStatusCode() == 308) {
            if (response.getHeaders("Range").length > 0) {
                String range = response.getHeaders("Range")[0].getValue();
                String[] parts = range.split("-");
                lastbyte = Long.parseLong(parts[1]) + 1;
            } else {
                // nothing uploaded, but file is there to start upload!
                lastbyte = 0;
            }
        }
        return lastbyte;
    } finally {
        if (response != null) {
            response.close();
        }
        if (httpclient != null) {
            httpclient.close();
        }
    }
}

From source file:io.uploader.drive.drive.largefile.DriveResumableUpload.java

public boolean checkMD5(String md5) throws IOException {
    logger.info("Querying metadata of completed upload...");

    Preconditions.checkState(org.apache.commons.lang3.StringUtils.isNotEmpty(md5));

    CloseableHttpClient httpclient = null;
    CloseableHttpResponse response = null;
    try {/*from www.  j av  a2 s  .  c  om*/
        httpclient = getHttpClient();
        BasicHttpRequest httpreq = new BasicHttpRequest("PUT", location);
        httpreq.addHeader("Authorization", auth.getAuthHeader());
        httpreq.addHeader("Content-Length", "0");
        httpreq.addHeader("Content-Range", "bytes */" + getFileSizeString());
        response = httpclient.execute(URIUtils.extractHost(uri), httpreq);
        BufferedHttpEntity entity = new BufferedHttpEntity(response.getEntity());
        EntityUtils.consume(response.getEntity());
        String retSrc = EntityUtils.toString(entity);
        String driveMd5 = null;
        if (useOldApi) {
            // Old API will return XML!
            JSONObject result = XML.toJSONObject(retSrc);
            logger.info("id          : "
                    + result.getJSONObject("entry").getString("gd:resourceId").replace("file:", ""));
            logger.info("title       : " + result.getJSONObject("entry").getString("title"));
            logger.info("link        : "
                    + result.getJSONObject("entry").getJSONArray("link").getJSONObject(0).getString("href"));
            logger.info("md5Checksum : " + result.getJSONObject("entry").getString("docs:md5Checksum"));
            driveMd5 = result.getJSONObject("entry").getString("docs:md5Checksum");
        } else {
            JSONObject result = new JSONObject(retSrc);
            logger.info("id          : " + result.getString("id"));
            logger.info("title       : " + result.getString("title"));
            logger.info("link        : " + result.getString("webContentLink"));
            logger.info("md5Checksum : " + result.getString("md5Checksum"));
            driveMd5 = result.getString("md5Checksum");
        }
        // verify the consistency of the md5 values
        return md5.equals(driveMd5);
    } finally {
        if (response != null) {
            response.close();
        }
        if (httpclient != null) {
            httpclient.close();
        }
    }
}