Skip to content

Commit

Permalink
SampleApp Enhancement for SMS in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
attdevsupport committed Apr 29, 2015
1 parent 745a374 commit 435d85f
Show file tree
Hide file tree
Showing 53 changed files with 1,970 additions and 1,289 deletions.
63 changes: 63 additions & 0 deletions RESTFul/SMS/Java/app1/.gitignore
@@ -0,0 +1,63 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
*.gem

# Logs and databases #
######################
*.log
*.sql
*.sqlite
*.db
tags

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db
*.swp
*.swo

# Custom ignores #
##################
sync.sh
excludes.txt
Makefile

# Less CSS ignores #
####################
Less-CSS/*.css

# eclipse files
.classpath
.project
.settings
target/

# docs
.yardoc/
doc/
Expand Up @@ -44,6 +44,10 @@ protected void clearSession(final HttpServletRequest request,
}

protected OAuthToken getFileToken() throws RESTException {
return this.getFileToken("scope");
}

protected OAuthToken getFileToken(final String scope) throws RESTException {
try {
final AppConfig cfg = AppConfig.getInstance();
final String path = "WEB-INF/token.properties";
Expand All @@ -56,7 +60,7 @@ protected OAuthToken getFileToken() throws RESTException {
final OAuthService service = new OAuthService(
appConfig.getOauthFQDN(), clientId, clientSecret);

token = service.getToken(cfg.getProperty("scope"));
token = service.getToken(cfg.getProperty(scope));
token.saveToken(tokenFile);
}

Expand All @@ -66,6 +70,12 @@ protected OAuthToken getFileToken() throws RESTException {
}
}

protected void setSessionToken(HttpServletRequest request,
OAuthToken token) {
final HttpSession session = request.getSession();
session.setAttribute("token", token);
}

protected OAuthToken getSessionToken(HttpServletRequest request,
HttpServletResponse response) throws RESTException {

Expand Down
Expand Up @@ -87,6 +87,9 @@ public class OAuthService {
/** Added to fqdn to use for sending OAuth requests. */
public static final String API_URL = "/oauth/v4/token";

/** Added to the fqdn to use for revoking tokens. */
public static final String REVOKE_URL = "/oauth/v4/revoke";

/** Fully qualified domain name. */
private final String fqdn;

Expand Down Expand Up @@ -246,4 +249,40 @@ public OAuthToken refreshToken(String refreshToken) throws RESTException {

return parseResponse(response);
}

/**
* Revokes a token.
*
* @param token token to revoke
* @param hint a hint for the type of token to revoke
*
* @throws RESTException if request was unsuccessful
*/
public void revokeToken(String token, String hint) throws RESTException {
RESTClient client =
new RESTClient(this.fqdn + REVOKE_URL)
.addParameter("client_id", clientId)
.addParameter("client_secret", clientSecret)
.addParameter("token", token)
.addParameter("token_type_hint", hint);
APIResponse response = sendRequest(client);
if (response.getStatusCode() != 200) {
throw new RESTException(response.getResponseBody());
}
}

/**
* Revokes a token, where the token hint set to "access_token"
*
* @param token token to revoke
* @param hint a hint for the type of token to revoke
*
* @throws RESTException if request was unsuccessful
* @see OAuthToken#revokeToken(String, String)
*/
public void revokeToken(String token) throws RESTException {
final String hint = "access_token";
this.revokeToken(token, hint);
}

}
Expand Up @@ -74,8 +74,11 @@ public class OAuthToken {
/** Access token. */
private final String accessToken;

/** Unix timestamp, in seconds, to denote access token expiry. */
private final long accessTokenExpiry;
/** Seconds at which access token will expire relative to creation. **/
private final long expiresIn;

/** Unix timestamp, in seconds, to denote access token creation date. */
private final long creationTime;

/** Refresh token. */
private final String refreshToken;
Expand Down Expand Up @@ -111,15 +114,10 @@ private static long xtimestamp() {
*/
public OAuthToken(String accessToken, long expiresIn, String refreshToken,
long creationTime) {

if (expiresIn == OAuthToken.NO_EXPIRATION) {
this.accessTokenExpiry = OAuthToken.NO_EXPIRATION;
} else {
this.accessTokenExpiry = expiresIn + creationTime;
}

this.accessToken = accessToken;
this.expiresIn = expiresIn;
this.refreshToken = refreshToken;
this.creationTime = creationTime;
}

/**
Expand All @@ -129,7 +127,6 @@ public OAuthToken(String accessToken, long expiresIn, String refreshToken,
* @param accessToken access token
* @param expiresIn time in seconds token expires from current time
* @param refreshToken refresh token
* @see #OAuthToken(String, long, String, long)
*/
public OAuthToken(String accessToken, long expiresIn, String refreshToken) {
this(accessToken, expiresIn, refreshToken, xtimestamp());
Expand All @@ -142,8 +139,8 @@ public OAuthToken(String accessToken, long expiresIn, String refreshToken) {
* otherwise
*/
public boolean isAccessTokenExpired() {
return accessTokenExpiry != NO_EXPIRATION
&& xtimestamp() >= accessTokenExpiry;
return this.getAccessTokenExpiry() != NO_EXPIRATION
&& xtimestamp() >= this.getAccessTokenExpiry();
}

/**
Expand All @@ -155,6 +152,35 @@ public String getAccessToken() {
return accessToken;
}

/**
* Get the Unix timestamp, in seconds, that the token will expire
*
* @return the accessTokenExpiry
*/
public long getAccessTokenExpiry() {
return this.expiresIn == OAuthToken.NO_EXPIRATION
? OAuthToken.NO_EXPIRATION : this.expiresIn + this.creationTime;
}

/**
* Get the number of seconds that the token will expire relative to
* creationTime.
*
* @return the expiresIn
*/
public long getExpiresIn() {
return expiresIn;
}

/**
* Get the Unix timestamp, in seconds, that the token was created.
*
* @return the creationTime
*/
public long getCreationTime() {
return creationTime;
}

/**
* Gets refresh token.
*
Expand Down Expand Up @@ -187,7 +213,8 @@ public void saveToken(String fpath) throws IOException {
fLock = fOutputStream.getChannel().lock();
Properties props = new Properties();
props.setProperty("accessToken", accessToken);
props.setProperty("accessTokenExpiry", String.valueOf(accessTokenExpiry));
props.setProperty("creationTime", String.valueOf(creationTime));
props.setProperty("expiresIn", String.valueOf(expiresIn));
props.setProperty("refreshToken", refreshToken);
props.store(fOutputStream, "Token Information");
} catch (IOException e) {
Expand All @@ -204,7 +231,8 @@ public void saveToken(String fpath) throws IOException {
* manner.
*
* <p>
* If <code>fpath</code> does not exist, null is returned.
* If <code>fpath</code> does not exist or some required values are missing
* from the saved file, null is returned.
* </p>
*
* <p>
Expand Down Expand Up @@ -238,14 +266,26 @@ public static OAuthToken loadToken(String fpath) throws IOException {
fLock = fInputStream.getChannel().lock(0L, Long.MAX_VALUE, true);
Properties props = new Properties();
props.load(fInputStream);
if (!props.containsKey("creationTime")
|| !props.containsKey("expiresIn")) {
return null;
}

String accessToken = props.getProperty("accessToken");
if (accessToken == null || accessToken.equals("")) {
return null;
}
String sExpiry = props.getProperty("accessTokenExpiry", "0");
long expiry = new Long(sExpiry).longValue();

String refreshToken = props.getProperty("refreshToken");
return new OAuthToken(accessToken, expiry, refreshToken);

String sExpiresIn = props.getProperty("expiresIn");
long expiresIn = new Long(sExpiresIn).longValue();

String sCreationTime = props.getProperty("creationTime");
long creationTime = new Long(sCreationTime).longValue();

return new OAuthToken(accessToken, expiresIn, refreshToken,
creationTime);
} catch (IOException e) {
throw e; // pass along exception
} finally {
Expand Down
@@ -1,4 +1,4 @@
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker */
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 */

/*
* Copyright 2014 AT&T
Expand Down Expand Up @@ -27,9 +27,9 @@
/**
* Immutable class that holds API response information.
*
* @author <a href="mailto:pk9069@att.com">Pavel Kazakov</a>
* @version 3.0
* @since 2.2
* @author pk9069
* @version 1.0
* @since 1.0
*/
public class APIResponse {

Expand Down Expand Up @@ -137,7 +137,11 @@ public HttpHeader[] getAllHeaders() {

/**
* Gets the the value of the specified http header name or <tt>null</tt> if
* none is found.
* none is found.
*
* <p>
* Note: the search function used is case insensitive.
* </p>
*
* @param name header name
* @return http header value
Expand All @@ -149,8 +153,9 @@ public String getHeader(String name) {
// small array than an algorithm that would require building a data
// structure.
HttpHeader[] headers = getAllHeaders();
String lname = name.toLowerCase();
for (HttpHeader header : headers) {
if (header.getName().equals(name)) {
if (header.getName().toLowerCase().equals(lname)) {
return header.getValue();
}
}
Expand All @@ -165,7 +170,7 @@ public String getHeader(String name) {
* @return response
* @throws RESTException if unable to parse http response
* @see #valueOf(HttpResponse)
* @since 3.0
* @since 1.0
*/
public static APIResponse fromHttpResponse(HttpResponse httpResponse)
throws RESTException {
Expand All @@ -181,14 +186,17 @@ public static APIResponse fromHttpResponse(HttpResponse httpResponse)
* @return response
* @throws RESTException if unable to parse http response
* @see org.apache.http.HttpResponse
* @since 3.0
* @since 1.0
*/
public static APIResponse valueOf(HttpResponse httpResponse)
throws RESTException {

try {
int statusCode = httpResponse.getStatusLine().getStatusCode();
String rb = EntityUtils.toString(httpResponse.getEntity());
String rb = "";
if (httpResponse.getEntity() != null) {
rb = EntityUtils.toString(httpResponse.getEntity());
}
HttpHeader[] headers = APIResponse.buildHeaders(httpResponse);
return new APIResponse(statusCode, rb, headers);
} catch (IOException ioe) {
Expand All @@ -206,7 +214,7 @@ public static APIResponse valueOf(HttpResponse httpResponse)
*
* @param headers HTTP headers to copy
* @return copy of http headers
* @since 3.0
* @since 1.0
*/
public static HttpHeader[] copyHeaders(final HttpHeader[] headers) {
final HttpHeader[] headersCopy = new HttpHeader[headers.length];
Expand Down
@@ -1,4 +1,4 @@
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker */
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 */

/*
* Copyright 2014 AT&T
Expand All @@ -21,9 +21,9 @@
/**
* Immutable class that holds an HTTP header.
*
* @author <a href="mailto:pk9069@att.com">Pavel Kazakov</a>
* @version 3.0
* @since 2.2
* @author pk9069
* @version 1.0
* @since 1.0
*/
public final class HttpHeader {
/** Http header name. */
Expand Down

0 comments on commit 435d85f

Please sign in to comment.