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.emc.vipr.s3.sample._01_CreateObject.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // create the ViPR S3 Client
    ViPRS3Client s3 = ViPRS3Factory.getS3Client();

    // retrieve object key/value from user
    System.out.println("Enter the object key:");
    String key = new BufferedReader(new InputStreamReader(System.in)).readLine();
    System.out.println("Enter the object content:");
    String content = new BufferedReader(new InputStreamReader(System.in)).readLine();

    // create the object in the demo bucket
    s3.putObject(ViPRS3Factory.S3_BUCKET, key, new StringInputStream(content), null);

    // print bucket key/value and content for validation
    System.out.println(//w w  w.  ja  v a  2 s. co m
            String.format("created object [%s/%s] with content: [%s]", ViPRS3Factory.S3_BUCKET, key, content));
}

From source file:com.emc.vipr.s3.sample._03_UpdateObject.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // create the ViPR S3 Client
    ViPRS3Client s3 = ViPRS3Factory.getS3Client();

    // retrieve the object key and new object value from user
    System.out.println("Enter the object key:");
    String key = new BufferedReader(new InputStreamReader(System.in)).readLine();
    System.out.println("Enter new object content:");
    String content = new BufferedReader(new InputStreamReader(System.in)).readLine();

    // update the object in the demo bucket
    PutObjectRequest updateRequest = new PutObjectRequest(ViPRS3Factory.S3_BUCKET, key,
            new StringInputStream(content), null);
    s3.putObject(updateRequest);/*from   w  ww.  j av a 2  s  .  co m*/

    // print out object key/value for validation
    System.out.println(String.format("update object [%s/%s] with new content: [%s]", ViPRS3Factory.S3_BUCKET,
            key, content));
}

From source file:com.emc.vipr.s3.sample._05_CreateObjectWithMetadata.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // create the ViPR S3 Client
    ViPRS3Client s3 = ViPRS3Factory.getS3Client();

    // retrieve the object key and value from user
    System.out.println("Enter the object key:");
    String key = new BufferedReader(new InputStreamReader(System.in)).readLine();
    System.out.println("Enter the object content:");
    String content = new BufferedReader(new InputStreamReader(System.in)).readLine();

    //retrieve the object metadata key and value from user
    System.out.println("Enter the metadata key:");
    String metaKey = new BufferedReader(new InputStreamReader(System.in)).readLine();
    System.out.println("Enter the metadata content:");
    String metaValue = new BufferedReader(new InputStreamReader(System.in)).readLine();

    // create the metadata
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.addUserMetadata(metaKey, metaValue);

    // create the object with the metadata in the demo bucket
    s3.putObject(ViPRS3Factory.S3_BUCKET, key, new StringInputStream(content), metadata);

    // print out object key/value and metadata key/value for validation
    System.out.println(String.format("created object [%s/%s] with metadata [%s=%s] and content: [%s]",
            ViPRS3Factory.S3_BUCKET, key, metaKey, metaValue, content));
}

From source file:com.emc.vipr.s3.sample._09_UpdateAppendExtensions.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // create the ViPR S3 Client
    ViPRS3Client s3 = ViPRS3Factory.getS3Client();

    // object key to create, update, and append
    String key = "update-append.txt";
    String bucketName = ViPRS3Factory.S3_BUCKET; // bucket to create object in
    String content = "Hello World!"; // initial object content
    int worldIndex = content.indexOf("World"); // Starting index
    StringInputStream stream;//from  www.j a  v a  2 s  .  c o  m

    // first create an initial object
    stream = new StringInputStream(content);
    System.out.println(String.format("creating initial object %s/%s", bucketName, key));
    s3.putObject(bucketName, key, stream, null);

    // read object and print content
    System.out.println(String.format("initial object %s/%s with content: [%s]", bucketName, key,
            readObject(s3, bucketName, key)));

    // update the object in the middle
    String content2 = "Universe!";
    System.out.println(String.format("updating object at offset %d", worldIndex));
    s3.updateObject(bucketName, key, new StringInputStream(content2), null, worldIndex);

    // read object and print content
    System.out.println(String.format("updated object %s/%s with content: [%s]", bucketName, key,
            readObject(s3, bucketName, key)));

    // append to the object
    String content3 = " ... and all!!";
    System.out.println(String.format("appending object at offset %d", content.length()));
    s3.updateObject(bucketName, key, new StringInputStream(content3), null, content.length());

    // read object and print content
    System.out.println(String.format("appended object %s/%s with content: [%s]", bucketName, key,
            readObject(s3, bucketName, key)));

    // create a sparse object by appending past the end of the object
    String content4 = "#last byte#";
    System.out.println(String.format("sparse append object at offset %d", 40));
    s3.updateObject(bucketName, key, new StringInputStream(content4), null, 40);

    // read object and print content
    System.out.println(String.format("sparse append object %s/%s with content: [%s]", bucketName, key,
            readObject(s3, bucketName, key)));

}

From source file:com.emc.vipr.s3.sample._10_AtomicAppend.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // create the ViPR S3 Client
    ViPRS3Client s3 = ViPRS3Factory.getS3Client();

    // object key to create, update, and append
    String key = "atomic-append.txt";
    String bucketName = ViPRS3Factory.S3_BUCKET; // bucket to create object in
    String content = "Hello World!"; // initial object content
    StringInputStream stream;/*from w  ww  .  j a va 2  s. com*/

    // first create an initial object
    stream = new StringInputStream(content);
    System.out.println(String.format("creating initial object %s/%s", bucketName, key));
    s3.putObject(bucketName, key, stream, null);

    // read object and print content
    System.out.println(String.format("initial object %s/%s with content: [%s]", bucketName, key,
            readObject(s3, bucketName, key)));

    // append to the end of the object
    String content2 = " ... and Universe!!";
    AppendObjectResult result = s3.appendObject(bucketName, key, new StringInputStream(content2), null);

    // the offset at which our appended data was written is returned
    // (this is the previous size of the object)
    long appendOffset = result.getAppendOffset();

    // read object and print content
    System.out.println(String.format("final object %s/%s with content: [%s]", bucketName, key,
            readObject(s3, bucketName, key)));
}

From source file:com.greglturnquist.springagram.fileservice.s3.FileService.java

License:Apache License

/**
 * Parse the {@link AmazonS3Exception} error result to capture the endpoint for
 * redirection.//from  w w  w  .j  a  v a  2  s  . c  om
 *
 * @param e
 */
private void updateEndpoint(AmazonS3Exception e) {

    try {
        Document errorResponseDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new StringInputStream(e.getErrorResponseXml()));

        XPathExpression endpointXpathExtr = XPathFactory.newInstance().newXPath().compile("/Error/Endpoint");

        this.s3Client.setEndpoint(endpointXpathExtr.evaluate(errorResponseDoc));
    } catch (Exception ex) {
        throw new RuntimeException(e);
    }
}

From source file:com.intuit.tank.http.xml.GenericXMLHandler.java

License:Open Source License

/**
 * Constructor/*from ww  w  .  j a v a 2 s  .  co m*/
 * 
 * @param xmlFile
 *            The string representation of the xml data
 */
public GenericXMLHandler(String xmlFile) {

    if (!xmlFile.equals("")) {
        this.xml = xmlFile;
        try {
            this.xmlFile = null;
            this.xmlDocument = new org.jdom.Document();
            SAXBuilder builder = new SAXBuilder();
            builder.setValidation(false);
            // logger.debug("XML string to load: "+xmlFile);
            xmlFile = xmlFile.substring(xmlFile.indexOf("<"));
            this.xmlDocument = builder.build(new StringReader(xmlFile));
            this.namespaces = new HashMap<String, String>();
            this.dDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                    .parse(new StringInputStream(xml));
        } catch (Exception ex) {
            this.xmlDocument = null;
            logger.error(LogUtil.getLogMessage("Error parsing xml File: " + xmlFile + ": " + ex.getMessage(),
                    LogEventType.Http));
        }
    }
}

From source file:com.ivona.services.tts.model.transform.createspeech.CreateSpeechPostRequestMarshaller.java

License:Open Source License

private void setRequestPayload(Request<CreateSpeechRequest> request, CreateSpeechRequest createSpeechRequest) {
    try {/*from w  ww  . j av  a2  s  .c o m*/
        StringWriter stringWriter = new StringWriter();
        JSONWriter jsonWriter = new JSONWriter(stringWriter);

        jsonWriter.object();

        if (createSpeechRequest.getInput() != null) {
            Input input = createSpeechRequest.getInput();

            jsonWriter.key(JSON_KEY_INPUT);
            jsonWriter.object();
            if (input.getData() != null) {
                jsonWriter.key(JSON_KEY_INPUT_DATA).value(input.getData());
            }
            if (input.getType() != null) {
                jsonWriter.key(JSON_KEY_INPUT_TYPE).value(input.getType());
            }
            jsonWriter.endObject();
        }

        if (createSpeechRequest.getOutputFormat() != null) {
            OutputFormat outputFormat = createSpeechRequest.getOutputFormat();

            jsonWriter.key(JSON_KEY_OUTPUT_FORMAT);
            jsonWriter.object();
            if (outputFormat.getCodec() != null) {
                jsonWriter.key(JSON_KEY_OUTPUT_FORMAT_CODEC).value(outputFormat.getCodec());
            }
            if (outputFormat.getSampleRate() != null && outputFormat.getSampleRate() > 0) {
                jsonWriter.key(JSON_KEY_OUTPUT_FORMAT_SAMPLE_RATE).value((long) outputFormat.getSampleRate());
            }
            if (outputFormat.getSpeechMarks() != null) {
                jsonWriter.key(JSON_KEY_OUTPUT_FORMAT_SPEECHMARKS);
                jsonWriter.object();
                SpeechMarks speechMarks = outputFormat.getSpeechMarks();
                if (speechMarks != null) {
                    if (speechMarks.isSentence()) {
                        jsonWriter.key(JSON_KEY_OUTPUT_FORMAT_SPEECHMARKS_SENTENCE).value(true);
                    }
                    if (speechMarks.isSsml()) {
                        jsonWriter.key(JSON_KEY_OUTPUT_FORMAT_SPEECHMARKS_SSML).value(true);
                    }
                    if (speechMarks.isViseme()) {
                        jsonWriter.key(JSON_KEY_OUTPUT_FORMAT_SPEECHMARKS_VISEME).value(true);
                    }
                    if (speechMarks.isWord()) {
                        jsonWriter.key(JSON_KEY_OUTPUT_FORMAT_SPEECHMARKS_WORD).value(true);
                    }
                }
                jsonWriter.endObject();
            }
            jsonWriter.endObject();
        }

        if (createSpeechRequest.getParameters() != null) {
            Parameters parameters = createSpeechRequest.getParameters();

            jsonWriter.key(JSON_KEY_PARAMETERS);
            jsonWriter.object();
            if (parameters.getRate() != null) {
                jsonWriter.key(JSON_KEY_PARAMETERS_RATE).value(parameters.getRate());
            }
            if (parameters.getVolume() != null) {
                jsonWriter.key(JSON_KEY_PARAMETERS_VOLUME).value(parameters.getVolume());
            }
            if (parameters.getSentenceBreak() != null) {
                jsonWriter.key(JSON_KEY_PARAMETERS_SENTENCE_BREAK).value((long) parameters.getSentenceBreak());
            }
            if (parameters.getParagraphBreak() != null) {
                jsonWriter.key(JSON_KEY_PARAMETERS_PARAGRAPH_BREAK)
                        .value((long) parameters.getParagraphBreak());
            }
            jsonWriter.endObject();
        }

        if (createSpeechRequest.getLexiconNames() != null) {
            List<String> names = createSpeechRequest.getLexiconNames();
            jsonWriter.key(JSON_KEY_LEXICONS).value(names);
        }

        if (createSpeechRequest.getVoice() != null) {
            Voice voice = createSpeechRequest.getVoice();

            jsonWriter.key(JSON_KEY_VOICE);
            jsonWriter.object();
            if (voice.getGender() != null) {
                jsonWriter.key(JSON_KEY_VOICE_GENDER).value(voice.getGender());
            }
            if (voice.getLanguage() != null) {
                jsonWriter.key(JSON_KEY_VOICE_LANGUAGE).value(voice.getLanguage());
            }
            if (voice.getName() != null) {
                jsonWriter.key(JSON_KEY_VOICE_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 (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.DeleteLexiconPostRequestMarshaller.java

License:Open Source License

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

    try {// w  ww  . j  ava2s  . c  o m
        jsonWriter.object();

        if (MarshallerHelper.stringIsNotBlank(deleteLexiconRequest.getLexiconName())) {
            jsonWriter.key("Name").value(deleteLexiconRequest.getLexiconName());
        } else {
            throw new AmazonClientException("null or empty lexicon name 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.lexicons.GetLexiconPostRequestMarshaller.java

License:Open Source License

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

    try {/*  ww  w  .java2 s  . com*/
        jsonWriter.object();

        if (MarshallerHelper.stringIsNotBlank(getLexiconRequest.getLexiconName())) {
            jsonWriter.key("Name").value(getLexiconRequest.getLexiconName());
        } else {
            throw new AmazonClientException("null or empty lexicon name 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);
    }
}