* ============LICENSE_START=======================================================
* ONAP Policy Engine
* ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
* Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
* Modifications Copyright (C) 2019 Bell Canada
* ================================================================================
import com.att.research.xacml.util.XACMLProperties;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
-
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
-
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonReader;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
+import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.elasticsearch.common.Strings;
import org.json.JSONArray;
import org.onap.policy.components.HumanPolicyComponent;
import org.onap.policy.controller.PolicyController;
import org.onap.policy.controller.PolicyExportAndImportController;
-import org.onap.policy.rest.XACMLRest;
-import org.onap.policy.rest.XACMLRestProperties;
+import org.onap.policy.rest.XacmlRest;
+import org.onap.policy.rest.XacmlRestProperties;
import org.onap.policy.rest.adapter.PolicyRestAdapter;
import org.onap.policy.rest.jpa.ActionBodyEntity;
import org.onap.policy.rest.jpa.ConfigurationDataEntity;
import org.onap.policy.utils.UserUtils.Pair;
import org.onap.policy.xacml.api.XACMLErrorConstants;
import org.onap.policy.xacml.util.XACMLPolicyScanner;
+import org.onap.portalsdk.core.domain.User;
import org.onap.portalsdk.core.web.support.UserUtils;
@WebServlet(
//
// Common initialization
//
- XACMLRest.xacmlInit(servletConfig);
+ XacmlRest.xacmlInit(servletConfig);
// init aes key from prop or env
- PeCryptoUtils.initAesKey(XACMLProperties.getProperty(XACMLRestProperties.PROP_AES_KEY));
+ PeCryptoUtils.initAesKey(XACMLProperties.getProperty(XacmlRestProperties.PROP_AES_KEY));
//
// Initialize ClosedLoop JSON
//
}
private static void initializeJsonLoad() {
- Path closedLoopJsonLocation = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_ADMIN_CLOSEDLOOP));
+ Path closedLoopJsonLocation = Paths.get(XACMLProperties.getProperty(XacmlRestProperties.PROP_ADMIN_CLOSEDLOOP));
String location = closedLoopJsonLocation.toString();
if (!location.endsWith("json")) {
LOGGER.warn("JSONConfig file does not end with extension .json");
LOGGER.debug("doPost");
try {
// if request contains multipart-form-data
- if (ServletFileUpload.isMultipartContent(request)) {
+ if (isMultipartContent(request)) {
uploadFile(request, response);
}
// all other post request has json params in body
}
}
+ protected boolean isMultipartContent(HttpServletRequest request) {
+ return ServletFileUpload.isMultipartContent(request);
+ }
+
// Set Error Message for Exception
private void setError(Exception exception, HttpServletResponse response) throws IOException {
try {
private void uploadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException {
try {
Map<String, InputStream> files = new HashMap<>();
-
+ String resp = null;
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (!item.isFormField()) {
// Process form file field (input type="file").
files.put(item.getName(), item.getInputStream());
- processFormFile(request, item);
+ resp = processFormFile(request, item, response);
}
}
- JSONObject responseJsonObject;
- responseJsonObject = this.success();
response.setContentType(CONTENTTYPE);
PrintWriter out = response.getWriter();
+ JSONObject responseJsonObject;
+ if (!StringUtils.isBlank(resp)) {
+ responseJsonObject = this.error("Import Issue " + resp);
+ } else {
+ responseJsonObject = this.success();
+ }
out.print(responseJsonObject);
out.flush();
} catch (Exception e) {
}
}
- private void processFormFile(HttpServletRequest request, FileItem item) {
+ private String processFormFile(HttpServletRequest request, FileItem item, HttpServletResponse response) {
String newFile;
- if (item.getName().endsWith(".xls") && item.getSize() <= PolicyController.getFileSizeLimit()) {
+ String outPutResp = null;
+ if (item.getName().endsWith(".xls") && item.getSize() <= getFileSizeLimit()) {
File file = new File(item.getName());
try (OutputStream outputStream = new FileOutputStream(file)) {
IOUtils.copy(item.getInputStream(), outputStream);
newFile = file.toString();
PolicyExportAndImportController importController = new PolicyExportAndImportController();
- importController.importRepositoryFile(newFile, request);
+ return importController.importRepositoryFile(newFile, request);
} catch (Exception e) {
LOGGER.error("Upload error : " + e);
}
} else if (!item.getName().endsWith(".xls")) {
- LOGGER.error("Non .xls filetype uploaded: " + item.getName());
+ outPutResp = "Non .xls filetype uploaded: " + item.getName();
+ LOGGER.error(outPutResp);
} else { // uploaded file size is greater than allowed
- LOGGER.error("Upload file size limit exceeded! File size (Bytes) is: " + item.getSize());
+ outPutResp = "Upload file size limit exceeded! File size (Bytes) is: " + item.getSize();
+ LOGGER.error(outPutResp);
}
+ return outPutResp;
+ }
+
+ protected long copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
+ return IOUtils.copy(inputStream, outputStream);
+ }
+
+ protected long getFileSizeLimit() {
+ return PolicyController.getFileSizeLimit();
}
// File Operation Functionality
JSONObject params = jsonObject.getJSONObject("params");
Mode mode = Mode.valueOf(params.getString("mode"));
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
LOGGER.info(
"********************Logging UserID while doing actions on Editor tab****************************");
LOGGER.info(
setResponse(response, responseJsonObject);
}
+ protected User getUserSession(HttpServletRequest request) {
+ return UserUtils.getUserSession(request);
+ }
+
private void setResponse(HttpServletResponse response, JSONObject responseJsonObject) {
response.setContentType(CONTENTTYPE);
try (PrintWriter out = response.getWriter()) {
private boolean lookupPolicyData(HttpServletRequest request, List<Object> policyData, JSONArray policyList,
PolicyController controller, List<JSONObject> resultList) {
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
List<Object> userRoles = controller.getRoles(userId);
- Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
- List<String> roles = pair.u;
- Set<String> scopes = pair.t;
+ Pair<Set<String>, List<String>> pair = checkRoleAndScope(userRoles);
+ List<String> roles = pair.second;
+ Set<String> scopes = pair.first;
if (roles.contains(ADMIN) || roles.contains(EDITOR) || roles.contains(GUEST)) {
if (scopes.isEmpty()) {
return false;
return true;
}
+ protected Pair<Set<String>, List<String>> checkRoleAndScope(List<Object> userRoles) {
+ return org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+ }
+
private void getPolicyDataForSuperRoles(List<Object> policyData, PolicyController controller,
List<JSONObject> resultList, List<String> roles, Set<String> scopes) {
if (roles.contains(SUPERADMIN) || roles.contains(SUPEREDITOR) || roles.contains(SUPERGUEST)) {
String path = params.getString("path");
String userId = null;
try {
- userId = UserUtils.getUserSession(request).getOrgUserId();
+ userId = getUserSession(request).getOrgUserId();
} catch (Exception e) {
LOGGER.error("Exception Occured while reading userid from cookie" + e);
}
private JSONObject processPolicyList(JSONObject params, HttpServletRequest request) throws ServletException {
// Get the Login Id of the User from Request
String testUserID = getTestUserId();
- String userId = testUserID != null ? testUserID : UserUtils.getUserSession(request).getOrgUserId();
+ String userId = testUserID != null ? testUserID : getUserSession(request).getOrgUserId();
List<Object> userRoles = getPolicyControllerInstance().getRoles(userId);
- Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
- List<String> roles = pair.u;
- Set<String> scopes = pair.t;
+ Pair<Set<String>, List<String>> pair = checkRoleAndScope(userRoles);
+ List<String> roles = pair.second;
+ Set<String> scopes = pair.first;
Map<String, String> roleByScope = org.onap.policy.utils.UserUtils.getRoleByScope(userRoles);
List<JSONObject> resultList = new ArrayList<>();
boolean isActive = false;
List<String> policyActiveInPdp = new ArrayList<>();
Set<String> scopeOfPolicyActiveInPdp = new HashSet<>();
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
String oldPath = params.getString("path");
String newPath = params.getString("newPath");
oldPath = oldPath.substring(oldPath.indexOf('/') + 1);
// Clone the Policy
private JSONObject copy(JSONObject params, HttpServletRequest request) throws ServletException {
try {
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
String oldPath = params.getString("path");
String newPath = params.getString("newPath");
oldPath = oldPath.substring(oldPath.indexOf('/') + 1);
PolicyEntity policyEntity = null;
String policyNamewithoutExtension;
try {
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
String deleteVersion = "";
String path = params.getString("path");
LOGGER.debug("delete {}" + path);
if (entity != null) {
return error("Scope Already Exists");
}
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
UserInfo userInfo = new UserInfo();
userInfo.setUserLoginId(userId);
PolicyEditorScopes newScope = new PolicyEditorScopes();