Skip to content

Instantly share code, notes, and snippets.

@t2-support-gists
Last active October 10, 2015 17:08
Show Gist options
  • Select an option

  • Save t2-support-gists/3723821 to your computer and use it in GitHub Desktop.

Select an option

Save t2-support-gists/3723821 to your computer and use it in GitHub Desktop.
DC Java RESTFul App 1
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.
<%
//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]
%>
<%
//Provisioned from App
//Client id
String client_id = "";
//Secret key
String secret_key = "";
//Application Scope
String scope = "DC";
//API Host name
String FQDN = "https://api.att.com";
//Device Capabilities
String getdc_url = "/rest/2/Devices/Info";
//Endpoint
String endpoint = FQDN + getdc_url;
//Application redirect URL, OAUTH will redirect to this URL. The value must match as provisioned by App
String redirectURL = "";
%>
<!--
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 [email protected]
-->
<%
response.addHeader("P3P", "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
%>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ page import="com.att.api.dc.handler.DCHandler" %>
<%@ page import="com.att.api.dc.model.DeviceInfo" %>
<%@ page import="com.att.api.util.DateUtil" %>
<%@ include file="oauth.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>AT&amp;T Sample DC Application - Get Device Capabilities Application</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<link rel="stylesheet" type="text/css" href="style/common.css" />
</head>
<body>
<%
String accessToken = (String) session.getAttribute("accessToken");
DCHandler dchandler = new DCHandler(request,endpoint,accessToken);
DeviceInfo deviceInfo = null;
if (accessToken != null)
{
deviceInfo = dchandler.processRequest();
}
else if (accessToken == null)
{
if (session.getAttribute("errorResponse") != null)
{
deviceInfo = new DeviceInfo();
deviceInfo.setStatus(false);
String errorResponse = (String) session.getAttribute("errorResponse");
deviceInfo.setErrorResponse(errorResponse);
session.removeAttribute("errorResponse");
}
else if(code.length() == 0)
{
//For the first time, we dont have access token, so we redirect to authenticate client id
response.sendRedirect(FQDN + "/oauth/authorize?client_id=" + client_id + "&scope=" + scope + "&redirect_uri=" + redirectURL);
}
}
%>
<div id="container">
<!-- open HEADER -->
<div id="header">
<div>
<div class="hcRight"><%=DateUtil.getServerTime()%></div>
<div class="hcLeft">Server Time:</div>
</div>
<div>
<div class="hcRight">
<script language="JavaScript" type="text/javascript">
var myDate = new Date();
document.write(myDate);
</script>
</div>
<div class="hcLeft">
Client Time:</div>
</div>
<div>
<div class="hcRight">
<script language="JavaScript" type="text/javascript">
document.write("" + navigator.userAgent);
</script>
</div>
<div class="hcLeft">
User Agent:</div>
</div>
<br clear="all" />
</div>
<!-- close HEADER -->
<div>
<div class="content">
<h1>
AT&amp;T Sample DC Application - Get Device Capabilities Application</h1>
<h2>
Feature 1: Get Device Capabilities</h2>
</div>
</div>
<br />
<br />
<div class="extra">
<table>
<tbody>
<div id="extraleft">
<div class="warning">
<strong>Note:</strong><br />
<strong>OnNet Flow:</strong> Request Device Capabilities details from the AT&T network
for the mobile device of an AT&T subscriber who is using an AT&T direct Mobile data
connection to access this application.
<br />
<strong>OffNet Flow:</strong> Where the end-user is not on an AT&T Mobile data connection
or using a Wi-Fi or tethering connection while accessing this application. This
will result in an HTTP 400 error.
</div>
</div>
</tbody>
</table>
</div>
<br clear="all" />
<%
if (deviceInfo != null && deviceInfo.isStatus())
{
%>
<div class="successWide" id="tb_dc_output" visible="false">
<strong>SUCCESS:</strong><br />
Device parameters listed below.
</div>
<br />
<div align="center">
<table width="500" cellpadding="1" cellspacing="1" border="0" runat="server" id="tbDeviceCapabilities"
visible="false">
<thead>
<tr>
<th width="50%" class="label">
Parameter
</th>
<th width="50%" class="label">
Value
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="cell" align="center">
<em>TypeAllocationCode</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getDeviceId() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>Name</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getName() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>Vendor</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getVendor() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>Model</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getModel() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>FirmwareVersion</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getFirmwareVersion() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>UaProf</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getUaProf() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>MmsCapable</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getMmsCapable() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>AssistedGps</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getAssitedGps() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>LocationTechnology</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getLocationTechnology() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>DeviceBrowser</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getBrowserType() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>WapPushCapable</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getWapPushCapable() %></label>
</em>
</td>
</tr>
</tbody>
</table>
</div>
<%
}
else if (deviceInfo != null && !deviceInfo.isStatus())
{
%>
<div id="tbDeviceCapabError" runat="server" cellspacing="1" class="errorWide" visible="false">
<b>ERROR:</b><br />
<label id="lblErrorMessage"><%=deviceInfo.getErrorResponse() %></label>
</div>
<%
}
%>
<br clear="all" />
<div id="footer">
<div style="float: right; width: 20%; font-size: 9px; text-align: right">
Powered by AT&amp;T Virtual Mobile</div>
<p>
&#169; 2012 AT&amp;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&amp;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></p>
</div>
</div>
<p>
&nbsp;</p>
</body>
</html>
<!--
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 [email protected]
-->
<%
response.addHeader("P3P", "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
%>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ page import="com.att.api.dc.handler.DCHandler" %>
<%@ page import="com.att.api.dc.model.DeviceInfo" %>
<%@ page import="com.att.api.util.DateUtil" %>
<%@ include file="oauth.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>AT&amp;T Sample DC Application - Get Device Capabilities Application</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<link rel="stylesheet" type="text/css" href="style/common.css" />
</head>
<body>
<%
String accessToken = (String) session.getAttribute("accessToken");
DCHandler dchandler = new DCHandler(request,endpoint,accessToken);
DeviceInfo deviceInfo = null;
if (accessToken != null)
{
deviceInfo = dchandler.processRequest();
}
else if (accessToken == null)
{
if (session.getAttribute("errorResponse") != null)
{
deviceInfo = new DeviceInfo();
deviceInfo.setStatus(false);
String errorResponse = (String) session.getAttribute("errorResponse");
deviceInfo.setErrorResponse(errorResponse);
session.removeAttribute("errorResponse");
}
else if(code.length() == 0)
{
//For the first time, we dont have access token, so we redirect to authenticate client id
response.sendRedirect(FQDN + "/oauth/authorize?client_id=" + client_id + "&scope=" + scope + "&redirect_uri=" + redirectURL);
}
}
%>
<div id="container">
<!-- open HEADER -->
<div id="header">
<div>
<div class="hcRight"><%=DateUtil.getServerTime()%></div>
<div class="hcLeft">Server Time:</div>
</div>
<div>
<div class="hcRight">
<script language="JavaScript" type="text/javascript">
var myDate = new Date();
document.write(myDate);
</script>
</div>
<div class="hcLeft">
Client Time:</div>
</div>
<div>
<div class="hcRight">
<script language="JavaScript" type="text/javascript">
document.write("" + navigator.userAgent);
</script>
</div>
<div class="hcLeft">
User Agent:</div>
</div>
<br clear="all" />
</div>
<!-- close HEADER -->
<div>
<div class="content">
<h1>
AT&amp;T Sample DC Application - Get Device Capabilities Application</h1>
<h2>
Feature 1: Get Device Capabilities</h2>
</div>
</div>
<br />
<br />
<div class="extra">
<table>
<tbody>
<div id="extraleft">
<div class="warning">
<strong>Note:</strong><br />
<strong>OnNet Flow:</strong> Request Device Capabilities details from the AT&T network
for the mobile device of an AT&T subscriber who is using an AT&T direct Mobile data
connection to access this application.
<br />
<strong>OffNet Flow:</strong> Where the end-user is not on an AT&T Mobile data connection
or using a Wi-Fi or tethering connection while accessing this application. This
will result in an HTTP 400 error.
</div>
</div>
</tbody>
</table>
</div>
<br clear="all" />
<%
if (deviceInfo != null && deviceInfo.isStatus())
{
%>
<div class="successWide" id="tb_dc_output" visible="false">
<strong>SUCCESS:</strong><br />
Device parameters listed below.
</div>
<br />
<div align="center">
<table width="500" cellpadding="1" cellspacing="1" border="0" runat="server" id="tbDeviceCapabilities"
visible="false">
<thead>
<tr>
<th width="50%" class="label">
Parameter
</th>
<th width="50%" class="label">
Value
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="cell" align="center">
<em>TypeAllocationCode</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getDeviceId() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>Name</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getName() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>Vendor</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getVendor() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>Model</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getModel() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>FirmwareVersion</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getFirmwareVersion() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>UaProf</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getUaProf() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>MmsCapable</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getMmsCapable() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>AssistedGps</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getAssitedGps() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>LocationTechnology</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getLocationTechnology() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>DeviceBrowser</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getBrowserType() %></label>
</em>
</td>
</tr>
<tr>
<td class="cell" align="center">
<em>WapPushCapable</em>
</td>
<td class="cell" align="center">
<em>
<label id="lblTypeAllocationCode"><%=deviceInfo.getWapPushCapable() %></label>
</em>
</td>
</tr>
</tbody>
</table>
</div>
<%
}
else if (deviceInfo != null && !deviceInfo.isStatus())
{
%>
<div id="tbDeviceCapabError" runat="server" cellspacing="1" class="errorWide" visible="false">
<b>ERROR:</b><br />
<label id="lblErrorMessage"><%=deviceInfo.getErrorResponse() %></label>
</div>
<%
}
%>
<br clear="all" />
<div id="footer">
<div style="float: right; width: 20%; font-size: 9px; text-align: right">
Powered by AT&amp;T Virtual Mobile</div>
<p>
&#169; 2012 AT&amp;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&amp;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></p>
</div>
</div>
<p>
&nbsp;</p>
</body>
</html>
<%
//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.Map" %>
<%@ page import="java.net.URLDecoder" %>
<%@ include file="OauthStorage.jsp" %>
<%@ include file="config.jsp"%>
<%
Long currentTime = System.currentTimeMillis();
String accessTokenError = "";
String code = request.getParameter("code");
if(code==null) code="";
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=" + client_id + "&client_secret=" + secret_key + "&grant_type=authorization_code&code=" + code;
method.addRequestHeader("Accept","application/json");
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());
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");
if (expires_in.equals("0"))
{
savedAccessTokenExpiry = currentTime + (Long.parseLong("3155692597470")); //100 years
}
savedRefreshTokenExpiry = currentTime + Long.parseLong("86400000");
method.releaseConnection();
//Applications could reuse the access token, since the token is reserved per device
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();
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();
}
//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=" + client_id + "&client_secret=" + secret_key + "&grant_type=refresh_token&refresh_token=" + refreshToken;
method.addRequestHeader("Content-Type","application/x-www-form-urlencoded");
method.setRequestBody(b);
int statusCode = client.executeMethod(method);
if(statusCode==200)
{
String accessToken = method.getResponseBodyAsString().substring(18,50);
session.setAttribute("accessToken", accessToken);
}
method.releaseConnection();
}
else if (request.getParameter("error") != null || request.getParameter("error_description") != 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);
}
%>
<%
String savedAccessToken = "";
Long savedAccessTokenExpiry = Long.parseLong("1");
String savedRefreshToken = "";
Long savedRefreshTokenExpiry = Long.parseLong("1");
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment