Created
July 16, 2012 23:21
-
-
Save t2-support-gists/3125770 to your computer and use it in GitHub Desktop.
TL Java RESTFul App 1
This file contains hidden or 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.' | |
2013 TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION: | |
http://developer.att.com/sdk_agreement/ Copyright 2013 AT&T Intellectual | |
Property. All rights reserved. http://developer.att.com For more information | |
contact [email protected] --> | |
--> | |
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://devconnect-api.att.com/ 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://devconnect-api.att.com/docs/oauth20/autonomous-client-application-oauth-flow | |
or | |
https://devconnect-api.att.com/docs/oauth20/web-server-client-application-oauth-flow | |
respectively). | |
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 hidden or 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 [email protected] | |
%> | |
<% | |
String scope = "TL"; | |
String clientIdAut = ""; | |
String clientSecretAut = ""; | |
String FQDN = "https://api-uat.bf.pacer.sl.attcompute.com"; | |
String redirectUri = ""; | |
%> |
This file contains hidden or 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 [email protected] | |
%> | |
<%@ page contentType="text/html; charset=iso-8859-1" language="java"%> | |
<%@ page import="java.util.Date" %> | |
<%@ page import="java.io.PrintWriter" %> | |
<%@ page import="java.io.BufferedWriter" %> | |
<%@ page import="java.io.FileWriter" %> | |
<%@ page import="org.apache.commons.httpclient.*"%> | |
<%@ page import="org.apache.commons.httpclient.methods.*"%> | |
<%@ page import="org.json.JSONObject"%> | |
<%@ page import="java.util.*" %> | |
<%@ page import="java.net.URLDecoder" %> | |
<%@ include file="config.jsp"%> | |
<% | |
Long currentTime = System.currentTimeMillis(); | |
String accessTokenError = ""; | |
String code = request.getParameter("code"); | |
if(code==null) code=""; | |
String getDeviceLocation = request.getParameter("getDeviceLocation"); | |
String refreshToken = request.getParameter("refreshToken"); | |
if (refreshToken==null) | |
refreshToken=(String) session.getAttribute("refreshToken"); | |
if (refreshToken==null) | |
refreshToken=""; | |
String getRefreshToken = request.getParameter("getRefreshToken"); | |
if (getRefreshToken==null) getRefreshToken=""; | |
//Second time, if client id is valid we should now have the code parameter on the url. Use the code get the access token and set the token in session | |
if(!code.equalsIgnoreCase("")) | |
{ | |
String url = FQDN + "/oauth/token"; | |
HttpClient client = new HttpClient(); | |
PostMethod method = new PostMethod(url); | |
String b = "client_id=" + clientIdAut + "&client_secret=" + clientSecretAut + "&grant_type=authorization_code&code=" + code; | |
method.addRequestHeader("Accept","application/json"); | |
method.addRequestHeader("Content-Type","application/x-www-form-urlencoded"); | |
method.setRequestEntity(new StringRequestEntity(b)); | |
int statusCode = client.executeMethod(method); | |
if(statusCode==200) | |
{ | |
JSONObject rpcObject = new JSONObject(method.getResponseBodyAsString()); | |
String accessToken = rpcObject.getString("access_token"); | |
refreshToken = rpcObject.getString("refresh_token"); | |
session.setAttribute("refreshToken", refreshToken); | |
session.setAttribute("accessToken", accessToken); | |
String expires_in = rpcObject.getString("expires_in"); | |
method.releaseConnection(); | |
String postOauth = (String) session.getAttribute("postOauth"); | |
if(postOauth!= null) | |
{ | |
session.setAttribute("postOauth", null); | |
response.sendRedirect(postOauth); | |
} | |
} | |
else | |
{ | |
accessTokenError = method.getResponseBodyAsString(); | |
if (accessTokenError == null || accessTokenError.length() == 0) accessTokenError = "" + statusCode; | |
session.setAttribute("errorResponse",accessTokenError); | |
} | |
method.releaseConnection(); | |
getDeviceLocation = "true"; | |
} | |
//Refresh token scenario | |
else if(!getRefreshToken.equalsIgnoreCase("")) | |
{ | |
String url = FQDN + "/oauth/token"; | |
HttpClient client = new HttpClient(); | |
PostMethod method = new PostMethod(url); | |
String b = "client_id=" + clientIdAut + "&client_secret=" + clientSecretAut + "&grant_type=refresh_token&refresh_token=" + refreshToken; | |
method.addRequestHeader("Content-Type","application/x-www-form-urlencoded"); | |
method.setRequestEntity(new StringRequestEntity(b)); | |
int statusCode = client.executeMethod(method); | |
if(statusCode==200) | |
{ | |
String accessToken = method.getResponseBodyAsString().substring(18,50); | |
session.setAttribute("accessToken", accessToken); | |
String postOauth = (String) session.getAttribute("postOauth"); | |
if(postOauth!= null) | |
{ | |
session.setAttribute("postOauth", null); | |
response.sendRedirect(postOauth); | |
} | |
} | |
method.releaseConnection(); | |
getDeviceLocation = "true"; | |
} | |
else if (request.getParameter("error") != null) | |
{ | |
String error = (String) request.getParameter("error"); | |
String errorResponse = ""; | |
String errorDescription = request.getParameter("error_description"); | |
if (error != null ) | |
{ | |
errorResponse = URLDecoder.decode(request.getQueryString(),"iso-8859-1"); | |
} | |
else if (errorDescription != null && errorDescription.length() != 0) | |
{ | |
errorResponse = errorDescription; | |
} | |
session.setAttribute("errorResponse",errorResponse); | |
getDeviceLocation = "true"; | |
} | |
%> |
This file contains hidden or 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 [email protected] | |
%> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | |
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head> | |
<title>AT&T Sample Application - TL Service Application</title> | |
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"> | |
<link rel="stylesheet" type="text/css" href="style/common.css"/ > | |
<script type="text/javascript" src="js/helper.js"> | |
</script> | |
<body> | |
<%@ 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"%> | |
<%@ include file="oauth.jsp" %> | |
<% | |
String requestedAccuracy = request.getParameter("requestedAccuracy"); | |
String acceptableAccuracy = request.getParameter("acceptableAccuracy"); | |
String tolerance = request.getParameter("tolerance"); | |
String accessToken = (String) session.getAttribute("accessToken"); | |
if (requestedAccuracy == null) | |
{ | |
requestedAccuracy = (String) session.getAttribute("requestedAccuracy"); | |
} | |
else | |
{ | |
session.setAttribute("requestedAccuracy",requestedAccuracy); | |
} | |
if (acceptableAccuracy == null) { | |
acceptableAccuracy = (String) session.getAttribute("acceptableAccuracy"); | |
} | |
else | |
{ | |
session.setAttribute("acceptableAccuracy",acceptableAccuracy); | |
} | |
if (tolerance == null) { | |
tolerance = (String) session.getAttribute("tolerance"); | |
} | |
else | |
{ | |
session.setAttribute("tolerance",tolerance); | |
} | |
%> | |
<div id="container"> | |
<!-- open HEADER --><div id="header"> | |
<div> | |
<div id="hcRight"> | |
<%=new java.util.Date()%> | |
</div> | |
<div id="hcLeft">Server Time:</div> | |
</div> | |
<div> | |
<div id="hcRight"><script language="JavaScript" type="text/javascript"> | |
var myDate = new Date(); | |
document.write(myDate); | |
</script></div> | |
<div id="hcLeft">Client Time:</div> | |
</div> | |
<div> | |
<div id="hcRight"><script language="JavaScript" type="text/javascript"> | |
document.write("" + navigator.userAgent); | |
</script></div> | |
<div id="hcLeft">User Agent:</div> | |
</div> | |
<br clear="all" /> | |
</div><!-- close HEADER --> | |
<div id="wrapper"> | |
<div id="content"> | |
<h1>AT&T Sample Application - TL</h1> | |
<h2>Feature 1: Map of Device Location</h2> | |
</div> | |
</div> | |
<form method="post" name="getDeviceLocation" action="TL.jsp"> | |
<div id="navigation"> | |
<table border="0" width="100%"> | |
<tbody> | |
<tr> | |
<td valign="top" class="label">Requested Accuracy:</td> | |
<td valign="top" class="cell"><input type="radio" name="requestedAccuracy" value="150" /> 150 m <input type="radio" name="requestedAccuracy" value="1000" checked /> 1,000 m <input type="radio" name="requestedAccuracy" value="10000" /> 10,000 m </td> | |
</tr> | |
<tr> | |
<td valign="top" class="label">Acceptable Accuracy:</td> | |
<td valign="top" class="cell"><input type="radio" name="acceptableAccuracy" value="150" /> 150 m <input type="radio" name="acceptableAccuracy" value="1000" /> 1,000 m <input type="radio" name="acceptableAccuracy" value="10000" checked /> 10,000 m </td> | |
</tr> | |
<tr> | |
<td valign="top" class="label">Delay Tolerance:</td> | |
<td valign="top" class="cell"><input type="radio" name="tolerance" value="NoDelay" /> No Delay <input type="radio" name="tolerance" value="LowDelay" checked /> Low Delay <input type="radio" name="tolerance" value="DelayTolerant" /> Delay Tolerant </td> | |
</td> | |
</tr> | |
</tbody></table> | |
</div> | |
<div id="extra"> | |
<table border="0" width="100%"> | |
<tbody> | |
<tr> | |
<td class="cell"><br /><br /><br /><br /><br /><br /><button type="submit" name="getDeviceLocation">Get Phone Location</button></td> | |
</tr> | |
</tbody></table> | |
</div> | |
<br clear="all" /> | |
<div align="center"></div> | |
</form> | |
<% | |
if(getDeviceLocation!=null) | |
{ | |
if((accessToken == null && request.getParameter("error_description") == null && code.length() == 0)) | |
{ | |
response.sendRedirect(FQDN + "/oauth/authorize?client_id=" + clientIdAut + "&scope=" + scope + "&redirect_uri=" + redirectUri); | |
} | |
else if (accessToken != null) | |
{ | |
String url = FQDN + "/2/devices/location"; | |
HttpClient client = new HttpClient(); | |
GetMethod method = new GetMethod(url); | |
method.setQueryString("requestedAccuracy=" + requestedAccuracy +"&acceptableAccuracy=" + acceptableAccuracy + "&tolerance=" + tolerance); | |
method.addRequestHeader("Accept","application/json"); | |
method.addRequestHeader("Authorization","Bearer " + accessToken); | |
Long start = System.currentTimeMillis(); | |
int statusCode = client.executeMethod(method); | |
Long end = System.currentTimeMillis(); | |
Long elapsed = (end-start)/1000; | |
if(statusCode==200 || statusCode==201) { | |
JSONObject rpcObject = new JSONObject(method.getResponseBodyAsString()); | |
%> | |
<div class="successWide"> | |
<strong>SUCCESS:</strong><br /> | |
<strong>Latitude:</strong> <%=rpcObject.getString("latitude")%><br /> | |
<strong>Longitude:</strong> <%=rpcObject.getString("longitude")%><br /> | |
<strong>Accuracy:</strong> <%=rpcObject.getString("accuracy")%><br /> | |
<strong>Response Time:</strong> <%=elapsed%> seconds | |
</div> | |
<br /><br /> | |
<div align="center"> | |
<iframe width="600" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" | |
src="http://maps.google.com/?q=<%=rpcObject.getString("latitude")%>+<%=rpcObject.getString("longitude")%>&output=embed"></iframe><br /></div><% | |
} else { | |
%> | |
<div class="errorWide"> | |
<strong>ERROR:</strong><br /> | |
<%=method.getResponseBodyAsString()%> | |
</div><br/> | |
<% | |
} | |
method.releaseConnection(); | |
session.removeAttribute("requestedAccuracy"); | |
session.removeAttribute("acceptableAccuracy"); | |
session.removeAttribute("tolerance"); | |
} | |
else if (request.getParameter("error_description") != null) | |
{ | |
%> | |
<div class="errorWide"> | |
<strong>ERROR:</strong><br /> | |
<%=request.getParameter("error_description")%> | |
</div><br/> | |
<% | |
} | |
} | |
%> | |
<div id="footer"> | |
<div style="float: right; width: 20%; font-size: 9px; text-align: right">Powered by AT&T Cloud Architecture</div> | |
<p>© 2012 AT&T Intellectual Property. All rights reserved. <a href="http://developer.att.com/" target="_blank">http://developer.att.com</a> | |
<br> | |
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> | |
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:[email protected]">[email protected]</a> | |
</div> | |
</div> | |
</body></html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment