|
|
@ -21,6 +21,7 @@ package io.entgra.device.mgt.core.ui.request.interceptor.util;
|
|
|
|
import com.fasterxml.jackson.core.JsonFactory;
|
|
|
|
import com.fasterxml.jackson.core.JsonFactory;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
@ -50,35 +51,25 @@ import org.apache.hc.core5.http.io.entity.BufferedHttpEntity;
|
|
|
|
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
|
|
|
|
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
|
|
|
|
import org.apache.hc.core5.http.io.entity.StringEntity;
|
|
|
|
import org.apache.hc.core5.http.io.entity.StringEntity;
|
|
|
|
import org.apache.hc.core5.ssl.SSLContextBuilder;
|
|
|
|
import org.apache.hc.core5.ssl.SSLContextBuilder;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
|
|
|
|
import org.apache.http.Consts;
|
|
|
|
import org.apache.http.Consts;
|
|
|
|
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
|
|
|
|
|
|
|
import org.apache.http.client.methods.HttpRequestBase;
|
|
|
|
|
|
|
|
import org.apache.http.cookie.SM;
|
|
|
|
import org.apache.http.cookie.SM;
|
|
|
|
import org.apache.xml.serialize.OutputFormat;
|
|
|
|
|
|
|
|
import org.apache.xml.serialize.XMLSerializer;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import org.w3c.dom.Document;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.ui.request.interceptor.beans.ProxyResponse;
|
|
|
|
import io.entgra.device.mgt.core.ui.request.interceptor.beans.ProxyResponse;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.spi.OTPManagementService;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.spi.OTPManagementService;
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import java.io.IOException;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.KeyStoreException;
|
|
|
|
import java.security.KeyStoreException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class HandlerUtil {
|
|
|
|
public class HandlerUtil {
|
|
|
|
|
|
|
|
|
|
|
@ -159,7 +150,7 @@ public class HandlerUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String getMemeType(HttpResponse response) {
|
|
|
|
public static String getMemeType(HttpResponse response) {
|
|
|
|
Header contentType = response.getEntity().getContentType();
|
|
|
|
Header contentType = response.getFirstHeader("Content-Type");
|
|
|
|
if (contentType != null) {
|
|
|
|
if (contentType != null) {
|
|
|
|
return contentType.getValue().split(";")[0].trim();
|
|
|
|
return contentType.getValue().split(";")[0].trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -273,29 +264,9 @@ public class HandlerUtil {
|
|
|
|
resp.setStatus(proxyResponse.getCode());
|
|
|
|
resp.setStatus(proxyResponse.getCode());
|
|
|
|
resp.setContentType(ContentType.APPLICATION_JSON.getMimeType());
|
|
|
|
resp.setContentType(ContentType.APPLICATION_JSON.getMimeType());
|
|
|
|
resp.setCharacterEncoding(Consts.UTF_8.name());
|
|
|
|
resp.setCharacterEncoding(Consts.UTF_8.name());
|
|
|
|
JSONObject response = new JSONObject();
|
|
|
|
|
|
|
|
String responseData = proxyResponse.getData();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!StringUtils.isEmpty(responseData)) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (responseData.startsWith("{")) {
|
|
|
|
|
|
|
|
JSONObject responseDataJsonObj = new JSONObject(responseData);
|
|
|
|
|
|
|
|
response.put("data", responseDataJsonObj);
|
|
|
|
|
|
|
|
} else if (responseData.startsWith("[")) {
|
|
|
|
|
|
|
|
JSONArray responseDataJsonArr = new JSONArray(responseData);
|
|
|
|
|
|
|
|
response.put("data", responseDataJsonArr);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.warn("Response data is not valid json string >> " + responseData);
|
|
|
|
|
|
|
|
response.put("data", responseData);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
log.error("Response data is not passable");
|
|
|
|
|
|
|
|
response.put("data", responseData);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try (PrintWriter writer = resp.getWriter()) {
|
|
|
|
try (PrintWriter writer = resp.getWriter()) {
|
|
|
|
writer.write(response.toString());
|
|
|
|
writer.write(proxyResponse.getData().toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -435,13 +406,13 @@ public class HandlerUtil {
|
|
|
|
* @throws FileUploadException If unable to parse the incoming request for multipart content extraction.
|
|
|
|
* @throws FileUploadException If unable to parse the incoming request for multipart content extraction.
|
|
|
|
* @throws IOException If error occurred while generating the request body.
|
|
|
|
* @throws IOException If error occurred while generating the request body.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void generateRequestEntity(HttpServletRequest req, HttpEntityEnclosingRequestBase proxyRequest)
|
|
|
|
public static void generateRequestEntity(HttpServletRequest req, ClassicHttpRequest proxyRequest)
|
|
|
|
throws FileUploadException, IOException {
|
|
|
|
throws FileUploadException, IOException {
|
|
|
|
if (ServletFileUpload.isMultipartContent(req)) {
|
|
|
|
if (ServletFileUpload.isMultipartContent(req)) {
|
|
|
|
ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
|
|
|
|
ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
|
|
|
|
List<FileItem> fileItemList = servletFileUpload.parseRequest(req);
|
|
|
|
List<FileItem> fileItemList = servletFileUpload.parseRequest(req);
|
|
|
|
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
|
|
|
|
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
|
|
|
|
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
|
|
|
entityBuilder.setMode(HttpMultipartMode.LEGACY);
|
|
|
|
for (FileItem item : fileItemList) {
|
|
|
|
for (FileItem item : fileItemList) {
|
|
|
|
if (!item.isFormField()) {
|
|
|
|
if (!item.isFormField()) {
|
|
|
|
entityBuilder.addPart(item.getFieldName(), new InputStreamBody(item.getInputStream(),
|
|
|
|
entityBuilder.addPart(item.getFieldName(), new InputStreamBody(item.getInputStream(),
|
|
|
@ -456,7 +427,7 @@ public class HandlerUtil {
|
|
|
|
if (StringUtils.isNotEmpty(req.getHeader(HttpHeaders.CONTENT_LENGTH)) ||
|
|
|
|
if (StringUtils.isNotEmpty(req.getHeader(HttpHeaders.CONTENT_LENGTH)) ||
|
|
|
|
StringUtils.isNotEmpty(req.getHeader(HttpHeaders.TRANSFER_ENCODING))) {
|
|
|
|
StringUtils.isNotEmpty(req.getHeader(HttpHeaders.TRANSFER_ENCODING))) {
|
|
|
|
InputStreamEntity entity = new InputStreamEntity(req.getInputStream(),
|
|
|
|
InputStreamEntity entity = new InputStreamEntity(req.getInputStream(),
|
|
|
|
Long.parseLong(req.getHeader(HttpHeaders.CONTENT_LENGTH)));
|
|
|
|
Long.parseLong(req.getHeader(HttpHeaders.CONTENT_LENGTH)), ContentType.parse(req.getContentType()));
|
|
|
|
proxyRequest.setEntity(new BufferedHttpEntity(entity));
|
|
|
|
proxyRequest.setEntity(new BufferedHttpEntity(entity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HandlerUtil.copyRequestHeaders(req, proxyRequest, true);
|
|
|
|
HandlerUtil.copyRequestHeaders(req, proxyRequest, true);
|
|
|
@ -470,12 +441,12 @@ public class HandlerUtil {
|
|
|
|
* @param proxyRequest proxy request instance.
|
|
|
|
* @param proxyRequest proxy request instance.
|
|
|
|
* @throws IOException If error occurred while generating the request body.
|
|
|
|
* @throws IOException If error occurred while generating the request body.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void generateChatRequestEntity(HttpServletRequest req, HttpEntityEnclosingRequestBase proxyRequest)
|
|
|
|
public static void generateChatRequestEntity(HttpServletRequest req, ClassicHttpRequest proxyRequest)
|
|
|
|
throws IOException {
|
|
|
|
throws IOException {
|
|
|
|
if (StringUtils.isNotEmpty(req.getHeader(HttpHeaders.CONTENT_LENGTH)) ||
|
|
|
|
if (StringUtils.isNotEmpty(req.getHeader(HttpHeaders.CONTENT_LENGTH)) ||
|
|
|
|
StringUtils.isNotEmpty(req.getHeader(HttpHeaders.TRANSFER_ENCODING))) {
|
|
|
|
StringUtils.isNotEmpty(req.getHeader(HttpHeaders.TRANSFER_ENCODING))) {
|
|
|
|
InputStreamEntity entity = new InputStreamEntity(req.getInputStream(),
|
|
|
|
InputStreamEntity entity = new InputStreamEntity(req.getInputStream(),
|
|
|
|
Long.parseLong(req.getHeader(HttpHeaders.CONTENT_LENGTH)));
|
|
|
|
Long.parseLong(req.getHeader(HttpHeaders.CONTENT_LENGTH)), ContentType.parse(req.getContentType()));
|
|
|
|
proxyRequest.setEntity(new BufferedHttpEntity(entity));
|
|
|
|
proxyRequest.setEntity(new BufferedHttpEntity(entity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -490,23 +461,26 @@ public class HandlerUtil {
|
|
|
|
* @param supportedGrantTypes - supported grant types
|
|
|
|
* @param supportedGrantTypes - supported grant types
|
|
|
|
* @return {@link StringEntity} of the payload to create the client application
|
|
|
|
* @return {@link StringEntity} of the payload to create the client application
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static StringEntity constructAppRegPayload(JsonArray tags, String appName, String username, String password,
|
|
|
|
public static StringEntity constructAppRegPayload(ArrayNode tags, String appName, String username, String password,
|
|
|
|
String callbackUrl, JsonArray supportedGrantTypes) {
|
|
|
|
String callbackUrl, ArrayList<String> supportedGrantTypes) {
|
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
|
|
|
|
jsonObject.addProperty(HandlerConstants.APP_NAME_KEY, appName);
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
jsonObject.addProperty(HandlerConstants.USERNAME, username);
|
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
|
jsonObject.addProperty(HandlerConstants.PASSWORD, password);
|
|
|
|
|
|
|
|
jsonObject.addProperty(HandlerConstants.IS_ALLOWED_TO_ALL_DOMAINS_KEY, "false");
|
|
|
|
data.put(HandlerConstants.APP_NAME_KEY, appName);
|
|
|
|
jsonObject.add(HandlerConstants.TAGS_KEY, tags);
|
|
|
|
data.put(HandlerConstants.USERNAME, username);
|
|
|
|
|
|
|
|
data.put(HandlerConstants.PASSWORD, password);
|
|
|
|
|
|
|
|
data.put(HandlerConstants.IS_ALLOWED_TO_ALL_DOMAINS_KEY, "false");
|
|
|
|
|
|
|
|
data.put(HandlerConstants.TAGS_KEY, tags);
|
|
|
|
if (callbackUrl != null) {
|
|
|
|
if (callbackUrl != null) {
|
|
|
|
jsonObject.addProperty(HandlerConstants.CALLBACK_URL_KEY, callbackUrl);
|
|
|
|
data.put(HandlerConstants.CALLBACK_URL_KEY, callbackUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (supportedGrantTypes != null) {
|
|
|
|
if (supportedGrantTypes != null) {
|
|
|
|
jsonObject.add(HandlerConstants.GRANT_TYPE_KEY, supportedGrantTypes);
|
|
|
|
data.put(HandlerConstants.GRANT_TYPE_KEY, supportedGrantTypes);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String payload = jsonObject.toString();
|
|
|
|
|
|
|
|
return new StringEntity(payload, ContentType.APPLICATION_JSON);
|
|
|
|
return new StringEntity(objectMapper.valueToTree(data).toString(), ContentType.APPLICATION_JSON);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
/***
|
|
|
@ -516,9 +490,9 @@ public class HandlerUtil {
|
|
|
|
* @param gatewayUrl - gateway endpoint URL
|
|
|
|
* @param gatewayUrl - gateway endpoint URL
|
|
|
|
* @param httpSession - current active HttpSession
|
|
|
|
* @param httpSession - current active HttpSession
|
|
|
|
* @param resp - HttpServletResponse
|
|
|
|
* @param resp - HttpServletResponse
|
|
|
|
* @return {@link JsonObject} of UI configurations
|
|
|
|
* @return {@link JsonNode} of UI configurations
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static JsonObject getUIConfigAndPersistInSession(String uiConfigUrl, String gatewayUrl, HttpSession httpSession,
|
|
|
|
public static JsonNode getUIConfigAndPersistInSession(String uiConfigUrl, String gatewayUrl, HttpSession httpSession,
|
|
|
|
HttpServletResponse resp) throws IOException {
|
|
|
|
HttpServletResponse resp) throws IOException {
|
|
|
|
HttpGet uiConfigEndpoint = new HttpGet(uiConfigUrl);
|
|
|
|
HttpGet uiConfigEndpoint = new HttpGet(uiConfigUrl);
|
|
|
|
ProxyResponse uiConfigResponse = HandlerUtil.execute(uiConfigEndpoint);
|
|
|
|
ProxyResponse uiConfigResponse = HandlerUtil.execute(uiConfigEndpoint);
|
|
|
@ -529,39 +503,28 @@ public class HandlerUtil {
|
|
|
|
HandlerUtil.handleError(resp, uiConfigResponse);
|
|
|
|
HandlerUtil.handleError(resp, uiConfigResponse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (uiConfigResponse.getData() == null) {
|
|
|
|
JsonNode responseData = uiConfigResponse.getData();
|
|
|
|
|
|
|
|
if (responseData == null) {
|
|
|
|
log.error("UI config retrieval is failed, and didn't find UI configuration for App manager.");
|
|
|
|
log.error("UI config retrieval is failed, and didn't find UI configuration for App manager.");
|
|
|
|
HandlerUtil.handleError(resp, null);
|
|
|
|
HandlerUtil.handleError(resp, null);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JsonParser jsonParser = new JsonParser();
|
|
|
|
httpSession.setAttribute(HandlerConstants.UI_CONFIG_KEY, responseData);
|
|
|
|
|
|
|
|
|
|
|
|
JsonElement uiConfigJsonElement = jsonParser.parse(uiConfigResponse.getData());
|
|
|
|
|
|
|
|
JsonObject uiConfigJsonObject = null;
|
|
|
|
|
|
|
|
if (uiConfigJsonElement.isJsonObject()) {
|
|
|
|
|
|
|
|
uiConfigJsonObject = uiConfigJsonElement.getAsJsonObject();
|
|
|
|
|
|
|
|
if (uiConfigJsonObject == null) {
|
|
|
|
|
|
|
|
log.error(
|
|
|
|
|
|
|
|
"Either UI config json element is not an json object or converting rom json element to json object is failed.");
|
|
|
|
|
|
|
|
HandlerUtil.handleError(resp, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
httpSession.setAttribute(HandlerConstants.UI_CONFIG_KEY, uiConfigJsonObject);
|
|
|
|
|
|
|
|
httpSession.setAttribute(HandlerConstants.PLATFORM, gatewayUrl);
|
|
|
|
httpSession.setAttribute(HandlerConstants.PLATFORM, gatewayUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uiConfigJsonObject;
|
|
|
|
return responseData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
/***
|
|
|
|
* Converts scopes from JsonArray to string with space separated values.
|
|
|
|
* Converts scopes from JsonArray to string with space separated values.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param scopes - scope Json Array and it is retrieved by reading UI config.
|
|
|
|
* @param scopes - scope Array and it is retrieved by reading UI config.
|
|
|
|
* @return string value of the defined scopes
|
|
|
|
* @return string value of the defined scopes
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static String getScopeString(JsonArray scopes) {
|
|
|
|
public static String getScopeString(JsonNode scopes) {
|
|
|
|
if (scopes != null && scopes.size() > 0) {
|
|
|
|
if (scopes != null && scopes.isArray() && !scopes.isEmpty()) {
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
for (JsonElement scope : scopes) {
|
|
|
|
for (JsonNode objNode : scopes) {
|
|
|
|
String tmpScope = scope.getAsString() + " ";
|
|
|
|
builder.append(objNode).append(" ");
|
|
|
|
builder.append(tmpScope);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
return builder.toString();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -569,35 +532,6 @@ public class HandlerUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
|
|
|
* Converts xml file into string.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param xmlFile - xmlFile which needs to be converted into string.
|
|
|
|
|
|
|
|
* @return string value of the xml file.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String xmlToString(File xmlFile) {
|
|
|
|
|
|
|
|
String stringOutput = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
|
|
|
|
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
|
|
|
|
|
Document doc = builder.parse(xmlFile);
|
|
|
|
|
|
|
|
OutputFormat format = new OutputFormat(doc);
|
|
|
|
|
|
|
|
StringWriter stringWriterOutput = new StringWriter();
|
|
|
|
|
|
|
|
XMLSerializer serial = new XMLSerializer(stringWriterOutput, format);
|
|
|
|
|
|
|
|
serial.serialize(doc);
|
|
|
|
|
|
|
|
stringOutput = stringWriterOutput.toString();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
log.error("Error occurred while sending the response into the socket. ", e);
|
|
|
|
|
|
|
|
} catch (ParserConfigurationException e) {
|
|
|
|
|
|
|
|
log.error("Error while creating the document builder.");
|
|
|
|
|
|
|
|
} catch (SAXException e) {
|
|
|
|
|
|
|
|
log.error("Error while parsing xml file.", e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return stringOutput;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
/***
|
|
|
|
* Search a key from a given json string object.
|
|
|
|
* Search a key from a given json string object.
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -640,11 +574,11 @@ public class HandlerUtil {
|
|
|
|
* Retry request again after refreshing the access token
|
|
|
|
* Retry request again after refreshing the access token
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param req incoming {@link HttpServletRequest}
|
|
|
|
* @param req incoming {@link HttpServletRequest}
|
|
|
|
* @param httpRequest subclass of {@link HttpRequestBase} related to the current request.
|
|
|
|
* @param httpRequest {@link ClassicHttpRequest} related to the current request.
|
|
|
|
* @return {@link ProxyResponse} if successful and <code>null</code> if failed.
|
|
|
|
* @return {@link ProxyResponse} if successful and <code>null</code> if failed.
|
|
|
|
* @throws IOException If an error occurs when try to retry the request.
|
|
|
|
* @throws IOException If an error occurs when try to retry the request.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static ProxyResponse retryRequestWithRefreshedToken(HttpServletRequest req, HttpRequestBase httpRequest,
|
|
|
|
public static ProxyResponse retryRequestWithRefreshedToken(HttpServletRequest req, ClassicHttpRequest httpRequest,
|
|
|
|
String apiEndpoint) throws IOException {
|
|
|
|
String apiEndpoint) throws IOException {
|
|
|
|
ProxyResponse retryResponse = refreshToken(req, apiEndpoint);
|
|
|
|
ProxyResponse retryResponse = refreshToken(req, apiEndpoint);
|
|
|
|
if (isResponseSuccessful(retryResponse)) {
|
|
|
|
if (isResponseSuccessful(retryResponse)) {
|
|
|
@ -660,7 +594,6 @@ public class HandlerUtil {
|
|
|
|
return proxyResponse;
|
|
|
|
return proxyResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return proxyResponse;
|
|
|
|
return proxyResponse;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retryResponse;
|
|
|
|
return retryResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -682,7 +615,6 @@ public class HandlerUtil {
|
|
|
|
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.");
|
|
|
|
tokenResultResponse = constructProxyResponseByErrorCode(HttpStatus.SC_UNAUTHORIZED);
|
|
|
|
tokenResultResponse = constructProxyResponseByErrorCode(HttpStatus.SC_UNAUTHORIZED);
|
|
|
|
// handleError(resp, HttpStatus.SC_UNAUTHORIZED);
|
|
|
|
|
|
|
|
return tokenResultResponse;
|
|
|
|
return tokenResultResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -690,23 +622,20 @@ public class HandlerUtil {
|
|
|
|
tokenResultResponse = getTokenResult(authData, keymanagerUrl);
|
|
|
|
tokenResultResponse = getTokenResult(authData, keymanagerUrl);
|
|
|
|
if (tokenResultResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
|
|
|
|
if (tokenResultResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
|
|
|
|
log.error("Error occurred while refreshing access token.");
|
|
|
|
log.error("Error occurred while refreshing access token.");
|
|
|
|
// HandlerUtil.handleError(resp, tokenResultResponse);
|
|
|
|
|
|
|
|
return tokenResultResponse;
|
|
|
|
return tokenResultResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JsonParser jsonParser = new JsonParser();
|
|
|
|
JsonNode tokenResponse = tokenResultResponse.getData();
|
|
|
|
JsonElement jTokenResult = jsonParser.parse(tokenResultResponse.getData());
|
|
|
|
if (tokenResponse != null) {
|
|
|
|
|
|
|
|
setNewAuthData(constructAuthDataFromTokenResult(tokenResponse, authData), session);
|
|
|
|
if (jTokenResult.isJsonObject()) {
|
|
|
|
|
|
|
|
setNewAuthData(constructAuthDataFromTokenResult(jTokenResult, authData), session);
|
|
|
|
|
|
|
|
return tokenResultResponse;
|
|
|
|
return tokenResultResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.error("Error Occurred in token renewal process.");
|
|
|
|
log.error("Error Occurred in token renewal process.");
|
|
|
|
tokenResultResponse = constructProxyResponseByErrorCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
tokenResultResponse = constructProxyResponseByErrorCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
// handleError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
return tokenResultResponse;
|
|
|
|
return tokenResultResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static ProxyResponse getTokenResult(AuthData authData, String keymanagerUrl) throws IOException {
|
|
|
|
public static ProxyResponse getTokenResult(AuthData authData, String keymanagerUrl) throws IOException {
|
|
|
|
HttpPost tokenEndpoint = new HttpPost(keymanagerUrl + HandlerConstants.OAUTH2_TOKEN_ENDPOINT);
|
|
|
|
HttpPost tokenEndpoint = new HttpPost(keymanagerUrl + HandlerConstants.OAUTH2_TOKEN_ENDPOINT);
|
|
|
|
StringEntity tokenEndpointPayload = new StringEntity(
|
|
|
|
StringEntity tokenEndpointPayload = new StringEntity(
|
|
|
@ -726,12 +655,17 @@ public class HandlerUtil {
|
|
|
|
session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, newAuthData);
|
|
|
|
session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, newAuthData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static AuthData constructAuthDataFromTokenResult(JsonElement tokenResult, AuthData authData) {
|
|
|
|
/**
|
|
|
|
JsonObject jTokenResultAsJsonObject = tokenResult.getAsJsonObject();
|
|
|
|
* Construct {@link AuthData} from token response
|
|
|
|
|
|
|
|
* @param tokenResult {@link JsonNode}
|
|
|
|
|
|
|
|
* @param authData {@link AuthData} existing auth data values
|
|
|
|
|
|
|
|
* @return new {@link AuthData} object
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static AuthData constructAuthDataFromTokenResult(JsonNode tokenResult, AuthData authData) {
|
|
|
|
AuthData newAuthData = new AuthData();
|
|
|
|
AuthData newAuthData = new AuthData();
|
|
|
|
newAuthData.setAccessToken(jTokenResultAsJsonObject.get("access_token").getAsString());
|
|
|
|
newAuthData.setAccessToken(tokenResult.get("access_token").textValue());
|
|
|
|
newAuthData.setRefreshToken(jTokenResultAsJsonObject.get("refresh_token").getAsString());
|
|
|
|
newAuthData.setRefreshToken(tokenResult.get("refresh_token").textValue());
|
|
|
|
newAuthData.setScope(jTokenResultAsJsonObject.get("scope").getAsString());
|
|
|
|
newAuthData.setScope(tokenResult.get("scope").textValue());
|
|
|
|
newAuthData.setClientId(authData.getClientId());
|
|
|
|
newAuthData.setClientId(authData.getClientId());
|
|
|
|
newAuthData.setClientSecret(authData.getClientSecret());
|
|
|
|
newAuthData.setClientSecret(authData.getClientSecret());
|
|
|
|
newAuthData.setEncodedClientApp(authData.getEncodedClientApp());
|
|
|
|
newAuthData.setEncodedClientApp(authData.getEncodedClientApp());
|
|
|
@ -748,7 +682,7 @@ public class HandlerUtil {
|
|
|
|
* This should be set to <code>false</code> when handling multipart requests as Http
|
|
|
|
* This should be set to <code>false</code> when handling multipart requests as Http
|
|
|
|
* client will generate the Content-Type header automatically.
|
|
|
|
* client will generate the Content-Type header automatically.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void copyRequestHeaders(HttpServletRequest req, HttpRequestBase httpRequest, boolean preserveContentType) {
|
|
|
|
public static void copyRequestHeaders(HttpServletRequest req, ClassicHttpRequest httpRequest, boolean preserveContentType) {
|
|
|
|
Enumeration<String> headerNames = req.getHeaderNames();
|
|
|
|
Enumeration<String> headerNames = req.getHeaderNames();
|
|
|
|
while (headerNames.hasMoreElements()) {
|
|
|
|
while (headerNames.hasMoreElements()) {
|
|
|
|
String headerName = headerNames.nextElement();
|
|
|
|
String headerName = headerNames.nextElement();
|
|
|
@ -774,18 +708,6 @@ public class HandlerUtil {
|
|
|
|
return headerValue;
|
|
|
|
return headerValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String getResponseString(HttpResponse response) throws IOException {
|
|
|
|
|
|
|
|
try (BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) {
|
|
|
|
|
|
|
|
StringBuilder responseBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
String line;
|
|
|
|
|
|
|
|
while ((line = rd.readLine()) != null) {
|
|
|
|
|
|
|
|
responseBuilder.append(line);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseBuilder.toString();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean isPropertyDefined(String property) {
|
|
|
|
public static boolean isPropertyDefined(String property) {
|
|
|
|
return StringUtils.isEmpty(System.getProperty(property));
|
|
|
|
return StringUtils.isEmpty(System.getProperty(property));
|
|
|
|
}
|
|
|
|
}
|
|
|
|