Example usage for java.net HttpURLConnection setChunkedStreamingMode

List of usage examples for java.net HttpURLConnection setChunkedStreamingMode

Introduction

In this page you can find the example usage for java.net HttpURLConnection setChunkedStreamingMode.

Prototype

public void setChunkedStreamingMode(int chunklen) 

Source Link

Document

This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is <b>not</b> known in advance.

Usage

From source file:org.jclouds.http.internal.JavaUrlHttpCommandExecutorService.java

@Override
protected HttpURLConnection convert(HttpRequest request) throws IOException {
    URL url = request.getEndpoint().toURL();
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    if (relaxHostname && connection instanceof HttpsURLConnection) {
        HttpsURLConnection sslCon = (HttpsURLConnection) connection;
        sslCon.setHostnameVerifier(new LogToMapHostnameVerifier());
    }//from  ww  w  .  java 2  s .com
    connection.setDoOutput(true);
    connection.setAllowUserInteraction(false);
    // do not follow redirects since https redirects don't work properly
    // ex. Caused by: java.io.IOException: HTTPS hostname wrong: should be
    // <adriancole.s3int0.s3-external-3.amazonaws.com>
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod(request.getMethod().toString());
    for (String header : request.getHeaders().keySet()) {
        for (String value : request.getHeaders().get(header)) {
            connection.setRequestProperty(header, value);

            if ("Transfer-Encoding".equals(header) && "chunked".equals(value)) {
                connection.setChunkedStreamingMode(8192);
            }
        }
    }
    connection.setRequestProperty(HttpHeaders.HOST, request.getEndpoint().getHost());
    if (request.getEntity() != null) {
        OutputStream out = connection.getOutputStream();
        try {
            if (request.getEntity() instanceof String) {
                OutputStreamWriter writer = new OutputStreamWriter(out);
                writer.write((String) request.getEntity());
                writer.close();
            } else if (request.getEntity() instanceof InputStream) {
                IOUtils.copy((InputStream) request.getEntity(), out);
            } else if (request.getEntity() instanceof File) {
                IOUtils.copy(new FileInputStream((File) request.getEntity()), out);
            } else if (request.getEntity() instanceof byte[]) {
                IOUtils.write((byte[]) request.getEntity(), out);
            } else {
                throw new UnsupportedOperationException(
                        "Content not supported " + request.getEntity().getClass());
            }
        } finally {
            IOUtils.closeQuietly(out);
        }
    } else {
        connection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, "0");
    }
    return connection;
}

From source file:com.facebook.GraphRequest.java

private static HttpURLConnection createConnection(URL url) throws IOException {
    HttpURLConnection connection;
    connection = (HttpURLConnection) url.openConnection();

    connection.setRequestProperty(USER_AGENT_HEADER, getUserAgent());
    connection.setRequestProperty(ACCEPT_LANGUAGE_HEADER, Locale.getDefault().toString());

    connection.setChunkedStreamingMode(0);
    return connection;
}

From source file:com.huixueyun.tifenwang.api.net.HttpEngine.java

/**
 * ?connection/*from   www  .jav  a  2  s. c o m*/
 *
 * @param paramUrl ??
 * @return HttpURLConnection
 */
private HttpURLConnection getConnection(String paramUrl) {
    HttpURLConnection connection = null;
    // ?connection
    try {
        // ??URL
        URL url = new URL(SERVER_URL + paramUrl);
        // ?URL
        connection = (HttpURLConnection) url.openConnection();
        // ?
        connection.setRequestMethod(REQUEST_MOTHOD);
        // ??POST?true
        connection.setDoInput(true);
        // ??POST?
        connection.setDoOutput(true);
        // ?
        connection.setUseCaches(false);
        // 
        connection.setReadTimeout(TIME_OUT);
        connection.setConnectTimeout(TIME_OUT);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("Connection", "keep-alive");
        connection.setRequestProperty("Response-Type", "json");
        connection.setChunkedStreamingMode(0);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return connection;
}

From source file:com.orange.oidc.tim.service.HttpOpenidConnect.java

public boolean logout(String server_url) {

    if (isEmpty(server_url)) {
        Logd(TAG, "revokSite no server url");
        return false;
    }/*from   w  w w.  j a v  a 2s .  com*/

    if (!server_url.endsWith("/"))
        server_url += "/";

    // get end session endpoint
    String end_session_endpoint = getEndpointFromConfigOidc("end_session_endpoint", server_url);
    if (isEmpty(end_session_endpoint)) {
        Logd(TAG, "logout : could not get end_session_endpoint on server : " + server_url);
        return false;
    }

    // set up connection
    HttpURLConnection huc = getHUC(end_session_endpoint);
    huc.setInstanceFollowRedirects(false);

    huc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    huc.setDoOutput(true);
    huc.setChunkedStreamingMode(0);
    // prepare parameters
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

    try {
        // write parameters to http connection
        OutputStream os = huc.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        // get URL encoded string from list of key value pairs
        String postParam = getQuery(nameValuePairs);
        Logd("Logout", "url: " + end_session_endpoint);
        Logd("Logout", "POST: " + postParam);
        writer.write(postParam);
        writer.flush();
        writer.close();
        os.close();

        // try to connect
        huc.connect();
        // connexion status
        int responseCode = huc.getResponseCode();
        Logd(TAG, "Logout response: " + responseCode);
        // if 200 - OK
        if (responseCode == 200) {
            return true;
        }
        huc.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return false;
}

From source file:com.daidiansha.acarry.control.network.core.volley.toolbox.HurlStack.java

/**
 * Perform a multipart request on a connection
 *
 * @param connection The Connection to perform the multi part request
 * @param request    The params to add to the Multi Part request
 *                   The files to upload
 * @throws ProtocolException/*w ww. j  a va 2s  . c  o  m*/
 */
private void setConnectionParametersForMultipartRequest(HttpURLConnection connection, MultiPartRequest request)
        throws IOException, ProtocolException {

    final String charset = request.getProtocolCharset();
    final int curTime = (int) (System.currentTimeMillis() / 1000);
    final String boundary = BOUNDARY_PREFIX + curTime;
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setRequestProperty(HEADER_CONTENT_TYPE, String.format(CONTENT_TYPE_MULTIPART, charset, curTime));

    Map<String, MultiPartParam> multipartParams = request.getMultipartParams();
    Map<String, String> filesToUpload = request.getFilesToUpload();

    if (request.isFixedStreamingMode()) {
        int contentLength = getContentLengthForMultipartRequest(boundary, multipartParams, filesToUpload);

        connection.setFixedLengthStreamingMode(contentLength);
    } else {
        connection.setChunkedStreamingMode(0);
    }
    // Modified end
    PrintWriter writer = null;
    try {
        OutputStream out = connection.getOutputStream();
        writer = new PrintWriter(new OutputStreamWriter(out, charset), true);

        for (String key : multipartParams.keySet()) {
            MultiPartParam param = multipartParams.get(key);

            writer.append(boundary).append(CRLF)
                    .append(String.format(HEADER_CONTENT_DISPOSITION + COLON_SPACE + FORM_DATA, key))
                    .append(CRLF).append(HEADER_CONTENT_TYPE + COLON_SPACE + param.contentType).append(CRLF)
                    .append(CRLF).append(param.value).append(CRLF).flush();
        }
        long allFileSize = 0;
        long allUpLoadSize = 0;
        int fileLocation = 0;
        for (String key : filesToUpload.keySet()) {
            File file = new File(filesToUpload.get(key));

            if (!file.exists() || file.isDirectory()) {
                continue;
            }
            allFileSize += file.length();
        }
        for (String key : filesToUpload.keySet()) {

            File file = new File(filesToUpload.get(key));

            if (!file.exists()) {
                throw new IOException(String.format("File not found: %s", file.getAbsolutePath()));
            }

            if (file.isDirectory()) {
                throw new IOException(String.format("File is a directory: %s", file.getAbsolutePath()));
            }

            writer.append(boundary).append(CRLF)
                    .append(String.format(
                            HEADER_CONTENT_DISPOSITION + COLON_SPACE + FORM_DATA + SEMICOLON_SPACE + FILENAME,
                            key, file.getName()))
                    .append(CRLF).append(HEADER_CONTENT_TYPE + COLON_SPACE + CONTENT_TYPE_OCTET_STREAM)
                    .append(CRLF).append(HEADER_CONTENT_TRANSFER_ENCODING + COLON_SPACE + BINARY).append(CRLF)
                    .append(CRLF).flush();

            BufferedInputStream input = null;
            try {
                FileInputStream fis = new FileInputStream(file);
                int transferredBytes = 0;
                int totalSize = (int) file.length();
                input = new BufferedInputStream(fis);
                int bufferLength = 0;

                byte[] buffer = new byte[1024];
                int progress = 0;
                while ((bufferLength = input.read(buffer)) > 0) {
                    out.write(buffer, 0, bufferLength);
                    transferredBytes += bufferLength;
                    allUpLoadSize += bufferLength;
                    int p = transferredBytes * 100 / totalSize;
                    if (p != progress) {
                        progress = p;
                        request.fileProgress(filesToUpload.get(key), fileLocation, transferredBytes, totalSize,
                                allUpLoadSize, allFileSize);
                    }
                }
                out.flush(); // Important! Output cannot be closed. Close of
                // writer will close
                // output as well.
            } finally {
                if (input != null)
                    try {
                        input.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
            }
            writer.append(CRLF).flush(); // CRLF is important! It indicates
            fileLocation++;
            // end of binary
            // boundary.
        }

        // End of multipart/form-data.
        writer.append(boundary + BOUNDARY_PREFIX).append(CRLF).flush();

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

    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:org.wso2.msf4j.HttpServerTest.java

public void testChunkAggregatedUploadFailure() throws IOException {
    //create a random file to be uploaded.
    int size = 78 * 1024;
    File fname = new File(tmpFolder, "testChunkAggregatedUploadFailure.txt");
    fname.createNewFile();// w ww  . ja va2s  .  c  om
    RandomAccessFile randf = new RandomAccessFile(fname, "rw");
    randf.setLength(size);
    randf.close();

    //test chunked upload
    HttpURLConnection urlConn = request("/test/v1/aggregate/upload", HttpMethod.PUT);
    urlConn.setChunkedStreamingMode(1024);
    Files.copy(Paths.get(fname.toURI()), urlConn.getOutputStream());
    assertEquals(500, urlConn.getResponseCode());
    urlConn.disconnect();
    fname.delete();
}

From source file:org.wso2.msf4j.HttpServerTest.java

@Test
public void testChunkAggregatedUpload() throws IOException {
    //create a random file to be uploaded.
    int size = 69 * 1024;
    File fname = new File(tmpFolder, "testChunkAggregatedUpload.txt");
    fname.createNewFile();/*ww  w.j  ava  2 s .  c o m*/
    RandomAccessFile randf = new RandomAccessFile(fname, "rw");
    randf.setLength(size);
    randf.close();

    //test chunked upload
    HttpURLConnection urlConn = request("/test/v1/aggregate/upload", HttpMethod.PUT);
    urlConn.setChunkedStreamingMode(1024);
    Files.copy(Paths.get(fname.toURI()), urlConn.getOutputStream());
    assertEquals(200, urlConn.getResponseCode());

    assertEquals(size, Integer.parseInt(getContent(urlConn).split(":")[1].trim()));
    urlConn.disconnect();
    fname.delete();
}

From source file:org.wso2.msf4j.HttpServerTest.java

@Test
public void testUploadReject() throws Exception {
    HttpURLConnection urlConn = request("/test/v1/uploadReject", HttpMethod.POST, true);
    try {//from   w  w w.  j  av  a  2 s .  c om
        urlConn.setChunkedStreamingMode(1024);
        urlConn.getOutputStream().write("Rejected Content".getBytes(Charsets.UTF_8));
        try {
            urlConn.getInputStream();
            fail();
        } catch (IOException e) {
            // Expect to get exception since server response with 400. Just drain the error stream.
            IOUtils.toByteArray(urlConn.getErrorStream());
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), urlConn.getResponseCode());
        }
    } finally {
        urlConn.disconnect();
    }
}

From source file:tr.edu.gsu.nerwip.retrieval.reader.wikipedia.WikipediaReader.java

/**
 * Reads the source code of the web page at the specified
 * URL./*  w  ww .  jav a  2s.co  m*/
 * 
 * @param url
 *       Address of the web page to be read.
 * @return
 *       String containing the read HTML source code.
 * 
 * @throws IOException
 *       Problem while accessing the specified URL.
 */
private String manuallyReadUrl(URL url) throws IOException {
    boolean trad = false;

    BufferedReader br = null;

    // open page the traditional way
    if (trad) {
        InputStream is = url.openStream();
        InputStreamReader isr = new InputStreamReader(is);
        br = new BufferedReader(isr);
    }

    // open with more options
    else { // setup connection
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.setReadTimeout(2000);
        connection.setChunkedStreamingMode(0);
        connection.setRequestProperty("Content-Length", "0");
        //         connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
        connection.connect();

        // setup input stream
        // part retrieved from http://stackoverflow.com/questions/538999/java-util-scanner-and-wikipedia
        // original author: Marco Beggio
        InputStream is = null;
        String encoding = connection.getContentEncoding();
        if (connection.getContentEncoding() != null && encoding.equals("gzip")) {
            is = new GZIPInputStream(connection.getInputStream());
        } else if (encoding != null && encoding.equals("deflate")) {
            is = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
        } else {
            is = connection.getInputStream();
        }

        // alternative to spot error details            
        //         InputStream is;
        //         if (connection.getResponseCode() != 200) 
        //            is = connection.getErrorStream();
        //         else 
        //            is = connection.getInputStream();

        InputStreamReader isr = new InputStreamReader(is);
        br = new BufferedReader(isr);
    }

    // read page
    StringBuffer sourceCode = new StringBuffer();
    String line = br.readLine();
    while (line != null) {
        sourceCode.append(line + "\n");
        line = br.readLine();
    }

    String result = sourceCode.toString();
    br.close();
    return result;
}

From source file:ca.osmcanada.osvuploadr.JPMain.java

private void Upload_Image(ImageProperties imp, String accessToken, long Sequence_id) {
    try {//from www. ja v  a 2s . c  o m
        URL url = new URL(URL_PHOTO);
        URLConnection con = url.openConnection();
        HttpURLConnection http = (HttpURLConnection) con;
        http.setRequestMethod("POST"); // PUT is another valid option
        http.setDoOutput(true);

        String boundary = UUID.randomUUID().toString();
        byte[] boundaryBytes = boundaryBytes = ("--" + boundary + "\r\n").getBytes(StandardCharsets.UTF_8);
        byte[] finishBoundaryBytes = ("--" + boundary + "--").getBytes(StandardCharsets.UTF_8);
        http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        http.setRequestProperty("Accept", "*/*");

        // Enable streaming mode with default settings
        http.setChunkedStreamingMode(0);
        System.out.println("Uploading:" + imp.getFilePath());
        // Send our fields:
        try (OutputStream out = http.getOutputStream()) {
            // Send our header
            out.write(boundaryBytes);

            //Send access token
            sendField(out, "access_token", accessToken);

            // Send a seperator
            out.write(boundaryBytes);

            if (imp.getCompass() >= 0) {
                // Send our compass
                sendField(out, "headers", Double.toString(imp.getCompass()));

                // Send another seperator
                out.write(boundaryBytes);
            }

            // Send our coordinates
            sendField(out, "coordinate", imp.getCoordinates());

            // Send a seperator
            out.write(boundaryBytes);

            // Send our sequence id
            sendField(out, "sequenceId", Long.toString(Sequence_id));

            // Send a seperator
            out.write(boundaryBytes);

            // Send our sequence index
            sendField(out, "sequenceIndex", Integer.toString(imp.getSequenceNumber()));

            // Send a seperator
            out.write(boundaryBytes);

            String[] tokens = imp.getFilePath().split("[\\\\|/]");
            String filename = tokens[tokens.length - 1];
            // Send our file
            try (InputStream file = new FileInputStream(imp.getFilePath())) {
                sendFile(out, "photo", file, filename);
            }

            // Finish the request
            out.write(finishBoundaryBytes);
            out.close();
        }
        InputStream is = http.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte buf[] = new byte[1024];
        int letti;

        while ((letti = is.read(buf)) > 0)
            baos.write(buf, 0, letti);

        String data = new String(baos.toByteArray());
        //TODO:Process returned JSON
        System.out.println(data);
        http.disconnect();
    } catch (Exception ex) {
        Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex);
    }
}