Example usage for com.amazonaws.util StringInputStream StringInputStream

List of usage examples for com.amazonaws.util StringInputStream StringInputStream

Introduction

In this page you can find the example usage for com.amazonaws.util StringInputStream StringInputStream.

Prototype

public StringInputStream(String s) throws UnsupportedEncodingException 

Source Link

Usage

From source file:com.ivona.services.tts.model.transform.lexicons.ListLexiconsPostRequestMarshaller.java

License:Open Source License

private void setRequestPayload(Request<ListLexiconsRequest> request, ListLexiconsRequest listLexiconsRequest) {
    StringWriter stringWriter = new StringWriter();
    JSONWriter jsonWriter = new JSONWriter(stringWriter);

    try {//from w w w .jav a 2s  .  c  o  m
        jsonWriter.object();
        jsonWriter.endObject();

        String snippet = stringWriter.toString();
        byte[] content = snippet.getBytes(UTF_8);
        request.setContent(new StringInputStream(snippet));
        request.addHeader("Content-Length", Integer.toString(content.length));
    } catch (JSONException e) {
        throw new AmazonClientException("Unable to marshall request to JSON", e);
    } catch (UnsupportedEncodingException e) {
        throw new AmazonClientException("Unable to marshall request to JSON", e);
    }
}

From source file:com.ivona.services.tts.model.transform.lexicons.PutLexiconPostRequestMarshaller.java

License:Open Source License

private void setRequestPayload(Request<PutLexiconRequest> request, PutLexiconRequest putLexiconRequest) {
    StringWriter stringWriter = new StringWriter();
    JSONWriter jsonWriter = new JSONWriter(stringWriter);

    try {/*from w w w.  j a  v a 2s .  c  o m*/
        jsonWriter.object();
        Lexicon lexicon = putLexiconRequest.getLexicon();
        if (lexicon != null) {
            jsonWriter.key(JSON_KEY_LEXICON);
            jsonWriter.object();

            if (MarshallerHelper.stringIsNotBlank(lexicon.getName())) {
                jsonWriter.key(JSON_KEY_NAME).value(lexicon.getName());
            } else {
                throw new AmazonClientException("null or empty lexicon name passed to marshall(...)");
            }

            if (MarshallerHelper.stringIsNotBlank(lexicon.getContents())) {
                jsonWriter.key(JSON_KEY_CONTENTS).value(lexicon.getContents());
            } else {
                throw new AmazonClientException("null or empty lexicon contents passed to marshall(...)");
            }

            jsonWriter.endObject();
        } else {
            throw new AmazonClientException("null lexicon object passed to marshall(...)");
        }
        jsonWriter.endObject();

        String snippet = stringWriter.toString();
        byte[] content = snippet.getBytes(UTF_8);
        request.setContent(new StringInputStream(snippet));
        request.addHeader("Content-Length", Integer.toString(content.length));
    } catch (JSONException e) {
        throw new AmazonClientException("Unable to marshall request to JSON", e);
    } catch (UnsupportedEncodingException e) {
        throw new AmazonClientException("Unable to marshall request to JSON", e);
    }
}

From source file:com.ivona.services.tts.model.transform.listvoices.ListVoicesPostRequestMarshaller.java

License:Open Source License

private void setRequestPayload(Request<ListVoicesRequest> request, ListVoicesRequest listVoicesRequest) {
    try {//  w  w w. ja  v a  2s.  c  o m
        StringWriter stringWriter = new StringWriter();
        JSONWriter jsonWriter = new JSONWriter(stringWriter);

        jsonWriter.object();
        if (listVoicesRequest.getVoice() != null) {
            Voice voice = listVoicesRequest.getVoice();

            jsonWriter.key(JSON_KEY_VOICE);
            jsonWriter.object();

            if (voice.getGender() != null) {
                jsonWriter.key(JSON_KEY_GENDER).value(voice.getGender());
            }
            if (voice.getLanguage() != null) {
                jsonWriter.key(JSON_KEY_LANGUAGE).value(voice.getLanguage());
            }
            if (voice.getName() != null) {
                jsonWriter.key(JSON_KEY_NAME).value(voice.getName());
            }

            jsonWriter.endObject();
        }
        jsonWriter.endObject();

        String snippet = stringWriter.toString();
        byte[] content = snippet.getBytes(UTF_8);
        request.setContent(new StringInputStream(snippet));
        request.addHeader("Content-Length", Integer.toString(content.length));
    } catch (Throwable t) {
        throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t);
    }
}

From source file:com.kodemore.aws.s3.KmS3Uploader.java

License:Open Source License

/**
 * Upload the string to the remote s3 repository.
 * The toPath (at s3) should NOT begin with a slash (/).
 *//*from   w ww.j  a v a  2s.  c  o  m*/
public void upload(String bucketName, String toPath, String fromSource) {
    try (StringInputStream is = new StringInputStream(fromSource)) {
        upload(bucketName, toPath, is);
    } catch (IOException ex) {
        throw Kmu.toRuntime(ex);
    }
}

From source file:io.helixservice.feature.configuration.dynamo.DynamoConfigResourceLocator.java

License:Open Source License

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private Optional<InputStream> getPropertiesFromDynamoDB(String resourcePath, Optional<InputStream> pResult) {
    Optional<InputStream> result = pResult;

    String environmentName = resourcePath.substring(0, resourcePath.lastIndexOf(APPLICATION_YAML_FILE));
    try {//  w  w w .  j  a v  a  2 s.  co  m
        Item item = configTable.getItem("environment", environmentName, "service", serviceName);
        if (item == null) {
            LOG.info("No configuration found for environment=" + environmentName
                    + "; creating default empty configuration");
            createEmptyConfiguration(environmentName);
        } else {
            String json = item.toJSON();
            JSONObject jsonObject = new JSONObject(json);
            JSONObject config = jsonObject.getJSONObject("config");

            ObjectMapper objectMapper = new ObjectMapper();
            Map mapOfValues = objectMapper.readValue(config.toString(), Map.class);

            ObjectMapper yamlObjectMapper = new ObjectMapper(new YAMLFactory());
            String yamlResult = yamlObjectMapper.writeValueAsString(mapOfValues);

            result = Optional.of(new StringInputStream(yamlResult));
        }
    } catch (Throwable t) {
        LOG.error("Unable to load configuration from DynamoDB for environment=" + environmentName, t);
    }

    return result;
}

From source file:io.sunrisedata.wikipedia.WikipediaPageRevision.java

License:Apache License

