2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.policy.controller;
25 import java.io.PrintWriter;
26 import java.nio.file.Path;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import org.json.JSONObject;
36 import org.openecomp.policy.dao.PolicyRolesDao;
37 import org.openecomp.policy.rest.jpa.PolicyRoles;
38 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
39 import org.openecomp.portalsdk.core.web.support.JsonMessage;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.MediaType;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.servlet.ModelAndView;
46 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
47 import com.fasterxml.jackson.annotation.PropertyAccessor;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.JsonNode;
50 import com.fasterxml.jackson.databind.ObjectMapper;
54 public class PolicyRolesController extends RestrictedBaseController{
57 PolicyRolesDao policyRolesDao;
58 List<String> scopelist;
60 @RequestMapping(value={"/get_RolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
61 public void getPolicyRolesEntityData(HttpServletRequest request, HttpServletResponse response){
63 Map<String, Object> model = new HashMap<String, Object>();
64 ObjectMapper mapper = new ObjectMapper();
65 model.put("rolesDatas", mapper.writeValueAsString(policyRolesDao.getUserRoles()));
66 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
67 JSONObject j = new JSONObject(msg);
68 response.getWriter().write(j.toString());
75 @RequestMapping(value={"/save_NonSuperRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
76 public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response){
78 ObjectMapper mapper = new ObjectMapper();
79 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
80 JsonNode root = mapper.readTree(request.getReader());
81 PolicyRoles adapter = mapper.readValue(root.get("editRoleData").toString(), PolicyRoles.class);
82 policyRolesDao.update(adapter);
83 response.setCharacterEncoding("UTF-8");
84 response.setContentType("application / json");
85 request.setCharacterEncoding("UTF-8");
87 PrintWriter out = response.getWriter();
88 String responseString = mapper.writeValueAsString(policyRolesDao.getUserRoles());
89 JSONObject j = new JSONObject("{rolesDatas: " + responseString + "}");
91 out.write(j.toString());
99 @RequestMapping(value={"/get_PolicyRolesScopeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
100 public void getPolicyScopesEntityData(HttpServletRequest request, HttpServletResponse response){
102 scopelist = new ArrayList<String>();
103 Map<String, Object> model = new HashMap<String, Object>();
104 ObjectMapper mapper = new ObjectMapper();
105 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
106 Path gitPath = PolicyController.getGitPath().toAbsolutePath();
107 model.put("scopeDatas", mapper.writeValueAsString(readFileRepository(gitPath)));
108 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
109 JSONObject j = new JSONObject(msg);
110 response.getWriter().write(j.toString());
117 public List<String> readFileRepository(Path path){
118 File root = new File(path.toString());
119 for ( File file : root.listFiles()){
120 if(file.isDirectory()){
121 String fileName = getDomain(file);
122 if (!(fileName.contains(".git") || fileName.equals(".DS_Store"))) {
123 scopelist.add(fileName);
125 readFileRepository(file.toPath());
131 public String getDomain(File file) {
132 String filePath = file.getAbsolutePath();
133 int startIndex = filePath.indexOf("repository") + 11;
134 return filePath.substring(startIndex, filePath.length());