X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fengine.git;a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fadmin%2FPolicyRestController.java;h=12d3fc3349af4658a3b4a0f0811f489028ff4dbd;hp=03dbccfd91535afe21093e5480cd255ef7d3c805;hb=f36e53a3637e1204a42491ec0eeed7b3c763f681;hpb=c1b69dfb1297365d35f2ada8690f13f787d38b4f diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyRestController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyRestController.java index 03dbccfd9..12d3fc334 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyRestController.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyRestController.java @@ -27,6 +27,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; + import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; @@ -39,8 +40,10 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; import java.util.List; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; @@ -121,29 +124,32 @@ public class PolicyRestController extends RestrictedBaseController { PolicyRestController.commonClassDao = commonClassDao; } - - + /** + * policyCreationController. + * + * @param request Request + * @param response Response + */ @RequestMapping(value = {"/policycreation/save_policy"}, method = {RequestMethod.POST}) public void policyCreationController(HttpServletRequest request, HttpServletResponse response) { - String userId = UserUtils.getUserSession(request).getOrgUserId(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); try { - updateAndSendToPAP(request, response, userId, mapper); + updateAndSendToPap(request, response, UserUtils.getUserSession(request).getOrgUserId(), mapper); } catch (Exception e) { policyLogger.error("Exception Occured while saving policy", e); } } - private void updateAndSendToPAP(HttpServletRequest request, HttpServletResponse response, String userId, + private void updateAndSendToPap(HttpServletRequest request, HttpServletResponse response, String userId, ObjectMapper mapper) throws IOException { JsonNode root = mapper.readTree(request.getReader()); policyLogger.info( - "****************************************Logging UserID while Create/Update Policy**************************************************"); + "********************Logging UserID while Create/Update Policy***********************************"); policyLogger.info(USER_ID + userId + "Policy Data Object: " + root.get(PolicyController.getPolicydata()).get("policy").toString()); policyLogger.info( - "***********************************************************************************************************************************"); + "************************************************************************************************"); PolicyRestAdapter policyData = mapper.readValue( root.get(PolicyController.getPolicydata()).get("policy").toString(), PolicyRestAdapter.class); @@ -164,17 +170,16 @@ public class PolicyRestController extends RestrictedBaseController { policyData.setUserId(userId); String result; - String body = PolicyUtils.objectToJsonString(policyData); - String uri = request.getRequestURI(); - ResponseEntity responseEntity = sendToPAP(body, uri, HttpMethod.POST); + ResponseEntity responseEntity = sendToPap(PolicyUtils.objectToJsonString(policyData), + request.getRequestURI(), HttpMethod.POST); if (responseEntity != null && responseEntity.getBody().equals(HttpServletResponse.SC_CONFLICT)) { result = "PolicyExists"; } else if (responseEntity != null) { result = responseEntity.getBody().toString(); String policyName = responseEntity.getHeaders().get(POLICY_NAME).get(0); if (policyData.isEditPolicy() && SUCCESS.equalsIgnoreCase(result)) { - PolicyNotificationMail email = new PolicyNotificationMail(); - String mode = "EditPolicy"; + final PolicyNotificationMail email = new PolicyNotificationMail(); + final String mode = "EditPolicy"; String watchPolicyName = policyName.replace(XML, ""); String version = watchPolicyName.substring(watchPolicyName.lastIndexOf('.') + 1); watchPolicyName = @@ -197,8 +202,8 @@ public class PolicyRestController extends RestrictedBaseController { PrintWriter out = response.getWriter(); String responseString = mapper.writeValueAsString(result); - JSONObject j = new JSONObject("{policyData: " + responseString + "}"); - out.write(j.toString()); + JSONObject json = new JSONObject("{policyData: " + responseString + "}"); + out.write(json.toString()); } private void modifyPolicyData(JsonNode root, PolicyRestAdapter policyData) { @@ -227,13 +232,12 @@ public class PolicyRestController extends RestrictedBaseController { } } - - private ResponseEntity sendToPAP(String body, String requestURI, HttpMethod method) { + private ResponseEntity sendToPap(String body, String requestUri, HttpMethod method) { String papUrl = PolicyController.getPapUrl(); - String papID = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID); String papPass = PeCryptoUtils.decrypt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS)); - Base64.Encoder encoder = Base64.getEncoder(); - String encoding = encoder.encodeToString((papID + ":" + papPass).getBytes(StandardCharsets.UTF_8)); + String encoding = Base64.getEncoder().encodeToString( + (XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID) + + ":" + papPass).getBytes(StandardCharsets.UTF_8)); HttpHeaders headers = new HttpHeaders(); headers.set(AUTHORIZATION, BASIC + encoding); headers.set(CONTENT_TYPE, PolicyController.getContenttype()); @@ -242,7 +246,7 @@ public class PolicyRestController extends RestrictedBaseController { HttpEntity requestEntity = new HttpEntity<>(body, headers); ResponseEntity result = null; HttpClientErrorException exception = null; - String uri = requestURI; + String uri = requestUri; if (uri.startsWith("/")) { uri = uri.substring(uri.indexOf('/') + 1); } @@ -279,27 +283,24 @@ public class PolicyRestController extends RestrictedBaseController { return result; } - private String callPAP(HttpServletRequest request, String method, String uriValue) { - String uri = uriValue; - String papUrl = PolicyController.getPapUrl(); - String papID = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID); + private String callPap(HttpServletRequest request, String method, String uriValue) { PeCryptoUtils.initAesKey(XACMLProperties.getProperty(XACMLRestProperties.PROP_AES_KEY)); String papPass = PeCryptoUtils.decrypt((XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS))); Base64.Encoder encoder = Base64.getEncoder(); - String encoding = encoder.encodeToString((papID + ":" + papPass).getBytes(StandardCharsets.UTF_8)); + String encoding = encoder.encodeToString((XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID) + + ":" + papPass).getBytes(StandardCharsets.UTF_8)); HttpHeaders headers = new HttpHeaders(); headers.set(AUTHORIZATION, BASIC + encoding); headers.set(CONTENT_TYPE, PolicyController.getContenttype()); - HttpURLConnection connection = null; - List items; FileItem item = null; File file = null; + String uri = uriValue; if (uri.contains(IMPORT_DICTIONARY)) { try { - items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); + List items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); item = items.get(0); file = new File(item.getName()); String newFile = file.toString(); @@ -310,7 +311,7 @@ public class PolicyRestController extends RestrictedBaseController { } try { - URL url = new URL(papUrl + uri); + URL url = new URL(PolicyController.getPapUrl() + uri); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method); connection.setUseCaches(false); @@ -324,7 +325,7 @@ public class PolicyRestController extends RestrictedBaseController { return doConnect(connection); } - checkURI(request, uri, connection, item); + checkUri(request, uri, connection, item); return doConnect(connection); } catch (Exception e) { @@ -351,9 +352,8 @@ public class PolicyRestController extends RestrictedBaseController { return null; } - private void checkURI(HttpServletRequest request, String uri, HttpURLConnection connection, FileItem item) + private void checkUri(HttpServletRequest request, String uri, HttpURLConnection connection, FileItem item) throws IOException { - String boundary; if (!(uri.endsWith("set_BRMSParamData") || uri.contains(IMPORT_DICTIONARY))) { connection.setRequestProperty(CONTENT_TYPE, PolicyController.getContenttype()); ObjectMapper mapper = new ObjectMapper(); @@ -380,7 +380,7 @@ public class PolicyRestController extends RestrictedBaseController { IOUtils.copy(request.getInputStream(), os); } } else { - boundary = "===" + System.currentTimeMillis() + "==="; + String boundary = "===" + System.currentTimeMillis() + "==="; connection.setRequestProperty(CONTENT_TYPE, "multipart/form-data; boundary=" + boundary); try (OutputStream os = connection.getOutputStream()) { if (item != null) { @@ -402,8 +402,7 @@ public class PolicyRestController extends RestrictedBaseController { private String doConnect(final HttpURLConnection connection) throws IOException { connection.connect(); - int responseCode = connection.getResponseCode(); - if (responseCode == 200) { + if (connection.getResponseCode() == 200) { // get the response content into a String String responseJson = null; // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file) @@ -425,11 +424,17 @@ public class PolicyRestController extends RestrictedBaseController { return null; } + /** + * getDictionaryController. + * + * @param request Request + * @param response Response + */ @RequestMapping(value = {"/getDictionary/*"}, method = {RequestMethod.GET}) public void getDictionaryController(HttpServletRequest request, HttpServletResponse response) { - String uri = request.getRequestURI().replace("/getDictionary", ""); String body; - ResponseEntity responseEntity = sendToPAP(null, uri, HttpMethod.GET); + ResponseEntity responseEntity = sendToPap(null, request.getRequestURI().replace("/getDictionary", ""), + HttpMethod.GET); if (responseEntity != null) { body = responseEntity.getBody().toString(); } else { @@ -442,6 +447,13 @@ public class PolicyRestController extends RestrictedBaseController { } } + /** + * saveDictionaryController. + * + * @param request Request + * @param response Response + * @throws IOException IO Exception + */ @RequestMapping(value = {"/saveDictionary/*/*"}, method = {RequestMethod.POST}) public void saveDictionaryController(HttpServletRequest request, HttpServletResponse response) throws IOException { String userId = ""; @@ -456,12 +468,12 @@ public class PolicyRestController extends RestrictedBaseController { } policyLogger.info( - "****************************************Logging UserID while Saving Dictionary*****************************************************"); + "********************Logging UserID while Saving Dictionary**************************************"); policyLogger.info(USER_ID + userId); policyLogger.info( - "***********************************************************************************************************************************"); + "************************************************************************************************"); - String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim()); + String body = callPap(request, "POST", uri.replaceFirst("/", "").trim()); if (body != null && !body.isEmpty()) { response.getWriter().write(body); } else { @@ -469,6 +481,13 @@ public class PolicyRestController extends RestrictedBaseController { } } + /** + * deletetDictionaryController. + * + * @param request Request + * @param response Response + * @throws IOException IO Exception + */ @RequestMapping(value = {"/deleteDictionary/*/*"}, method = {RequestMethod.POST}) public void deletetDictionaryController(HttpServletRequest request, HttpServletResponse response) throws IOException { @@ -480,12 +499,12 @@ public class PolicyRestController extends RestrictedBaseController { String userId = UserUtils.getUserSession(request).getOrgUserId(); policyLogger.info( - "****************************************Logging UserID while Deleting Dictionary*****************************************************"); + "**********************Logging UserID while Deleting Dictionary*************************************"); policyLogger.info(USER_ID + userId); policyLogger.info( - "*************************************************************************************************************************************"); + "***************************************************************************************************"); - String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim()); + String body = callPap(request, "POST", uri.replaceFirst("/", "").trim()); if (body != null && !body.isEmpty()) { response.getWriter().write(body); } else { @@ -493,6 +512,14 @@ public class PolicyRestController extends RestrictedBaseController { } } + /** + * searchDictionaryController. + * + * @param request Request + * @param response Response + * @return ModelAndView object + * @throws IOException IO Exception + */ @RequestMapping(value = {"/searchDictionary"}, method = {RequestMethod.POST}) public ModelAndView searchDictionaryController(HttpServletRequest request, HttpServletResponse response) throws IOException { @@ -503,14 +530,13 @@ public class PolicyRestController extends RestrictedBaseController { } uri = ONAP + uri.substring(uri.indexOf('/')); try { - String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim()); + String body = callPap(request, "POST", uri.replaceFirst("/", "").trim()); if (body.contains("CouldNotConnectException")) { List data = new ArrayList<>(); data.add("Elastic Search Server is down"); resultList = data; } else { - JSONObject json = new JSONObject(body); - resultList = json.get("policyresult"); + resultList = new JSONObject(body).get("policyresult"); } } catch (Exception e) { policyLogger.error( @@ -522,12 +548,18 @@ public class PolicyRestController extends RestrictedBaseController { response.setCharacterEncoding(PolicyController.getCharacterencoding()); response.setContentType(PolicyController.getContenttype()); - PrintWriter out = response.getWriter(); - JSONObject j = new JSONObject("{result: " + resultList + "}"); - out.write(j.toString()); + response.getWriter().write(new JSONObject("{result: " + resultList + "}").toString()); return null; } + /** + * searchPolicy. + * + * @param request request + * @param response response + * @return ModelAndView object + * @throws IOException IO exception + */ @RequestMapping(value = {"/searchPolicy"}, method = {RequestMethod.POST}) public ModelAndView searchPolicy(HttpServletRequest request, HttpServletResponse response) throws IOException { Object resultList; @@ -536,7 +568,7 @@ public class PolicyRestController extends RestrictedBaseController { uri = uri.substring(uri.indexOf('/') + 1); } uri = ONAP + uri.substring(uri.indexOf('/')); - String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim()); + String body = callPap(request, "POST", uri.replaceFirst("/", "").trim()); JSONObject json = new JSONObject(body); try { @@ -554,21 +586,27 @@ public class PolicyRestController extends RestrictedBaseController { response.setContentType("application / json"); request.setCharacterEncoding(UTF_8); - PrintWriter out = response.getWriter(); - JSONObject j = new JSONObject("{result: " + resultList + "}"); - out.write(j.toString()); + response.getWriter().write(new JSONObject("{result: " + resultList + "}").toString()); return null; } public void deleteElasticData(String fileName) { String uri = "searchPolicy?action=delete&policyName='" + fileName + "'"; - callPAP(null, "POST", uri.trim()); + callPap(null, "POST", uri.trim()); } - public String notifyOtherPAPSToUpdateConfigurations(String mode, String newName, String oldName) { + /** + * notifyOtherPAPSToUpdateConfigurations. + * + * @param mode Mode + * @param newName New Name + * @param oldName Old Name + * @return String + */ + public String notifyOtherPapsToUpdateConfigurations(String mode, String newName, String oldName) { String uri = "onap/notifyOtherPAPs?action=" + mode + "&newPolicyName=" + newName + "&oldPolicyName=" + oldName + ""; - return callPAP(null, "POST", uri.trim()); + return callPap(null, "POST", uri.trim()); } }