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 6254679..c1143db 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
@@ -164,6 +164,16 @@ public ADSResponse getAdvertisement(Category category, String userAgent,
*/
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.");
@@ -177,11 +187,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 (JSONException 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 aed0b46..fa2435f 100644
--- a/codekit/src/main/java/com/att/api/controller/APIController.java
+++ b/codekit/src/main/java/com/att/api/controller/APIController.java
@@ -54,7 +54,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);
@@ -82,7 +83,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 9d7791e..47384fb 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
@@ -60,16 +60,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 (JSONException 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 1c8ec6e..8f0f7b5 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
@@ -19,6 +19,7 @@
package com.att.api.immn.service;
import org.apache.commons.lang3.StringUtils;
+import org.apache.http.HttpResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
@@ -32,20 +33,23 @@
/**
* 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);
@@ -93,6 +97,18 @@ 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";
JSONObject jsonBody = new JSONObject();
@@ -126,12 +142,7 @@ public SendResponse sendMessage(String[] addresses, String msg,
.httpPost(jsonBody.toString()) : rest.httpPost(jsonBody,
attachments);
- try {
- JSONObject jobj = new JSONObject(response.getResponseBody());
- return SendResponse.valueOf(jobj);
- } catch (JSONException pe) {
- throw new RESTException(pe);
- }
+ return response.getResponseBody();
}
public MessageList getMessageList(int limit, int offset)
@@ -142,6 +153,17 @@ public MessageList getMessageList(int limit, int offset)
public MessageList getMessageList(MessageListArgs args)
throws RESTException {
+ try {
+ JSONObject jobj = new JSONObject(
+ getMessageListAndReturnRawJson(args));
+ return MessageList.valueOf(jobj);
+ } catch (ParseException pe) {
+ throw new RESTException(pe);
+ }
+ }
+
+ public String getMessageListAndReturnRawJson(MessageListArgs args)
+ throws RESTException {
final String endpoint = getFQDN() + "/myMessages/v2/messages";
final RESTClient client = new RESTClient(endpoint)
@@ -172,28 +194,27 @@ public MessageList getMessageList(MessageListArgs args)
client.addParameter("isIncoming", args.isIncoming() ? "true"
: "false");
+ return client.httpGet().getResponseBody();
+ }
+
+ public Message getMessage(final String msgId) throws RESTException {
try {
- APIResponse response = client.httpGet();
- JSONObject jobj = new JSONObject(response.getResponseBody());
- return MessageList.valueOf(jobj);
+ JSONObject jobj = new JSONObject(getMessageAndReturnRawJson(msgId));
+ return Message.valueOf(jobj.getJSONObject("message"));
} catch (JSONException pe) {
throw new RESTException(pe);
}
}
- public Message getMessage(final String msgId) throws RESTException {
+ public String getMessageAndReturnRawJson(final String msgId)
+ throws RESTException {
final String endpoint = getFQDN() + "/myMessages/v2/messages/" + msgId;
- final APIResponse response = new RESTClient(endpoint)
+ final String responseBody = new RESTClient(endpoint)
.addAuthorizationHeader(getToken())
- .setHeader("Accept", "application/json").httpGet();
-
- try {
- JSONObject jobj = new JSONObject(response.getResponseBody());
- return Message.valueOf(jobj.getJSONObject("message"));
- } catch (JSONException pe) {
- throw new RESTException(pe);
- }
+ .setHeader("Accept", "application/json").httpGet()
+ .getResponseBody();
+ return responseBody;
}
public MessageContent getMessageContent(String msgId, String partNumber)
@@ -218,23 +239,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 (JSONException 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";
@@ -242,7 +280,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());
@@ -256,10 +294,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();
@@ -268,23 +306,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();
@@ -299,10 +339,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();
@@ -315,9 +354,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();
@@ -330,9 +368,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();
@@ -342,40 +379,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 (JSONException 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 (JSONException 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/mms/service/MMSService.java b/codekit/src/main/java/com/att/api/mms/service/MMSService.java
index 2fdb531..f19d17f 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
@@ -15,7 +15,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package com.att.api.mms.service;
import com.att.api.mms.model.MMSStatus;
@@ -31,19 +30,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);
@@ -52,19 +54,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 (JSONException 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();
@@ -75,50 +96,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 (JSONException 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 (JSONException 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 4300242..c803813 100644
--- a/codekit/src/main/java/com/att/api/oauth/OAuthService.java
+++ b/codekit/src/main/java/com/att/api/oauth/OAuthService.java
@@ -98,6 +98,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
@@ -122,6 +125,10 @@ private OAuthToken parseResponse(APIResponse response)
expiresIn = OAuthToken.NO_EXPIRATION;
}
+ if(expiresInOverride > 0) {
+ expiresIn = expiresInOverride;
+ }
+
return new OAuthToken(accessToken, expiresIn, refreshToken);
} catch (JSONException e) {
String msg = e.getMessage();
@@ -153,10 +160,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);
}
/**
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 7bdda9c..419e6a8 100644
--- a/codekit/src/main/java/com/att/api/oauth/OAuthToken.java
+++ b/codekit/src/main/java/com/att/api/oauth/OAuthToken.java
@@ -92,7 +92,7 @@ public class OAuthToken {
* @return seconds since Unix epoch
*/
private static long xtimestamp() {
- return System.currentTimeMillis() / 1000;
+ return System.currentTimeMillis() / 1000L;
}
/**
@@ -158,12 +158,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 e7ac078..5bd76dd 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
@@ -20,13 +20,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) {
@@ -42,21 +40,16 @@ 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");
@@ -66,21 +59,27 @@ public Notary getNotary(String rawStr) throws RESTException {
}
}
+ 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 8f99483..c3e0eab 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
@@ -14,31 +14,25 @@
import com.att.api.service.APIService;
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());
@@ -47,69 +41,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 (JSONException 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 getTransactionStatus(Transaction.Type type, String id) throws RESTException {
- return this.getTransactionStatus(type.toString(), id);
+ 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 {
+ public JSONObject getSubscriptionStatus(String type, String id)
+ throws RESTException {
String surl = SUBSCRIPTION_SUBURL + "/" + type + "/" + id;
return this.getPaymentInfo(surl);
}
- public JSONObject getSubscriptionStatus(Subscription.Type type, String id) throws RESTException {
- return this.getSubscriptionStatus(type.toString(), id);
+ 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;
+ 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());
@@ -118,49 +136,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 JSONObject refundSubscription(String subId, String reasonTxt,
- int reasonCode) throws RESTException {
+ public String cancelSubscriptionAndReturnRawJson(String subId,
+ String reasonTxt, int reasonCode) throws RESTException {
String surl = TRANS_SUBURL + "/" + subId;
- return this.sendTransOptStatus(reasonTxt, reasonCode, "Refunded", surl);
+ return this.sendTransOptStatusAndReturnRawJson(reasonTxt, reasonCode,
+ "SubscriptionCancelled", surl);
}
- public JSONObject refundSubscription(String subId, String reasonTxt,
- RefundReason reasonCode) throws RESTException {
- return this.refundSubscription(subId, reasonTxt,
- reasonCode.getValue());
+ 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 JSONObject refundTransaction(String transId, String reasonTxt,
- RefundReason reasonCode) throws RESTException {
- return this.refundTransaction(transId, reasonTxt,
- reasonCode.getValue());
- }
+ 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 be0ce9c..58ed3e8 100644
--- a/codekit/src/main/java/com/att/api/rest/RESTClient.java
+++ b/codekit/src/main/java/com/att/api/rest/RESTClient.java
@@ -334,6 +334,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();
@@ -503,6 +506,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()
.
*
@@ -708,7 +736,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);
@@ -836,7 +864,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 80363e6..2e129e6 100644
--- a/codekit/src/main/java/com/att/api/rest/RESTConfig.java
+++ b/codekit/src/main/java/com/att/api/rest/RESTConfig.java
@@ -35,6 +35,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;
@@ -44,6 +47,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;
@@ -111,6 +117,7 @@ public RESTConfig(String url, String proxyHost,
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
this.trustAllCerts = trustAllCerts;
+ this.clientSdk = this.defaultClientSdk;
}
/**
@@ -140,6 +147,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.
*
@@ -161,6 +177,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 eb36615..e5715a9 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
@@ -67,6 +67,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) {
@@ -93,6 +102,11 @@ public SMSSendResponse sendSMS(String rawAddr, String msg,
.httpPost(rpcObject.toString())
.getResponseBody();
+<<<<<<< HEAD
+ return responseBody;
+ }
+
+=======
try {
return SMSSendResponse.valueOf(new JSONObject(responseBody));
} catch (JSONException pe) {
@@ -100,6 +114,7 @@ public SMSSendResponse sendSMS(String rawAddr, String msg,
}
}
+>>>>>>> efc7dea6f506bef8fe89e64659bea2ccfb7609fa
/**
* Sends a request for getting delivery status information about an SMS.
*
@@ -108,6 +123,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)
@@ -115,12 +138,16 @@ public SMSStatus getSMSDeliveryStatus(String msgId) throws RESTException {
.addHeader("Accept", "application/json")
.httpGet()
.getResponseBody();
+<<<<<<< HEAD
+ return responseBody;
+=======
try {
return SMSStatus.valueOf(new JSONObject(responseBody));
} catch (JSONException pe) {
throw new RESTException(pe);
}
+>>>>>>> efc7dea6f506bef8fe89e64659bea2ccfb7609fa
}
/**
@@ -133,6 +160,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;
@@ -141,11 +177,15 @@ public SMSGetResponse getSMS(String registrationID) throws RESTException {
.addHeader("Accept", "application/json")
.httpGet()
.getResponseBody();
+<<<<<<< HEAD
+ return responseBody;
+=======
try {
return SMSGetResponse.valueOf(new JSONObject(responseBody));
} catch (JSONException pe) {
throw new RESTException(pe);
}
+>>>>>>> efc7dea6f506bef8fe89e64659bea2ccfb7609fa
}
}
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 51cd717..e316dbb 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
@@ -62,35 +62,31 @@ public SpeechResponse speechToText(File audio, File grammar,
/**
* Request the API to convert audio to text
*
- * @param audio audio to convert to to text.
+ * @param attachments audio files to convert to to text.
* @param grammar grammar file
- * @param dictionary dictionary file
- * @param speechContext modify the speech context of the request
* @param xArg add additional xarg values
*
* @return response in the form of a SpeechResponse object
* @throws RESTException
*/
- public SpeechResponse speechToText(File audio, File grammar,
- File dictionary, String speechContext,
- String xArg) throws RESTException {
+ 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");
+ .addAuthorizationHeader(getToken())
+ .addHeader("Accept", "application/json")
+ .addHeader("X-SpeechContext", speechContext)
+ .addHeader("Content-Language", isoLanguage);
- if (speechContext != null && !speechContext.equals(""))
- restClient.addHeader("X-SpeechContext", speechContext);
-
- if (xArg != null && !xArg.equals(""))
- restClient.addHeader("X-Arg", xArg);
-
- String[] attachments = {
- dictionary.getAbsolutePath(),
- grammar.getAbsolutePath(),
- audio.getAbsolutePath()
- };
+ if (xArg != null && !xArg.equals("")) {
+ restClient.setHeader("X-Arg", xArg);
+ }
String subType = "x-srgs-audio";
String[] bodyNameAttribute = {
@@ -99,15 +95,7 @@ public SpeechResponse speechToText(File audio, File grammar,
"x-voice"
};
- APIResponse apiResponse = restClient.httpPost(attachments, subType,
- bodyNameAttribute);
- try {
- return SpeechResponse.valueOf(
- new JSONObject(apiResponse.getResponseBody())
- );
- } catch (JSONException e) {
- // Wrap in a RESTException
- throw new RESTException(e);
- }
+ APIResponse apiResponse = restClient.httpPost(attachments, subType, bodyNameAttribute);
+ 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 dc43d95..f285eaf 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,48 +90,29 @@ public SpeechResponse speechToText(File audio, String xArgs,
* @throws RESTException
* @see SpeechResponse
*/
- public SpeechResponse speechToText(File audio, String xArgs,
- String speechContext, String subContext) throws RESTException {
+ public SpeechResponse sendRequest(File audio, String xArg,
+ String speechContext, String subContext) throws Exception {
+ return parseSuccess(sendRequestAndReturnRawJson(audio, 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");
+ .addAuthorizationHeader(getToken())
+ .addHeader("Accept", "application/json")
+ .addHeader("X-SpeechContext", speechContext)
+ .addHeader("Content-Language", isoLanguage);
- if (speechContext != null && !speechContext.equals(""))
- restClient.addHeader("X-SpeechContext", speechContext);
-
- if (xArgs != null && !xArgs.equals("")) {
- restClient.addHeader("X-Arg", xArgs);
+ if (xArg != null && !xArg.equals("")) {
+ restClient.setHeader("X-Arg", xArg);
}
if (subContext != null && !subContext.equals("")
&& speechContext.equals("Gaming")) {
restClient.addHeader("X-SpeechSubContext", subContext);
}
- APIResponse apiResponse = restClient.httpPost(audio);
- try {
- return SpeechResponse.valueOf(
- new JSONObject(apiResponse.getResponseBody()));
- } catch (JSONException e) {
- throw new RESTException(e);
- }
- }
-
- /**
- * Sends the request to the server.
- *
- * @param audio audio file to convert to text
- * @param xArgs Special information about the request
- * @param speechContext additional context information about the audio
- * @param subContext speechContext additional information
- *
- * @return a response in the form of a SpeechResponse object
- * @throws RESTException
- * @see SpeechResponse
- * @deprecated use speechToText instead
- */
- public SpeechResponse sendRequest(File audio, String xArgs,
- String speechContext, String subContext) throws RESTException {
- return speechToText(audio, xArgs, speechContext, subContext);
+ APIResponse apiResponse = restClient.httpPost(file);
+ 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 a3cb148..0f0fe3e 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
@@ -11,7 +11,7 @@
/**
* Class that handles communication with the speech server.
- *
+ *
*/
public class TtsService extends APIService {
@@ -26,7 +26,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
@@ -44,7 +44,7 @@ private byte[] parseSuccess(APIResponse wavResponse) throws RESTException {
/**
* 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
@@ -63,26 +63,25 @@ private void parseFailure(APIResponse response) throws RESTException {
/**
* Sends the request to the server.
- *
- * @param file
- * file to send.
- * @param speechContext
- * speech context
+ *
+ * @param contentType
+ * @param speechText
+ * @param xArg
* @return a byte array
- * @throws RESTException
* @see SpeechResponse
*/
- public byte[] sendRequest(String contentType, String speechText, String xArg)
- throws RESTException {
+ 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);