Search in sources :

Example 1 with DefaultRequest

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

the class RegisterDeviceRequestMarshaller method marshall.

public Request<RegisterDeviceRequest> marshall(RegisterDeviceRequest registerDeviceRequest) {
    if (registerDeviceRequest == null) {
        throw new AmazonClientException("Invalid argument passed to marshall(...)");
    }
    Request<RegisterDeviceRequest> request = new DefaultRequest<RegisterDeviceRequest>(registerDeviceRequest, "AmazonCognitoSync");
    String target = "AWSCognitoSyncService.RegisterDevice";
    request.addHeader("X-Amz-Target", target);
    request.setHttpMethod(HttpMethodName.POST);
    String uriResourcePath = "/identitypools/{IdentityPoolId}/identity/{IdentityId}/device";
    uriResourcePath = uriResourcePath.replace("{IdentityPoolId}", (registerDeviceRequest.getIdentityPoolId() == null) ? "" : StringUtils.fromString(registerDeviceRequest.getIdentityPoolId()));
    uriResourcePath = uriResourcePath.replace("{IdentityId}", (registerDeviceRequest.getIdentityId() == null) ? "" : StringUtils.fromString(registerDeviceRequest.getIdentityId()));
    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 (registerDeviceRequest.getPlatform() != null) {
            jsonWriter.name("Platform").value(registerDeviceRequest.getPlatform());
        }
        if (registerDeviceRequest.getToken() != null) {
            jsonWriter.name("Token").value(registerDeviceRequest.getToken());
        }
        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 : AwsJsonWriter(com.amazonaws.util.json.AwsJsonWriter) StringInputStream(com.amazonaws.util.StringInputStream) DefaultRequest(com.amazonaws.DefaultRequest) StringWriter(java.io.StringWriter) AmazonClientException(com.amazonaws.AmazonClientException)

Example 2 with DefaultRequest

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

the class SubscribeToDatasetRequestMarshaller method marshall.

public Request<SubscribeToDatasetRequest> marshall(SubscribeToDatasetRequest subscribeToDatasetRequest) {
    if (subscribeToDatasetRequest == null) {
        throw new AmazonClientException("Invalid argument passed to marshall(...)");
    }
    Request<SubscribeToDatasetRequest> request = new DefaultRequest<SubscribeToDatasetRequest>(subscribeToDatasetRequest, "AmazonCognitoSync");
    String target = "AWSCognitoSyncService.SubscribeToDataset";
    request.addHeader("X-Amz-Target", target);
    request.setHttpMethod(HttpMethodName.POST);
    String uriResourcePath = "/identitypools/{IdentityPoolId}/identities/{IdentityId}/datasets/{DatasetName}/subscriptions/{DeviceId}";
    uriResourcePath = uriResourcePath.replace("{IdentityPoolId}", (subscribeToDatasetRequest.getIdentityPoolId() == null) ? "" : StringUtils.fromString(subscribeToDatasetRequest.getIdentityPoolId()));
    uriResourcePath = uriResourcePath.replace("{IdentityId}", (subscribeToDatasetRequest.getIdentityId() == null) ? "" : StringUtils.fromString(subscribeToDatasetRequest.getIdentityId()));
    uriResourcePath = uriResourcePath.replace("{DatasetName}", (subscribeToDatasetRequest.getDatasetName() == null) ? "" : StringUtils.fromString(subscribeToDatasetRequest.getDatasetName()));
    uriResourcePath = uriResourcePath.replace("{DeviceId}", (subscribeToDatasetRequest.getDeviceId() == null) ? "" : StringUtils.fromString(subscribeToDatasetRequest.getDeviceId()));
    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);
    request.addHeader("Content-Length", "0");
    request.setContent(new ByteArrayInputStream(new byte[0]));
    if (!request.getHeaders().containsKey("Content-Type")) {
        request.addHeader("Content-Type", "application/x-amz-json-1.0");
    }
    return request;
}
Also used : DefaultRequest(com.amazonaws.DefaultRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) AmazonClientException(com.amazonaws.AmazonClientException)

Example 3 with DefaultRequest

use of com.amazonaws.DefaultRequest 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)

Aggregations

AmazonClientException (com.amazonaws.AmazonClientException)3 DefaultRequest (com.amazonaws.DefaultRequest)3 StringInputStream (com.amazonaws.util.StringInputStream)2 AwsJsonWriter (com.amazonaws.util.json.AwsJsonWriter)2 StringWriter (java.io.StringWriter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1