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.openxdata.server.servlet.DataImportServletTest.java

@Test
@Ignore("this is an integration test for localhost")
public void integrationTest() throws Exception {
    final String urlString = "http://localhost:8888/org.openxdata.server.admin.OpenXDataServerAdmin/dataimport?msisdn=2222222";
    final String userName = "dagmar";
    final String password = "dagmar";

    // open url connection
    URL url = new URL(urlString);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setDoOutput(true);/*from  w w w  . j a va 2 s . c  o m*/
    con.setDoInput(true);
    con.setChunkedStreamingMode(1024);

    // stuff the Authorization request header
    byte[] encodedPassword = (userName + ":" + password).getBytes();
    con.setRequestProperty("Authorization", "Basic " + new String(Base64.encodeBase64(encodedPassword)));

    // put the form data on the output stream
    DataOutputStream out = new DataOutputStream(con.getOutputStream());
    out.writeByte(new Integer(1));
    String data = "<?xml version='1.0' encoding='UTF-8' ?><cmt_lesotho_tlpp_session_report_v1 formKey=\"cmt_lesotho_tlpp_session_report_v1\" id=\"2\" name=\"Treatment literacy programme_v1\" xmlns:xf=\"http://www.w3.org/2002/xforms\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">  <name>TEEHEE</name>  <session_type>treatment_literacy_session</session_type>  <session_date>2010-04-15</session_date>  <district>berea</district>  <session_location>secondary_school</session_location>  <session_location_other/>  <session_place_name>dsds</session_place_name>  <participant_number_male>1</participant_number_male>  <participant_number_female>2</participant_number_female>  <session_start_time>10:22:00</session_start_time>  <session_finish_time>01:22:00</session_finish_time>  <session_topic>children_HIV_incl_ARVs</session_topic>  <session_topic_other/>  <support_group>false</support_group>  <one_on_one_session>false</one_on_one_session>  <CMT_AV_kit>false</CMT_AV_kit>  <CMT_DVD>false</CMT_DVD>  <CMT_co_facilitator>false</CMT_co_facilitator>  <co_facilitator_name/>  <clinic_hospital_department/>  <clinic_hospital_department_other/>  <clinic_hospital_TV/>  <school_grade>12</school_grade>  <school_CMT_materials>true</school_CMT_materials>  <session_confirmed>true</session_confirmed></cmt_lesotho_tlpp_session_report_v1>";
    out.writeUTF(data);
    out.flush();

    // pull the information back from the URL
    InputStream is = con.getInputStream();
    StringBuffer buf = new StringBuffer();
    int c;
    while ((c = is.read()) != -1) {
        buf.append((char) c);
    }
    con.disconnect();

    // output the information
    System.out.println(buf);
}

From source file:com.mars.framework.volley.net.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());/*from  www .  j  ava2s  .co  m*/
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    connection.setChunkedStreamingMode(0);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(),
            connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    return response;
}

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

public static boolean logout(String server_url, String tim_access_token) {

    if (isEmpty(server_url)) {
        Logd(TAG, "logout failed : no server url");
        return false;
    }/*from  w  ww.j a v a2s  . c o m*/

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

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

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

    huc.setDoOutput(true);
    huc.setChunkedStreamingMode(0);
    // prepare parameters
    List<NameValuePair> nameValuePairs = null;
    if (tim_access_token != null && tim_access_token.length() > 0) {
        nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("tat", tim_access_token));
    }

    try {
        // write parameters to http connection
        if (nameValuePairs != null) {
            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: " + revoke_logout_endpoint);
            Logd("Logout", "POST: " + postParam);
            writer.write(postParam);
            writer.flush();
            writer.close();
            os.close();
        }

        // try to connect
        huc.connect();
        // connection 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;
}

From source file:org.fabric3.admin.interpreter.communication.DomainConnectionImpl.java

public HttpURLConnection put(String path, URL resource) throws CommunicationException {
    try {/* w  ww  .  j a v  a 2 s . c o m*/
        URL url = createUrl(addresses.getLast() + path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setChunkedStreamingMode(4096);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("PUT");
        connection.setRequestProperty("Content-type", "application/json");
        setBasicAuth(connection);

        if (connection instanceof HttpsURLConnection) {
            HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
            setSocketFactory(httpsConnection);
        }

        InputStream is = null;
        OutputStream os = null;
        try {
            os = connection.getOutputStream();
            is = resource.openStream();
            copy(is, os);
            os.flush();
        } finally {
            if (os != null) {
                os.close();
            }
            if (is != null) {
                is.close();
            }
        }

        return connection;
    } catch (IOException e) {
        throw new CommunicationException(e);
    }
}

From source file:eu.crushedpixel.littlstar.api.upload.S3Uploader.java

/**
 * Executes a multipart form upload to the S3 Bucket as described in
 * <a href="http://docs.aws.amazon.com/AmazonS3/latest/dev/HTTPPOSTExamples.html">the AWS Documentation</a>.
 * @param s3UploadProgressListener An S3UploadProgressListener which is called whenever
 *                               bytes are written to the outgoing connection. May be null.
 * @throws IOException in case of a problem or the connection was aborted
 * @throws ClientProtocolException in case of an http protocol error
 *///  w  w w.  j a v  a 2s.c o m
public void uploadFileToS3(S3UploadProgressListener s3UploadProgressListener)
        throws IOException, ClientProtocolException {
    //unfortunately, we can't use Unirest to execute the call, because there is no support
    //for Progress listeners (yet). See https://github.com/Mashape/unirest-java/issues/26

    int bufferSize = 1024;

    //opening a connection to the S3 Bucket
    HttpURLConnection urlConnection = (HttpURLConnection) new URL(s3_bucket).openConnection();
    urlConnection.setUseCaches(false);
    urlConnection.setDoOutput(true);
    urlConnection.setRequestMethod("POST");
    urlConnection.setChunkedStreamingMode(bufferSize);
    urlConnection.setRequestProperty("Connection", "Keep-Alive");
    urlConnection.setRequestProperty("Cache-Control", "no-cache");
    String boundary = "*****";
    urlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

    //writing the request headers
    DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream());

    String newline = "\r\n";
    String twoHyphens = "--";
    dos.writeBytes(twoHyphens + boundary + newline);

    String attachmentName = "file";
    String attachmentFileName = "file";

    dos.writeBytes("Content-Disposition: form-data; name=\"" + attachmentName + "\";filename=\""
            + attachmentFileName + "\"" + newline);
    dos.writeBytes(newline);

    //sending the actual file
    byte[] buf = new byte[bufferSize];

    FileInputStream fis = new FileInputStream(file);
    long totalBytes = fis.getChannel().size();
    long writtenBytes = 0;

    int len;
    while ((len = fis.read(buf)) != -1) {
        dos.write(buf);
        writtenBytes += len;

        s3UploadProgressListener.onProgressUpdated(new S3UpdateProgressEvent(writtenBytes, totalBytes,
                (float) ((double) writtenBytes / totalBytes)));

        if (interrupt) {
            fis.close();
            dos.close();
            return;
        }
    }

    fis.close();

    //finish the call
    dos.writeBytes(newline);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + newline);

    dos.close();

    urlConnection.disconnect();
}

From source file:com.splout.db.dnode.HttpFileExchanger.java

public void send(final String tablespace, final int partition, final long version, final File binaryFile,
        final String url, boolean blockUntilComplete) {
    Future<?> future = clientExecutors.submit(new Runnable() {
        @Override/*from www.java  2 s .  c o m*/
        public void run() {
            DataOutputStream writer = null;
            InputStream input = null;
            try {
                HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
                connection.setChunkedStreamingMode(config.getInt(FetcherProperties.DOWNLOAD_BUFFER));
                connection.setDoOutput(true);
                connection.setRequestProperty("filename", binaryFile.getName());
                connection.setRequestProperty("tablespace", tablespace);
                connection.setRequestProperty("partition", partition + "");
                connection.setRequestProperty("version", version + "");

                Checksum checkSum = new CRC32();

                writer = new DataOutputStream(new GZIPOutputStream(connection.getOutputStream()));
                // 1 - write file size
                writer.writeLong(binaryFile.length());
                writer.flush();
                // 2 - write file content
                input = new FileInputStream(binaryFile);
                byte[] buffer = new byte[config.getInt(FetcherProperties.DOWNLOAD_BUFFER)];
                long wrote = 0;
                for (int length = 0; (length = input.read(buffer)) > 0;) {
                    writer.write(buffer, 0, length);
                    checkSum.update(buffer, 0, length);
                    wrote += length;
                }
                // 3 - add the CRC so that we can verify the download
                writer.writeLong(checkSum.getValue());
                writer.flush();
                log.info("Sent file " + binaryFile + " to " + url + " with #bytes: " + wrote + " and checksum: "
                        + checkSum.getValue());
            } catch (IOException e) {
                log.error(e);
            } finally {
                try {
                    if (input != null) {
                        input.close();
                    }
                    if (writer != null) {
                        writer.close();
                    }
                } catch (IOException ignore) {
                }
            }
        }
    });
    try {
        if (blockUntilComplete) {
            while (future.isDone() || future.isCancelled()) {
                Thread.sleep(1000);
            }
        }
    } catch (InterruptedException e) {
        // interrupted!
    }
}

From source file:com.joyent.manta.client.MantaClientSigningIT.java

@Test
public final void testCanCreateSignedPUTUriFromPath() throws IOException, InterruptedException {
    if (config.isClientEncryptionEnabled()) {
        throw new SkipException("Signed URLs are not decrypted by the client");
    }//from  w ww  .  ja  v  a2s  . co m

    final String name = UUID.randomUUID().toString();
    final String path = testPathPrefix + name;

    Instant expires = Instant.now().plus(1, ChronoUnit.HOURS);
    URI uri = mantaClient.getAsSignedURI(path, "PUT", expires);

    HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();

    connection.setReadTimeout(3000);
    connection.setRequestMethod("PUT");
    connection.setDoOutput(true);
    connection.setChunkedStreamingMode(10);
    connection.connect();

    try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), UTF_8)) {
        out.write(TEST_DATA);
    } finally {
        connection.disconnect();
    }

    // Wait for file to become available
    for (int i = 0; i < 10; i++) {
        Thread.sleep(500);

        if (mantaClient.existsAndIsAccessible(path)) {
            break;
        }
    }

    String actual = mantaClient.getAsString(path);
    Assert.assertEquals(actual, TEST_DATA);
}

From source file:org.mf.api.net.stack.HurlStack.java

/**
 * Opens an {@link HttpURLConnection} with parameters.
 * @return an open connection//from w  w w  .jav a 2  s .  c om
 */
private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
    HttpURLConnection connection = createConnection(url);

    int timeoutMs = request.getTimeoutMs();
    connection.setConnectTimeout(timeoutMs);
    connection.setReadTimeout(timeoutMs);
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setChunkedStreamingMode(0);

    // use caller-provided custom SslSocketFactory, if any, for HTTPS
    if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
        ((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory);
    }

    return connection;
}

From source file:org.andstatus.app.net.http.HttpConnectionOAuthJavaNet.java

private void writeMedia(HttpURLConnection conn, JSONObject formParams)
        throws ConnectionException, IOException, JSONException {
    Uri mediaUri = Uri.parse(formParams.getString(KEY_MEDIA_PART_URI));
    conn.setChunkedStreamingMode(0);
    conn.setRequestProperty("Content-Type", MyContentType.uri2MimeType(mediaUri, null));
    setAuthorization(conn, getConsumer(), false);

    InputStream in = MyContextHolder.get().context().getContentResolver().openInputStream(mediaUri);
    try {// ww w  .j  av a2  s.  c  o m
        byte[] buffer = new byte[1024];
        int length;
        OutputStream out = new BufferedOutputStream(conn.getOutputStream());
        try {
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
        } finally {
            DbUtils.closeSilently(out);
        }
    } finally {
        DbUtils.closeSilently(in);
    }
}

From source file:com.androidex.volley.toolbox.HurlStack.java

/**
 * Perform a multipart request on a connection
 * //from   w w  w. j  a v  a2s  . co  m
 * @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
 */
private static void setConnectionParametersForMultipartRequest(HttpURLConnection connection, Request<?> request)
        throws IOException, ProtocolException {

    final String charset = ((MultiPartRequest<?>) 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 = ((MultiPartRequest<?>) request).getMultipartParams();
    Map<String, String> filesToUpload = ((MultiPartRequest<?>) request).getFilesToUpload();

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

        connection.setFixedLengthStreamingMode(contentLength);
    } else {
        connection.setChunkedStreamingMode(0);
    }
    // Modified end

    ProgressListener progressListener;
    progressListener = (ProgressListener) request;

    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();
        }

        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];
                while ((bufferLength = input.read(buffer)) > 0) {
                    out.write(buffer, 0, bufferLength);
                    transferredBytes += bufferLength;
                    progressListener.onProgress(transferredBytes, totalSize);
                }
                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
            // 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();
        }
    }
}