Java tutorial
/* * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.amazonaws.tvmclient; import java.security.SecureRandom; import java.util.Locale; import org.apache.commons.codec.binary.Hex; import android.content.Context; /** * This class is used to communicate with the Token Vending Machine specific for this application. */ public class TVMClient { /** * The endpoint for the Token Vending Machine to connect to. */ private String endpoint; /** * Use SSL when making connections to the Token Vending Machine. */ private boolean useSSL; /** * */ private String user; private String password; private String accountId; private String accountSecret; private String deviceId; private String deviceNick; private String cryptoKey; public TVMClient(String endpoint, boolean useSSL, String accountId, String accountSecret, String deviceId, String cryptoKey, String deviceNick, String user, String password) { this.endpoint = this.getEndpointDomainName(endpoint.toLowerCase(Locale.getDefault())); this.useSSL = useSSL; this.accountId = accountId; this.accountSecret = accountSecret; this.deviceId = deviceId; this.cryptoKey = cryptoKey; this.deviceNick = deviceNick; this.user = user; this.password = password; } /** * Get account id and secret for device */ public Response getAccount(Context pCtx) { Request getAccountRequest = new GetAccountRequest(this.endpoint, this.useSSL); ResponseHandler handler = new GetAccountResponseHandler(); GetAccountResponse getAccountResponse = (GetAccountResponse) this.processRequest(pCtx, getAccountRequest, handler, "GET"); return getAccountResponse; } /** * Anonymously register the current application/device with the Token Vending Machine. */ public Response registerDevice(Context pCtx) { Response response = Response.SUCCESSFUL; RegisterDeviceRequest registerDeviceRequest = new RegisterDeviceRequest(this.endpoint, this.useSSL, this.accountId, this.accountSecret, this.deviceId, this.cryptoKey, this.deviceNick); ResponseHandler handler = new ResponseHandler(); response = this.processRequest(pCtx, registerDeviceRequest, handler, "POST"); return response; } public Response updateRegisterDevice(Context pCtx) { Response response = Response.SUCCESSFUL; UpdateRegisterDeviceRequest registerDeviceRequest = new UpdateRegisterDeviceRequest(this.endpoint, this.useSSL, this.accountId, this.deviceId, this.cryptoKey, this.deviceNick); ResponseHandler handler = new ResponseHandler(); response = this.processRequest(pCtx, registerDeviceRequest, handler, "PUT"); return response; } /** * Gets a token from the Token Vending Machine. The registered key is used to secure the communication. */ public Response getToken(Context pCtx) { Request getTokenRequest = new GetTokenRequest(this.endpoint, this.useSSL, this.deviceId, this.cryptoKey, this.accountId); ResponseHandler handler = new GetTokenResponseHandler(this.cryptoKey); GetTokenResponse getTokenResponse = (GetTokenResponse) this.processRequest(pCtx, getTokenRequest, handler, "POST"); return getTokenResponse; } /** * Process Request */ protected Response processRequest(Context pCtx, Request request, ResponseHandler handler, String method) { Response response = null; int retries = 2; do { response = TVMService.sendRequest(pCtx, request, handler, method, this.user, this.password); if (response.requestWasSuccessful()) { return response; } else { } } while (retries-- > 0); return response; } private String getEndpointDomainName(String endpoint) { int startIndex = 0; int endIndex = 0; if (endpoint.startsWith("http://") || endpoint.startsWith("https://")) { startIndex = endpoint.indexOf("://") + 3; } else { startIndex = 0; } if (endpoint.charAt(endpoint.length() - 1) == '/') { endIndex = endpoint.length() - 1; } else { endIndex = endpoint.length(); } return endpoint.substring(startIndex, endIndex); } }