public void readFromXml(String xml) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new StringInputStream(xml));

    // and now the fun part

    NodeList n = doc.getChildNodes().item(0).getChildNodes();
    for (int i = 0; i < n.getLength(); i++) {
        Node node = n.item(i);/*www.  j  a  va  2s.  c o  m*/
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) node;
            switch (e.getTagName()) {
            case XML_TAG_CONTRIBUTOR:
                NodeList contribNodes = e.getChildNodes();
                for (int j = 0; j < contribNodes.getLength(); j++) {
                    Node contribNode = contribNodes.item(j);
                    if (contribNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element contribEl = (Element) contribNode;
                        switch (contribEl.getTagName()) {
                        case XML_TAG_CONTRIBUTOR_ID:
                            this.contributorId = contribEl.getTextContent();
                            break;
                        case XML_TAG_CONTRIBUTOR_IP:
                            this.contributorIp = contribEl.getTextContent();
                            break;
                        case XML_TAG_CONTRIBUTOR_USERNAME:
                            this.contributorUsername = contribEl.getTextContent();
                            break;
                        }
                    }
                }
                break;

            case XML_TAG_TEXT:
                contentWikiMarkup = e.getTextContent();
                if (e.hasAttribute(XML_ATTRIBUTE_TEXT_BYTES)) {
                    this.declaredContentLength = Integer.parseInt(e.getAttribute(XML_ATTRIBUTE_TEXT_BYTES));
                    if (this.declaredContentLength > 0 && isEmpty()) {
                        this.isMetadata = true;
                    }
                }
                // determine if article is a disambiguation, redirection, and/or stub page.
                // the first characters of the text must be equal to IDENTIFIER_REDIRECTION_UPPERCASE or IDENTIFIER_REDIRECTION_LOWERCASE
                this.isRedirect = contentWikiMarkup.startsWith(IDENTIFIER_REDIRECTION_LOWERCASE)
                        || contentWikiMarkup.startsWith(IDENTIFIER_REDIRECTION_UPPERCASE);

                // to be a stub, the article must contain the IDENTIFIER_STUB_WIKIPEDIA_NAMESPACE or IDENTIFIER_STUB_TEMPLATE
                this.isStub = contentWikiMarkup.contains(IDENTIFIER_STUB_TEMPLATE);

                break;

            case XML_TAG_ID:
                this.revisionId = e.getTextContent();
                break;

            case XML_TAG_TIMESTAMP:
                this.timestamp = e.getTextContent();
                break;

            case XML_TAG_MINOR:
                // presence of the empty <minor/> tag indicates it is a minor revision
                this.isMinor = true;
                break;

            case XML_TAG_COMMENT:
                this.comment = e.getTextContent();
                break;

            case XML_TAG_SHA1:
                this.sha1 = e.getTextContent();
                break;

            case XML_TAG_MODEL:
                this.model = e.getTextContent();
                break;

            case XML_TAG_FORMAT:
                this.format = e.getTextContent();
                break;

            case XML_TAG_PARENTID:
                this.parentRevisionId = e.getTextContent();
                break;
            }
        }
    }

}

From source file:org.cloudml.deployer.Scaler.java

License:Open Source License

private Deployment cloneCurrentModel() {
    //need to clone the model
    JsonCodec jsonCodec = new JsonCodec();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    jsonCodec.save(currentModel, baos);/* w  ww . ja va  2  s.  c  o  m*/

    Deployment targetModel = new Deployment();
    try {
        String aString = new String(baos.toByteArray(), "UTF-8");
        InputStream is = new StringInputStream(aString);
        targetModel = (Deployment) jsonCodec.load(is);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return targetModel;
}

From source file:org.cloudml.facade.RemoteFacade.java

License:Open Source License

@Override
public void handle(LoadDeployment command) {
    wsClient.send("!extended { name : LoadDeployment }");
    final File f = new File(command.getPathToModel());
    try {//  ww  w  .j a va 2s. c o m
        String json = new String(Files.readAllBytes(f.toPath()));
        JsonCodec jc = new JsonCodec();
        InputStream is = new StringInputStream(json);
        Deployment temp = (Deployment) jc.load(is);
        for (Provider p : temp.getProviders()) {
            if (p.getCredentials() instanceof FileCredentials) {
                String login = "";
                String password = "";
                if (p.getCredentials().getLogin() != null)
                    login = p.getCredentials().getLogin();
                if (p.getCredentials().getPassword() != null)
                    password = p.getCredentials().getPassword();
                MemoryCredentials mc = new MemoryCredentials(login, password);
                p.setCredentials(mc);
            }
        }
        for (VM v : temp.getComponents().onlyVMs()) {
            File file = new File(v.getPrivateKey());
            if (file.exists() && !file.isDirectory()) {
                String contentKey = FileUtils.readFileToString(new File(v.getPrivateKey()));
                v.setPrivateKey(contentKey);
            }
        }

        JsonCodec jsonCodec = new JsonCodec();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        jsonCodec.save(temp, baos);
        wsClient.send("!additional json-string:" + baos.toString());
        Thread.sleep(3000);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}