More sonar cleanup and line consolidation
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / PolicyValidationController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controller;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24
25 import java.io.IOException;
26 import java.io.PrintWriter;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.json.JSONObject;
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
34 import org.onap.policy.rest.adapter.PolicyRestAdapter;
35 import org.onap.policy.rest.util.PolicyValidation;
36 import org.onap.policy.rest.util.PolicyValidationRequestWrapper;
37 import org.onap.policy.utils.PolicyUtils;
38 import org.onap.portalsdk.core.controller.RestrictedBaseController;
39 import org.onap.portalsdk.core.web.support.JsonMessage;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.servlet.ModelAndView;
43
44 @Controller
45 @RequestMapping("/")
46 public class PolicyValidationController extends RestrictedBaseController {
47
48     private static final Logger LOGGER = FlexLogger.getLogger(PolicyValidationController.class);
49
50     /**
51      * validatePolicy.
52      *
53      * @param request HttpServletRequest
54      * @param response HttpServletResponse
55      * @return ModelAndView
56      * @throws IOException IOException
57      */
58     @RequestMapping(
59             value = {"/policyController/validate_policy.htm"},
60             method = {org.springframework.web.bind.annotation.RequestMethod.POST})
61     public ModelAndView validatePolicy(HttpServletRequest request, HttpServletResponse response) throws IOException {
62         try {
63
64             PolicyValidation validation = new PolicyValidation();
65             PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper();
66             StringBuilder responseString;
67             ObjectMapper mapper = new ObjectMapper();
68
69             PolicyRestAdapter policyData = wrapper.populateRequestParameters(request);
70             responseString = validation.validatePolicy(policyData);
71
72             response.getWriter().write(new JSONObject(
73                     new JsonMessage(mapper.writeValueAsString(responseString.toString()))).toString());
74         } catch (Exception e) {
75             LOGGER.error("Exception Occured During Policy Validation" + e);
76             response.setCharacterEncoding("UTF-8");
77             request.setCharacterEncoding("UTF-8");
78             PrintWriter out = response.getWriter();
79             out.write(PolicyUtils.CATCH_EXCEPTION);
80         }
81         return null;
82     }
83 }