2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controller;
23 import com.fasterxml.jackson.databind.ObjectMapper;
25 import java.io.IOException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import org.json.JSONObject;
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
33 import org.onap.policy.rest.adapter.PolicyRestAdapter;
34 import org.onap.policy.rest.util.PolicyValidation;
35 import org.onap.policy.rest.util.PolicyValidationRequestWrapper;
36 import org.onap.policy.utils.PolicyUtils;
37 import org.onap.portalsdk.core.controller.RestrictedBaseController;
38 import org.onap.portalsdk.core.web.support.JsonMessage;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.servlet.ModelAndView;
45 public class PolicyValidationController extends RestrictedBaseController {
47 private static final Logger LOGGER = FlexLogger.getLogger(PolicyValidationController.class);
52 * @param request HttpServletRequest
53 * @param response HttpServletResponse
54 * @return ModelAndView
55 * @throws IOException IOException
58 value = {"/policyController/validate_policy.htm"},
59 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
60 public ModelAndView validatePolicy(HttpServletRequest request, HttpServletResponse response) throws IOException {
63 PolicyValidation validation = new PolicyValidation();
64 PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper();
65 StringBuilder responseString;
66 ObjectMapper mapper = new ObjectMapper();
68 PolicyRestAdapter policyData = wrapper.populateRequestParameters(request);
69 responseString = validation.validatePolicy(policyData);
71 response.getWriter().write(new JSONObject(
72 new JsonMessage(mapper.writeValueAsString(responseString.toString()))).toString());
73 } catch (Exception e) {
74 LOGGER.error("Exception Occured During Policy Validation" + e);
75 response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
76 request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
77 response.getWriter().write(PolicyUtils.CATCH_EXCEPTION);