|
|
@ -18,6 +18,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
package io.entgra.device.mgt.core.ui.request.interceptor.util;
|
|
|
|
package io.entgra.device.mgt.core.ui.request.interceptor.util;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonFactory;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
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;
|
|
|
@ -32,27 +35,25 @@ import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.entity.mime.InputStreamBody;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.ssl.TrustAllStrategy;
|
|
|
|
|
|
|
|
import org.apache.hc.core5.http.*;
|
|
|
|
|
|
|
|
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.StringEntity;
|
|
|
|
|
|
|
|
import org.apache.hc.core5.ssl.SSLContextBuilder;
|
|
|
|
import org.apache.http.Consts;
|
|
|
|
import org.apache.http.Consts;
|
|
|
|
import org.apache.http.Header;
|
|
|
|
|
|
|
|
import org.apache.http.HttpHeaders;
|
|
|
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
|
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
|
|
|
|
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
|
|
|
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
|
|
|
import org.apache.http.client.methods.HttpRequestBase;
|
|
|
|
import org.apache.http.client.methods.HttpRequestBase;
|
|
|
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
|
|
|
|
|
import org.apache.http.conn.ssl.X509HostnameVerifier;
|
|
|
|
|
|
|
|
import org.apache.http.cookie.SM;
|
|
|
|
import org.apache.http.cookie.SM;
|
|
|
|
import org.apache.http.entity.BufferedHttpEntity;
|
|
|
|
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
|
|
|
|
import org.apache.http.entity.InputStreamEntity;
|
|
|
|
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
|
|
|
|
import org.apache.http.entity.mime.HttpMultipartMode;
|
|
|
|
|
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
|
|
|
|
|
import org.apache.http.entity.mime.content.InputStreamBody;
|
|
|
|
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
|
|
|
|
import org.apache.xml.serialize.OutputFormat;
|
|
|
|
import org.apache.xml.serialize.OutputFormat;
|
|
|
|
import org.apache.xml.serialize.XMLSerializer;
|
|
|
|
import org.apache.xml.serialize.XMLSerializer;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONArray;
|
|
|
@ -70,13 +71,11 @@ import javax.servlet.http.HttpSession;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.*;
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
|
|
|
import java.io.StringWriter;
|
|
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.math.BigInteger;
|
|
|
|
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
|
|
|
|
import java.security.KeyStoreException;
|
|
|
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
@ -96,71 +95,75 @@ public class HandlerUtil {
|
|
|
|
* @return response as string
|
|
|
|
* @return response as string
|
|
|
|
* @throws IOException IO exception returns if error occurs when executing the httpMethod
|
|
|
|
* @throws IOException IO exception returns if error occurs when executing the httpMethod
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static ProxyResponse execute(HttpRequestBase httpRequest) throws IOException {
|
|
|
|
public static ProxyResponse execute(ClassicHttpRequest httpRequest) throws IOException {
|
|
|
|
|
|
|
|
|
|
|
|
try (CloseableHttpClient client = getHttpClient()) {
|
|
|
|
try (CloseableHttpClient client = getHttpClient()) {
|
|
|
|
HttpResponse response = client.execute(httpRequest);
|
|
|
|
|
|
|
|
ProxyResponse proxyResponse = new ProxyResponse();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (response == null) {
|
|
|
|
JsonFactory jsonFactory = new JsonFactory();
|
|
|
|
log.error("Received null response for http request : " + httpRequest.getMethod() + " " + httpRequest
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper(jsonFactory);
|
|
|
|
.getURI().toString());
|
|
|
|
|
|
|
|
proxyResponse.setCode(HandlerConstants.INTERNAL_ERROR_CODE);
|
|
|
|
return client.execute(httpRequest, response -> {
|
|
|
|
proxyResponse.setStatus(ProxyResponse.Status.ERROR);
|
|
|
|
final HttpEntity responseEntity = response.getEntity();
|
|
|
|
proxyResponse.setExecutorResponse(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(
|
|
|
|
ProxyResponse handlerResponse = new ProxyResponse();
|
|
|
|
|
|
|
|
int statusCode = response.getCode();
|
|
|
|
|
|
|
|
if (responseEntity == null) {
|
|
|
|
|
|
|
|
log.error("Received null response for http request : " + httpRequest.getMethod() + " " + httpRequest.getRequestUri());
|
|
|
|
|
|
|
|
handlerResponse.setCode(HandlerConstants.INTERNAL_ERROR_CODE);
|
|
|
|
|
|
|
|
handlerResponse.setStatus(ProxyResponse.Status.ERROR);
|
|
|
|
|
|
|
|
handlerResponse.setExecutorResponse(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(
|
|
|
|
HandlerConstants.INTERNAL_ERROR_CODE));
|
|
|
|
HandlerConstants.INTERNAL_ERROR_CODE));
|
|
|
|
return proxyResponse;
|
|
|
|
return handlerResponse;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
int statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
try (InputStream inputStream = responseEntity.getContent()) {
|
|
|
|
String jsonString = getResponseString(response);
|
|
|
|
JsonNode responseData = objectMapper.readTree(inputStream);
|
|
|
|
if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
|
|
|
|
if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
|
|
|
|
proxyResponse.setCode(statusCode);
|
|
|
|
handlerResponse.setCode(statusCode);
|
|
|
|
proxyResponse.setData(jsonString);
|
|
|
|
handlerResponse.setData(responseData);
|
|
|
|
proxyResponse.setStatus(ProxyResponse.Status.SUCCESS);
|
|
|
|
handlerResponse.setStatus(ProxyResponse.Status.SUCCESS);
|
|
|
|
proxyResponse.setExecutorResponse("SUCCESS");
|
|
|
|
handlerResponse.setExecutorResponse("SUCCESS");
|
|
|
|
proxyResponse.setHeaders(response.getAllHeaders());
|
|
|
|
handlerResponse.setHeaders(response.getHeaders());
|
|
|
|
return proxyResponse;
|
|
|
|
return handlerResponse;
|
|
|
|
} else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
|
|
|
|
} else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
|
|
|
|
if (isTokenExpired(jsonString)) {
|
|
|
|
if (isTokenExpired(responseData)) {
|
|
|
|
proxyResponse.setCode(statusCode);
|
|
|
|
handlerResponse.setCode(statusCode);
|
|
|
|
proxyResponse.setStatus(ProxyResponse.Status.ERROR);
|
|
|
|
handlerResponse.setStatus(ProxyResponse.Status.ERROR);
|
|
|
|
proxyResponse.setExecutorResponse(HandlerConstants.TOKEN_IS_EXPIRED);
|
|
|
|
handlerResponse.setExecutorResponse(HandlerConstants.TOKEN_IS_EXPIRED);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
log.error(
|
|
|
|
log.error(
|
|
|
|
"Received " + statusCode + " response for http request : " + httpRequest.getMethod()
|
|
|
|
"Received " + statusCode + " response for http request : " + httpRequest.getMethod()
|
|
|
|
+ " " + httpRequest.getURI().toString() + ". Error message: " + jsonString);
|
|
|
|
+ " " + httpRequest.getRequestUri() + ". Error message: " + responseData.textValue());
|
|
|
|
proxyResponse.setCode(statusCode);
|
|
|
|
handlerResponse.setCode(statusCode);
|
|
|
|
proxyResponse.setData(jsonString);
|
|
|
|
handlerResponse.setData(responseData);
|
|
|
|
proxyResponse.setStatus(ProxyResponse.Status.ERROR);
|
|
|
|
handlerResponse.setStatus(ProxyResponse.Status.ERROR);
|
|
|
|
proxyResponse.setExecutorResponse(
|
|
|
|
handlerResponse.setExecutorResponse(
|
|
|
|
HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(statusCode));
|
|
|
|
HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(statusCode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return proxyResponse;
|
|
|
|
return handlerResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.error("Received " + statusCode +
|
|
|
|
log.error("Received " + statusCode + " response for http request : " + httpRequest.getMethod()
|
|
|
|
" response for http request : " + httpRequest.getMethod() + " " + httpRequest.getURI()
|
|
|
|
+ " " + httpRequest.getRequestUri() + ". Error message: " + responseData.textValue());
|
|
|
|
.toString() + ". Error message: " + jsonString);
|
|
|
|
handlerResponse.setCode(statusCode);
|
|
|
|
proxyResponse.setCode(statusCode);
|
|
|
|
handlerResponse.setData(responseData);
|
|
|
|
proxyResponse.setData(jsonString);
|
|
|
|
handlerResponse.setStatus(ProxyResponse.Status.ERROR);
|
|
|
|
proxyResponse.setStatus(ProxyResponse.Status.ERROR);
|
|
|
|
handlerResponse
|
|
|
|
proxyResponse
|
|
|
|
|
|
|
|
.setExecutorResponse(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(statusCode));
|
|
|
|
.setExecutorResponse(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(statusCode));
|
|
|
|
return proxyResponse;
|
|
|
|
return handlerResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean isTokenExpired(String jsonBody) {
|
|
|
|
public static boolean isTokenExpired(JsonNode jsonBody) {
|
|
|
|
return jsonBody.contains("Access token expired") || jsonBody
|
|
|
|
return jsonBody.textValue().contains("Access token expired") || jsonBody.textValue()
|
|
|
|
.contains("Invalid input. Access token validation failed");
|
|
|
|
.contains("Invalid input. Access token validation failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String getMemeType(HttpResponse response) {
|
|
|
|
public static String getMemeType(HttpResponse response) {
|
|
|
|
String memeType = "";
|
|
|
|
|
|
|
|
Header contentType = response.getEntity().getContentType();
|
|
|
|
Header contentType = response.getEntity().getContentType();
|
|
|
|
if (contentType != null) {
|
|
|
|
if (contentType != null) {
|
|
|
|
memeType = contentType.getValue().split(";")[0].trim();
|
|
|
|
return contentType.getValue().split(";")[0].trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return memeType;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
/***
|
|
|
@ -367,7 +370,21 @@ public class HandlerUtil {
|
|
|
|
boolean isIgnoreHostnameVerification = Boolean.parseBoolean(System.
|
|
|
|
boolean isIgnoreHostnameVerification = Boolean.parseBoolean(System.
|
|
|
|
getProperty("org.wso2.ignoreHostnameVerification"));
|
|
|
|
getProperty("org.wso2.ignoreHostnameVerification"));
|
|
|
|
if (isIgnoreHostnameVerification) {
|
|
|
|
if (isIgnoreHostnameVerification) {
|
|
|
|
return HttpClients.custom().setHostnameVerifier((X509HostnameVerifier) NoopHostnameVerifier.INSTANCE).build();
|
|
|
|
try {
|
|
|
|
|
|
|
|
return HttpClients.custom()
|
|
|
|
|
|
|
|
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
|
|
|
|
|
|
|
|
.setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
|
|
|
|
|
|
|
|
.setSslContext(SSLContextBuilder.create()
|
|
|
|
|
|
|
|
.loadTrustMaterial(TrustAllStrategy.INSTANCE)
|
|
|
|
|
|
|
|
.build())
|
|
|
|
|
|
|
|
.setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
|
|
|
|
|
|
|
|
.build())
|
|
|
|
|
|
|
|
.build())
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
|
|
|
|
|
|
|
|
log.error("Error Occurred while creating the custom http client", e);
|
|
|
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return HttpClients.createDefault();
|
|
|
|
return HttpClients.createDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|