public static JsonErrorResponse fromResponse(HttpResponse response) throws IOException { int statusCode = response.getStatusCode(); // parse error body Map<String, String> map = JsonUtils.jsonToMap(new BufferedReader(new InputStreamReader(response.getContent()))); /* * Services using AWS JSON 1.1 protocol with HTTP binding send the * error type information in the response headers, instead of the * content. */ String errorCode = response.getHeaders().get(X_AMZN_ERROR_TYPE); if (errorCode != null) { int separator = errorCode.indexOf(':'); if (separator != -1) { errorCode = errorCode.substring(0, separator); } } else if (map.containsKey("__type")) { // check body otherwise String type = map.get("__type"); int separator = type.lastIndexOf("#"); errorCode = type.substring(separator + 1); } return new JsonErrorResponse(statusCode, errorCode, map); }
public Request<PutEventsRequest> marshall(PutEventsRequest putEventsRequest) { if (putEventsRequest == null) { throw new AmazonClientException("Invalid argument passed to marshall(PutEventsRequest)"); } Request<PutEventsRequest> request = new DefaultRequest<PutEventsRequest>(putEventsRequest, "AmazonMobileAnalytics"); request.setHttpMethod(HttpMethodName.POST); if (putEventsRequest.getClientContext() != null) { request.addHeader( "x-amz-Client-Context", StringUtils.fromString(putEventsRequest.getClientContext())); } if (putEventsRequest.getClientContextEncoding() != null) { request.addHeader( "x-amz-Client-Context-Encoding", StringUtils.fromString(putEventsRequest.getClientContextEncoding())); } String uriResourcePath = "/2014-06-05/events"; request.setResourcePath(uriResourcePath); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gos = new GZIPOutputStream(baos, 8192); Writer writer = new OutputStreamWriter(gos, StringUtils.UTF8); AwsJsonWriter jsonWriter = JsonUtils.getJsonWriter(writer); jsonWriter.beginObject(); if (putEventsRequest.getEvents() != null) { java.util.List<Event> events = putEventsRequest.getEvents(); jsonWriter.name("events"); jsonWriter.beginArray(); for (Event eventsItem : events) { if (eventsItem != null) { EventJsonMarshaller.getInstance().marshall(eventsItem, jsonWriter); } } jsonWriter.endArray(); } jsonWriter.endObject(); jsonWriter.flush(); gos.finish(); writer.close(); byte[] content = baos.toByteArray(); request.setContent(new ByteArrayInputStream(content)); request.addHeader("Content-Length", Integer.toString(content.length)); request.addHeader("Content-Encoding", "gzip"); } catch (Throwable t) { throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t); } if (!request.getHeaders().containsKey("Content-Type")) { request.addHeader("Content-Type", "application/x-amz-json-1.0"); } return request; }
@Test public void testStringJsonUnmarshaller() throws Exception { StringWriter sw = new StringWriter(); AwsJsonWriter jw = JsonUtils.getJsonWriter(sw); jw.beginObject(); jw.name("s"); jw.value("String"); jw.endObject(); String json = sw.toString(); StringReader sr = new StringReader(json); AwsJsonReader jr = JsonUtils.getJsonReader(sr); JsonUnmarshallerContext context = new JsonUnmarshallerContext(jr); context.getReader().beginObject(); context.getReader().nextName(); SimpleTypeJsonUnmarshallers.StringJsonUnmarshaller sUnmarshaller = SimpleTypeJsonUnmarshallers.StringJsonUnmarshaller.getInstance(); assertEquals(sUnmarshaller.unmarshall(context), "String"); }
@Test public void testLongJsonUnmarshaller() throws Exception { Long l = new Long(5L); StringWriter sw = new StringWriter(); AwsJsonWriter jw = JsonUtils.getJsonWriter(sw); jw.beginObject(); jw.name("l"); jw.value(l); jw.endObject(); String json = sw.toString(); StringReader sr = new StringReader(json); AwsJsonReader jr = JsonUtils.getJsonReader(sr); JsonUnmarshallerContext context = new JsonUnmarshallerContext(jr); context.getReader().beginObject(); context.getReader().nextName(); SimpleTypeJsonUnmarshallers.LongJsonUnmarshaller lUnmarshaller = SimpleTypeJsonUnmarshallers.LongJsonUnmarshaller.getInstance(); Long unmarshalledL = lUnmarshaller.unmarshall(context); assertEquals(unmarshalledL.longValue(), 5L); }
@Test public void testIntegerJsonUnmarshaller() throws Exception { Integer i = new Integer(5); StringWriter sw = new StringWriter(); AwsJsonWriter jw = JsonUtils.getJsonWriter(sw); jw.beginObject(); jw.name("i"); jw.value(i); jw.endObject(); String json = sw.toString(); StringReader sr = new StringReader(json); AwsJsonReader jr = JsonUtils.getJsonReader(sr); JsonUnmarshallerContext context = new JsonUnmarshallerContext(jr); context.getReader().beginObject(); context.getReader().nextName(); SimpleTypeJsonUnmarshallers.IntegerJsonUnmarshaller iUnmarshaller = SimpleTypeJsonUnmarshallers.IntegerJsonUnmarshaller.getInstance(); Integer unmarshalledI = iUnmarshaller.unmarshall(context); assertEquals(unmarshalledI.intValue(), 5); }
@Test public void testDoubleJsonUnmarshaller() throws Exception { Double dub = new Double(5.5); StringWriter sw = new StringWriter(); AwsJsonWriter jw = JsonUtils.getJsonWriter(sw); jw.beginObject(); jw.name("dub"); jw.value(dub); jw.endObject(); String json = sw.toString(); StringReader sr = new StringReader(json); AwsJsonReader jr = JsonUtils.getJsonReader(sr); JsonUnmarshallerContext context = new JsonUnmarshallerContext(jr); context.getReader().beginObject(); context.getReader().nextName(); SimpleTypeJsonUnmarshallers.DoubleJsonUnmarshaller dubUnmarshaller = SimpleTypeJsonUnmarshallers.DoubleJsonUnmarshaller.getInstance(); Double unmarshalledDub = dubUnmarshaller.unmarshall(context); assertEquals(unmarshalledDub.doubleValue(), 5.5, .001); }
@Test public void testBigDecimalJsonUnmarshaller() throws Exception { BigDecimal bd = new BigDecimal("1.5"); StringWriter sw = new StringWriter(); AwsJsonWriter jw = JsonUtils.getJsonWriter(sw); jw.beginObject(); jw.name("bd"); jw.value(bd.toPlainString()); jw.endObject(); String json = sw.toString(); StringReader sr = new StringReader(json); AwsJsonReader jr = JsonUtils.getJsonReader(sr); JsonUnmarshallerContext context = new JsonUnmarshallerContext(jr); context.getReader().beginObject(); context.getReader().nextName(); SimpleTypeJsonUnmarshallers.BigDecimalJsonUnmarshaller bdUnmarshaller = SimpleTypeJsonUnmarshallers.BigDecimalJsonUnmarshaller.getInstance(); BigDecimal unmarshalledBD = bdUnmarshaller.unmarshall(context); assertEquals(unmarshalledBD.toPlainString(), bd.toPlainString()); }
@Test public void testByteJsonUnmarshaller() throws Exception { StringWriter sw = new StringWriter(); AwsJsonWriter jw = JsonUtils.getJsonWriter(sw); jw.beginObject(); jw.name("b"); jw.value(Byte.valueOf("127")); jw.endObject(); String json = sw.toString(); StringReader sr = new StringReader(json); AwsJsonReader jr = JsonUtils.getJsonReader(sr); JsonUnmarshallerContext context = new JsonUnmarshallerContext(jr); context.getReader().beginObject(); context.getReader().nextName(); SimpleTypeJsonUnmarshallers.ByteJsonUnmarshaller bUnmarshaller = SimpleTypeJsonUnmarshallers.ByteJsonUnmarshaller.getInstance(); Byte unmarshalledB = bUnmarshaller.unmarshall(context); Byte expected = Byte.valueOf("127"); assertTrue(unmarshalledB.equals(expected)); }
@Test public void testByteBufferJsonUnmarshaller() throws Exception { StringWriter sw = new StringWriter(); AwsJsonWriter jw = JsonUtils.getJsonWriter(sw); jw.beginObject(); jw.name("bb"); jw.value(Base64.encodeAsString("byte".getBytes())); jw.endObject(); String json = sw.toString(); StringReader sr = new StringReader(json); AwsJsonReader jr = JsonUtils.getJsonReader(sr); JsonUnmarshallerContext context = new JsonUnmarshallerContext(jr); context.getReader().beginObject(); context.getReader().nextName(); SimpleTypeJsonUnmarshallers.ByteBufferJsonUnmarshaller bbUnmarshaller = SimpleTypeJsonUnmarshallers.ByteBufferJsonUnmarshaller.getInstance(); ByteBuffer unmarshalledBb = bbUnmarshaller.unmarshall(context); ByteBuffer expected = ByteBuffer.wrap("byte".getBytes()); assertTrue(unmarshalledBb.equals(expected)); }
@Test public void testBigIntegerJsonUnmarshaller() throws Exception { BigInteger bi = new BigInteger("13459712934871293847891231293874"); StringWriter sw = new StringWriter(); AwsJsonWriter jw = JsonUtils.getJsonWriter(sw); jw.beginObject(); jw.name("bi"); jw.value(bi.toString()); jw.endObject(); String json = sw.toString(); StringReader sr = new StringReader(json); AwsJsonReader jr = JsonUtils.getJsonReader(sr); JsonUnmarshallerContext context = new JsonUnmarshallerContext(jr); context.getReader().beginObject(); context.getReader().nextName(); SimpleTypeJsonUnmarshallers.BigIntegerJsonUnmarshaller biUnmarshaller = SimpleTypeJsonUnmarshallers.BigIntegerJsonUnmarshaller.getInstance(); BigInteger unmarshalledBI = biUnmarshaller.unmarshall(context); assertEquals(unmarshalledBI.toString(), bi.toString()); }
@Test public void testBooleanJsonUnmarshaller() throws Exception { StringWriter sw = new StringWriter(); AwsJsonWriter jw = JsonUtils.getJsonWriter(sw); jw.beginObject(); jw.name("boolean"); jw.value("true"); jw.endObject(); String json = sw.toString(); StringReader sr = new StringReader(json); AwsJsonReader jr = JsonUtils.getJsonReader(sr); JsonUnmarshallerContext context = new JsonUnmarshallerContext(jr); context.getReader().beginObject(); context.getReader().nextName(); SimpleTypeJsonUnmarshallers.BooleanJsonUnmarshaller bUnmarshaller = SimpleTypeJsonUnmarshallers.BooleanJsonUnmarshaller.getInstance(); Boolean unmarshalledB = bUnmarshaller.unmarshall(context); assertTrue(unmarshalledB); }
@Test public void testFloatJsonUnmarshaller() throws Exception { Float f = new Float(5.5); StringWriter sw = new StringWriter(); AwsJsonWriter jw = JsonUtils.getJsonWriter(sw); jw.beginObject(); jw.name("f"); jw.value(f); jw.endObject(); String json = sw.toString(); StringReader sr = new StringReader(json); AwsJsonReader jr = JsonUtils.getJsonReader(sr); JsonUnmarshallerContext context = new JsonUnmarshallerContext(jr); context.getReader().beginObject(); context.getReader().nextName(); SimpleTypeJsonUnmarshallers.FloatJsonUnmarshaller fUnmarshaller = SimpleTypeJsonUnmarshallers.FloatJsonUnmarshaller.getInstance(); Float unmarshalledF = fUnmarshaller.unmarshall(context); assertEquals(unmarshalledF.floatValue(), 5.5, .001); }
public Request<RetireGrantRequest> marshall(RetireGrantRequest retireGrantRequest) { if (retireGrantRequest == null) { throw new AmazonClientException("Invalid argument passed to marshall(RetireGrantRequest)"); } Request<RetireGrantRequest> request = new DefaultRequest<RetireGrantRequest>(retireGrantRequest, "AWSKMS"); String target = "TrentService.RetireGrant"; request.addHeader("X-Amz-Target", target); request.setHttpMethod(HttpMethodName.POST); String uriResourcePath = "/"; request.setResourcePath(uriResourcePath); try { StringWriter stringWriter = new StringWriter(); AwsJsonWriter jsonWriter = JsonUtils.getJsonWriter(stringWriter); jsonWriter.beginObject(); if (retireGrantRequest.getGrantToken() != null) { String grantToken = retireGrantRequest.getGrantToken(); jsonWriter.name("GrantToken"); jsonWriter.value(grantToken); } if (retireGrantRequest.getKeyId() != null) { String keyId = retireGrantRequest.getKeyId(); jsonWriter.name("KeyId"); jsonWriter.value(keyId); } if (retireGrantRequest.getGrantId() != null) { String grantId = retireGrantRequest.getGrantId(); jsonWriter.name("GrantId"); jsonWriter.value(grantId); } 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)); } catch (Throwable t) { throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t); } if (!request.getHeaders().containsKey("Content-Type")) { request.addHeader("Content-Type", "application/x-amz-json-1.1"); } return request; }
@Test public void testDateJsonUnmarshaller() throws Exception { Date date = new Date(); date.setTime(1000); StringWriter sw = new StringWriter(); AwsJsonWriter jw = JsonUtils.getJsonWriter(sw); jw.beginObject(); jw.name("date"); jw.value(date); jw.endObject(); String json = sw.toString(); StringReader sr = new StringReader(json); AwsJsonReader jr = JsonUtils.getJsonReader(sr); JsonUnmarshallerContext context = new JsonUnmarshallerContext(jr); context.getReader().beginObject(); context.getReader().nextName(); SimpleTypeJsonUnmarshallers.DateJsonUnmarshaller dateUnmarshaller = SimpleTypeJsonUnmarshallers.DateJsonUnmarshaller.getInstance(); Date unmarshalledDate = dateUnmarshaller.unmarshall(context); assertEquals(unmarshalledDate.getTime(), date.getTime()); }
public Request<GetRecordsRequest> marshall(GetRecordsRequest getRecordsRequest) { if (getRecordsRequest == null) { throw new AmazonClientException("Invalid argument passed to marshall(GetRecordsRequest)"); } Request<GetRecordsRequest> request = new DefaultRequest<GetRecordsRequest>(getRecordsRequest, "AmazonKinesis"); String target = "Kinesis_20131202.GetRecords"; request.addHeader("X-Amz-Target", target); request.setHttpMethod(HttpMethodName.POST); String uriResourcePath = "/"; request.setResourcePath(uriResourcePath); try { StringWriter stringWriter = new StringWriter(); AwsJsonWriter jsonWriter = JsonUtils.getJsonWriter(stringWriter); jsonWriter.beginObject(); if (getRecordsRequest.getShardIterator() != null) { String shardIterator = getRecordsRequest.getShardIterator(); jsonWriter.name("ShardIterator"); jsonWriter.value(shardIterator); } if (getRecordsRequest.getLimit() != null) { Integer limit = getRecordsRequest.getLimit(); jsonWriter.name("Limit"); jsonWriter.value(limit); } 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)); } catch (Throwable t) { throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t); } if (!request.getHeaders().containsKey("Content-Type")) { request.addHeader("Content-Type", "application/x-amz-json-1.1"); } return request; }
public Request<IncreaseStreamRetentionPeriodRequest> marshall( IncreaseStreamRetentionPeriodRequest increaseStreamRetentionPeriodRequest) { if (increaseStreamRetentionPeriodRequest == null) { throw new AmazonClientException("Invalid argument passed to marshall(...)"); } Request<IncreaseStreamRetentionPeriodRequest> request = new DefaultRequest<IncreaseStreamRetentionPeriodRequest>( increaseStreamRetentionPeriodRequest, "AmazonKinesis"); String target = "Kinesis_20131202.IncreaseStreamRetentionPeriod"; request.addHeader("X-Amz-Target", target); request.setHttpMethod(HttpMethodName.POST); request.setResourcePath(""); try { StringWriter stringWriter = new StringWriter(); AwsJsonWriter jsonWriter = JsonUtils.getJsonWriter(stringWriter); jsonWriter.beginObject(); if (increaseStreamRetentionPeriodRequest.getStreamName() != null) { jsonWriter.name("StreamName").value(increaseStreamRetentionPeriodRequest.getStreamName()); } if (increaseStreamRetentionPeriodRequest.getRetentionPeriodHours() != null) { jsonWriter .name("RetentionPeriodHours") .value(increaseStreamRetentionPeriodRequest.getRetentionPeriodHours()); } 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.1"); } catch (Throwable t) { throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t); } return request; }
/** * Adds mappings to a ContentValues object for the data in the passed in ObjectMetadata * * @param metadata The ObjectMetadata the content values should be filled with * @return the ContentValues */ private ContentValues generateContentValuesForObjectMetadata(ObjectMetadata metadata) { ContentValues values = new ContentValues(); values.put( TransferTable.COLUMN_USER_METADATA, JsonUtils.mapToString(metadata.getUserMetadata())); values.put(TransferTable.COLUMN_HEADER_CONTENT_TYPE, metadata.getContentType()); values.put(TransferTable.COLUMN_HEADER_CONTENT_ENCODING, metadata.getContentEncoding()); values.put(TransferTable.COLUMN_HEADER_CACHE_CONTROL, metadata.getCacheControl()); values.put(TransferTable.COLUMN_CONTENT_MD5, metadata.getContentMD5()); values.put(TransferTable.COLUMN_HEADER_CONTENT_DISPOSITION, metadata.getContentDisposition()); values.put(TransferTable.COLUMN_SSE_ALGORITHM, metadata.getSSEAlgorithm()); values.put(TransferTable.COLUMN_EXPIRATION_TIME_RULE_ID, metadata.getExpirationTimeRuleId()); if (metadata.getHttpExpiresDate() != null) { values.put( TransferTable.COLUMN_HTTP_EXPIRES_DATE, String.valueOf(metadata.getHttpExpiresDate().getTime())); } return values; }
public Request<ListKeyPoliciesRequest> marshall(ListKeyPoliciesRequest listKeyPoliciesRequest) { if (listKeyPoliciesRequest == null) { throw new AmazonClientException("Invalid argument passed to marshall(...)"); } Request<ListKeyPoliciesRequest> request = new DefaultRequest<ListKeyPoliciesRequest>(listKeyPoliciesRequest, "AWSKMS"); String target = "TrentService.ListKeyPolicies"; request.addHeader("X-Amz-Target", target); request.setHttpMethod(HttpMethodName.POST); request.setResourcePath(""); try { StringWriter stringWriter = new StringWriter(); AwsJsonWriter jsonWriter = JsonUtils.getJsonWriter(stringWriter); jsonWriter.beginObject(); if (listKeyPoliciesRequest.getKeyId() != null) { jsonWriter.name("KeyId").value(listKeyPoliciesRequest.getKeyId()); } if (listKeyPoliciesRequest.getLimit() != null) { jsonWriter.name("Limit").value(listKeyPoliciesRequest.getLimit()); } if (listKeyPoliciesRequest.getMarker() != null) { jsonWriter.name("Marker").value(listKeyPoliciesRequest.getMarker()); } 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.1"); } catch (Throwable t) { throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t); } return request; }
public Request<ListUsersRequest> marshall(ListUsersRequest listUsersRequest) { if (listUsersRequest == null) { throw new AmazonClientException("Invalid argument passed to marshall(ListUsersRequest)"); } Request<ListUsersRequest> request = new DefaultRequest<ListUsersRequest>(listUsersRequest, "AmazonCognitoIdentityProvider"); String target = "AWSCognitoIdentityProviderService.ListUsers"; request.addHeader("X-Amz-Target", target); request.setHttpMethod(HttpMethodName.POST); String uriResourcePath = "/"; request.setResourcePath(uriResourcePath); try { StringWriter stringWriter = new StringWriter(); AwsJsonWriter jsonWriter = JsonUtils.getJsonWriter(stringWriter); jsonWriter.beginObject(); if (listUsersRequest.getUserPoolId() != null) { String userPoolId = listUsersRequest.getUserPoolId(); jsonWriter.name("UserPoolId"); jsonWriter.value(userPoolId); } if (listUsersRequest.getAttributesToGet() != null) { java.util.List<String> attributesToGet = listUsersRequest.getAttributesToGet(); jsonWriter.name("AttributesToGet"); jsonWriter.beginArray(); for (String attributesToGetItem : attributesToGet) { if (attributesToGetItem != null) { jsonWriter.value(attributesToGetItem); } } jsonWriter.endArray(); } if (listUsersRequest.getLimit() != null) { Integer limit = listUsersRequest.getLimit(); jsonWriter.name("Limit"); jsonWriter.value(limit); } if (listUsersRequest.getPaginationToken() != null) { String paginationToken = listUsersRequest.getPaginationToken(); jsonWriter.name("PaginationToken"); jsonWriter.value(paginationToken); } if (listUsersRequest.getFilter() != null) { String filter = listUsersRequest.getFilter(); jsonWriter.name("Filter"); jsonWriter.value(filter); } 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)); } catch (Throwable t) { throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t); } if (!request.getHeaders().containsKey("Content-Type")) { request.addHeader("Content-Type", "application/x-amz-json-1.1"); } return request; }