From 0dc58007630cf35297e73d2e1f121edd4c138897 Mon Sep 17 00:00:00 2001 From: Bruce Williams Date: Fri, 7 Feb 2014 08:10:16 -0500 Subject: [PATCH 1/2] support .srgs file extension java codekit was failing for file inputs that ruby codekit would accept. --- codekit/src/main/java/com/att/api/rest/RESTClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/codekit/src/main/java/com/att/api/rest/RESTClient.java b/codekit/src/main/java/com/att/api/rest/RESTClient.java index 2d2531a..116afe4 100644 --- a/codekit/src/main/java/com/att/api/rest/RESTClient.java +++ b/codekit/src/main/java/com/att/api/rest/RESTClient.java @@ -771,6 +771,7 @@ public APIResponse httpPost(String[] fnames, String subType, } if (contentType == null) contentType = this.getMIMEType(new File(fname)); + if (fname.endsWith("srgs")) contentType = "application/srgs+xml"; if (fname.endsWith("grxml")) contentType = "application/srgs+xml"; if (fname.endsWith("pls")) contentType="application/pls+xml"; FileBody fb = new FileBody(new File(fname), contentType, "UTF-8"); From bad3d9ac557eb1089888f5cb6bf6d02906009c53 Mon Sep 17 00:00:00 2001 From: mattcobb Date: Fri, 24 Oct 2014 15:13:51 -0700 Subject: [PATCH 2/2] Add the latest changes from the HTML5 SDK. This includes the changes to optionally return raw json (from Bruce Williams), configurable expires_in override for OAuth and setting ClientSdk version. --- .../com/att/api/ads/service/ADSService.java | 18 +- .../com/att/api/controller/APIController.java | 6 +- .../com/att/api/dc/service/DCService.java | 16 +- .../com/att/api/immn/service/IMMNService.java | 270 +++++++++++------- .../att/api/immn/service/package-info.java | 2 +- .../com/att/api/mms/service/MMSService.java | 95 +++--- .../java/com/att/api/oauth/OAuthService.java | 62 +++- .../java/com/att/api/oauth/OAuthToken.java | 44 ++- .../api/payment/service/NotaryService.java | 45 ++- .../api/payment/service/PaymentService.java | 150 ++++++---- .../java/com/att/api/rest/RESTClient.java | 37 ++- .../java/com/att/api/rest/RESTConfig.java | 25 ++ .../com/att/api/sms/service/SMSService.java | 50 ++-- .../speech/service/SpeechCustomService.java | 12 +- .../att/api/speech/service/SpeechService.java | 14 +- .../att/api/speech/service/TtsService.java | 40 ++- 16 files changed, 599 insertions(+), 287 deletions(-) diff --git a/codekit/src/main/java/com/att/api/ads/service/ADSService.java b/codekit/src/main/java/com/att/api/ads/service/ADSService.java index 5db5ee8..d325b76 100644 --- a/codekit/src/main/java/com/att/api/ads/service/ADSService.java +++ b/codekit/src/main/java/com/att/api/ads/service/ADSService.java @@ -154,13 +154,22 @@ public ADSResponse getAdvertisement(Category category, String userAgent, * Sends a request to the API for getting an advertisement. * * @param args arguments - * @param udid universally unique identifier * @return API response * @throws RESTException if API request was not successful * @throws IllegalArgumentException if args is null */ public ADSResponse getAdvertisement(ADSArguments args) throws RESTException { + try { + final String responseBody = getAdvertisementAndReturnRawJson(args); + return ADSResponse.valueOf(new JSONObject(responseBody)); + } catch (ParseException pe) { + throw new RESTException(pe); + } + } + + public String getAdvertisementAndReturnRawJson(ADSArguments args) throws RESTException { + if (args == null) throw new IllegalArgumentException("Arguments must not be null."); @@ -174,11 +183,6 @@ public ADSResponse getAdvertisement(ADSArguments args) throws RESTException { this.appendArguments(args, client); - try { - final String responseBody = client.httpGet().getResponseBody(); - return ADSResponse.valueOf(new JSONObject(responseBody)); - } catch (ParseException pe) { - throw new RESTException(pe); - } + return client.httpGet().getResponseBody(); } } diff --git a/codekit/src/main/java/com/att/api/controller/APIController.java b/codekit/src/main/java/com/att/api/controller/APIController.java index bc1dc44..e115bcb 100644 --- a/codekit/src/main/java/com/att/api/controller/APIController.java +++ b/codekit/src/main/java/com/att/api/controller/APIController.java @@ -50,7 +50,8 @@ protected OAuthToken getFileToken() throws RESTException { final String clientId = cfg.getClientId(); final String clientSecret = cfg.getClientSecret(); final OAuthService service = new OAuthService( - appConfig.getOauthFQDN(), clientId, clientSecret); + appConfig.getOauthFQDN(), clientId, clientSecret, + Long.parseLong(appConfig.getProperty("tokenExpireSeconds"))); token = service.getToken(cfg.getProperty("scope")); token.saveToken(tokenFile); @@ -78,7 +79,8 @@ protected OAuthToken getSessionToken(HttpServletRequest request, final String code = (String) request.getParameter("code"); if (code != null) { final OAuthService service = new OAuthService( - appConfig.getOauthFQDN(), clientId, clientSecret); + appConfig.getOauthFQDN(), clientId, clientSecret, + Long.parseLong(appConfig.getProperty("tokenExpireSeconds"))); token = service.getTokenUsingCode(code); session.setAttribute("token", token); return token; diff --git a/codekit/src/main/java/com/att/api/dc/service/DCService.java b/codekit/src/main/java/com/att/api/dc/service/DCService.java index c793dca..06d62fa 100644 --- a/codekit/src/main/java/com/att/api/dc/service/DCService.java +++ b/codekit/src/main/java/com/att/api/dc/service/DCService.java @@ -55,16 +55,20 @@ public DCService(final String fqdn, final OAuthToken token) { public DCResponse getDeviceCapabilities() throws RESTException { String endpoint = getFQDN() + "/rest/2/Devices/Info"; - final String responseBody = new RESTClient(endpoint) - .addAuthorizationHeader(getToken()) - .httpGet() - .getResponseBody(); - try { - JSONObject jsonResponse = new JSONObject(responseBody); + JSONObject jsonResponse = new JSONObject(getDeviceCapabilitiesAndReturnRawJson()); return DCResponse.valueOf(jsonResponse); } catch (ParseException pe) { throw new RESTException(pe); } } + + public String getDeviceCapabilitiesAndReturnRawJson() throws RESTException { + String endpoint = getFQDN() + "/rest/2/Devices/Info"; + + return new RESTClient(endpoint) + .addAuthorizationHeader(getToken()) + .httpGet() + .getResponseBody(); + } } diff --git a/codekit/src/main/java/com/att/api/immn/service/IMMNService.java b/codekit/src/main/java/com/att/api/immn/service/IMMNService.java index 84a4421..8a5fcf7 100644 --- a/codekit/src/main/java/com/att/api/immn/service/IMMNService.java +++ b/codekit/src/main/java/com/att/api/immn/service/IMMNService.java @@ -14,9 +14,12 @@ package com.att.api.immn.service; +import java.io.InputStream; +import java.io.IOException; import java.text.ParseException; import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpResponse; import org.json.JSONArray; import org.json.JSONObject; @@ -29,64 +32,81 @@ /** * Used to interact with version 1 of the In-app Messaging from Mobile * Number(IMMN) API. - * + * * @author pk9069 * @author kh455g * @version 1.0 * @since 1.0 - * @see Documentation + * @see Documentation */ public class IMMNService extends APIService { /** * Creates an IMMNService object. - * - * @param fqdn fully qualified domain name to use for sending requests - * @param token OAuth token to use for authorization + * + * @param fqdn + * fully qualified domain name to use for sending requests + * @param token + * OAuth token to use for authorization */ public IMMNService(String fqdn, OAuthToken token) { super(fqdn, token); } - - public SendResponse sendMessage(String address, String msg) throws RESTException { - String[] addrs = {address}; + public SendResponse sendMessage(String address, String msg) + throws RESTException { + String[] addrs = { address }; return this.sendMessage(addrs, msg); } - public SendResponse sendMessage(String[] addresses, String msg) throws RESTException { + public SendResponse sendMessage(String[] addresses, String msg) + throws RESTException { return this.sendMessage(addresses, msg, null, false, null); } - public SendResponse sendMessage(String address, String subject, + public SendResponse sendMessage(String address, String subject, boolean group) throws RESTException { return sendMessage(address, null, subject, group); } - public SendResponse sendMessage(String address, String msg, String subject, + public SendResponse sendMessage(String address, String msg, String subject, boolean group) throws RESTException { - String[] addrs = {address}; + String[] addrs = { address }; return sendMessage(addrs, null, subject, group); } - public SendResponse sendMessage(String[] addresses, String subject, + public SendResponse sendMessage(String[] addresses, String subject, boolean group) throws RESTException { return sendMessage(addresses, null, subject, group); } - public SendResponse sendMessage(String[] addresses, String msg, + public SendResponse sendMessage(String[] addresses, String msg, String subject, boolean group) throws RESTException { return sendMessage(addresses, msg, subject, group, null); } - public SendResponse sendMessage(String address, String msg, - String subject, boolean group, String[] attachments) throws RESTException { - String[] addrs = {address}; + public SendResponse sendMessage(String address, String msg, String subject, + boolean group, String[] attachments) throws RESTException { + String[] addrs = { address }; return sendMessage(addrs, msg, subject, group, attachments); } - public SendResponse sendMessage(String[] addresses, String msg, - String subject, boolean group, String[] attachments) throws RESTException { + public SendResponse sendMessage(String[] addresses, String msg, + String subject, boolean group, String[] attachments) + throws RESTException { + + try { + JSONObject jobj = new JSONObject(sendMessageAndReturnRawJson(addresses, msg, subject, group, attachments)); + return SendResponse.valueOf(jobj); + } catch (ParseException pe) { + throw new RESTException(pe); + } + } + + public String sendMessageAndReturnRawJson(String[] addresses, String msg, + String subject, boolean group, String[] attachments) + throws RESTException { final String endpoint = getFQDN() + "/myMessages/v2/messages"; @@ -113,34 +133,43 @@ public SendResponse sendMessage(String[] addresses, String msg, jsonBody.put("messageRequest", body); final RESTClient rest = new RESTClient(endpoint) - .setHeader("Accept", "application/json") - .setHeader("Content-Type", "application/json") - .addAuthorizationHeader(this.getToken()); + .setHeader("Accept", "application/json") + .setHeader("Content-Type", "application/json") + .addAuthorizationHeader(this.getToken()); + + final APIResponse response = (attachments == null) ? rest + .httpPost(jsonBody.toString()) : rest.httpPost(jsonBody, + attachments); + + return response.getResponseBody(); + } - final APIResponse response = (attachments == null) - ? rest.httpPost(jsonBody.toString()) - : rest.httpPost(jsonBody, attachments); + public MessageList getMessageList(int limit, int offset) + throws RESTException { + return getMessageList(new MessageListArgs.Builder(limit, offset) + .build()); + } + public MessageList getMessageList(MessageListArgs args) + throws RESTException { try { - JSONObject jobj = new JSONObject(response.getResponseBody()); - return SendResponse.valueOf(jobj); + JSONObject jobj = new JSONObject( + getMessageListAndReturnRawJson(args)); + return MessageList.valueOf(jobj); } catch (ParseException pe) { throw new RESTException(pe); } } - public MessageList getMessageList(int limit, int offset) throws RESTException { - return getMessageList(new MessageListArgs.Builder(limit, offset).build()); - } - - public MessageList getMessageList(MessageListArgs args) throws RESTException { + public String getMessageListAndReturnRawJson(MessageListArgs args) + throws RESTException { final String endpoint = getFQDN() + "/myMessages/v2/messages"; - final RESTClient client = new RESTClient(endpoint) - .addAuthorizationHeader(getToken()) - .setHeader("Accept", "application/json") - .setParameter("limit", "" + args.getLimit()) - .setParameter("offset", "" + args.getOffset()); + final RESTClient client = new RESTClient(endpoint) + .addAuthorizationHeader(getToken()) + .setHeader("Accept", "application/json") + .setParameter("limit", "" + args.getLimit()) + .setParameter("offset", "" + args.getOffset()); if (args.getMessageIds() != null) { String msgIds = StringUtils.join(args.getMessageIds(), ","); @@ -148,10 +177,11 @@ public MessageList getMessageList(MessageListArgs args) throws RESTException { } if (args.isFavorite() != null) - client.addParameter("isFavorite", args.isFavorite() ? "true" : "false"); + client.addParameter("isFavorite", args.isFavorite() ? "true" + : "false"); if (args.isUnread() != null) - client.addParameter("isUnread", args.isUnread() ? "true" : "false" ); + client.addParameter("isUnread", args.isUnread() ? "true" : "false"); if (args.getType() != null) client.addParameter("type", args.getType().getString()); @@ -160,33 +190,32 @@ public MessageList getMessageList(MessageListArgs args) throws RESTException { client.addParameter("keyword", args.getKeyword()); if (args.isIncoming() != null) - client.addParameter("isIncoming", args.isIncoming() ? "true" : "false" ); + client.addParameter("isIncoming", args.isIncoming() ? "true" + : "false"); - try { - APIResponse response = client.httpGet(); - JSONObject jobj = new JSONObject(response.getResponseBody()); - return MessageList.valueOf(jobj); - } catch (ParseException pe) { - throw new RESTException(pe); - } + return client.httpGet().getResponseBody(); } public Message getMessage(final String msgId) throws RESTException { - final String endpoint = getFQDN() + "/myMessages/v2/messages/" + msgId; - - final APIResponse response = new RESTClient(endpoint) - .addAuthorizationHeader(getToken()) - .setHeader("Accept", "application/json") - .httpGet(); - try { - JSONObject jobj = new JSONObject(response.getResponseBody()); + JSONObject jobj = new JSONObject(getMessageAndReturnRawJson(msgId)); return Message.valueOf(jobj.getJSONObject("message")); } catch (ParseException pe) { throw new RESTException(pe); } } + public String getMessageAndReturnRawJson(final String msgId) + throws RESTException { + final String endpoint = getFQDN() + "/myMessages/v2/messages/" + msgId; + + final String responseBody = new RESTClient(endpoint) + .addAuthorizationHeader(getToken()) + .setHeader("Accept", "application/json").httpGet() + .getResponseBody(); + return responseBody; + } + public MessageContent getMessageContent(String msgId, String partNumber) throws RESTException { @@ -194,9 +223,8 @@ public MessageContent getMessageContent(String msgId, String partNumber) + "/parts/" + partNumber; final APIResponse response = new RESTClient(endpoint) - .addAuthorizationHeader(getToken()) - .setHeader("Accept", "application/json") - .httpGet(); + .addAuthorizationHeader(getToken()) + .setHeader("Accept", "application/json").httpGet(); String ctype = response.getHeader("Content-Type"); String clength = response.getHeader("Content-Length"); @@ -204,23 +232,40 @@ public MessageContent getMessageContent(String msgId, String partNumber) return new MessageContent(ctype, clength, content); } - public DeltaResponse getDelta(final String state) throws RESTException { - final String endpoint = getFQDN() + "/myMessages/v2/delta"; + public InputStream getMessageContentAndReturnStream(String msgId, + String partNumber) throws RESTException { - final APIResponse response = new RESTClient(endpoint) - .addAuthorizationHeader(getToken()) - .setHeader("Accept", "application/json") - .addParameter("state", state) - .httpGet(); + final String endpoint = getFQDN() + "/myMessages/v2/messages/" + msgId + + "/parts/" + partNumber; + + try { + return new RESTClient(endpoint).addAuthorizationHeader(getToken()) + .httpGetAndReturnRawResponse().getEntity().getContent(); + } catch (IOException e) { + throw new RESTException(e); + } + } + public DeltaResponse getDelta(final String state) throws RESTException { try { - JSONObject jobj = new JSONObject(response.getResponseBody()); + JSONObject jobj = new JSONObject(getDeltaAndReturnRawJson(state)); return DeltaResponse.valueOf(jobj); } catch (ParseException pe) { throw new RESTException(pe); } } + public String getDeltaAndReturnRawJson(final String state) + throws RESTException { + final String endpoint = getFQDN() + "/myMessages/v2/delta"; + + final String responseBody = new RESTClient(endpoint) + .addAuthorizationHeader(getToken()) + .setHeader("Accept", "application/json") + .addParameter("state", state).httpGet().getResponseBody(); + return responseBody; + } + public void updateMessages(DeltaChange[] messages) throws RESTException { final String endpoint = getFQDN() + "/myMessages/v2/messages"; @@ -228,7 +273,7 @@ public void updateMessages(DeltaChange[] messages) throws RESTException { for (final DeltaChange change : messages) { JSONObject jchange = new JSONObject(); jchange.put("messageId", change.getMessageId()); - + if (change.isUnread() != null) jchange.put("isUnread", change.isUnread()); @@ -242,10 +287,10 @@ public void updateMessages(DeltaChange[] messages) throws RESTException { jobj.put("messages", jmsgs); final APIResponse response = new RESTClient(endpoint) - .addAuthorizationHeader(getToken()) - .setHeader("Accept", "application/json") - .setHeader("Content-Type", "application/json") - .httpPut(jobj.toString()); + .addAuthorizationHeader(getToken()) + .setHeader("Accept", "application/json") + .setHeader("Content-Type", "application/json") + .httpPut(jobj.toString()); if (response.getStatusCode() != 204) { final int code = response.getStatusCode(); @@ -254,23 +299,25 @@ public void updateMessages(DeltaChange[] messages) throws RESTException { } } - public void updateMessage(String msgId, Boolean isUnread, - Boolean isFavorite) throws RESTException { + public void updateMessage(String msgId, Boolean isUnread, Boolean isFavorite) + throws RESTException { final String endpoint = getFQDN() + "/myMessages/v2/messages/" + msgId; JSONObject jmsg = new JSONObject(); - if (isUnread != null) jmsg.put("isUnread", isUnread); - if (isFavorite != null) jmsg.put("isFavorite", isFavorite); - + if (isUnread != null) + jmsg.put("isUnread", isUnread); + if (isFavorite != null) + jmsg.put("isFavorite", isFavorite); + JSONObject jobj = new JSONObject(); jobj.put("message", jmsg); final APIResponse response = new RESTClient(endpoint) - .addAuthorizationHeader(getToken()) - .setHeader("Accept", "application/json") - .setHeader("Content-Type", "application/json") - .httpPut(jobj.toString()); + .addAuthorizationHeader(getToken()) + .setHeader("Accept", "application/json") + .setHeader("Content-Type", "application/json") + .httpPut(jobj.toString()); if (response.getStatusCode() != 204) { final int code = response.getStatusCode(); @@ -285,10 +332,9 @@ public void deleteMessages(String[] msgIds) throws RESTException { String msgIdsStr = StringUtils.join(msgIds, ","); final APIResponse response = new RESTClient(endpoint) - .setHeader("Accept", "application/json") - .addAuthorizationHeader(getToken()) - .addParameter("messageIds", msgIdsStr) - .httpDelete(); + .setHeader("Accept", "application/json") + .addAuthorizationHeader(getToken()) + .addParameter("messageIds", msgIdsStr).httpDelete(); if (response.getStatusCode() != 204) { final int code = response.getStatusCode(); @@ -301,9 +347,8 @@ public void deleteMessage(String msgId) throws RESTException { final String endpoint = getFQDN() + "/myMessages/v2/messages/" + msgId; final APIResponse response = new RESTClient(endpoint) - .setHeader("Accept", "application/json") - .addAuthorizationHeader(getToken()) - .httpDelete(); + .setHeader("Accept", "application/json") + .addAuthorizationHeader(getToken()).httpDelete(); if (response.getStatusCode() != 204) { final int code = response.getStatusCode(); @@ -316,9 +361,8 @@ public void createMessageIndex() throws RESTException { final String endpoint = getFQDN() + "/myMessages/v2/messages/index"; final APIResponse response = new RESTClient(endpoint) - .setHeader("Accept", "application/json") - .addAuthorizationHeader(getToken()) - .httpPost(); + .setHeader("Accept", "application/json") + .addAuthorizationHeader(getToken()).httpPost(); if (response.getStatusCode() != 202) { final int code = response.getStatusCode(); @@ -328,40 +372,48 @@ public void createMessageIndex() throws RESTException { } public MessageIndexInfo getMessageIndexInfo() throws RESTException { - final String endpoint = getFQDN() + "/myMessages/v2/messages/index/info"; - - final APIResponse response = new RESTClient(endpoint) - .setHeader("Accept", "application/json") - .addAuthorizationHeader(getToken()) - .httpGet(); - try { - JSONObject jobj = new JSONObject(response.getResponseBody()); - + JSONObject jobj = new JSONObject( + getMessageIndexInfoAndReturnRawJson()); return MessageIndexInfo.valueOf(jobj); } catch (ParseException pe) { throw new RESTException(pe); } } - public NotificationConnectionDetails getNotificationConnectionDetails( - String queues) throws RESTException { - + public String getMessageIndexInfoAndReturnRawJson() throws RESTException { final String endpoint = getFQDN() - + "/myMessages/v2/notificationConnectionDetails"; + + "/myMessages/v2/messages/index/info"; - final APIResponse response = new RESTClient(endpoint) - .setHeader("Accept", "application/json") - .addAuthorizationHeader(getToken()) - .setParameter("queues", queues) - .httpGet(); + final String jsonResponse = new RESTClient(endpoint) + .setHeader("Accept", "application/json") + .addAuthorizationHeader(getToken()).httpGet().getResponseBody(); + return jsonResponse; + } + + public NotificationConnectionDetails getNotificationConnectionDetails( + String queues) throws RESTException { try { - JSONObject jobj = new JSONObject(response.getResponseBody()); + JSONObject jobj = new JSONObject(getNotificationConnectionDetailsAndReturnRawJson(queues)); return NotificationConnectionDetails.valueOf(jobj); } catch (ParseException pe) { throw new RESTException(pe); } } + public String getNotificationConnectionDetailsAndReturnRawJson( + String queues) throws RESTException { + + final String endpoint = getFQDN() + + "/myMessages/v2/notificationConnectionDetails"; + + final APIResponse response = new RESTClient(endpoint) + .setHeader("Accept", "application/json") + .addAuthorizationHeader(getToken()) + .setParameter("queues", queues).httpGet(); + + return response.getResponseBody(); + } + } diff --git a/codekit/src/main/java/com/att/api/immn/service/package-info.java b/codekit/src/main/java/com/att/api/immn/service/package-info.java index fb27aae..370b673 100644 --- a/codekit/src/main/java/com/att/api/immn/service/package-info.java +++ b/codekit/src/main/java/com/att/api/immn/service/package-info.java @@ -25,7 +25,7 @@ * @author pk9069 * @author kh455g * @since 1.0 - * @see com.att.api.mmn.service.IMMNService + * @see com.att.api.immn.service.IMMNService */ package com.att.api.immn.service; diff --git a/codekit/src/main/java/com/att/api/mms/service/MMSService.java b/codekit/src/main/java/com/att/api/mms/service/MMSService.java index 4db533f..3bc4989 100644 --- a/codekit/src/main/java/com/att/api/mms/service/MMSService.java +++ b/codekit/src/main/java/com/att/api/mms/service/MMSService.java @@ -27,19 +27,22 @@ /** * Used to interact with version 3 of the MMS API. - * + * * @author pk9069 * @version 1.0 * @since 1.0 - * @see MMS Documentation + * @see MMS + * Documentation */ public class MMSService extends APIService { /** * Creates an MMSService object. - * - * @param fqdn fully qualified domain name to use for sending requests - * @param token OAuth token to use for authorization + * + * @param fqdn + * fully qualified domain name to use for sending requests + * @param token + * OAuth token to use for authorization */ public MMSService(String fqdn, OAuthToken token) { super(fqdn, token); @@ -48,19 +51,38 @@ public MMSService(String fqdn, OAuthToken token) { /** * Sends request to the API for sending an MMS using the specified * parameters. - * - * @param rawAddrs addresses to use for sending mms - * @param fnames path of attachments - * @param subject subject or null if none - * @param priority priority or null if to use default - * @param notifyDelStatus whether to notify of delivery status + * + * @param rawAddrs + * addresses to use for sending mms + * @param fnames + * path of attachments + * @param subject + * subject or null if none + * @param priority + * priority or null if to use default + * @param notifyDelStatus + * whether to notify of delivery status * @return API response - * @throws RESTException if API request was not successful + * @throws RESTException + * if API request was not successful */ public SendMMSResponse sendMMS(String rawAddrs, String[] fnames, String subject, String priority, boolean notifyDelStatus) throws RESTException { + try { + return SendMMSResponse.valueOf(new JSONObject( + sendMMSAndReturnRawJson(rawAddrs, fnames, subject, + priority, notifyDelStatus))); + } catch (ParseException e) { + throw new RESTException(e); + } + } + + public String sendMMSAndReturnRawJson(String rawAddrs, String[] fnames, + String subject, String priority, boolean notifyDelStatus) + throws RESTException { + JSONObject outboundRequest = new JSONObject(); String[] addrs = APIService.formatAddresses(rawAddrs); JSONArray jaddrs = new JSONArray(); @@ -71,50 +93,53 @@ public SendMMSResponse sendMMS(String rawAddrs, String[] fnames, Object addrStr = addrs.length == 1 ? addrs[0] : jaddrs; outboundRequest.put("address", addrStr); - if (subject != null) { outboundRequest.put("subject", subject); } - if (priority != null) { outboundRequest.put("priority", priority); } + if (subject != null) { + outboundRequest.put("subject", subject); + } + if (priority != null) { + outboundRequest.put("priority", priority); + } outboundRequest.put("notifyDeliveryStatus", notifyDelStatus); JSONObject jvars = new JSONObject(); jvars.put("outboundMessageRequest", outboundRequest); - try { - final String endpoint = getFQDN() + "/mms/v3/messaging/outbox"; - final String responseBody = - new RESTClient(endpoint) + final String endpoint = getFQDN() + "/mms/v3/messaging/outbox"; + + final String responseBody = new RESTClient(endpoint) .setHeader("Content-Type", "application/json") .setHeader("Accept", "application/json") - .addAuthorizationHeader(getToken()) - .httpPost(jvars, fnames) + .addAuthorizationHeader(getToken()).httpPost(jvars, fnames) .getResponseBody(); - return SendMMSResponse.valueOf(new JSONObject(responseBody)); - } catch (ParseException e) { - throw new RESTException(e); - } + return responseBody; } /** * Sends a request to the API for getting MMS status. - * - * @param mmsId MMS id to get status for + * + * @param mmsId + * MMS id to get status for * @return API response - * @throws RESTException if API request was not successful + * @throws RESTException + * if API request was not successful */ public MMSStatus getMMSStatus(String mmsId) throws RESTException { try { - String endpoint = getFQDN() + "/mms/v3/messaging/outbox/" + mmsId; - final String responseBody = new RESTClient(endpoint) - .setHeader("Accept", "application/json") - .addAuthorizationHeader(getToken()) - .httpGet() - .getResponseBody(); - - return MMSStatus.valueOf(new JSONObject(responseBody)); + return MMSStatus.valueOf(new JSONObject( + getMMSStatusAndReturnRawJson(mmsId))); } catch (ParseException e) { throw new RESTException(e); } } + public String getMMSStatusAndReturnRawJson(String mmsId) + throws RESTException { + String endpoint = getFQDN() + "/mms/v3/messaging/outbox/" + mmsId; + final String responseBody = new RESTClient(endpoint) + .setHeader("Accept", "application/json") + .addAuthorizationHeader(getToken()).httpGet().getResponseBody(); + return responseBody; + } } diff --git a/codekit/src/main/java/com/att/api/oauth/OAuthService.java b/codekit/src/main/java/com/att/api/oauth/OAuthService.java index 410a015..cab8d5d 100644 --- a/codekit/src/main/java/com/att/api/oauth/OAuthService.java +++ b/codekit/src/main/java/com/att/api/oauth/OAuthService.java @@ -82,7 +82,10 @@ public class OAuthService { /** Added to fqdn to use for sending OAuth requests. */ - public static final String API_URL = "/oauth/token"; + public static final String API_URL = "/oauth/v4/token"; + + /** Added to the fqdn to use for revoking tokens. */ + public static final String REVOKE_URL = "/oauth/v4/revoke"; /** Fully qualified domain name. */ private final String fqdn; @@ -92,6 +95,9 @@ public class OAuthService { /** Client secret to use for requestion an OAuth token. */ private final String clientSecret; + + /** Override the expires_in value from the serivce, if > 0 */ + private final long expiresInOverride; /** * Parses the API response from the API server when an access token was @@ -116,6 +122,10 @@ private OAuthToken parseResponse(APIResponse response) expiresIn = OAuthToken.NO_EXPIRATION; } + if(expiresInOverride > 0) { + expiresIn = expiresInOverride; + } + return new OAuthToken(accessToken, expiresIn, refreshToken); } catch (ParseException e) { String msg = e.getMessage(); @@ -146,10 +156,22 @@ private APIResponse sendRequest(RESTClient client) throws RESTException { * @param clientId client id to use * @param clientSecret client secret to use */ - public OAuthService(String fqdn, String clientId, String clientSecret) { + public OAuthService(String fqdn, String clientId, String clientSecret, long expiresInOverride) { this.fqdn = fqdn; this.clientId = clientId; this.clientSecret = clientSecret; + this.expiresInOverride = expiresInOverride; + } + + /** + * Creates an OAuthService object. + * + * @param fqdn fully qualified domain used for sending request + * @param clientId client id to use + * @param clientSecret client secret to use + */ + public OAuthService(String fqdn, String clientId, String clientSecret) { + this(fqdn, clientId, clientSecret, 0); } /** @@ -242,4 +264,40 @@ public OAuthToken refreshToken(String refreshToken) throws RESTException { return parseResponse(response); } + + /** + * Revokes a token. + * + * @param token token to revoke + * @param hint a hint for the type of token to revoke + * + * @throws RESTException if request was unsuccessful + */ + public void revokeToken(String token, String hint) throws RESTException { + RESTClient client = + new RESTClient(this.fqdn + REVOKE_URL) + .addParameter("client_id", clientId) + .addParameter("client_secret", clientSecret) + .addParameter("token", token) + .addParameter("token_type_hint", hint); + APIResponse response = sendRequest(client); + if (response.getStatusCode() != 200) { + throw new RESTException(response.getResponseBody()); + } + } + + /** + * Revokes a token, where the token hint set to "access_token" + * + * @param token token to revoke + * @param hint a hint for the type of token to revoke + * + * @throws RESTException if request was unsuccessful + * @see OAuthToken#revokeToken(String, String) + */ + public void revokeToken(String token) throws RESTException { + final String hint = "access_token"; + this.revokeToken(token, hint); + } + } diff --git a/codekit/src/main/java/com/att/api/oauth/OAuthToken.java b/codekit/src/main/java/com/att/api/oauth/OAuthToken.java index 9c365f3..dfc898c 100644 --- a/codekit/src/main/java/com/att/api/oauth/OAuthToken.java +++ b/codekit/src/main/java/com/att/api/oauth/OAuthToken.java @@ -88,7 +88,7 @@ public class OAuthToken { * @return seconds since Unix epoch */ private static long xtimestamp() { - return System.currentTimeMillis() / 1000; + return System.currentTimeMillis() / 1000L; } /** @@ -154,12 +154,52 @@ public String getAccessToken() { /** * Gets refresh token. * - * @return refresh token + * @return String refresh token */ public String getRefreshToken() { return refreshToken; } + /** + * Convert the token object to a string + * + * @return String The string representation of the token object + */ + @Override public String toString() { + return "{ token: " + getAccessToken() + + ", expires_in (sec): " + Long.toString(accessTokenExpiry - xtimestamp(), 10) + + ", refresh_token: " + getRefreshToken() + " }"; + } + + /** + * blur token values + * @return String Blur out some of the token characters + * + */ + private static String blurToken(String token) { + String hidden = null; + if(token!=null && token.length()>4) { + hidden = token.substring(0, 4) + "**...**" + token.substring(token.length()-5, token.length()-1); + } else if (token == null){ + hidden = "null"; + } else { + hidden = "*"; + } + return hidden; + } + + /** + * Convert the token object to a string, but blur the tokens + * + * @return String The string representation of the token object, blured + */ + public String toBluredString() { + String token = getAccessToken(); + return "{ token: " + blurToken(getAccessToken()) + + ", expires_in (sec): " + Long.toString(accessTokenExpiry - xtimestamp(), 10) + + ", refresh_token: " + blurToken(getRefreshToken()) + " }"; + } + /** * Saves this token to a file in an asynchronous-safe manner. * diff --git a/codekit/src/main/java/com/att/api/payment/service/NotaryService.java b/codekit/src/main/java/com/att/api/payment/service/NotaryService.java index cec041e..ad21e6c 100644 --- a/codekit/src/main/java/com/att/api/payment/service/NotaryService.java +++ b/codekit/src/main/java/com/att/api/payment/service/NotaryService.java @@ -21,13 +21,11 @@ private JSONObject buildJSON(Transaction args) { "Description", "MerchantTransactionId", "MerchantProductId", "MerchantPaymentRedirectUrl" }; - final String[] varValues = { - String.valueOf(args.getAmount()), - String.valueOf(args.getAppCategory().getValue()), - args.getChannel(), args.getDescription(), - args.getTransactionId(), args.getProductId(), - args.getPaymentRedirectUrl() - }; + final String[] varValues = { String.valueOf(args.getAmount()), + String.valueOf(args.getAppCategory().getValue()), + args.getChannel(), args.getDescription(), + args.getTransactionId(), args.getProductId(), + args.getPaymentRedirectUrl() }; JSONObject jrequest = new JSONObject(); for (int i = 0; i < varNames.length; ++i) { @@ -43,45 +41,46 @@ private void setHeaders(RESTClient client) { .setHeader("Client_secret", this.clientSecret); } - public NotaryService(String fqdn, String clientId, - String clientSecret) { + public NotaryService(String fqdn, String clientId, String clientSecret) { super(fqdn, null); this.clientId = clientId; this.clientSecret = clientSecret; } public Notary getNotary(String rawStr) throws RESTException { - String url = getFQDN() + "/Security/Notary/Rest/1/SignedPayload"; - RESTClient client = new RESTClient(url); - this.setHeaders(client); - APIResponse response = client.httpPost(rawStr); - try { - JSONObject jresponse = new JSONObject(response.getResponseBody()); + JSONObject jresponse = new JSONObject( + getNotaryAndReturnRawJson(rawStr)); String signedDoc = jresponse.getString("SignedDocument"); String signature = jresponse.getString("Signature"); return new Notary(rawStr, signedDoc, signature); - } catch (ParseException e){ + } catch (ParseException e) { throw new RESTException(e); } } + public String getNotaryAndReturnRawJson(String rawStr) throws RESTException { + String url = getFQDN() + "/Security/Notary/Rest/1/SignedPayload"; + RESTClient client = new RESTClient(url); + this.setHeaders(client); + APIResponse response = client.httpPost(rawStr); + return response.getResponseBody(); + } + public Notary getTransactionNotary(Transaction args) throws RESTException { JSONObject jrequest = this.buildJSON(args); return this.getNotary(jrequest.toString()); } - public Notary getSubscriptionNotary(Subscription args) - throws RESTException { + public Notary getSubscriptionNotary(Subscription args) throws RESTException { JSONObject jrequest = this.buildJSON(args); - jrequest.put("IsPurchaseOnNoActiveSubscription", + jrequest.put("IsPurchaseOnNoActiveSubscription", args.isPurchaseOnNoActiveSubscription()); - jrequest.put("SubscriptionRecurrences", + jrequest.put("SubscriptionRecurrences", args.getSubscriptionRecurrences()); - jrequest.put("SubscriptionPeriod", - args.getSubscriptionPeriod()); - jrequest.put("SubscriptionPeriodAmount", + jrequest.put("SubscriptionPeriod", args.getSubscriptionPeriod()); + jrequest.put("SubscriptionPeriodAmount", args.getSubscriptionPeriodAmount()); jrequest.put("MerchantSubscriptionIdList", args.getMerchantSubscriptionIdList()); diff --git a/codekit/src/main/java/com/att/api/payment/service/PaymentService.java b/codekit/src/main/java/com/att/api/payment/service/PaymentService.java index 53ae8e9..4d72222 100644 --- a/codekit/src/main/java/com/att/api/payment/service/PaymentService.java +++ b/codekit/src/main/java/com/att/api/payment/service/PaymentService.java @@ -12,31 +12,25 @@ import org.json.JSONObject; public class PaymentService extends APIService { - private static final String TRANS_SUBURL = - "/rest/3/Commerce/Payment/Transactions"; - private static final String SUBSCRIPTION_SUBURL = - "/rest/3/Commerce/Payment/Subscriptions"; - public static final String NOTIFICATION_SUBURL = - "/rest/3/Commerce/Payment/Notifications"; - public static final String SIG_SUBURL = - "/Security/Notary/Rest/1/SignedPayload"; - - private static String generateUrl(String FQDN, String cid, Notary notary, + private static final String TRANS_SUBURL = "/rest/3/Commerce/Payment/Transactions"; + private static final String SUBSCRIPTION_SUBURL = "/rest/3/Commerce/Payment/Subscriptions"; + public static final String NOTIFICATION_SUBURL = "/rest/3/Commerce/Payment/Notifications"; + public static final String SIG_SUBURL = "/Security/Notary/Rest/1/SignedPayload"; + + private static String generateUrl(String FQDN, String cid, Notary notary, String type) { String URL = FQDN + "/rest/3/Commerce/Payment/" + type; String signedDoc = notary.getSignedDocument(); String signature = notary.getSignature(); - return URL + "?clientid=" + cid + "&SignedPaymentDetail=" + - signedDoc + "&Signature=" + signature; + return URL + "?clientid=" + cid + "&SignedPaymentDetail=" + signedDoc + + "&Signature=" + signature; } private JSONObject getPaymentInfo(final String suburl) throws RESTException { final String url = getFQDN() + suburl; - APIResponse response = - new RESTClient(url) - .setHeader("Accept", "application/json") - .addAuthorizationHeader(getToken()) - .httpGet(); + APIResponse response = new RESTClient(url) + .setHeader("Accept", "application/json") + .addAuthorizationHeader(getToken()).httpGet(); try { return new JSONObject(response.getResponseBody()); @@ -45,61 +39,93 @@ private JSONObject getPaymentInfo(final String suburl) throws RESTException { } } - private JSONObject sendTransOptStatus(String rReasonTxt, int rReasonCode, + private String getPaymentInfoAndReturnRawJson(final String suburl) + throws RESTException { + final String url = getFQDN() + suburl; + APIResponse response = new RESTClient(url) + .setHeader("Accept", "application/json") + .addAuthorizationHeader(getToken()).httpGet(); + return response.getResponseBody(); + } + + private JSONObject sendTransOptStatus(String rReasonTxt, int rReasonCode, String transOptStatus, String suburl) throws RESTException { + try { + return new JSONObject(sendTransOptStatusAndReturnRawJson(rReasonTxt, rReasonCode, transOptStatus, suburl)); + } catch (ParseException e) { + throw new RESTException(e); + } + } + + private String sendTransOptStatusAndReturnRawJson(String rReasonTxt, + int rReasonCode, String transOptStatus, String suburl) + throws RESTException { JSONObject req = new JSONObject(); req.put("TransactionOperationStatus", transOptStatus); req.put("RefundReasonCode", rReasonCode); req.put("RefundReasonText", rReasonTxt); String url = getFQDN() + suburl; - APIResponse response = - new RESTClient(url) - .setHeader("Accept", "application/json") - .setHeader("Content-Type", "application/json") - .addAuthorizationHeader(getToken()) - .httpPut(req.toString()); + APIResponse response = new RESTClient(url) + .setHeader("Accept", "application/json") + .setHeader("Content-Type", "application/json") + .addAuthorizationHeader(getToken()).httpPut(req.toString()); + return response.getResponseBody(); + } - try { - return new JSONObject(response.getResponseBody()); - } catch (ParseException e) { - throw new RESTException(e); - } - } - public PaymentService(String fqdn, OAuthToken token) { super(fqdn, token); } - public JSONObject getTransactionStatus(String type, String id) throws RESTException { + public JSONObject getTransactionStatus(String type, String id) + throws RESTException { String surl = TRANS_SUBURL + "/" + type + "/" + id; return this.getPaymentInfo(surl); } - public JSONObject getSubscriptionStatus(String type, String id) throws RESTException { + public String getTransactionStatusAndReturnRawJson(String type, String id) + throws RESTException { + String surl = TRANS_SUBURL + "/" + type + "/" + id; + return this.getPaymentInfoAndReturnRawJson(surl); + } + + public JSONObject getSubscriptionStatus(String type, String id) + throws RESTException { String surl = SUBSCRIPTION_SUBURL + "/" + type + "/" + id; return this.getPaymentInfo(surl); } - public JSONObject getSubscriptionDetails(String merchId, - String consumerId) throws RESTException { - String surl = SUBSCRIPTION_SUBURL + "/" + merchId + "/Detail/" + consumerId; + public String getSubscriptionStatusAndReturnRawJson(String type, String id) + throws RESTException { + String surl = SUBSCRIPTION_SUBURL + "/" + type + "/" + id; + return this.getPaymentInfoAndReturnRawJson(surl); + } + + public JSONObject getSubscriptionDetails(String merchId, String consumerId) + throws RESTException { + String surl = SUBSCRIPTION_SUBURL + "/" + merchId + "/Detail/" + + consumerId; return this.getPaymentInfo(surl); } + public String getSubscriptionDetailsAndReturnRawJson(String merchId, + String consumerId) throws RESTException { + String surl = SUBSCRIPTION_SUBURL + "/" + merchId + "/Detail/" + + consumerId; + return this.getPaymentInfoAndReturnRawJson(surl); + } + public JSONObject getNotification(String id) throws RESTException { String surl = NOTIFICATION_SUBURL + "/" + id; return this.getPaymentInfo(surl); } public JSONObject deleteNotification(String id) throws RESTException { - final String url = getFQDN() + PaymentService.NOTIFICATION_SUBURL + - "/" + id; - APIResponse response = - new RESTClient(url) - .setHeader("Accept", "application/json") - .addAuthorizationHeader(getToken()) - .httpPut(""); + final String url = getFQDN() + PaymentService.NOTIFICATION_SUBURL + "/" + + id; + APIResponse response = new RESTClient(url) + .setHeader("Accept", "application/json") + .addAuthorizationHeader(getToken()).httpPut(""); try { return new JSONObject(response.getResponseBody()); @@ -108,37 +134,61 @@ public JSONObject deleteNotification(String id) throws RESTException { } } - public JSONObject cancelSubscription(String subId, String reasonTxt, + public JSONObject cancelSubscription(String subId, String reasonTxt, int reasonCode) throws RESTException { String surl = TRANS_SUBURL + "/" + subId; - return this.sendTransOptStatus(reasonTxt, reasonCode, + return this.sendTransOptStatus(reasonTxt, reasonCode, + "SubscriptionCancelled", surl); + } + + public String cancelSubscriptionAndReturnRawJson(String subId, + String reasonTxt, int reasonCode) throws RESTException { + String surl = TRANS_SUBURL + "/" + subId; + return this.sendTransOptStatusAndReturnRawJson(reasonTxt, reasonCode, "SubscriptionCancelled", surl); } - public JSONObject refundSubscription(String subId, String reasonTxt, + public JSONObject refundSubscription(String subId, String reasonTxt, int reasonCode) throws RESTException { String surl = TRANS_SUBURL + "/" + subId; return this.sendTransOptStatus(reasonTxt, reasonCode, "Refunded", surl); } - public JSONObject refundTransaction(String transId, String reasonTxt, + public JSONObject refundTransaction(String transId, String reasonTxt, int reasonCode) throws RESTException { String surl = TRANS_SUBURL + "/" + transId; return this.sendTransOptStatus(reasonTxt, reasonCode, "Refunded", surl); } + public String refundTransactionAndReturnRawJson(String transId, String reasonTxt, + int reasonCode) throws RESTException { + String surl = TRANS_SUBURL + "/" + transId; + + return this.sendTransOptStatusAndReturnRawJson(reasonTxt, reasonCode, "Refunded", surl); + } public static String getNewTransactionURL(String FQDN, String clientId, Notary notary) { - return PaymentService.generateUrl(FQDN, clientId, notary, + return PaymentService.generateUrl(FQDN, clientId, notary, + "Transactions"); + } + + public static String getNewTransactionURLAndReturnRawJson(String FQDN, + String clientId, Notary notary) { + return PaymentService.generateUrl(FQDN, clientId, notary, "Transactions"); } - + public static String getNewSubscriptionURL(String FQDN, String clientId, Notary notary) { - return PaymentService.generateUrl(FQDN, clientId, notary, + return PaymentService.generateUrl(FQDN, clientId, notary, "Subscriptions"); } + public static String getNewSubscriptionURLAndReturnRawJson(String FQDN, + String clientId, Notary notary) { + return PaymentService.generateUrl(FQDN, clientId, notary, + "Subscriptions"); + } } diff --git a/codekit/src/main/java/com/att/api/rest/RESTClient.java b/codekit/src/main/java/com/att/api/rest/RESTClient.java index 116afe4..d893346 100644 --- a/codekit/src/main/java/com/att/api/rest/RESTClient.java +++ b/codekit/src/main/java/com/att/api/rest/RESTClient.java @@ -330,6 +330,9 @@ public RESTClient(String url) throws RESTException { */ public RESTClient(RESTConfig cfg) throws RESTException { this.headers = new HashMap>(); + if(cfg.getClientSdk() != null) { + this.setHeader("X-Arg", cfg.getClientSdk()); + } this.parameters = new HashMap>(); this.url = cfg.getURL(); this.trustAllCerts = cfg.trustAllCerts(); @@ -495,6 +498,31 @@ public APIResponse httpGet() throws RESTException { } } + public HttpResponse httpGetAndReturnRawResponse() throws RESTException { + HttpClient httpClient = null; + HttpResponse response = null; + + try { + httpClient = createClient(); + + String query = ""; + if (!buildQuery().equals("")) { + query = "?" + buildQuery(); + } + HttpGet httpGet = new HttpGet(url + query); + addInternalHeaders(httpGet); + + return httpClient.execute(httpGet); + + } catch (IOException ioe) { + throw new RESTException(ioe); + } finally { + if (response != null) { + this.releaseConnection(response); + } + } + } + /** * Alias for httpPost(). * @@ -700,7 +728,7 @@ public APIResponse httpPost(JSONObject jsonObj, String[] fnames) HttpPost httpPost = new HttpPost(url); this.setHeader("Content-Type", - "multipart/form-data; type=\"application/json\"; " + "multipart/related; type=\"application/json\"; " + "start=\"\"; boundary=\"foo\""); addInternalHeaders(httpPost); @@ -771,7 +799,6 @@ public APIResponse httpPost(String[] fnames, String subType, } if (contentType == null) contentType = this.getMIMEType(new File(fname)); - if (fname.endsWith("srgs")) contentType = "application/srgs+xml"; if (fname.endsWith("grxml")) contentType = "application/srgs+xml"; if (fname.endsWith("pls")) contentType="application/pls+xml"; FileBody fb = new FileBody(new File(fname), contentType, "UTF-8"); @@ -828,7 +855,11 @@ public APIResponse httpDelete() throws RESTException { try { httpClient = createClient(); - HttpDelete httpDelete = new HttpDelete(this.url); + String query = ""; + if (!buildQuery().equals("")) { + query = "?" + buildQuery(); + } + HttpDelete httpDelete = new HttpDelete(this.url + query); addInternalHeaders(httpDelete); diff --git a/codekit/src/main/java/com/att/api/rest/RESTConfig.java b/codekit/src/main/java/com/att/api/rest/RESTConfig.java index f433df7..06506f3 100644 --- a/codekit/src/main/java/com/att/api/rest/RESTConfig.java +++ b/codekit/src/main/java/com/att/api/rest/RESTConfig.java @@ -31,6 +31,9 @@ public class RESTConfig { /** Default setting for accepting ssl certificates. */ private static boolean defaultTrustAllCerts = false; + + /** Default client sdk to use, if any. */ + private static String defaultClientSdk = null; /** Url to use for RESTFul request. */ private final String url; @@ -40,6 +43,9 @@ public class RESTConfig { /** Proxy host to use or null if none. */ private final String proxyHost; + + /** ClientSdk to set for all api calls */ + private final String clientSdk; /** Proxy port to use or -1 if none. */ private final int proxyPort; @@ -107,6 +113,7 @@ public RESTConfig(String url, String proxyHost, this.proxyHost = proxyHost; this.proxyPort = proxyPort; this.trustAllCerts = trustAllCerts; + this.clientSdk = this.defaultClientSdk; } /** @@ -136,6 +143,15 @@ public String getProxyHost() { return this.proxyHost; } + /** + * Get client sdk value to use or null if none. + * + * @return clientSdk to use + */ + public String getClientSdk() { + return this.clientSdk; + } + /** * Gets proxy port to use or -1 if none. * @@ -157,6 +173,15 @@ public static synchronized void setDefaultProxy(String host, int port) { RESTConfig.defaultProxyPort = port; } + /** + * Sets the default clientSdk to use + * + * @param clientSdkValue + */ + public static synchronized void setDefaultClientSdk(String clientSdkValue) { + RESTConfig.defaultClientSdk = clientSdkValue; + } + /** * Sets the default ssl certificate setting to use if none is specified * during object creation. diff --git a/codekit/src/main/java/com/att/api/sms/service/SMSService.java b/codekit/src/main/java/com/att/api/sms/service/SMSService.java index 936b081..a07f65e 100644 --- a/codekit/src/main/java/com/att/api/sms/service/SMSService.java +++ b/codekit/src/main/java/com/att/api/sms/service/SMSService.java @@ -63,6 +63,15 @@ public SMSService(String fqdn, OAuthToken token) { public SMSSendResponse sendSMS(String rawAddr, String msg, boolean notifyDeliveryStatus) throws RESTException { + try { + return SMSSendResponse.valueOf(new JSONObject(sendSMSAndReturnRawJson(rawAddr, msg, notifyDeliveryStatus))); + } catch (ParseException pe) { + throw new RESTException(pe); + } + } + + public String sendSMSAndReturnRawJson(String rawAddr, String msg, + boolean notifyDeliveryStatus) throws RESTException { String[] addrs = APIService.formatAddresses(rawAddr); JSONArray jaddrs = new JSONArray(); for (String addr : addrs) { @@ -89,13 +98,9 @@ public SMSSendResponse sendSMS(String rawAddr, String msg, .httpPost(rpcObject.toString()) .getResponseBody(); - try { - return SMSSendResponse.valueOf(new JSONObject(responseBody)); - } catch (ParseException pe) { - throw new RESTException(pe); - } - } - + return responseBody; + } + /** * Sends a request for getting delivery status information about an SMS. * @@ -104,6 +109,14 @@ public SMSSendResponse sendSMS(String rawAddr, String msg, * @throws RESTException if API request was not successful */ public SMSStatus getSMSDeliveryStatus(String msgId) throws RESTException { + try { + return SMSStatus.valueOf(new JSONObject(getSMSDeliveryStatusAndReturnRawJson(msgId))); + } catch (ParseException pe) { + throw new RESTException(pe); + } + } + + public String getSMSDeliveryStatusAndReturnRawJson(String msgId) throws RESTException { String endpoint = getFQDN() + "/sms/v3/messaging/outbox/" + msgId; final String responseBody = new RESTClient(endpoint) @@ -111,12 +124,7 @@ public SMSStatus getSMSDeliveryStatus(String msgId) throws RESTException { .addHeader("Accept", "application/json") .httpGet() .getResponseBody(); - - try { - return SMSStatus.valueOf(new JSONObject(responseBody)); - } catch (ParseException pe) { - throw new RESTException(pe); - } + return responseBody; } /** @@ -129,6 +137,15 @@ public SMSStatus getSMSDeliveryStatus(String msgId) throws RESTException { */ public SMSGetResponse getSMS(String registrationID) throws RESTException { + try { + return SMSGetResponse.valueOf(new JSONObject(this.getSMSAndReturnRawJson(registrationID))); + } catch (ParseException pe) { + throw new RESTException(pe); + } + } + + public String getSMSAndReturnRawJson(String registrationID) throws RESTException { + String fqdn = getFQDN(); String endpoint = fqdn + "/sms/v3/messaging/inbox/" + registrationID; @@ -137,11 +154,6 @@ public SMSGetResponse getSMS(String registrationID) throws RESTException { .addHeader("Accept", "application/json") .httpGet() .getResponseBody(); - - try { - return SMSGetResponse.valueOf(new JSONObject(responseBody)); - } catch (ParseException pe) { - throw new RESTException(pe); - } + return responseBody; } } diff --git a/codekit/src/main/java/com/att/api/speech/service/SpeechCustomService.java b/codekit/src/main/java/com/att/api/speech/service/SpeechCustomService.java index 333bc7a..0f58e88 100644 --- a/codekit/src/main/java/com/att/api/speech/service/SpeechCustomService.java +++ b/codekit/src/main/java/com/att/api/speech/service/SpeechCustomService.java @@ -81,15 +81,21 @@ private SpeechResponse parseSuccess(String response) */ public SpeechResponse sendRequest(String [] attachments, String speechContext, String xArg) throws Exception { + return parseSuccess(sendRequestAndReturnRawJson(attachments, speechContext, xArg, "en-US")); + } + + public String sendRequestAndReturnRawJson(String [] attachments, + String speechContext, String xArg, String isoLanguage) throws Exception { final String endpoint = getFQDN() + "/speech/v3/speechToTextCustom"; RESTClient restClient = new RESTClient(endpoint) .addAuthorizationHeader(getToken()) .addHeader("Accept", "application/json") - .addHeader("X-SpeechContext", speechContext); + .addHeader("X-SpeechContext", speechContext) + .addHeader("Content-Language", isoLanguage); if (xArg != null && !xArg.equals("")) { - restClient.addHeader("X-Arg", xArg); + restClient.setHeader("X-Arg", xArg); } String subType = "x-srgs-audio"; @@ -99,6 +105,6 @@ public SpeechResponse sendRequest(String [] attachments, bodyNameAttribute[2] = "x-voice"; APIResponse apiResponse = restClient.httpPost(attachments, subType, bodyNameAttribute); - return parseSuccess(apiResponse.getResponseBody()); + return apiResponse.getResponseBody(); } } diff --git a/codekit/src/main/java/com/att/api/speech/service/SpeechService.java b/codekit/src/main/java/com/att/api/speech/service/SpeechService.java index 59d775e..62b4898 100644 --- a/codekit/src/main/java/com/att/api/speech/service/SpeechService.java +++ b/codekit/src/main/java/com/att/api/speech/service/SpeechService.java @@ -18,7 +18,7 @@ */ public class SpeechService extends APIService { private boolean chunked; - + public SpeechService(String fqdn, OAuthToken token) { super(fqdn, token); this.chunked = false; @@ -90,20 +90,26 @@ public void setChunked(boolean chunked) { */ public SpeechResponse sendRequest(File file, String xArg, String speechContext, String subContext) throws Exception { + return parseSuccess(sendRequestAndReturnRawJson(file, xArg, speechContext, subContext, "en-US")); + } + + public String sendRequestAndReturnRawJson(File file, String xArg, + String speechContext, String subContext, String isoLanguage) throws Exception { final String endpoint = getFQDN() + "/speech/v3/speechToText"; RESTClient restClient = new RESTClient(endpoint) .addAuthorizationHeader(getToken()) .addHeader("Accept", "application/json") - .addHeader("X-SpeechContext", speechContext); + .addHeader("X-SpeechContext", speechContext) + .addHeader("Content-Language", isoLanguage); if (xArg != null && !xArg.equals("")) { - restClient.addHeader("X-Arg", xArg); + restClient.setHeader("X-Arg", xArg); } if (subContext != null && !subContext.equals("") && speechContext.equals("Gaming")){ restClient.addHeader("X-SpeechSubContext",subContext); } APIResponse apiResponse = restClient.httpPost(file); - return parseSuccess(apiResponse.getResponseBody()); + return apiResponse.getResponseBody(); } } diff --git a/codekit/src/main/java/com/att/api/speech/service/TtsService.java b/codekit/src/main/java/com/att/api/speech/service/TtsService.java index 71fab32..2b2a8b6 100644 --- a/codekit/src/main/java/com/att/api/speech/service/TtsService.java +++ b/codekit/src/main/java/com/att/api/speech/service/TtsService.java @@ -12,7 +12,7 @@ /** * Class that handles communication with the speech server. - * + * */ public class TtsService extends APIService { @@ -27,7 +27,7 @@ public TtsService(String fqdn, OAuthToken token) { /** * If the server returned a successful response, this method parses the * response and returns a {@link SpeechResponse} object. - * + * * @param response * the response returned by the server * @return the server response as a binary byte[] array @@ -35,17 +35,15 @@ public TtsService(String fqdn, OAuthToken token) { * if unable to read the passed-in response * @throws java.text.ParseException */ - private byte[] parseSuccess(APIResponse wavResponse) - throws IOException - { - //decodes binary properly with iso-8859-1 charset + private byte[] parseSuccess(APIResponse wavResponse) throws IOException { + // decodes binary properly with iso-8859-1 charset return wavResponse.getResponseBody().getBytes("ISO-8859-1"); } /** * If the server responds with a failure, this method returns a * {@link SpeechResponse} object with the failure message. - * + * * @param response * response to parse * @return error in a SpeechResponse object @@ -54,8 +52,8 @@ private byte[] parseSuccess(APIResponse wavResponse) * @throws IOException * if unable to read the passed-in response */ - private void parseFailure(APIResponse response) - throws ParseException, IOException { + private void parseFailure(APIResponse response) throws ParseException, + IOException { String result; if (response.getResponseBody() == null) { result = String.valueOf(response.getStatusCode()); @@ -67,25 +65,25 @@ private void parseFailure(APIResponse response) /** * Sends the request to the server. - * - * @param file - * file to send. - * @param speechContext - * speech context - * @return a byte array + * + * @param contentType + * @param speechText + * @param xArg + * @return a byte array * @see SpeechResponse */ - public byte[] sendRequest(String contentType, - String speechText, String xArg) throws Exception { + public byte[] sendRequest(String contentType, String speechText, + String xArg, String accept, String isoLanguage) throws Exception { final String endpoint = getFQDN() + "/speech/v3/textToSpeech"; RESTClient restClient = new RESTClient(endpoint) - .addAuthorizationHeader(getToken()) - .addHeader("Content-Type", contentType) - .addHeader("Accept", "audio/x-wav"); + .addAuthorizationHeader(getToken()) + .addHeader("Content-Type", contentType) + .addHeader("Accept", accept) + .addHeader("Content-Language", isoLanguage); if (xArg != null && !xArg.equals("")) { - restClient.addHeader("X-Arg", xArg); + restClient.setHeader("X-Arg", xArg); } APIResponse apiResponse = restClient.httpPost(speechText);