|
|
@ -62,170 +62,172 @@ public class InvokerHandler extends HttpServlet {
|
|
|
|
private static String serverUrl;
|
|
|
|
private static String serverUrl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
|
|
|
|
// @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
|
|
|
|
try {
|
|
|
|
// try {
|
|
|
|
if (!validateRequest(req, resp)) {
|
|
|
|
// if (!validateRequest(req, resp)) {
|
|
|
|
return;
|
|
|
|
// return;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
HttpRequestBase executor = constructExecutor(req);
|
|
|
|
// HttpRequestBase executor = constructExecutor(req);
|
|
|
|
if (executor == null) {
|
|
|
|
// if (executor == null) {
|
|
|
|
resp.sendError(HTTP_BAD_REQUEST, "Bad Request, method: " + method + " is not supported");
|
|
|
|
// resp.sendError(HTTP_BAD_REQUEST, "Bad Request, method: " + method + " is not supported");
|
|
|
|
return;
|
|
|
|
// return;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
String accessToken = authData.getAccessToken();
|
|
|
|
// String accessToken = authData.getAccessToken();
|
|
|
|
executor.setHeader("Authorization", "Bearer " + accessToken);
|
|
|
|
// executor.setHeader("Authorization", "Bearer " + accessToken);
|
|
|
|
|
|
|
|
//
|
|
|
|
HttpResponse response = execute(executor);
|
|
|
|
// String result = execute(executor, HttpStatus.SC_OK);
|
|
|
|
if (response == null) {
|
|
|
|
//
|
|
|
|
resp.sendError(HTTP_INTERNAL_ERROR, "Empty response retried for the API call.");
|
|
|
|
//// unauthorized
|
|
|
|
return;
|
|
|
|
//// if (response == null) {
|
|
|
|
}
|
|
|
|
//// resp.sendError(HTTP_INTERNAL_ERROR, "Empty response retried for the API call.");
|
|
|
|
|
|
|
|
//// return;
|
|
|
|
int responseCode = response.getStatusLine().getStatusCode();
|
|
|
|
//// }
|
|
|
|
String result = retrieveResponseString(response);
|
|
|
|
//
|
|
|
|
|
|
|
|
//// int responseCode = response.getStatusLine().getStatusCode();
|
|
|
|
if (responseCode == HttpStatus.SC_UNAUTHORIZED && (result.contains("Access token expired") || result
|
|
|
|
//// String result = retrieveResponseString(response);
|
|
|
|
.contains("Invalid input. Access token validation failed"))) {
|
|
|
|
//
|
|
|
|
if (!refreshToken(req, resp)) {
|
|
|
|
// if (responseCode == HttpStatus.SC_UNAUTHORIZED && (result.contains("Access token expired") || result
|
|
|
|
return;
|
|
|
|
// .contains("Invalid input. Access token validation failed"))) {
|
|
|
|
}
|
|
|
|
// if (!refreshToken(req, resp)) {
|
|
|
|
response = execute(executor);
|
|
|
|
// return;
|
|
|
|
if (response == null) {
|
|
|
|
// }
|
|
|
|
resp.sendError(HTTP_INTERNAL_ERROR, "Empty response retried for the token renewal API call.");
|
|
|
|
// response = execute(executor);
|
|
|
|
return;
|
|
|
|
// if (response == null) {
|
|
|
|
}
|
|
|
|
// resp.sendError(HTTP_INTERNAL_ERROR, "Empty response retried for the token renewal API call.");
|
|
|
|
responseCode = response.getStatusLine().getStatusCode();
|
|
|
|
// return;
|
|
|
|
result = retrieveResponseString(response);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// responseCode = response.getStatusLine().getStatusCode();
|
|
|
|
if (responseCode != HttpStatus.SC_OK && responseCode != HttpStatus.SC_CREATED) {
|
|
|
|
// result = retrieveResponseString(response);
|
|
|
|
resp.sendError(responseCode, "Error response retrieved for the API call.");
|
|
|
|
// }
|
|
|
|
return;
|
|
|
|
// if (responseCode != HttpStatus.SC_OK && responseCode != HttpStatus.SC_CREATED) {
|
|
|
|
}
|
|
|
|
// resp.sendError(responseCode, "Error response retrieved for the API call.");
|
|
|
|
try (PrintWriter writer = resp.getWriter()) {
|
|
|
|
// return;
|
|
|
|
writer.write(result);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// try (PrintWriter writer = resp.getWriter()) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
// writer.write(result);
|
|
|
|
log.error("Error occured when processing invoke call.", e);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// } catch (IOException e) {
|
|
|
|
}
|
|
|
|
// log.error("Error occured when processing invoke call.", e);
|
|
|
|
|
|
|
|
// }
|
|
|
|
/***
|
|
|
|
// }
|
|
|
|
*
|
|
|
|
//
|
|
|
|
* @param req {@link HttpServletRequest}
|
|
|
|
// /***
|
|
|
|
* @return {@link HttpRequestBase} if method equals to either GET, POST, PUT or DELETE otherwise returns NULL.
|
|
|
|
// *
|
|
|
|
*/
|
|
|
|
// * @param req {@link HttpServletRequest}
|
|
|
|
private HttpRequestBase constructExecutor(HttpServletRequest req) {
|
|
|
|
// * @return {@link HttpRequestBase} if method equals to either GET, POST, PUT or DELETE otherwise returns NULL.
|
|
|
|
String payload = req.getParameter("payload");
|
|
|
|
// */
|
|
|
|
String contentType = req.getParameter("content-type");
|
|
|
|
// private HttpRequestBase constructExecutor(HttpServletRequest req) {
|
|
|
|
if (contentType == null || contentType.isEmpty()) {
|
|
|
|
// String payload = req.getParameter("payload");
|
|
|
|
contentType = ContentType.APPLICATION_JSON.toString();
|
|
|
|
// String contentType = req.getParameter("content-type");
|
|
|
|
}
|
|
|
|
// if (contentType == null || contentType.isEmpty()) {
|
|
|
|
|
|
|
|
// contentType = ContentType.APPLICATION_JSON.toString();
|
|
|
|
HttpRequestBase executor;
|
|
|
|
// }
|
|
|
|
if (HttpGet.METHOD_NAME.equalsIgnoreCase(method)) {
|
|
|
|
//
|
|
|
|
executor = new HttpGet(serverUrl + HandlerConstants.API_COMMON_CONTEXT + apiEndpoint);
|
|
|
|
// HttpRequestBase executor;
|
|
|
|
} else if (HttpPost.METHOD_NAME.equalsIgnoreCase(method)) {
|
|
|
|
// if (HttpGet.METHOD_NAME.equalsIgnoreCase(method)) {
|
|
|
|
executor = new HttpPost(serverUrl + HandlerConstants.API_COMMON_CONTEXT + apiEndpoint);
|
|
|
|
// executor = new HttpGet(serverUrl + HandlerConstants.API_COMMON_CONTEXT + apiEndpoint);
|
|
|
|
StringEntity payloadEntity = new StringEntity(payload, ContentType.create(contentType));
|
|
|
|
// } else if (HttpPost.METHOD_NAME.equalsIgnoreCase(method)) {
|
|
|
|
((HttpPost) executor).setEntity(payloadEntity);
|
|
|
|
// executor = new HttpPost(serverUrl + HandlerConstants.API_COMMON_CONTEXT + apiEndpoint);
|
|
|
|
} else if (HttpPut.METHOD_NAME.equalsIgnoreCase(method)) {
|
|
|
|
// StringEntity payloadEntity = new StringEntity(payload, ContentType.create(contentType));
|
|
|
|
executor = new HttpPut(serverUrl + HandlerConstants.API_COMMON_CONTEXT + apiEndpoint);
|
|
|
|
// ((HttpPost) executor).setEntity(payloadEntity);
|
|
|
|
StringEntity payloadEntity = new StringEntity(payload, ContentType.create(contentType));
|
|
|
|
// } else if (HttpPut.METHOD_NAME.equalsIgnoreCase(method)) {
|
|
|
|
((HttpPut) executor).setEntity(payloadEntity);
|
|
|
|
// executor = new HttpPut(serverUrl + HandlerConstants.API_COMMON_CONTEXT + apiEndpoint);
|
|
|
|
} else if (HttpDelete.METHOD_NAME.equalsIgnoreCase(method)) {
|
|
|
|
// StringEntity payloadEntity = new StringEntity(payload, ContentType.create(contentType));
|
|
|
|
executor = new HttpDelete(serverUrl + HandlerConstants.API_COMMON_CONTEXT + apiEndpoint);
|
|
|
|
// ((HttpPut) executor).setEntity(payloadEntity);
|
|
|
|
} else {
|
|
|
|
// } else if (HttpDelete.METHOD_NAME.equalsIgnoreCase(method)) {
|
|
|
|
return null;
|
|
|
|
// executor = new HttpDelete(serverUrl + HandlerConstants.API_COMMON_CONTEXT + apiEndpoint);
|
|
|
|
}
|
|
|
|
// } else {
|
|
|
|
return executor;
|
|
|
|
// return null;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return executor;
|
|
|
|
/***
|
|
|
|
// }
|
|
|
|
*
|
|
|
|
//
|
|
|
|
* @param req {@link HttpServletRequest}
|
|
|
|
// /***
|
|
|
|
* @param resp {@link HttpServletResponse}
|
|
|
|
// *
|
|
|
|
* @return If request is a valid one, returns TRUE, otherwise return FALSE
|
|
|
|
// * @param req {@link HttpServletRequest}
|
|
|
|
* @throws IOException If and error occurs while witting error response to client side
|
|
|
|
// * @param resp {@link HttpServletResponse}
|
|
|
|
*/
|
|
|
|
// * @return If request is a valid one, returns TRUE, otherwise return FALSE
|
|
|
|
private static boolean validateRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
|
// * @throws IOException If and error occurs while witting error response to client side
|
|
|
|
HttpSession session = req.getSession(false);
|
|
|
|
// */
|
|
|
|
if (session == null) {
|
|
|
|
// private static boolean validateRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
|
resp.sendError(HTTP_UNAUTHORIZED, "Unauthorized, You are not logged in. Please log in to the portal");
|
|
|
|
// HttpSession session = req.getSession(false);
|
|
|
|
return false;
|
|
|
|
// if (session == null) {
|
|
|
|
}
|
|
|
|
// resp.sendError(HTTP_UNAUTHORIZED, "Unauthorized, You are not logged in. Please log in to the portal");
|
|
|
|
authData = (AuthData) session.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY);
|
|
|
|
// return false;
|
|
|
|
if (authData == null) {
|
|
|
|
// }
|
|
|
|
resp.sendError(HTTP_UNAUTHORIZED, "Unauthorized, Access token couldn't found in the current session");
|
|
|
|
// authData = (AuthData) session.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY);
|
|
|
|
return false;
|
|
|
|
// if (authData == null) {
|
|
|
|
}
|
|
|
|
// resp.sendError(HTTP_UNAUTHORIZED, "Unauthorized, Access token couldn't found in the current session");
|
|
|
|
|
|
|
|
// return false;
|
|
|
|
apiEndpoint = req.getParameter("api-endpoint");
|
|
|
|
// }
|
|
|
|
method = req.getParameter("method");
|
|
|
|
//
|
|
|
|
|
|
|
|
// apiEndpoint = req.getParameter("api-endpoint");
|
|
|
|
serverUrl = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort();
|
|
|
|
// method = req.getParameter("method");
|
|
|
|
if (apiEndpoint == null || method == null) {
|
|
|
|
//
|
|
|
|
resp.sendError(HTTP_BAD_REQUEST, "Bad Request, Either api-endpoint or method is empty");
|
|
|
|
// serverUrl = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort();
|
|
|
|
return false;
|
|
|
|
// if (apiEndpoint == null || method == null) {
|
|
|
|
}
|
|
|
|
// resp.sendError(HTTP_BAD_REQUEST, "Bad Request, Either api-endpoint or method is empty");
|
|
|
|
return true;
|
|
|
|
// return false;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return true;
|
|
|
|
/***
|
|
|
|
// }
|
|
|
|
*
|
|
|
|
//
|
|
|
|
* @param req {@link HttpServletRequest}
|
|
|
|
// /***
|
|
|
|
* @param resp {@link HttpServletResponse}
|
|
|
|
// *
|
|
|
|
* @return If successfully renew tokens, returns TRUE otherwise return FALSE
|
|
|
|
// * @param req {@link HttpServletRequest}
|
|
|
|
* @throws IOException If and error occurs while witting error response to client side or invoke token renewal API
|
|
|
|
// * @param resp {@link HttpServletResponse}
|
|
|
|
*/
|
|
|
|
// * @return If successfully renew tokens, returns TRUE otherwise return FALSE
|
|
|
|
private static boolean refreshToken(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
|
// * @throws IOException If and error occurs while witting error response to client side or invoke token renewal API
|
|
|
|
log.debug("refreshing the token");
|
|
|
|
// */
|
|
|
|
HttpPost tokenEndpoint = new HttpPost(
|
|
|
|
// private static boolean refreshToken(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
|
serverUrl + HandlerConstants.API_COMMON_CONTEXT + HandlerConstants.TOKEN_ENDPOINT);
|
|
|
|
// log.debug("refreshing the token");
|
|
|
|
HttpSession session = req.getSession(false);
|
|
|
|
// HttpPost tokenEndpoint = new HttpPost(
|
|
|
|
if (session == null) {
|
|
|
|
// serverUrl + HandlerConstants.API_COMMON_CONTEXT + HandlerConstants.TOKEN_ENDPOINT);
|
|
|
|
resp.sendError(HTTP_UNAUTHORIZED, "Session is expired. Please log in to the server.");
|
|
|
|
// HttpSession session = req.getSession(false);
|
|
|
|
return false;
|
|
|
|
// if (session == null) {
|
|
|
|
}
|
|
|
|
// resp.sendError(HTTP_UNAUTHORIZED, "Session is expired. Please log in to the server.");
|
|
|
|
|
|
|
|
// return false;
|
|
|
|
StringEntity tokenEndpointPayload = new StringEntity(
|
|
|
|
// }
|
|
|
|
"grant_type=refresh_token&refresh_token=" + authData.getRefreshToken() + "&scope=PRODUCTION",
|
|
|
|
//
|
|
|
|
ContentType.APPLICATION_FORM_URLENCODED);
|
|
|
|
// StringEntity tokenEndpointPayload = new StringEntity(
|
|
|
|
|
|
|
|
// "grant_type=refresh_token&refresh_token=" + authData.getRefreshToken() + "&scope=PRODUCTION",
|
|
|
|
tokenEndpoint.setEntity(tokenEndpointPayload);
|
|
|
|
// ContentType.APPLICATION_FORM_URLENCODED);
|
|
|
|
String encodedClientApp = authData.getEncodedClientApp();
|
|
|
|
//
|
|
|
|
tokenEndpoint.setHeader("Authorization", "Basic " + encodedClientApp);
|
|
|
|
// tokenEndpoint.setEntity(tokenEndpointPayload);
|
|
|
|
tokenEndpoint.setHeader("Content-Type", ContentType.APPLICATION_FORM_URLENCODED.toString());
|
|
|
|
// String encodedClientApp = authData.getEncodedClientApp();
|
|
|
|
|
|
|
|
// tokenEndpoint.setHeader("Authorization", "Basic " + encodedClientApp);
|
|
|
|
HttpResponse response = execute(tokenEndpoint);
|
|
|
|
// tokenEndpoint.setHeader("Content-Type", ContentType.APPLICATION_FORM_URLENCODED.toString());
|
|
|
|
if (response == null) {
|
|
|
|
//
|
|
|
|
resp.sendError(HTTP_INTERNAL_ERROR,
|
|
|
|
// HttpResponse response = execute(tokenEndpoint);
|
|
|
|
"Internal Server Error, response of the token refresh API call is null.");
|
|
|
|
// if (response == null) {
|
|
|
|
return false;
|
|
|
|
// resp.sendError(HTTP_INTERNAL_ERROR,
|
|
|
|
} else if ((response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)) {
|
|
|
|
// "Internal Server Error, response of the token refresh API call is null.");
|
|
|
|
resp.sendError(response.getStatusLine().getStatusCode(),
|
|
|
|
// return false;
|
|
|
|
"Error occured while getting new access token by using refresh token.");
|
|
|
|
// } else if ((response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)) {
|
|
|
|
return false;
|
|
|
|
// resp.sendError(response.getStatusLine().getStatusCode(),
|
|
|
|
}
|
|
|
|
// "Error occured while getting new access token by using refresh token.");
|
|
|
|
String tokenResult = retrieveResponseString(response);
|
|
|
|
// return false;
|
|
|
|
JsonParser jsonParser = new JsonParser();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// String tokenResult = retrieveResponseString(response);
|
|
|
|
JsonElement jTokenResult = jsonParser.parse(tokenResult);
|
|
|
|
// JsonParser jsonParser = new JsonParser();
|
|
|
|
if (jTokenResult.isJsonObject()) {
|
|
|
|
//
|
|
|
|
JsonObject jTokenResultAsJsonObject = jTokenResult.getAsJsonObject();
|
|
|
|
// JsonElement jTokenResult = jsonParser.parse(tokenResult);
|
|
|
|
AuthData newAuthData = new AuthData();
|
|
|
|
// if (jTokenResult.isJsonObject()) {
|
|
|
|
|
|
|
|
// JsonObject jTokenResultAsJsonObject = jTokenResult.getAsJsonObject();
|
|
|
|
newAuthData.setAccessToken(jTokenResultAsJsonObject.get("access_token").getAsString());
|
|
|
|
// AuthData newAuthData = new AuthData();
|
|
|
|
newAuthData.setRefreshToken(jTokenResultAsJsonObject.get("refresh_token").getAsString());
|
|
|
|
//
|
|
|
|
newAuthData.setScope(jTokenResultAsJsonObject.get("scope").getAsString());
|
|
|
|
// newAuthData.setAccessToken(jTokenResultAsJsonObject.get("access_token").getAsString());
|
|
|
|
newAuthData.setClientId(authData.getClientId());
|
|
|
|
// newAuthData.setRefreshToken(jTokenResultAsJsonObject.get("refresh_token").getAsString());
|
|
|
|
newAuthData.setClientSecret(authData.getClientSecret());
|
|
|
|
// newAuthData.setScope(jTokenResultAsJsonObject.get("scope").getAsString());
|
|
|
|
newAuthData.setEncodedClientApp(authData.getEncodedClientApp());
|
|
|
|
// newAuthData.setClientId(authData.getClientId());
|
|
|
|
newAuthData.setUsername(authData.getUsername());
|
|
|
|
// newAuthData.setClientSecret(authData.getClientSecret());
|
|
|
|
authData = newAuthData;
|
|
|
|
// newAuthData.setEncodedClientApp(authData.getEncodedClientApp());
|
|
|
|
session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, newAuthData);
|
|
|
|
// newAuthData.setUsername(authData.getUsername());
|
|
|
|
return true;
|
|
|
|
// authData = newAuthData;
|
|
|
|
}
|
|
|
|
// session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, newAuthData);
|
|
|
|
return false;
|
|
|
|
// return true;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|