Java URI Create createDatastream(String registrationUri, String APIKey, String xmlDescription)

Here you can find the source of createDatastream(String registrationUri, String APIKey, String xmlDescription)

Description

Attempts to create a new datastream.

License

Open Source License

Parameter

Parameter Description
registrationUri The URL at which the datastream description is to be posted. For sensor.network, this URI would be http://sensor.network.com/rest/resources/datastreams
APIKey Authentication token for datastream creation. An API Key can be obtained by registering at http://sensor.network.com/rest
xmlDescription XML description of the datastream.

Exception

Parameter Description
IOException an exception

Return

a URI corresponding to the newly created datastream or null if datastream creation fails.

Declaration

public static String createDatastream(String registrationUri,
        String APIKey, String xmlDescription) throws IOException 

Method Source Code

//package com.java2s;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    /**/*from w w w .  ja  va2  s .  com*/
     * Attempts to create a new datastream.
     *
     * @param registrationUri The URL at which the datastream description is to
     * be posted. For sensor.network, this URI would be
     * http://sensor.network.com/rest/resources/datastreams
     * @param APIKey Authentication token for datastream creation. An API Key
     * can be obtained by registering at http://sensor.network.com/rest
     * @param xmlDescription XML description of the datastream.
     * @return a URI corresponding to the newly created datastream or null if
     * datastream creation fails.
     * @throws IOException
     */
    public static String createDatastream(String registrationUri,
            String APIKey, String xmlDescription) throws IOException {
        URL datastreamRegistrationURL = null;
        HttpURLConnection conn = null;
        OutputStream os = null;

        String result = null;
        byte[] bytesToPost = xmlDescription.getBytes();
        String error = null;

        try {
            datastreamRegistrationURL = new URL(registrationUri);
            // Here we assume that we don't need an HTTP Proxy to access
            // Sensor.Network
            conn = (HttpURLConnection) datastreamRegistrationURL
                    .openConnection();
            // The API to create datastreams returns an HTTP 303 (See Other)
            // "redirect" to the existing datastream if a user attempts to
            // create a datastream with a duplicate name. We wish to be able
            // to report that redirect instead of following it blindly
            conn.setInstanceFollowRedirects(false);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("X-SensorNetworkAPIKey", APIKey);
            conn.setRequestProperty("Content-Type", "application/xml");
            conn.setRequestProperty("Content-Length", bytesToPost.length
                    + "");

            os = conn.getOutputStream();
            // Post the XML description
            os.write(bytesToPost);

            System.out.println("Got back: " + conn.getResponseCode() + " ("
                    + conn.getResponseMessage() + ")");
            // See what response we got back
            switch (conn.getResponseCode()) {
            case HttpURLConnection.HTTP_CREATED:
            case HttpURLConnection.HTTP_SEE_OTHER:
                // Save the URI for the newly created datastream
                if (conn.getResponseCode() == HttpURLConnection.HTTP_SEE_OTHER) {
                    System.out.println("Warning: a datastream with this "
                            + "name exists already.");
                }
                result = conn.getHeaderField("Location");
                break;

            case HttpURLConnection.HTTP_UNAUTHORIZED:
                error = "Authentication required. Be sure to"
                        + " use a valid API Key obtained by registering "
                        + "at sensor.network.com. The API Key used for "
                        + "this request is \"" + APIKey + "\".";
                break;

            case HttpURLConnection.HTTP_BAD_REQUEST:
                error = "Bad request. Either you have already "
                        + "created a datastream with the same name or the "
                        + "data you POSTed is malformed.";
                break;

            default:
                break;
            }

        } catch (Exception e) {
            throw new IOException("Encountered " + e.getMessage()
                    + " in registerDatastream");
        } finally {
            if (os != null) {
                os.close();
            }
        }

        if (error != null)
            throw new IOException(error);

        return result;
    }
}

Related

  1. convertToURI(String uriString, Map aliasMap)
  2. convertToURI(String value)
  3. create(final String uri)
  4. createAbsoluteURI(URI base, String uri)
  5. createConnection(final String uri, final String authValue)
  6. createDefaultName(final String uri)
  7. createExtensionUri(final String ext)
  8. createURI()
  9. createUri(File file)