|
|
@ -45,10 +45,10 @@ import javax.servlet.http.HttpSession;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
|
|
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
|
|
|
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
|
|
|
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
|
|
|
|
|
|
|
|
import static org.wso2.carbon.device.application.mgt.handler.util.HandlerUtil.execute;
|
|
|
|
import static org.wso2.carbon.device.application.mgt.handler.util.HandlerUtil.execute;
|
|
|
|
|
|
|
|
|
|
|
|
@MultipartConfig @WebServlet("/invoke")
|
|
|
|
@MultipartConfig
|
|
|
|
|
|
|
|
@WebServlet("/invoke")
|
|
|
|
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;
|
|
|
@ -58,7 +58,8 @@ public class InvokerHandler extends HttpServlet {
|
|
|
|
private static String serverUrl;
|
|
|
|
private static String serverUrl;
|
|
|
|
private static String platform;
|
|
|
|
private static String platform;
|
|
|
|
|
|
|
|
|
|
|
|
@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;
|
|
|
@ -69,8 +70,8 @@ public class InvokerHandler extends HttpServlet {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
executor.setHeader(HandlerConstants.AUTHORIZATION_HEADER_KEY, "Bearer " + authData.getAccessToken());
|
|
|
|
executor.setHeader(HandlerConstants.AUTHORIZATION_HEADER_KEY, "Bearer " + authData.getAccessToken());
|
|
|
|
|
|
|
|
|
|
|
|
ProxyResponse proxyResponse = execute(executor);
|
|
|
|
ProxyResponse proxyResponse = execute(executor);
|
|
|
|
|
|
|
|
|
|
|
|
if (HandlerConstants.TOKEN_IS_EXPIRED.equals(proxyResponse.getExecutorResponse())) {
|
|
|
|
if (HandlerConstants.TOKEN_IS_EXPIRED.equals(proxyResponse.getExecutorResponse())) {
|
|
|
|
if (!refreshToken(req, resp)) {
|
|
|
|
if (!refreshToken(req, resp)) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
@ -83,15 +84,12 @@ public class InvokerHandler extends HttpServlet {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
|
|
|
|
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
|
|
|
|
log.error("Error occurred while invoking the API endpoint.");
|
|
|
|
log.error("Error occurred while invoking the API endpoint.");
|
|
|
|
HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse);
|
|
|
|
HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HandlerUtil.handleSuccess(req, resp, serverUrl, platform, proxyResponse);
|
|
|
|
HandlerUtil.handleSuccess(req, resp, serverUrl, platform, proxyResponse);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
log.error("Error occured when processing invoke call.", e);
|
|
|
|
log.error("Error occured when processing invoke call.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -136,23 +134,38 @@ public class InvokerHandler extends HttpServlet {
|
|
|
|
* @throws IOException If and error occurs while witting error response to client side
|
|
|
|
* @throws IOException If and error occurs while witting error response to client side
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private static boolean validateRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
|
private static boolean validateRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
|
|
|
|
|
serverUrl = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort();
|
|
|
|
|
|
|
|
apiEndpoint = req.getParameter("api-endpoint");
|
|
|
|
|
|
|
|
method = req.getParameter("method");
|
|
|
|
HttpSession session = req.getSession(false);
|
|
|
|
HttpSession session = req.getSession(false);
|
|
|
|
if (session == null) {
|
|
|
|
if (session == null) {
|
|
|
|
resp.sendError(HTTP_UNAUTHORIZED, "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();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
authData = (AuthData) session.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY);
|
|
|
|
authData = (AuthData) session.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY);
|
|
|
|
platform = (String) session.getAttribute(HandlerConstants.PLATFORM);
|
|
|
|
platform = (String) session.getAttribute(HandlerConstants.PLATFORM);
|
|
|
|
if (authData == null) {
|
|
|
|
if (authData == null) {
|
|
|
|
resp.sendError(HTTP_UNAUTHORIZED, "Unauthorized, Access token couldn't found in the current session");
|
|
|
|
log.error("Unauthorized, Access token couldn't found in the current session");
|
|
|
|
|
|
|
|
ProxyResponse proxyResponse = new ProxyResponse();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
apiEndpoint = req.getParameter("api-endpoint");
|
|
|
|
|
|
|
|
method = req.getParameter("method");
|
|
|
|
|
|
|
|
serverUrl = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort();
|
|
|
|
|
|
|
|
if (apiEndpoint == null || method == null) {
|
|
|
|
if (apiEndpoint == null || method == null) {
|
|
|
|
resp.sendError(HTTP_BAD_REQUEST, "Bad Request, Either api-endpoint or method is empty");
|
|
|
|
log.error("Bad Request, Either api-endpoint or method is empty");
|
|
|
|
|
|
|
|
ProxyResponse proxyResponse = new ProxyResponse();
|
|
|
|
|
|
|
|
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;
|
|
|
|