Last active
October 6, 2015 18:38
-
-
Save t2-support-gists/3036598 to your computer and use it in GitHub Desktop.
Speech RESTful Java App 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AT&T API Platform Sample Application | |
------------------------------------- | |
This file describes how to set up, configure, and run the Java Application | |
using AT&T's API Platform services. It covers all steps required to register | |
the application, and create and run one's own full-fledged sample applications | |
based on the generated API keys and secrets. | |
1. Configuration | |
Configuration consists of a few steps necessary to get an application | |
registered with the proper services and endpoints, depending on the type of | |
client-side application (autonomous/non-autonomous). | |
To register an application, go to https://developer.att.com/developer/mvc/auth/login and login | |
with your valid username and password. Next, choose "My Apps" from the bar | |
at the top of the page and click the "Setup a New Application" button. | |
Fill in the form--particularly all fields marked as "required." | |
NOTE: You MUST select the application used in the list of services under | |
field 'Services' in order to use this sample application code. | |
Having your application registered, you will get back an important pair of | |
data: an API key and Secret key. They are necessary to get your applications | |
working with the AT&T APIs. See 'Adjusting parameters' below to learn how to | |
use these keys. | |
Initially your newly registered application is restricted to the "Sandbox" | |
environment only. To move it to production, you may promote it by clicking | |
the "Promote to production" button. Notice that you will get a different API | |
key and secret, so these values in your application should be adjusted | |
accordingly. | |
Depending on the kind of authentication used, an application may be based on | |
either the Autonomous Client or the Web-Server Client OAuth flow (see | |
https://developer.att.com/apis/oauth-2/docs). | |
2. Installation | |
** Requirements | |
To run the examples, you will need the Java Runtime Environment (JRE), Java | |
Development Kit (JDK), and Apache Maven. | |
** Setting up multiple sample applications simultaneously | |
In case multiple applications need to be run at the same time, make sure to | |
put each app in separate folders. | |
3. Parameters | |
Each sample application contains an application.properties file. This file | |
is located in the 'src/main/resources/' folder. This file holds configurable | |
parameters described in an easy-to-read format. Please modify the | |
application.properties file using the comments specified within the file. | |
Note: If your application is promoted from Sandbox environment to Production | |
environment and you decide to use production application settings, you must | |
update parameters as per production application details. | |
4. Running the application | |
The project follows Apache Maven's Standard Directory Layout and can be | |
built using Apache Maven. For information about Apache Maven and for more | |
detailed instructions, consult Apache Maven's documentation. | |
Using a terminal, change the current directory to the root folder of the | |
sample application (the directory should contain pom.xml). Run the following | |
command in order to build and run the application: | |
mvn clean jetty:run | |
This command should run the application on port 8080. Make sure no other | |
application is running on port 8080. In order to change the port, consult | |
Jetty's documentation. To connect to the sample application, open a web | |
browser and visit 'http://localhost:8080/<appname>' replacing <appname> with | |
the application's name. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%//Licensed by AT&T under 'Software Development Kit Tools Agreement.' 2012 | |
//TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION: http://developer.att.com/sdk_agreement/ | |
//Copyright 2012 AT&T Intellectual Property. All rights reserved. http://developer.att.com | |
//For more information contact developer.support@att.com%> | |
<%@ page import="com.att.api.speech.handler.Config"%> | |
<% | |
Config cfg = new Config(); | |
cfg.clientIdAuth = ""; | |
cfg.clientSecretAuth = ""; | |
cfg.FQDN = "https://api.att.com"; | |
cfg.endPointURL = cfg.FQDN + "/rest/2/SpeechToText"; | |
cfg.speechContexts = new String[] { "BusinessSearch", "Websearch", "SMS", "Voicemail", | |
"QuestionAndAnswer", "TV", "Generic" }; | |
// max size permitted for upload file | |
cfg.maxUploadFileSize = 10 * 1024 * 1024; //10 mb | |
cfg.defaultFile = "bostonCeltics.wav"; | |
/* | |
the optional X-Arg HTTP Header may also be set | |
Example: | |
cfg.xarg.put("ClientVersion", "1.0.0"); | |
cfg.xarg.put("DeviceType", "SAMSUNG-SGH-I927"); | |
*/ | |
cfg.trustAllCerts = false; | |
cfg.linkSource = "#"; | |
cfg.linkDownload = "#"; | |
cfg.linkHelp = "#"; | |
cfg.audioFolder = "audio/"; | |
session.setAttribute("cfg", cfg); | |
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% | |
//Licensed by AT&T under 'Software Development Kit Tools Agreement.' 2012 | |
//TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION: http://developer.att.com/sdk_agreement/ | |
//Copyright 2012 AT&T Intellectual Property. All rights reserved. http://developer.att.com | |
//For more information contact developer.support@att.com | |
%> | |
<%@ page contentType="text/html; charset=iso-8859-1" language="java"%> | |
<%@ page import="org.apache.commons.httpclient.*"%> | |
<%@ page import="org.apache.commons.httpclient.methods.*"%> | |
<%@ page import="org.json.JSONObject"%> | |
<%@ page import="org.json.JSONArray"%> | |
<%@ page import="com.att.api.speech.handler.Config"%> | |
<%@ page import="java.io.*"%> | |
<%@ include file="OauthStorage.jsp"%> | |
<%@ include file="config.jsp"%> | |
<%@ page import="java.lang.Math"%> | |
<% | |
//Initialize some variables here, check if relevant variables were passed in, if not then check session, otherwise set default. | |
String scope = "SPEECH"; | |
String accessToken = ""; | |
String refreshToken = ""; | |
String expires_in = "null"; | |
Long date = System.currentTimeMillis(); | |
if (session.getAttribute("accessToken") == null) { | |
//This application uses the Autonomous Client OAuth consumption model | |
//Check if there is a valid access token that has not expired | |
if (date < savedAccessTokenExpiry) { | |
accessToken = savedAccessToken; | |
session.setAttribute("accessToken", accessToken); | |
} else if (date < savedRefreshTokenExpiry) { //Otherwise if there is a refresh token that has not expired, use that to renew and save to file | |
String url = cfg.FQDN + "/oauth/token"; | |
HttpClient client = new HttpClient(); | |
PostMethod method = new PostMethod(url); | |
String b = "client_id=" + cfg.clientIdAuth + "&client_secret=" + cfg.clientSecretAuth | |
+ "&grant_type=refresh_token&refresh_token=" + savedRefreshToken; | |
method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | |
method.setRequestBody(b); | |
int statusCode = client.executeMethod(method); | |
if (statusCode == 200) { | |
JSONObject rpcObject = new JSONObject(method.getResponseBodyAsString()); | |
accessToken = rpcObject.getString("access_token"); | |
refreshToken = rpcObject.getString("refresh_token"); | |
expires_in = rpcObject.getString("expires_in"); | |
if (expires_in.equals("0")) { | |
savedAccessTokenExpiry = date + (Long.parseLong("3155692597470")); //100 years | |
} | |
savedRefreshTokenExpiry = date + Long.parseLong("86400000"); | |
method.releaseConnection(); | |
PrintWriter outWrite = new PrintWriter(new BufferedWriter(new FileWriter( | |
application.getRealPath("/OauthStorage.jsp"))), false); | |
String toSave = "\u003C\u0025\nString savedAccessToken = \"" + accessToken | |
+ "\";\nLong savedAccessTokenExpiry = Long.parseLong(\"" | |
+ savedAccessTokenExpiry + "\");\nString savedRefreshToken = \"" | |
+ refreshToken | |
+ "\";\nLong savedRefreshTokenExpiry = Long.parseLong(\"" | |
+ savedRefreshTokenExpiry + "\");\n\u0025\u003E"; | |
outWrite.write(toSave); | |
outWrite.close(); | |
session.setAttribute("accessToken", accessToken); | |
} else { | |
session.setAttribute("statusCode", statusCode); | |
session.setAttribute("errorResponse", | |
method.getResponseBodyAsString() == null ? method.getStatusText() | |
: method.getResponseBodyAsString()); | |
} | |
} else if (date > savedRefreshTokenExpiry) { //Otherwise get a new access token and refresh token, and save them to file | |
String url = cfg.FQDN + "/oauth/token"; | |
HttpClient client = new HttpClient(); | |
PostMethod method = new PostMethod(url); | |
String b = "client_id=" + cfg.clientIdAuth + "&client_secret=" + cfg.clientSecretAuth | |
+ "&grant_type=client_credentials&scope=" + scope; | |
method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | |
method.setRequestBody(b); | |
int statusCode = client.executeMethod(method); | |
if (statusCode == 200) { | |
JSONObject rpcObject = new JSONObject(method.getResponseBodyAsString()); | |
accessToken = rpcObject.getString("access_token"); | |
refreshToken = rpcObject.getString("refresh_token"); | |
expires_in = rpcObject.getString("expires_in"); | |
if (expires_in.equals("0")) { | |
savedRefreshTokenExpiry = date + (Long.parseLong("86400000")); //24 hours | |
} | |
savedAccessTokenExpiry = date + (Long.parseLong(expires_in) * 1000); | |
method.releaseConnection(); | |
PrintWriter outWrite = new PrintWriter(new BufferedWriter(new FileWriter( | |
application.getRealPath("/OauthStorage.jsp"))), false); | |
String toSave = "\u003C\u0025\nString savedAccessToken = \"" + accessToken | |
+ "\";\nLong savedAccessTokenExpiry = Long.parseLong(\"" | |
+ savedAccessTokenExpiry + "\");\nString savedRefreshToken = \"" | |
+ refreshToken | |
+ "\";\nLong savedRefreshTokenExpiry = Long.parseLong(\"" | |
+ savedRefreshTokenExpiry + "\");\n\u0025\u003E"; | |
outWrite.write(toSave); | |
outWrite.close(); | |
session.setAttribute("accessToken", accessToken); | |
} else { | |
session.setAttribute("statusCode", statusCode); | |
session.setAttribute("errorResponse", | |
method.getResponseBodyAsString() == null ? method.getStatusText() | |
: method.getResponseBodyAsString()); | |
} | |
} | |
} | |
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% | |
String savedAccessToken = ""; | |
Long savedAccessTokenExpiry = Long.parseLong("1"); | |
String savedRefreshToken = ""; | |
Long savedRefreshTokenExpiry = Long.parseLong("1"); | |
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<%@ page import="com.att.api.util.DateUtil"%> | |
<%@ page import="com.att.api.speech.model.SpeechResponse"%> | |
<%@ page import="com.att.api.speech.handler.Config"%> | |
<%@ page import="java.util.HashMap" %> | |
<%@ include file="getToken.jsp"%> | |
<% | |
String cfgError = cfg.getError(); | |
if (cfgError != null) { | |
request.setAttribute("response", new SpeechResponse(cfgError)); | |
} | |
%> | |
<!-- | |
Licensed by AT&T under 'Software Development Kit Tools Agreement.' | |
September 2011 | |
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION: | |
http://developer.att.com/sdk_agreement/ | |
Copyright 2011 AT&T Intellectual Property. All rights reserved. | |
http://developer.att.com | |
For more information contact developer.support@att.com | |
--> | |
<!--[if lt IE 7]> <html class="ie6" lang="en"> <![endif]--> | |
<!--[if IE 7]> <html class="ie7" lang="en"> <![endif]--> | |
<!--[if IE 8]> <html class="ie8" lang="en"> <![endif]--> | |
<!--[if gt IE 8]><!--> | |
<html lang="en"> | |
<!--<![endif]--> | |
<head> | |
<title>AT&T Sample Speech Application - Speech to Text | |
(Generic)</title> | |
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> | |
<meta id="viewport" name="viewport" | |
content="width=device-width,minimum-scale=1,maximum-scale=1"> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> | |
<link rel="stylesheet" type="text/css" href="style/common.css"> | |
</head> | |
<body> | |
<div id="pageContainer" class="pageContainer"> | |
<div id="header"> | |
<div class="logo" id="top"></div> | |
<div id="menuButton" class="hide"> | |
<a id="jump" href="#nav">Main Navigation</a> | |
</div> | |
<ul class="links" id="nav"> | |
<li><a href="#" target="_blank">Full Page<img | |
src="images/max.png"></img></a> <span class="divider"> | </span> | |
</li> | |
<li><a href="<%=cfg.linkSource%>" target="_blank">Source<img | |
src="images/source.png" /></a> <span class="divider"> | </span></li> | |
<li><a href="<%=cfg.linkDownload%>" target="_blank">Download<img | |
src="images/download.png"></a> <span class="divider"> | |
| </span></li> | |
<li><a href="<%=cfg.linkHelp%>" target="_blank">Help</a></li> | |
<li id="back"><a href="#top">Back to top</a></li> | |
</ul> | |
<!-- end of links --> | |
</div> | |
<!-- end of header --> | |
<div class="content"> | |
<div class="contentHeading"> | |
<h1>AT&T Sample Application - Speech to Text</h1> | |
<div id="introtext"> | |
<div> | |
<b>Server Time:</b> | |
<%=DateUtil.getServerTime()%></div> | |
<div> | |
<b>Client Time :</b> | |
<script> | |
document.write("" + new Date()); | |
</script> | |
</div> | |
<div> | |
<b>User Agent :</b> | |
<script> | |
document.write("" + navigator.userAgent); | |
</script> | |
</div> | |
</div> | |
<!-- end of introtext --> | |
</div> | |
<% | |
HashMap<String,String> fieldValues = (HashMap<String,String>) session.getAttribute("formFields"); | |
String selectedContext = ""; | |
String sendChunked = ""; | |
String xArg = cfg.getXArgHTTPValue(); | |
String selectedFileName = ""; | |
if (fieldValues != null) { | |
if (fieldValues.get("chkChunked") != null) sendChunked = " checked"; | |
if (fieldValues.get("x-arg") != null) xArg = fieldValues.get("x-arg"); | |
if (fieldValues.get("filename") != null) selectedFileName = fieldValues.get("filename"); | |
} | |
%> | |
<!-- end of contentHeading --> | |
<div class="formBox" id="formBox"> | |
<div id="formContainer" class="formContainer"> | |
<form name="SpeechToText" action="upload" method="post"> | |
<div id="formData"> | |
<h3>Speech Context:</h3> | |
<select name="context"> | |
<% | |
if (cfg.speechContexts != null) { | |
for (final String sContext : cfg.speechContexts) { | |
if (fieldValues != null && fieldValues.get("context").equals(sContext)) { | |
selectedContext = " selected"; | |
} | |
else | |
{ | |
selectedContext = ""; | |
} | |
%> | |
<option value="<%=sContext%>" <%=selectedContext%>><%=sContext%></option> | |
<% | |
} | |
} | |
%> | |
</select> | |
<h3>Audio File:</h3> | |
<select name="filename"> | |
<% | |
String selectedAtt = ""; | |
String directory = request.getSession().getServletContext() | |
.getRealPath("/") + cfg.audioFolder; | |
File[] list = new File(directory).listFiles(); | |
for (File f : list) { | |
if (f.getName().equals(selectedFileName)) { | |
selectedAtt = " selected"; | |
} | |
else | |
{ | |
selectedAtt = ""; | |
} | |
%> | |
<option value="<%=f.getName()%>" <%=selectedAtt%>><%=f.getName()%></option> | |
<% | |
} | |
%> | |
</select> | |
<div id="chunked"> | |
<br /> | |
<b>Send Chunked:</b> | |
<input name="chkChunked" value="Send Chunked" type="checkbox" <%=sendChunked %>> | |
</div> | |
<h3>X-Arg:</h3> | |
<textarea id="x_arg" name="x-arg" readonly="readonly" rows="4"><%=xArg%></textarea> | |
<br> | |
<button type="submit" name="SpeechToText">Submit</button> | |
</div> | |
</form> | |
</div> | |
<% | |
String error = (String) session.getAttribute("errorResponse"); | |
SpeechResponse speechResponse = null; | |
if (error == null) { | |
speechResponse = (SpeechResponse) request | |
.getAttribute("response"); | |
if (speechResponse != null && speechResponse.hasError()) { | |
error = speechResponse.getError(); | |
} | |
} | |
if (error != null) { | |
%> | |
<div class="errorWide"> | |
<strong>ERROR:</strong><br /> | |
<%=error%> | |
</div> | |
<% | |
} else if (speechResponse != null) { | |
%> | |
<div class="successWide"> | |
<strong>SUCCESS:</strong> <br />Response parameters listed below. | |
</div> | |
<table class="kvp"> | |
<thead> | |
<tr> | |
<th class="label">Parameter</th> | |
<th class="label">Value</th> | |
</tr> | |
</thead> | |
<tbody> | |
<% | |
for (final String[] kvp : speechResponse.getResult()) { | |
final String key = kvp[0]; | |
final String value = kvp[1]; | |
%> | |
<tr> | |
<td class="cell" align="center"><em><%=key%></em></td> | |
<td class="cell" align="center"><em><%=value%></em></td> | |
</tr> | |
<% | |
} | |
%> | |
</tbody> | |
</table> | |
<% | |
} | |
%> | |
</div> | |
</div> | |
<!-- end of content --> | |
<div id="footer"> | |
<div id="ft"> | |
<div id="powered_by">Powered by AT&T Cloud Architecture</div> | |
<p> | |
The Application hosted on this site are working examples intended | |
to be used for reference in creating products to consume AT&T | |
Services and not meant to be used as part of your product. The data | |
in these pages is for test purposes only and intended only for use | |
as a reference in how the services perform. <br> <br> For | |
download of tools and documentation, please go to <a | |
href="https://devconnect-api.att.com/" target="_blank">https://devconnect-api.att.com</a> | |
<br> For more information contact <a | |
href="mailto:developer.support@att.com">developer.support@att.com</a> | |
<br> <br> � 2012 AT&T Intellectual Property. All | |
rights reserved. <a href="http://developer.att.com/" | |
target="_blank">http://developer.att.com</a> | |
</p> | |
</div> | |
<!-- end of ft --> | |
</div> | |
<!-- end of footer --> | |
</div> | |
<!-- end of page_container --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment