Unverified Commit 8ce68906 authored by AMIT KUMAR's avatar AMIT KUMAR Committed by GitHub
Browse files

Issue #SB-000 feat: added request context to HttpClientUtil class met… (#924)

* Issue #SB-000 feat: added request context to HttpClientUtil class methods
No related merge requests found
Showing with 116 additions and 174 deletions
+116 -174
...@@ -53,8 +53,8 @@ public class UserGetRequestValidator extends BaseRequestValidator { ...@@ -53,8 +53,8 @@ public class UserGetRequestValidator extends BaseRequestValidator {
public void validateGetUserByKeyRequestaWithCaptcha(Request request, Http.Request httpRequest) { public void validateGetUserByKeyRequestaWithCaptcha(Request request, Http.Request httpRequest) {
String captcha = httpRequest.getQueryString(JsonKey.CAPTCHA_RESPONSE); String captcha = httpRequest.getQueryString(JsonKey.CAPTCHA_RESPONSE);
logger.info("QueryString: " + httpRequest.uri()); logger.debug("QueryString: " + httpRequest.uri());
logger.info("Captach: " + captcha); logger.debug("Captach: " + captcha);
String mobileApp = httpRequest.getQueryString(JsonKey.MOBILE_APP); String mobileApp = httpRequest.getQueryString(JsonKey.MOBILE_APP);
if (Boolean.parseBoolean(ProjectUtil.getConfigValue(JsonKey.ENABLE_CAPTCHA)) if (Boolean.parseBoolean(ProjectUtil.getConfigValue(JsonKey.ENABLE_CAPTCHA))
&& !new CaptchaHelper().validate(captcha, mobileApp)) { && !new CaptchaHelper().validate(captcha, mobileApp)) {
......
...@@ -46,7 +46,7 @@ public class CaptchaHelper { ...@@ -46,7 +46,7 @@ public class CaptchaHelper {
.toString(); .toString();
logger.info("Calling Api: " + url); logger.info("Calling Api: " + url);
logger.info("Captcha: " + captcha); logger.info("Captcha: " + captcha);
String response = HttpClientUtil.postFormData(url, requestMap, headers); String response = HttpClientUtil.postFormData(url, requestMap, headers, null);
Map<String, Object> responseMap = mapper.readValue(response, Map.class); Map<String, Object> responseMap = mapper.readValue(response, Map.class);
isCaptchaValid = (boolean) responseMap.get("success"); isCaptchaValid = (boolean) responseMap.get("success");
if (!isCaptchaValid) { if (!isCaptchaValid) {
......
...@@ -29,6 +29,7 @@ import org.sunbird.http.HttpClientUtil; ...@@ -29,6 +29,7 @@ import org.sunbird.http.HttpClientUtil;
import org.sunbird.keys.JsonKey; import org.sunbird.keys.JsonKey;
import org.sunbird.model.user.UserDeclareEntity; import org.sunbird.model.user.UserDeclareEntity;
import org.sunbird.request.HeaderParam; import org.sunbird.request.HeaderParam;
import org.sunbird.request.RequestContext;
import org.sunbird.response.Response; import org.sunbird.response.Response;
import org.sunbird.response.ResponseParams; import org.sunbird.response.ResponseParams;
import org.sunbird.util.ProjectUtil; import org.sunbird.util.ProjectUtil;
...@@ -577,7 +578,7 @@ public class UserControllerTest extends BaseApplicationTest { ...@@ -577,7 +578,7 @@ public class UserControllerTest extends BaseApplicationTest {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(map); String s = objectMapper.writeValueAsString(map);
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
when(HttpClientUtil.postFormData(Mockito.anyString(), Mockito.anyMap(), Mockito.anyMap())) when(HttpClientUtil.postFormData(Mockito.anyString(), Mockito.anyMap(), Mockito.anyMap(),Mockito.any(RequestContext.class)))
.thenReturn(s); .thenReturn(s);
Result result = performTest("/v2/user/exists/email/demo@gmail.com", "GET", null); Result result = performTest("/v2/user/exists/email/demo@gmail.com", "GET", null);
assertTrue(getResponseStatus(result) == 200); assertTrue(getResponseStatus(result) == 200);
......
...@@ -16,6 +16,7 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore; ...@@ -16,6 +16,7 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import org.sunbird.http.HttpClientUtil; import org.sunbird.http.HttpClientUtil;
import org.sunbird.request.RequestContext;
import org.sunbird.util.ProjectUtil; import org.sunbird.util.ProjectUtil;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
...@@ -54,7 +55,7 @@ public class CaptchaHelperTest { ...@@ -54,7 +55,7 @@ public class CaptchaHelperTest {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(map); String s = objectMapper.writeValueAsString(map);
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
when(HttpClientUtil.postFormData(Mockito.anyString(), Mockito.anyMap(), Mockito.anyMap())) when(HttpClientUtil.postFormData(Mockito.anyString(), Mockito.anyMap(), Mockito.anyMap(), Mockito.any()))
.thenReturn(s); .thenReturn(s);
} }
} }
...@@ -75,7 +75,7 @@ public class NICGatewaySmsProvider implements ISmsProvider { ...@@ -75,7 +75,7 @@ public class NICGatewaySmsProvider implements ISmsProvider {
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json"); headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json"); headers.put("Accept", "application/json");
String response = HttpClientUtil.get(URI.toString(), headers); String response = HttpClientUtil.get(URI.toString(), headers, context);
if (StringUtils.isNotBlank(response)) { if (StringUtils.isNotBlank(response)) {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
Map<String, Object> resultMap; Map<String, Object> resultMap;
......
package org.sunbird.http; package org.sunbird.http;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -22,6 +23,7 @@ import org.apache.http.message.BasicNameValuePair; ...@@ -22,6 +23,7 @@ import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.sunbird.logging.LoggerUtil; import org.sunbird.logging.LoggerUtil;
import org.sunbird.request.RequestContext;
public class HttpClientUtil { public class HttpClientUtil {
private static LoggerUtil logger = new LoggerUtil(HttpClientUtil.class); private static LoggerUtil logger = new LoggerUtil(HttpClientUtil.class);
...@@ -68,7 +70,7 @@ public class HttpClientUtil { ...@@ -68,7 +70,7 @@ public class HttpClientUtil {
return httpClientUtil; return httpClientUtil;
} }
public static String get(String requestURL, Map<String, String> headers) { public static String get(String requestURL, Map<String, String> headers, RequestContext context) {
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
try { try {
HttpGet httpGet = new HttpGet(requestURL); HttpGet httpGet = new HttpGet(requestURL);
...@@ -78,53 +80,16 @@ public class HttpClientUtil { ...@@ -78,53 +80,16 @@ public class HttpClientUtil {
} }
} }
response = httpclient.execute(httpGet); response = httpclient.execute(httpGet);
int status = response.getStatusLine().getStatusCode(); return getResponse(response, context, "GET");
if (status >= 200 && status < 300) {
HttpEntity httpEntity = response.getEntity();
byte[] bytes = EntityUtils.toByteArray(httpEntity);
StatusLine sl = response.getStatusLine();
logger.debug(
"Response from get call : " + sl.getStatusCode() + " - " + sl.getReasonPhrase());
return new String(bytes);
} else {
getErrorResponse(response, "GET");
return "";
}
} catch (Exception ex) { } catch (Exception ex) {
logger.error("Exception occurred while calling get method", ex); logger.error(context,"Exception occurred while calling get method", ex);
return ""; return "";
} finally { } finally {
if (null != response) { closeResponse(response, context, "GET");
try {
response.close();
} catch (Exception ex) {
logger.error("Exception occurred while closing get response object", ex);
}
}
}
}
private static void getErrorResponse(CloseableHttpResponse response, String method) {
try {
HttpEntity httpEntity = response.getEntity();
byte[] bytes = EntityUtils.toByteArray(httpEntity);
StatusLine sl = response.getStatusLine();
String resp = new String(bytes);
logger.info(
"Response from : "
+ method
+ " call "
+ resp
+ " status "
+ sl.getStatusCode()
+ " - "
+ sl.getReasonPhrase());
} catch (Exception ex) {
logger.error("Exception occurred while fetching response", ex);
} }
} }
public static String post(String requestURL, String params, Map<String, String> headers) { public static String post(String requestURL, String params, Map<String, String> headers, RequestContext context) {
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
try { try {
HttpPost httpPost = new HttpPost(requestURL); HttpPost httpPost = new HttpPost(requestURL);
...@@ -137,34 +102,17 @@ public class HttpClientUtil { ...@@ -137,34 +102,17 @@ public class HttpClientUtil {
httpPost.setEntity(entity); httpPost.setEntity(entity);
response = httpclient.execute(httpPost); response = httpclient.execute(httpPost);
int status = response.getStatusLine().getStatusCode(); return getResponse(response, context, "POST");
if (status >= 200 && status < 300) {
HttpEntity httpEntity = response.getEntity();
byte[] bytes = EntityUtils.toByteArray(httpEntity);
StatusLine sl = response.getStatusLine();
logger.debug(
"Response from post call : " + sl.getStatusCode() + " - " + sl.getReasonPhrase());
return new String(bytes);
} else {
getErrorResponse(response, "POST");
return "";
}
} catch (Exception ex) { } catch (Exception ex) {
logger.error("Exception occurred while calling Post method", ex); logger.error(context,"Exception occurred while calling Post method", ex);
return ""; return "";
} finally { } finally {
if (null != response) { closeResponse(response, context, "POST");
try {
response.close();
} catch (Exception ex) {
logger.error("Exception occurred while closing Post response object", ex);
}
}
} }
} }
public static String postFormData( public static String postFormData(
String requestURL, Map<String, String> params, Map<String, String> headers) { String requestURL, Map<String, String> params, Map<String, String> headers, RequestContext context) {
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
try { try {
HttpPost httpPost = new HttpPost(requestURL); HttpPost httpPost = new HttpPost(requestURL);
...@@ -183,33 +131,16 @@ public class HttpClientUtil { ...@@ -183,33 +131,16 @@ public class HttpClientUtil {
httpPost.setEntity(entity); httpPost.setEntity(entity);
response = httpclient.execute(httpPost); response = httpclient.execute(httpPost);
int status = response.getStatusLine().getStatusCode(); return getResponse(response, context, "postFormData");
if (status >= 200 && status < 300) {
HttpEntity httpEntity = response.getEntity();
byte[] bytes = EntityUtils.toByteArray(httpEntity);
StatusLine sl = response.getStatusLine();
logger.debug(
"Response from post call : " + sl.getStatusCode() + " - " + sl.getReasonPhrase());
return new String(bytes);
} else {
getErrorResponse(response, "POST FORM DATA");
return "";
}
} catch (Exception ex) { } catch (Exception ex) {
logger.error("Exception occurred while calling Post method", ex); logger.error(context,"Exception occurred while calling postFormData method", ex);
return ""; return "";
} finally { } finally {
if (null != response) { closeResponse(response, context, "postFormData");
try {
response.close();
} catch (Exception ex) {
logger.error("Exception occurred while closing Post response object", ex);
}
}
} }
} }
public static String patch(String requestURL, String params, Map<String, String> headers) { public static String patch(String requestURL, String params, Map<String, String> headers, RequestContext context) {
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
try { try {
HttpPatch httpPatch = new HttpPatch(requestURL); HttpPatch httpPatch = new HttpPatch(requestURL);
...@@ -222,35 +153,16 @@ public class HttpClientUtil { ...@@ -222,35 +153,16 @@ public class HttpClientUtil {
httpPatch.setEntity(entity); httpPatch.setEntity(entity);
response = httpclient.execute(httpPatch); response = httpclient.execute(httpPatch);
int status = response.getStatusLine().getStatusCode(); return getResponse(response, context, "PATCH");
if (status >= 200 && status < 300) {
HttpEntity httpEntity = response.getEntity();
byte[] bytes = EntityUtils.toByteArray(httpEntity);
StatusLine sl = response.getStatusLine();
logger.debug(
"Response from patch call : " + sl.getStatusCode() + " - " + sl.getReasonPhrase());
String resp = new String(bytes);
logger.info("Got response from patch call : " + resp);
return resp;
} else {
getErrorResponse(response, "PATCH");
return "";
}
} catch (Exception ex) { } catch (Exception ex) {
logger.error("Exception occurred while calling patch method", ex); logger.error(context,"Exception occurred while calling patch method", ex);
return ""; return "";
} finally { } finally {
if (null != response) { closeResponse(response, context, "PATCH");
try {
response.close();
} catch (Exception ex) {
logger.error("Exception occurred while closing patch response object", ex);
}
}
} }
} }
public static String delete(String requestURL, Map<String, String> headers) { public static String delete(String requestURL, Map<String, String> headers, RequestContext context) {
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
try { try {
HttpDelete httpDelete = new HttpDelete(requestURL); HttpDelete httpDelete = new HttpDelete(requestURL);
...@@ -260,32 +172,63 @@ public class HttpClientUtil { ...@@ -260,32 +172,63 @@ public class HttpClientUtil {
} }
} }
response = httpclient.execute(httpDelete); response = httpclient.execute(httpDelete);
int status = response.getStatusLine().getStatusCode(); return getResponse(response, context, "DELETE");
if (status >= 200 && status < 300) { } catch (Exception ex) {
HttpEntity httpEntity = response.getEntity(); logger.error(context,"Exception occurred while calling delete method", ex);
StatusLine sl = response.getStatusLine(); return "";
logger.debug( } finally {
"Response from delete call : " + sl.getStatusCode() + " - " + sl.getReasonPhrase()); closeResponse(response, context, "DELETE");
if (null != httpEntity) { }
byte[] bytes = EntityUtils.toByteArray(httpEntity); }
return new String(bytes);
} else {
return ""; private static String getResponse(CloseableHttpResponse response, RequestContext context, String method) throws IOException {
} int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity httpEntity = response.getEntity();
StatusLine sl = response.getStatusLine();
logger.debug(context,
"Response from "+method+" call : " + sl.getStatusCode() + " - " + sl.getReasonPhrase());
if (null != httpEntity) {
byte[] bytes = EntityUtils.toByteArray(httpEntity);
String resp = new String(bytes);
logger.info(context,"Got response from "+method+" call : " + resp);
return resp;
} else { } else {
getErrorResponse(response, "DELETE");
return ""; return "";
} }
} catch (Exception ex) { } else {
logger.error("Exception occurred while calling delete method", ex); getErrorResponse(response, method, context);
return ""; return "";
} finally { }
if (null != response) { }
try {
response.close(); private static void getErrorResponse(CloseableHttpResponse response, String method, RequestContext context) {
} catch (Exception ex) { try {
logger.error("Exception occurred while closing delete response object", ex); HttpEntity httpEntity = response.getEntity();
} byte[] bytes = EntityUtils.toByteArray(httpEntity);
StatusLine sl = response.getStatusLine();
String resp = new String(bytes);
logger.info(context,
"Response from : "
+ method
+ " call "
+ resp
+ " status "
+ sl.getStatusCode()
+ " - "
+ sl.getReasonPhrase());
} catch (Exception ex) {
logger.error(context, "Exception occurred while fetching response for method "+method, ex);
}
}
private static void closeResponse(CloseableHttpResponse response, RequestContext context, String method) {
if (null != response) {
try {
response.close();
} catch (Exception ex) {
logger.error(context,"Exception occurred while closing "+method+" response object", ex);
} }
} }
} }
......
...@@ -33,7 +33,7 @@ public class KeycloakBruteForceAttackUtil { ...@@ -33,7 +33,7 @@ public class KeycloakBruteForceAttackUtil {
+ "/attack-detection/brute-force/users/" + "/attack-detection/brute-force/users/"
+ fedUserPrefix + fedUserPrefix
+ userId; + userId;
String response = HttpClientUtil.get(url, getHeaders(context)); String response = HttpClientUtil.get(url, getHeaders(context), context);
logger.info(context, "KeycloakBruteForceAttackUtil:getUserStatus: Response = " + response); logger.info(context, "KeycloakBruteForceAttackUtil:getUserStatus: Response = " + response);
Map<String, Object> attackStatus = new ObjectMapper().readValue(response, Map.class); Map<String, Object> attackStatus = new ObjectMapper().readValue(response, Map.class);
boolean isDisabled = ((boolean) attackStatus.get("disabled")); boolean isDisabled = ((boolean) attackStatus.get("disabled"));
...@@ -57,7 +57,7 @@ public class KeycloakBruteForceAttackUtil { ...@@ -57,7 +57,7 @@ public class KeycloakBruteForceAttackUtil {
+ "/attack-detection/brute-force/users/" + "/attack-detection/brute-force/users/"
+ fedUserPrefix + fedUserPrefix
+ userId; + userId;
HttpClientUtil.delete(url, getHeaders(context)); HttpClientUtil.delete(url, getHeaders(context), context);
logger.info(context, "clear Brute Force For User for userId : " + userId); logger.info(context, "clear Brute Force For User for userId : " + userId);
return true; return true;
} }
......
...@@ -94,7 +94,7 @@ public class KeycloakRequiredActionLinkUtil { ...@@ -94,7 +94,7 @@ public class KeycloakRequiredActionLinkUtil {
+ "realms/" + "realms/"
+ ProjectUtil.getConfigValue(JsonKey.SUNBIRD_SSO_RELAM) + ProjectUtil.getConfigValue(JsonKey.SUNBIRD_SSO_RELAM)
+ SUNBIRD_KEYCLOAK_REQD_ACTION_LINK; + SUNBIRD_KEYCLOAK_REQD_ACTION_LINK;
String response = HttpClientUtil.post(url, mapper.writeValueAsString(request), headers); String response = HttpClientUtil.post(url, mapper.writeValueAsString(request), headers, context);
logger.info(context, "KeycloakRequiredActionLinkUtil:generateLink: Response = " + response); logger.info(context, "KeycloakRequiredActionLinkUtil:generateLink: Response = " + response);
......
...@@ -24,7 +24,7 @@ public class KeycloakUtil { ...@@ -24,7 +24,7 @@ public class KeycloakUtil {
fields.put("client_secret", ProjectUtil.getConfigValue(JsonKey.SUNBIRD_SSO_CLIENT_SECRET)); fields.put("client_secret", ProjectUtil.getConfigValue(JsonKey.SUNBIRD_SSO_CLIENT_SECRET));
fields.put("grant_type", "client_credentials"); fields.put("grant_type", "client_credentials");
String response = HttpClientUtil.postFormData(url, fields, headers); String response = HttpClientUtil.postFormData(url, fields, headers, context);
logger.debug(context, "KeycloakUtil:getAdminAccessToken: Response = " + response); logger.debug(context, "KeycloakUtil:getAdminAccessToken: Response = " + response);
Map<String, Object> responseMap = new ObjectMapper().readValue(response, Map.class); Map<String, Object> responseMap = new ObjectMapper().readValue(response, Map.class);
return (String) responseMap.get("access_token"); return (String) responseMap.get("access_token");
......
package org.sunbird.url; package org.sunbird.url;
import org.sunbird.request.RequestContext;
public interface URLShortner { public interface URLShortner {
public String shortUrl(String url); public String shortUrl(String url, RequestContext context);
} }
...@@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils; ...@@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils;
import org.sunbird.http.HttpClientUtil; import org.sunbird.http.HttpClientUtil;
import org.sunbird.keys.JsonKey; import org.sunbird.keys.JsonKey;
import org.sunbird.logging.LoggerUtil; import org.sunbird.logging.LoggerUtil;
import org.sunbird.request.RequestContext;
import org.sunbird.util.ProjectUtil; import org.sunbird.util.ProjectUtil;
import org.sunbird.util.PropertiesCache; import org.sunbird.util.PropertiesCache;
...@@ -19,12 +20,12 @@ public class URLShortnerImpl implements URLShortner { ...@@ -19,12 +20,12 @@ public class URLShortnerImpl implements URLShortner {
private static final String SUNBIRD_WEB_URL = "sunbird_web_url"; private static final String SUNBIRD_WEB_URL = "sunbird_web_url";
@Override @Override
public String shortUrl(String url) { public String shortUrl(String url, RequestContext context) {
boolean flag = false; boolean flag = false;
try { try {
flag = Boolean.parseBoolean(ProjectUtil.getConfigValue(JsonKey.SUNBIRD_URL_SHORTNER_ENABLE)); flag = Boolean.parseBoolean(ProjectUtil.getConfigValue(JsonKey.SUNBIRD_URL_SHORTNER_ENABLE));
} catch (Exception ex) { } catch (Exception ex) {
logger.error("Exception occurred while parsing sunbird_url_shortner_enable key", ex); logger.error(context,"Exception occurred while parsing sunbird_url_shortner_enable key", ex);
} }
if (flag) { if (flag) {
String baseUrl = PropertiesCache.getInstance().getProperty("sunbird_url_shortner_base_url"); String baseUrl = PropertiesCache.getInstance().getProperty("sunbird_url_shortner_base_url");
...@@ -34,7 +35,7 @@ public class URLShortnerImpl implements URLShortner { ...@@ -34,7 +35,7 @@ public class URLShortnerImpl implements URLShortner {
PropertiesCache.getInstance().getProperty("sunbird_url_shortner_access_token"); PropertiesCache.getInstance().getProperty("sunbird_url_shortner_access_token");
} }
String requestURL = baseUrl + accessToken + "&longUrl=" + url; String requestURL = baseUrl + accessToken + "&longUrl=" + url;
String response = HttpClientUtil.get(requestURL, null); String response = HttpClientUtil.get(requestURL, null, context);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = null; Map<String, Object> map = null;
if (!StringUtils.isBlank(response)) { if (!StringUtils.isBlank(response)) {
...@@ -43,7 +44,7 @@ public class URLShortnerImpl implements URLShortner { ...@@ -43,7 +44,7 @@ public class URLShortnerImpl implements URLShortner {
Map<String, String> dataMap = (Map<String, String>) map.get("data"); Map<String, String> dataMap = (Map<String, String>) map.get("data");
return dataMap.get("url"); return dataMap.get("url");
} catch (IOException | ClassCastException e) { } catch (IOException | ClassCastException e) {
logger.error("Exception occurred while parsing " + e.getMessage(), e); logger.error(context,"Exception occurred while parsing " + e.getMessage(), e);
} }
} }
} }
...@@ -51,13 +52,13 @@ public class URLShortnerImpl implements URLShortner { ...@@ -51,13 +52,13 @@ public class URLShortnerImpl implements URLShortner {
} }
/** @return the url */ /** @return the url */
public String getUrl() { public String getUrl(RequestContext context) {
if (StringUtils.isBlank(resUrl)) { if (StringUtils.isBlank(resUrl)) {
String webUrl = System.getenv(SUNBIRD_WEB_URL); String webUrl = System.getenv(SUNBIRD_WEB_URL);
if (StringUtils.isBlank(webUrl)) { if (StringUtils.isBlank(webUrl)) {
webUrl = PropertiesCache.getInstance().getProperty(SUNBIRD_WEB_URL); webUrl = PropertiesCache.getInstance().getProperty(SUNBIRD_WEB_URL);
} }
return shortUrl(webUrl); return shortUrl(webUrl, context);
} else { } else {
return resUrl; return resUrl;
} }
......
...@@ -25,6 +25,7 @@ import org.powermock.api.mockito.PowerMockito; ...@@ -25,6 +25,7 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import org.sunbird.request.RequestContext;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PowerMockIgnore({ @PowerMockIgnore({
...@@ -72,7 +73,7 @@ public class HttpClientUtilTest { ...@@ -72,7 +73,7 @@ public class HttpClientUtilTest {
PowerMockito.when(EntityUtils.toByteArray(Mockito.any(HttpEntity.class))).thenReturn(bytes); PowerMockito.when(EntityUtils.toByteArray(Mockito.any(HttpEntity.class))).thenReturn(bytes);
PowerMockito.when(httpclient.execute(Mockito.any(HttpGet.class))).thenReturn(response); PowerMockito.when(httpclient.execute(Mockito.any(HttpGet.class))).thenReturn(response);
HttpClientUtil.getInstance(); HttpClientUtil.getInstance();
String res = HttpClientUtil.get("http://localhost:80/user/read", headers()); String res = HttpClientUtil.get("http://localhost:80/user/read", headers(), null);
assertNotNull(res); assertNotNull(res);
} }
...@@ -96,7 +97,7 @@ public class HttpClientUtilTest { ...@@ -96,7 +97,7 @@ public class HttpClientUtilTest {
HttpClientUtil.getInstance(); HttpClientUtil.getInstance();
String res = String res =
HttpClientUtil.post( HttpClientUtil.post(
"http://localhost:80/user/read", "{\"message\":\"success\"}", headers()); "http://localhost:80/user/read", "{\"message\":\"success\"}", headers(), null);
assertNotNull(res); assertNotNull(res);
} }
...@@ -120,7 +121,7 @@ public class HttpClientUtilTest { ...@@ -120,7 +121,7 @@ public class HttpClientUtilTest {
Map<String, String> fields = new HashMap<>(); Map<String, String> fields = new HashMap<>();
fields.put("message", "success"); fields.put("message", "success");
HttpClientUtil.getInstance(); HttpClientUtil.getInstance();
String res = HttpClientUtil.postFormData("http://localhost:80/user/read", fields, headers()); String res = HttpClientUtil.postFormData("http://localhost:80/user/read", fields, headers(), null);
assertNotNull(res); assertNotNull(res);
} }
...@@ -144,7 +145,7 @@ public class HttpClientUtilTest { ...@@ -144,7 +145,7 @@ public class HttpClientUtilTest {
HttpClientUtil.getInstance(); HttpClientUtil.getInstance();
String res = String res =
HttpClientUtil.patch( HttpClientUtil.patch(
"http://localhost:80/user/read", "{\"message\":\"success\"}", headers()); "http://localhost:80/user/read", "{\"message\":\"success\"}", headers(), null);
assertNotNull(res); assertNotNull(res);
} }
...@@ -166,7 +167,7 @@ public class HttpClientUtilTest { ...@@ -166,7 +167,7 @@ public class HttpClientUtilTest {
PowerMockito.when(EntityUtils.toByteArray(Mockito.any(HttpEntity.class))).thenReturn(bytes); PowerMockito.when(EntityUtils.toByteArray(Mockito.any(HttpEntity.class))).thenReturn(bytes);
PowerMockito.when(httpclient.execute(Mockito.any(HttpPatch.class))).thenReturn(response); PowerMockito.when(httpclient.execute(Mockito.any(HttpPatch.class))).thenReturn(response);
HttpClientUtil.getInstance(); HttpClientUtil.getInstance();
String res = HttpClientUtil.delete("http://localhost:80/user/read", headers()); String res = HttpClientUtil.delete("http://localhost:80/user/read", headers(), null);
assertNotNull(res); assertNotNull(res);
} }
} }
...@@ -34,9 +34,9 @@ public class KeycloakBruteForceAttackUtilTest { ...@@ -34,9 +34,9 @@ public class KeycloakBruteForceAttackUtilTest {
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
when(KeycloakUtil.getAdminAccessTokenWithoutDomain(Mockito.any(RequestContext.class))) when(KeycloakUtil.getAdminAccessTokenWithoutDomain(Mockito.any(RequestContext.class)))
.thenReturn("accessToken"); .thenReturn("accessToken");
when(HttpClientUtil.get(Mockito.anyString(), Mockito.anyMap())) when(HttpClientUtil.get(Mockito.anyString(), Mockito.anyMap(), Mockito.any(RequestContext.class)))
.thenReturn("{\"disabled\":true}"); .thenReturn("{\"disabled\":true}");
when(HttpClientUtil.delete(Mockito.anyString(), Mockito.anyMap())).thenReturn(""); when(HttpClientUtil.delete(Mockito.anyString(), Mockito.anyMap(),Mockito.any(RequestContext.class))).thenReturn("");
} }
@Test @Test
......
...@@ -29,7 +29,7 @@ public class KeycloakUtilTest { ...@@ -29,7 +29,7 @@ public class KeycloakUtilTest {
public void setup() { public void setup() {
PowerMockito.mockStatic(HttpClientUtil.class); PowerMockito.mockStatic(HttpClientUtil.class);
PowerMockito.mockStatic(ProjectUtil.class); PowerMockito.mockStatic(ProjectUtil.class);
when(HttpClientUtil.postFormData(Mockito.anyString(), Mockito.anyMap(), Mockito.anyMap())) when(HttpClientUtil.postFormData(Mockito.anyString(), Mockito.anyMap(), Mockito.anyMap(), Mockito.any(RequestContext.class)))
.thenReturn("{\"access_token\":\"accesstoken\"}"); .thenReturn("{\"access_token\":\"accesstoken\"}");
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
} }
......
...@@ -35,7 +35,7 @@ public class URLShortnerImplTest { ...@@ -35,7 +35,7 @@ public class URLShortnerImplTest {
@Test @Test
public void urlShortTest() { public void urlShortTest() {
URLShortner shortner = new URLShortnerImpl(); URLShortner shortner = new URLShortnerImpl();
String url = shortner.shortUrl("https://staging.open-sunbird.org/"); String url = shortner.shortUrl("https://staging.open-sunbird.org/", null);
Assert.assertNotNull(url); Assert.assertNotNull(url);
} }
...@@ -50,7 +50,7 @@ public class URLShortnerImplTest { ...@@ -50,7 +50,7 @@ public class URLShortnerImplTest {
} }
URLShortnerImpl shortnerImpl = new URLShortnerImpl(); URLShortnerImpl shortnerImpl = new URLShortnerImpl();
String url = shortnerImpl.getUrl(); String url = shortnerImpl.getUrl(null);
Assert.assertEquals(url, webUrl); Assert.assertEquals(url, webUrl);
} }
} }
...@@ -378,16 +378,6 @@ public class ProjectUtilTest extends BaseHttpTest { ...@@ -378,16 +378,6 @@ public class ProjectUtilTest extends BaseHttpTest {
assertTrue(bool); assertTrue(bool);
} }
@Test
public void testSendGetRequestSuccessWithEkStepBaseUrl() throws Exception {
String ekStepBaseUrl = System.getenv(JsonKey.EKSTEP_BASE_URL);
if (StringUtils.isBlank(ekStepBaseUrl)) {
ekStepBaseUrl = PropertiesCache.getInstance().getProperty(JsonKey.EKSTEP_BASE_URL);
}
String response = HttpClientUtil.get(ekStepBaseUrl + "/search/health", headers);
assertNotNull(response);
}
@Test @Test
public void testGetLmsUserIdSuccessWithoutFedUserId() { public void testGetLmsUserIdSuccessWithoutFedUserId() {
String userid = ProjectUtil.getLmsUserId("1234567890"); String userid = ProjectUtil.getLmsUserId("1234567890");
......
...@@ -43,7 +43,7 @@ public class BackGroundNotificationActor extends BaseActor { ...@@ -43,7 +43,7 @@ public class BackGroundNotificationActor extends BaseActor {
headers.put("Content-type", "application/json"); headers.put("Content-type", "application/json");
headers.put("requestId", reqObj.getRequestId()); headers.put("requestId", reqObj.getRequestId());
ProjectUtil.setTraceIdInHeader(headers, reqObj.getRequestContext()); ProjectUtil.setTraceIdInHeader(headers, reqObj.getRequestContext());
String response = HttpClientUtil.post(NOTIFICATION_SERVICE_URL, json, headers); String response = HttpClientUtil.post(NOTIFICATION_SERVICE_URL, json, headers, reqObj.getRequestContext());
logger.info( logger.info(
reqObj.getRequestContext(), reqObj.getRequestContext(),
"BackGroundNotificationActor:callNotificationService :: Response =" + response); "BackGroundNotificationActor:callNotificationService :: Response =" + response);
......
...@@ -84,7 +84,8 @@ public class OrganisationBackgroundActor extends BaseActor { ...@@ -84,7 +84,8 @@ public class OrganisationBackgroundActor extends BaseActor {
+ "/" + "/"
+ tagId, + tagId,
body, body,
header); header,
context);
logger.info( logger.info(
context, context,
"OrganisationBackgroundActor:registertag ,call end with id and status = " "OrganisationBackgroundActor:registertag ,call end with id and status = "
......
...@@ -171,7 +171,8 @@ public class OrgServiceImpl implements OrgService { ...@@ -171,7 +171,8 @@ public class OrgServiceImpl implements OrgService {
(ekStepBaseUrl (ekStepBaseUrl
+ PropertiesCache.getInstance().getProperty(JsonKey.EKSTEP_CHANNEL_REG_API_URL)), + PropertiesCache.getInstance().getProperty(JsonKey.EKSTEP_CHANNEL_REG_API_URL)),
reqString, reqString,
headerMap); headerMap,
context);
logger.info(context, "end call for channel registration for org id ==" + req.get(JsonKey.ID)); logger.info(context, "end call for channel registration for org id ==" + req.get(JsonKey.ID));
} catch (Exception e) { } catch (Exception e) {
logger.error( logger.error(
...@@ -229,7 +230,8 @@ public class OrgServiceImpl implements OrgService { ...@@ -229,7 +230,8 @@ public class OrgServiceImpl implements OrgService {
+ "/" + "/"
+ req.get(JsonKey.ID), + req.get(JsonKey.ID),
reqString, reqString,
headerMap); headerMap,
context);
logger.info( logger.info(
context, "end call for channel update for org id ==" + req.get(JsonKey.HASHTAGID)); context, "end call for channel update for org id ==" + req.get(JsonKey.HASHTAGID));
} catch (Exception e) { } catch (Exception e) {
......
...@@ -32,8 +32,8 @@ public class ResetPasswordService { ...@@ -32,8 +32,8 @@ public class ResetPasswordService {
context); context);
templateMap.put( templateMap.put(
JsonKey.SET_PASSWORD_LINK, isUrlShortRequired ? urlShortner.shortUrl(url) : url); JsonKey.SET_PASSWORD_LINK, isUrlShortRequired ? urlShortner.shortUrl(url,context) : url);
return isUrlShortRequired ? urlShortner.shortUrl(url) : url; return isUrlShortRequired ? urlShortner.shortUrl(url, context) : url;
} else { } else {
String url = String url =
...@@ -43,8 +43,8 @@ public class ResetPasswordService { ...@@ -43,8 +43,8 @@ public class ResetPasswordService {
KeycloakRequiredActionLinkUtil.VERIFY_EMAIL, KeycloakRequiredActionLinkUtil.VERIFY_EMAIL,
context); context);
templateMap.put( templateMap.put(
JsonKey.VERIFY_EMAIL_LINK, isUrlShortRequired ? urlShortner.shortUrl(url) : url); JsonKey.VERIFY_EMAIL_LINK, isUrlShortRequired ? urlShortner.shortUrl(url, context) : url);
return isUrlShortRequired ? urlShortner.shortUrl(url) : url; return isUrlShortRequired ? urlShortner.shortUrl(url, context) : url;
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment