Search in sources :

Example 6 with StringInputStream

use of com.amazonaws.util.StringInputStream in project amazon-cognito-android by aws.

the class UpdateRecordsRequestMarshaller method marshall.

public Request<UpdateRecordsRequest> marshall(UpdateRecordsRequest updateRecordsRequest) {
    if (updateRecordsRequest == null) {
        throw new AmazonClientException("Invalid argument passed to marshall(...)");
    }
    Request<UpdateRecordsRequest> request = new DefaultRequest<UpdateRecordsRequest>(updateRecordsRequest, "AmazonCognitoSync");
    String target = "AWSCognitoSyncService.UpdateRecords";
    request.addHeader("X-Amz-Target", target);
    request.setHttpMethod(HttpMethodName.POST);
    if (updateRecordsRequest.getClientContext() != null)
        request.addHeader("x-amz-Client-Context", StringUtils.fromString(updateRecordsRequest.getClientContext()));
    String uriResourcePath = "/identitypools/{IdentityPoolId}/identities/{IdentityId}/datasets/{DatasetName}";
    uriResourcePath = uriResourcePath.replace("{IdentityPoolId}", (updateRecordsRequest.getIdentityPoolId() == null) ? "" : StringUtils.fromString(updateRecordsRequest.getIdentityPoolId()));
    uriResourcePath = uriResourcePath.replace("{IdentityId}", (updateRecordsRequest.getIdentityId() == null) ? "" : StringUtils.fromString(updateRecordsRequest.getIdentityId()));
    uriResourcePath = uriResourcePath.replace("{DatasetName}", (updateRecordsRequest.getDatasetName() == null) ? "" : StringUtils.fromString(updateRecordsRequest.getDatasetName()));
    uriResourcePath = uriResourcePath.replaceAll("//", "/");
    if (uriResourcePath.contains("?")) {
        String queryString = uriResourcePath.substring(uriResourcePath.indexOf("?") + 1);
        uriResourcePath = uriResourcePath.substring(0, uriResourcePath.indexOf("?"));
        for (String s : queryString.split("[;&]")) {
            String[] nameValuePair = s.split("=");
            if (nameValuePair.length == 2) {
                if (!(nameValuePair[1].isEmpty()))
                    request.addParameter(nameValuePair[0], nameValuePair[1]);
            }
        }
    }
    request.setResourcePath(uriResourcePath);
    try {
        StringWriter stringWriter = new StringWriter();
        AwsJsonWriter jsonWriter = JsonUtils.getJsonWriter(stringWriter);
        jsonWriter.beginObject();
        if (updateRecordsRequest.getDeviceId() != null) {
            jsonWriter.name("DeviceId").value(updateRecordsRequest.getDeviceId());
        }
        com.amazonaws.internal.ListWithAutoConstructFlag<RecordPatch> recordPatchesList = (com.amazonaws.internal.ListWithAutoConstructFlag<RecordPatch>) (updateRecordsRequest.getRecordPatches());
        if (recordPatchesList != null && !(recordPatchesList.isAutoConstruct() && recordPatchesList.isEmpty())) {
            jsonWriter.name("RecordPatches");
            jsonWriter.beginArray();
            for (RecordPatch recordPatchesListValue : recordPatchesList) {
                if (recordPatchesListValue != null) {
                    jsonWriter.beginObject();
                    if (recordPatchesListValue.getOp() != null) {
                        jsonWriter.name("Op").value(recordPatchesListValue.getOp());
                    }
                    if (recordPatchesListValue.getKey() != null) {
                        jsonWriter.name("Key").value(recordPatchesListValue.getKey());
                    }
                    if (recordPatchesListValue.getValue() != null) {
                        jsonWriter.name("Value").value(recordPatchesListValue.getValue());
                    }
                    if (recordPatchesListValue.getSyncCount() != null) {
                        jsonWriter.name("SyncCount").value(recordPatchesListValue.getSyncCount());
                    }
                    if (recordPatchesListValue.getDeviceLastModifiedDate() != null) {
                        jsonWriter.name("DeviceLastModifiedDate").value(recordPatchesListValue.getDeviceLastModifiedDate());
                    }
                    jsonWriter.endObject();
                }
            }
            jsonWriter.endArray();
        }
        if (updateRecordsRequest.getSyncSessionToken() != null) {
            jsonWriter.name("SyncSessionToken").value(updateRecordsRequest.getSyncSessionToken());
        }
        jsonWriter.endObject();
        jsonWriter.close();
        String snippet = stringWriter.toString();
        byte[] content = snippet.getBytes(UTF8);
        request.setContent(new StringInputStream(snippet));
        request.addHeader("Content-Length", Integer.toString(content.length));
        request.addHeader("Content-Type", "application/x-amz-json-1.0");
    } catch (Throwable t) {
        throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t);
    }
    return request;
}
Also used : DefaultRequest(com.amazonaws.DefaultRequest) AmazonClientException(com.amazonaws.AmazonClientException) AwsJsonWriter(com.amazonaws.util.json.AwsJsonWriter) StringInputStream(com.amazonaws.util.StringInputStream) StringWriter(java.io.StringWriter)

Example 7 with StringInputStream

use of com.amazonaws.util.StringInputStream in project nifi by apache.

the class TestFetchS3Object method testGetObjectVersion.

@Test
public void testGetObjectVersion() throws IOException {
    runner.setProperty(FetchS3Object.REGION, "us-east-1");
    runner.setProperty(FetchS3Object.BUCKET, "request-bucket");
    runner.setProperty(FetchS3Object.VERSION_ID, "${s3.version}");
    final Map<String, String> attrs = new HashMap<>();
    attrs.put("filename", "request-key");
    attrs.put("s3.version", "request-version");
    runner.enqueue(new byte[0], attrs);
    S3Object s3ObjectResponse = new S3Object();
    s3ObjectResponse.setBucketName("response-bucket-name");
    s3ObjectResponse.setObjectContent(new StringInputStream("Some Content"));
    ObjectMetadata metadata = Mockito.spy(ObjectMetadata.class);
    metadata.setContentDisposition("key/path/to/file.txt");
    Mockito.when(metadata.getVersionId()).thenReturn("response-version");
    s3ObjectResponse.setObjectMetadata(metadata);
    Mockito.when(mockS3Client.getObject(Mockito.any())).thenReturn(s3ObjectResponse);
    runner.run(1);
    ArgumentCaptor<GetObjectRequest> captureRequest = ArgumentCaptor.forClass(GetObjectRequest.class);
    Mockito.verify(mockS3Client, Mockito.times(1)).getObject(captureRequest.capture());
    GetObjectRequest request = captureRequest.getValue();
    assertEquals("request-bucket", request.getBucketName());
    assertEquals("request-key", request.getKey());
    assertEquals("request-version", request.getVersionId());
    runner.assertAllFlowFilesTransferred(FetchS3Object.REL_SUCCESS, 1);
    final List<MockFlowFile> ffs = runner.getFlowFilesForRelationship(FetchS3Object.REL_SUCCESS);
    MockFlowFile ff = ffs.get(0);
    ff.assertAttributeEquals("s3.bucket", "response-bucket-name");
    ff.assertAttributeEquals(CoreAttributes.FILENAME.key(), "file.txt");
    ff.assertAttributeEquals(CoreAttributes.PATH.key(), "key/path/to");
    ff.assertAttributeEquals(CoreAttributes.ABSOLUTE_PATH.key(), "key/path/to/file.txt");
    ff.assertAttributeEquals("s3.version", "response-version");
    ff.assertContentEquals("Some Content");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) StringInputStream(com.amazonaws.util.StringInputStream) HashMap(java.util.HashMap) S3Object(com.amazonaws.services.s3.model.S3Object) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) Test(org.junit.Test)

Example 8 with StringInputStream

use of com.amazonaws.util.StringInputStream in project nifi by apache.

the class TestFetchS3Object method testGetObject.

@Test
public void testGetObject() throws IOException {
    runner.setProperty(FetchS3Object.REGION, "us-east-1");
    runner.setProperty(FetchS3Object.BUCKET, "request-bucket");
    final Map<String, String> attrs = new HashMap<>();
    attrs.put("filename", "request-key");
    runner.enqueue(new byte[0], attrs);
    S3Object s3ObjectResponse = new S3Object();
    s3ObjectResponse.setBucketName("response-bucket-name");
    s3ObjectResponse.setKey("response-key");
    s3ObjectResponse.setObjectContent(new StringInputStream("Some Content"));
    ObjectMetadata metadata = Mockito.spy(ObjectMetadata.class);
    metadata.setContentDisposition("key/path/to/file.txt");
    metadata.setContentType("text/plain");
    metadata.setContentMD5("testMD5hash");
    Date expiration = new Date();
    metadata.setExpirationTime(expiration);
    metadata.setExpirationTimeRuleId("testExpirationRuleId");
    Map<String, String> userMetadata = new HashMap<>();
    userMetadata.put("userKey1", "userValue1");
    userMetadata.put("userKey2", "userValue2");
    metadata.setUserMetadata(userMetadata);
    metadata.setSSEAlgorithm("testAlgorithm");
    Mockito.when(metadata.getETag()).thenReturn("test-etag");
    s3ObjectResponse.setObjectMetadata(metadata);
    Mockito.when(mockS3Client.getObject(Mockito.any())).thenReturn(s3ObjectResponse);
    runner.run(1);
    ArgumentCaptor<GetObjectRequest> captureRequest = ArgumentCaptor.forClass(GetObjectRequest.class);
    Mockito.verify(mockS3Client, Mockito.times(1)).getObject(captureRequest.capture());
    GetObjectRequest request = captureRequest.getValue();
    assertEquals("request-bucket", request.getBucketName());
    assertEquals("request-key", request.getKey());
    assertNull(request.getVersionId());
    runner.assertAllFlowFilesTransferred(FetchS3Object.REL_SUCCESS, 1);
    final List<MockFlowFile> ffs = runner.getFlowFilesForRelationship(FetchS3Object.REL_SUCCESS);
    MockFlowFile ff = ffs.get(0);
    ff.assertAttributeEquals("s3.bucket", "response-bucket-name");
    ff.assertAttributeEquals(CoreAttributes.FILENAME.key(), "file.txt");
    ff.assertAttributeEquals(CoreAttributes.PATH.key(), "key/path/to");
    ff.assertAttributeEquals(CoreAttributes.ABSOLUTE_PATH.key(), "key/path/to/file.txt");
    ff.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(), "text/plain");
    ff.assertAttributeEquals("hash.value", "testMD5hash");
    ff.assertAttributeEquals("hash.algorithm", "MD5");
    ff.assertAttributeEquals("s3.etag", "test-etag");
    ff.assertAttributeEquals("s3.expirationTime", String.valueOf(expiration.getTime()));
    ff.assertAttributeEquals("s3.expirationTimeRuleId", "testExpirationRuleId");
    ff.assertAttributeEquals("userKey1", "userValue1");
    ff.assertAttributeEquals("userKey2", "userValue2");
    ff.assertAttributeEquals("s3.sseAlgorithm", "testAlgorithm");
    ff.assertContentEquals("Some Content");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) StringInputStream(com.amazonaws.util.StringInputStream) HashMap(java.util.HashMap) S3Object(com.amazonaws.services.s3.model.S3Object) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) Date(java.util.Date) Test(org.junit.Test)

Example 9 with StringInputStream

use of com.amazonaws.util.StringInputStream in project pipeline-aws-plugin by jenkinsci.

the class YAMLParameterFileParserTests method parseParameters.

@Test
public void parseParameters() throws IOException {
    YAMLParameterFileParser parser = new YAMLParameterFileParser();
    String json = "bar: foo";
    Collection<Parameter> parameters = parser.parseParams(new StringInputStream(json));
    Assertions.assertThat(parameters).containsExactlyInAnyOrder(new Parameter().withParameterKey("bar").withParameterValue("foo"));
}
Also used : StringInputStream(com.amazonaws.util.StringInputStream) Parameter(com.amazonaws.services.cloudformation.model.Parameter) Test(org.junit.Test)

Aggregations

StringInputStream (com.amazonaws.util.StringInputStream)9 Test (org.junit.Test)7 Parameter (com.amazonaws.services.cloudformation.model.Parameter)4 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)3 AmazonClientException (com.amazonaws.AmazonClientException)2 DefaultRequest (com.amazonaws.DefaultRequest)2 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)2 S3Object (com.amazonaws.services.s3.model.S3Object)2 AwsJsonWriter (com.amazonaws.util.json.AwsJsonWriter)2 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 MockFlowFile (org.apache.nifi.util.MockFlowFile)2 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)1 S3ObjectInputStream (com.amazonaws.services.s3.model.S3ObjectInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 Expression (org.springframework.expression.Expression)1 ValueExpression (org.springframework.integration.expression.ValueExpression)1