|
|
@ -63,22 +63,11 @@ import static io.entgra.ui.request.interceptor.util.HandlerUtil.execute;
|
|
|
|
public class InvokerHandler extends HttpServlet {
|
|
|
|
public class InvokerHandler extends HttpServlet {
|
|
|
|
private static final Log log = LogFactory.getLog(LoginHandler.class);
|
|
|
|
private static final Log log = LogFactory.getLog(LoginHandler.class);
|
|
|
|
private static final long serialVersionUID = -6508020875358160165L;
|
|
|
|
private static final long serialVersionUID = -6508020875358160165L;
|
|
|
|
// private static final HeaderGroup nonForwardingHeaders = new HeaderGroup();
|
|
|
|
|
|
|
|
private static AuthData authData;
|
|
|
|
private static AuthData authData;
|
|
|
|
private static String apiEndpoint;
|
|
|
|
private static String apiEndpoint;
|
|
|
|
private static String serverUrl;
|
|
|
|
private static String serverUrl;
|
|
|
|
private static String platform;
|
|
|
|
private static String platform;
|
|
|
|
|
|
|
|
|
|
|
|
// static {
|
|
|
|
|
|
|
|
// // Initializing hop-by-hop headers to omit them from forwarding to the backend
|
|
|
|
|
|
|
|
// String[] headers = {HttpHeaders.CONNECTION, HttpHeaders.TRANSFER_ENCODING, HttpHeaders.PROXY_AUTHENTICATE,
|
|
|
|
|
|
|
|
// HttpHeaders.PROXY_AUTHORIZATION, HttpHeaders.UPGRADE, HttpHeaders.TE, HttpHeaders.TRAILER,
|
|
|
|
|
|
|
|
// HandlerConstants.KEEP_ALIVE, HandlerConstants.PUBLIC};
|
|
|
|
|
|
|
|
// for (String header : headers) {
|
|
|
|
|
|
|
|
// nonForwardingHeaders.addHeader(new BasicHeader(header, null));
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
|
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -221,6 +210,7 @@ public class InvokerHandler extends HttpServlet {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
/***
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param req {@link HttpServletRequest}
|
|
|
|
* @param req {@link HttpServletRequest}
|
|
|
@ -232,35 +222,32 @@ public class InvokerHandler extends HttpServlet {
|
|
|
|
throws IOException {
|
|
|
|
throws IOException {
|
|
|
|
serverUrl = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort();
|
|
|
|
serverUrl = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort();
|
|
|
|
apiEndpoint = req.getPathInfo();
|
|
|
|
apiEndpoint = req.getPathInfo();
|
|
|
|
|
|
|
|
String sessionAuthDataKey = req.getHeader(HandlerConstants.X_PLATFORM_HEADER);
|
|
|
|
HttpSession session = req.getSession(false);
|
|
|
|
HttpSession session = req.getSession(false);
|
|
|
|
if (session == null) {
|
|
|
|
if (session == null) {
|
|
|
|
log.error("Unauthorized, You are not logged in. Please log in to the portal");
|
|
|
|
log.error("Unauthorized, You are not logged in. Please log in to the portal");
|
|
|
|
ProxyResponse proxyResponse = new ProxyResponse();
|
|
|
|
handleError(req, resp, HttpStatus.SC_UNAUTHORIZED);
|
|
|
|
proxyResponse.setCode(HttpStatus.SC_UNAUTHORIZED);
|
|
|
|
return false;
|
|
|
|
proxyResponse.setExecutorResponse(
|
|
|
|
}
|
|
|
|
HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(HttpStatus.SC_UNAUTHORIZED));
|
|
|
|
|
|
|
|
HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse);
|
|
|
|
if (StringUtils.isEmpty(sessionAuthDataKey)) {
|
|
|
|
|
|
|
|
log.error("\"X-Platform\" header is empty in the request. Header is required to obtain the auth data from" +
|
|
|
|
|
|
|
|
" session.");
|
|
|
|
|
|
|
|
handleError(req, resp, HttpStatus.SC_BAD_REQUEST);
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
authData = (AuthData) session.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY);
|
|
|
|
|
|
|
|
|
|
|
|
authData = (AuthData) session.getAttribute(sessionAuthDataKey);
|
|
|
|
platform = (String) session.getAttribute(HandlerConstants.PLATFORM);
|
|
|
|
platform = (String) session.getAttribute(HandlerConstants.PLATFORM);
|
|
|
|
if (authData == null) {
|
|
|
|
if (authData == null) {
|
|
|
|
log.error("Unauthorized, Access token not found in the current session");
|
|
|
|
log.error("Unauthorized, Access token not found in the current session");
|
|
|
|
ProxyResponse proxyResponse = new ProxyResponse();
|
|
|
|
handleError(req, resp, HttpStatus.SC_UNAUTHORIZED);
|
|
|
|
proxyResponse.setCode(HttpStatus.SC_UNAUTHORIZED);
|
|
|
|
|
|
|
|
proxyResponse.setExecutorResponse(
|
|
|
|
|
|
|
|
HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(HttpStatus.SC_UNAUTHORIZED));
|
|
|
|
|
|
|
|
HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (apiEndpoint == null || req.getMethod() == null) {
|
|
|
|
if (apiEndpoint == null || req.getMethod() == null) {
|
|
|
|
log.error("Bad Request, Either destination api-endpoint or method is empty");
|
|
|
|
log.error("Bad Request, Either destination api-endpoint or method is empty");
|
|
|
|
ProxyResponse proxyResponse = new ProxyResponse();
|
|
|
|
handleError(req, resp, HttpStatus.SC_BAD_REQUEST);
|
|
|
|
proxyResponse.setCode(HttpStatus.SC_BAD_REQUEST);
|
|
|
|
|
|
|
|
proxyResponse.setExecutorResponse(
|
|
|
|
|
|
|
|
HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(HttpStatus.SC_BAD_REQUEST));
|
|
|
|
|
|
|
|
HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
@ -307,11 +294,7 @@ public class InvokerHandler extends HttpServlet {
|
|
|
|
HttpSession session = req.getSession(false);
|
|
|
|
HttpSession session = req.getSession(false);
|
|
|
|
if (session == null) {
|
|
|
|
if (session == null) {
|
|
|
|
log.error("Couldn't find a session, hence it is required to login and proceed.");
|
|
|
|
log.error("Couldn't find a session, hence it is required to login and proceed.");
|
|
|
|
ProxyResponse proxyResponse = new ProxyResponse();
|
|
|
|
handleError(req, resp, HttpStatus.SC_UNAUTHORIZED);
|
|
|
|
proxyResponse.setCode(HttpStatus.SC_UNAUTHORIZED);
|
|
|
|
|
|
|
|
proxyResponse.setExecutorResponse(
|
|
|
|
|
|
|
|
HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(HttpStatus.SC_UNAUTHORIZED));
|
|
|
|
|
|
|
|
HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -352,11 +335,24 @@ public class InvokerHandler extends HttpServlet {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.error("Error Occurred in token renewal process.");
|
|
|
|
log.error("Error Occurred in token renewal process.");
|
|
|
|
|
|
|
|
handleError(req, resp, HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Handle error requests
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param req {@link HttpServletRequest}
|
|
|
|
|
|
|
|
* @param resp {@link HttpServletResponse}
|
|
|
|
|
|
|
|
* @param errorCode HTTP error status code
|
|
|
|
|
|
|
|
* @throws IOException If error occurred when trying to send the error response.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static void handleError(HttpServletRequest req, HttpServletResponse resp, int errorCode)
|
|
|
|
|
|
|
|
throws IOException {
|
|
|
|
ProxyResponse proxyResponse = new ProxyResponse();
|
|
|
|
ProxyResponse proxyResponse = new ProxyResponse();
|
|
|
|
proxyResponse.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
proxyResponse.setCode(errorCode);
|
|
|
|
proxyResponse.setExecutorResponse(
|
|
|
|
proxyResponse.setExecutorResponse(
|
|
|
|
HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(HttpStatus.SC_INTERNAL_SERVER_ERROR));
|
|
|
|
HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(errorCode));
|
|
|
|
HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse);
|
|
|
|
HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|