Java HTTP Port Find replace(final String host, final int port, final String user, final String program, final String relation, final String dataset, final String format)

Here you can find the source of replace(final String host, final int port, final String user, final String program, final String relation, final String dataset, final String format)

Description

Replace the contents of a dataset.

License

Open Source License

Parameter

Parameter Description
host master hostname
port master port
user user parameter of the dataset
program program parameter of the dataset
relation relation parameter of the dataset
dataset the dataset to upload
format the format of the relation ("json", "csv", "tsv")

Exception

Parameter Description
IOException if an error occurs

Declaration

public static void replace(final String host, final int port, final String user, final String program,
        final String relation, final String dataset, final String format) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    /**//from   w w  w. ja va2s. c o  m
     * Replace the contents of a dataset.
     * 
     * @param host master hostname
     * @param port master port
     * @param user user parameter of the dataset
     * @param program program parameter of the dataset
     * @param relation relation parameter of the dataset
     * @param dataset the dataset to upload
     * @param format the format of the relation ("json", "csv", "tsv")
     * 
     * @throws IOException if an error occurs
     */
    public static void replace(final String host, final int port, final String user, final String program,
            final String relation, final String dataset, final String format) throws IOException {
        URL url = getDatasetUrl(host, port, user, program, relation, format);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/octet-stream");
        conn.setRequestMethod("PUT");

        byte[] payload = dataset.getBytes("UTF-8");
        conn.setFixedLengthStreamingMode(payload.length);
        try {
            OutputStream out = conn.getOutputStream();
            out.write(payload);
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                throw new IOException("Failed upload: " + conn.getResponseCode());
            }
        } finally {
            conn.disconnect();
        }
    }

    /**
     * Construct a URL for get/put of a dataset.
     * 
     * @param host master hostname
     * @param port master port
     * @param user user parameter of the dataset
     * @param program program parameter of the dataset
     * @param relation relation parameter of the dataset
     * @param format the format of the relation ("json", "csv", "tsv")
     * @return a URL for the dataset
     * @throws IOException if an error occurs
     */
    private static URL getDatasetUrl(final String host, final int port, final String user, final String program,
            final String relation, final String format) throws IOException {
        return new URL(String.format("http://%s:%d/dataset/user-%s/program-%s/relation-%s/data?format=%s", host,
                port, user, program, relation, format));
    }
}

Related

  1. randomFreePort()
  2. randomFreePort()
  3. randomFreePort()
  4. read(String host, int port)
  5. readHttpContent(String domain, String resource, int port)
  6. resolve(final String desc, final int defaultPort)
  7. safePort(final String port)
  8. searchPort(int number, int even, boolean stream)
  9. selectAvailablePort(int preferedPort)