Search in sources :

Example 1 with NoCloseInputStream

use of com.fathomdb.io.NoCloseInputStream in project platformlayer by platformlayer.

the class ConfigurationOptions method buildPlatformLayerClient.

public PlatformLayerClient buildPlatformLayerClient() throws IOException, OpsException {
    HttpPlatformLayerClient client;
    if (configFile == null) {
        throw new IllegalArgumentException("Config file is required");
    }
    InputStream is = null;
    try {
        if (configFile.equals("-")) {
            // Read from stdin
            // Don't auto-close it: that terminates nailgun
            is = new NoCloseInputStream(System.in);
        } else {
            if (isServerMode()) {
                throw new IllegalArgumentException("Must pass config file over stdin in server mode");
            }
            File file = IoUtils.resolve(configFile);
            if (!file.exists()) {
                throw new FileNotFoundException("Configuration file not found: " + file);
            }
            is = new FileInputStream(file);
        }
        final Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            throw new IOException("Error reading configuration file", e);
        }
        if (properties.getProperty("platformlayer.username") == null) {
            throw new CliException("User property not set in configuration file");
        }
        if (debug) {
            client = buildPlatformLayerClient(properties, debug);
        } else {
            String propertiesKey = PropertyUtils.serialize(properties);
            try {
                client = platformLayerClientCache.get(propertiesKey, new Callable<HttpPlatformLayerClient>() {

                    @Override
                    public HttpPlatformLayerClient call() throws Exception {
                        return buildPlatformLayerClient(properties, false);
                    }
                });
            } catch (ExecutionException e) {
                throw new CliException("Error building platformlayer client", e);
            }
        }
    } finally {
        if (is != System.in) {
            Closeables.closeQuietly(is);
        }
    }
    return client;
}
Also used : HttpPlatformLayerClient(org.platformlayer.HttpPlatformLayerClient) FileInputStream(java.io.FileInputStream) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) Callable(java.util.concurrent.Callable) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) CliException(com.fathomdb.cli.CliException) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Example 2 with NoCloseInputStream

use of com.fathomdb.io.NoCloseInputStream in project platformlayer by platformlayer.

the class PutItem method runCommand.

@Override
public Object runCommand() throws PlatformLayerClientException, JSONException {
    PlatformLayerClient client = getPlatformLayerClient();
    if (json == null) {
        InputStream stream = new NoCloseInputStream(System.in);
        byte[] data;
        try {
            data = ByteStreams.toByteArray(stream);
            json = new String(data, Charsets.UTF_8);
        } catch (IOException e) {
            throw new CliException("Error reading stdin", e);
        }
    }
    JSONObject jsonObject = new JSONObject(json);
    PlatformLayerKey parentKey = null;
    if (parent != null || !tags.isEmpty()) {
        JSONObject tagsObject = null;
        if (jsonObject.has("tags")) {
            tagsObject = jsonObject.getJSONObject("tags");
        } else {
            tagsObject = new JSONObject();
            jsonObject.put("tags", tagsObject);
        }
        JSONArray tagsArray;
        if (tagsObject.has("tags")) {
            tagsArray = tagsObject.getJSONArray("tags");
        } else {
            tagsArray = new JSONArray();
            tagsObject.put("tags", tagsArray);
        }
        if (parent != null) {
            parentKey = parent.resolve(getContext());
            Tag parentTag = Tag.buildParentTag(parentKey);
            JSONObject jsonTag = new JSONObject();
            jsonTag.put("key", parentTag.getKey());
            jsonTag.put("value", parentTag.getValue());
            tagsArray.put(jsonTag);
        }
        for (String tag : tags) {
            int equalsIndex = tag.indexOf('=');
            if (equalsIndex == -1) {
                throw new CliException("Expected tagname=tagvalue");
            }
            String tagName = tag.substring(0, equalsIndex);
            String tagValue = tag.substring(equalsIndex + 1);
            JSONObject jsonTag = new JSONObject();
            jsonTag.put("key", tagName);
            jsonTag.put("value", tagValue);
            tagsArray.put(jsonTag);
        }
    }
    PlatformLayerKey key = path.resolve(getContext());
    boolean wrap = false;
    String data;
    if (wrap) {
        JSONObject wrapped = new JSONObject();
        wrapped.put(key.getItemType().getKey(), jsonObject);
        data = wrapped.toString();
    } else {
        data = jsonObject.toString();
    }
    UntypedItem retval = client.putItem(key, data, Format.JSON);
    return retval;
}
Also used : UntypedItem(org.platformlayer.common.UntypedItem) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) IOException(java.io.IOException) PlatformLayerClient(org.platformlayer.PlatformLayerClient) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) CliException(com.fathomdb.cli.CliException) JSONObject(org.json.JSONObject) Tag(org.platformlayer.core.model.Tag)

Example 3 with NoCloseInputStream

use of com.fathomdb.io.NoCloseInputStream in project platformlayer by platformlayer.

the class KeystoneCliOptions method getConfigurationProperties.

public Properties getConfigurationProperties() {
    if (config == null) {
        Properties build = new Properties();
        if (configFile != null) {
            InputStream is = null;
            try {
                if (configFile.equals("-")) {
                    // Read from stdin
                    // Don't auto-close it, and that terminates nailgun
                    is = new NoCloseInputStream(System.in);
                } else {
                    if (isServerMode()) {
                        throw new IllegalArgumentException("Must pass config file over stdin in server mode");
                    }
                    File file = IoUtils.resolve(configFile);
                    if (!file.exists()) {
                        throw new FileNotFoundException("Configuration file not found: " + file);
                    }
                    is = new FileInputStream(file);
                }
                try {
                    build.load(is);
                } catch (IOException e) {
                    throw new IOException("Error reading configuration file", e);
                }
            } catch (IOException e) {
                throw new IllegalArgumentException("Error reading configuration file", e);
            } finally {
                IoUtils.safeClose(is);
            }
        }
        if (this.username != null) {
            build.setProperty("platformlayer.username", this.username);
        }
        if (this.password != null) {
            build.setProperty("platformlayer.password", this.password);
        }
        this.config = build;
    }
    return config;
}
Also used : NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) FileInputStream(java.io.FileInputStream) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with NoCloseInputStream

use of com.fathomdb.io.NoCloseInputStream in project platformlayer by platformlayer.

the class SetProperty method runCommand.

@Override
public Object runCommand() throws PlatformLayerClientException, IOException {
    PlatformLayerClient client = getPlatformLayerClient();
    if (stdin) {
        if (value != null) {
            throw new CliException("You cannot specify a value when using -stdin");
        }
        InputStream stream = new NoCloseInputStream(System.in);
        byte[] data = ByteStreams.toByteArray(stream);
        if ("base64".equals(format)) {
            value = Base64.encode(data);
        } else {
            value = new String(data);
        }
    } else {
        if (value == null) {
            throw new CliException("Value is required (if not using -stdin)");
        }
    }
    return runCommand(path);
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) CliException(com.fathomdb.cli.CliException) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) InputStream(java.io.InputStream)

Aggregations

NoCloseInputStream (com.fathomdb.io.NoCloseInputStream)4 InputStream (java.io.InputStream)4 CliException (com.fathomdb.cli.CliException)3 IOException (java.io.IOException)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 Properties (java.util.Properties)2 PlatformLayerClient (org.platformlayer.PlatformLayerClient)2 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 HttpPlatformLayerClient (org.platformlayer.HttpPlatformLayerClient)1 UntypedItem (org.platformlayer.common.UntypedItem)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 Tag (org.platformlayer.core.model.Tag)1