-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAPIController.java
120 lines (99 loc) · 4.35 KB
/
APIController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.att.api.controller;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.att.api.config.AppConfig;
import com.att.api.oauth.OAuthService;
import com.att.api.oauth.OAuthToken;
import com.att.api.rest.RESTConfig;
import com.att.api.rest.RESTException;
public abstract class APIController extends HttpServlet {
private static final long serialVersionUID = -343932180266512049L;
protected AppConfig appConfig;
protected void copyToSession(final HttpServletRequest request,
final String[] names) {
final HttpSession session = request.getSession();
for (final String name : names) {
final String[] values = (String[]) request.getParameterValues(name);
if (values != null && !name.matches("^.*\\Q[\\E.*\\Q]\\E$")) {
if (values.length == 1)
session.setAttribute(name, values[0]);
else
session.setAttribute(name, values);
}
}
}
protected void clearSession(final HttpServletRequest request,
final String[] names) {
final HttpSession session = request.getSession();
for (final String name : names) {
session.removeAttribute(name);
}
}
protected OAuthToken getFileToken() throws RESTException {
try {
final AppConfig cfg = AppConfig.getInstance();
final String path = "WEB-INF/token.properties";
final String tokenFile = getServletContext().getRealPath(path);
OAuthToken token = OAuthToken.loadToken(tokenFile);
if (token == null || token.isAccessTokenExpired()) {
final String clientId = cfg.getClientId();
final String clientSecret = cfg.getClientSecret();
final OAuthService service = new OAuthService(
appConfig.getOauthFQDN(), clientId, clientSecret,
Long.parseLong(appConfig.getProperty("tokenExpireSeconds")));
token = service.getToken(cfg.getProperty("scope"));
token.saveToken(tokenFile);
}
return token;
} catch (IOException ioe) {
throw new RESTException(ioe);
}
}
protected OAuthToken getSessionToken(HttpServletRequest request,
HttpServletResponse response) throws RESTException {
final HttpSession session = request.getSession();
OAuthToken token = (OAuthToken) session.getAttribute("token");
if (token != null && !token.isAccessTokenExpired()) {
return token;
}
final String FQDN = appConfig.getOauthFQDN();
final String clientId = appConfig.getClientId();
final String clientSecret = appConfig.getClientSecret();
final String code = (String) request.getParameter("code");
if (code != null) {
final OAuthService service = new OAuthService(
appConfig.getOauthFQDN(), clientId, clientSecret,
Long.parseLong(appConfig.getProperty("tokenExpireSeconds")));
token = service.getTokenUsingCode(code);
session.setAttribute("token", token);
return token;
}
final String scope = appConfig.getProperty("scope");
final String redirectUri = appConfig.getProperty("redirectUri");
final String redirect = FQDN + "/oauth/v4/authorize?client_id=" +
clientId + "&scope=" + scope + "&redirect_uri=" + redirectUri;
try {
response.sendRedirect(redirect);
} catch (IOException e) {
throw new RESTException(e);
}
return null; // indicate redirection is needed
}
@Override
public void init() {
try {
this.appConfig = AppConfig.getInstance();
boolean shouldTrust = appConfig.getTrustAllCerts();
String proxyHost = appConfig.getProxyHost();
int proxyPort = appConfig.getProxyPort();
RESTConfig.setDefaultTrustAllCerts(shouldTrust);
RESTConfig.setDefaultProxy(proxyHost, proxyPort);
} catch (IOException e) {
// print stack trace instead of handling
e.printStackTrace();
}
}
}