Sonar cleanup in controllers etc
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / ExportAndImportDecisionBlackListEntries.java
index 8a37e9d..b50ca6d 100644 (file)
@@ -2,14 +2,14 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Engine
  * ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,6 +21,7 @@
 package org.onap.policy.controller;
 
 import com.google.gson.Gson;
+
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -38,8 +39,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
+
 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.FileUploadException;
@@ -65,8 +68,6 @@ import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
-
-
 /**
  * This class is used to import and export the black list entries which were used in the Decision Blacklist Guard YAML
  * Policy.
@@ -84,7 +85,7 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
     /**
      * This method is used to Export the Black List entries data from Decision BlackList Guard YAML Policy. So, user can
      * update the file on adding or removing the entries, for updating the policies or using in other Environments.
-     * 
+     *
      * @param request the request contains the policy data. So, based on that we can populate and read and write the
      *        entries.
      * @param response after reading and writing the blacklist list entries to file, the file is copied to tmp directory
@@ -102,8 +103,8 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
             List<String> blackLists = adapter.getYamlparams().getBlackList();
             HSSFSheet sheet = workBook.createSheet("BlackList");
             HSSFRow headingRow = sheet.createRow(0);
-            headingRow.createCell(0).setCellValue("Action");
-            headingRow.createCell(1).setCellValue("BlackListEntry");
+            headingRow.createCell(0).setCellValue(ACTION);
+            headingRow.createCell(1).setCellValue(BLACKLISTENTRY);
 
             short rowNo = 1;
             for (Object object : blackLists) {
@@ -117,7 +118,7 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
 
             /*
              * Export FileName is the combination of BlacList+Scope+PolicyName+Version+PolicyCreatedDate.
-             * 
+             *
              */
 
             SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@@ -156,46 +157,46 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
             out.write(jsonResposne.toString());
         } catch (Exception e) {
             policyLogger.error(
-                    XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Exception Occured while Exporting BlackList Entries" , e);
+                    XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Exception Occured while Exporting BlackList Entries", e);
         }
     }
 
     /**
      * This method is used to import the BlackList excel file into the system. Which is used to create Decision
      * Blacklist Guard YAML Policy.
-     * 
+     *
      * @param request the HTTP request contains file upload stream form GUI.
      * @param response the response is send to the GUI after reading the file input stream.
-     * @throws FileUploadException throws fileUpload Exception.
-     * @throws IOException throws IO Exceptions.
      */
     @RequestMapping(value = {"/policycreation/importBlackListForDecisionPolicy"}, method = {RequestMethod.POST})
-    public void importBlackListFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
-        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
-        List<String> errorLogs = new ArrayList<>();
-        Gson mapper = new Gson();
-        errorLogs.add("error");
-        Map<String, Object> model = new HashMap<>();
-        if (items.isEmpty()) {
-            errorLogs.add("The File doesn't have any content and it is invalid.");
-            model.put(BLACKLISTENTRIESDATA, errorLogs);
-        } else {
-            readItems(items, errorLogs, model);
+    public void importBlackListFile(HttpServletRequest request, HttpServletResponse response) {
+        try {
+            List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
+            List<String> errorLogs = new ArrayList<>();
+            Gson mapper = new Gson();
+            errorLogs.add("error");
+            Map<String, Object> model = new HashMap<>();
+            if (items.isEmpty()) {
+                errorLogs.add("The File doesn't have any content and it is invalid.");
+                model.put(BLACKLISTENTRIESDATA, errorLogs);
+            } else {
+                readItems(items, errorLogs, model);
+            }
+            response.getWriter().write(new JSONObject(new JsonMessage(mapper.toJson(model))).toString());
+        } catch (FileUploadException | IOException e) {
+            policyLogger.error("Exception Occured while importing the BlackListEntry", e);
         }
-        JsonMessage msg = new JsonMessage(mapper.toJson(model));
-        JSONObject jsonResposne = new JSONObject(msg);
-        response.getWriter().write(jsonResposne.toString());
     }
 
     /**
      * This method is used to read the first item, as we expect only one entry in the file upload.
-     * 
+     *
      * @param items The file entries which were uploaded from GUI.
      * @param errorLogs on adding all incorrect entries, we can let user know what need to fixed.
      * @param model Map which stores key value (blacklist and append list data)
      * @throws Exception throws exception if it is not .xls format
      */
-    private void readItems(List<FileItem> items, List<String> errorLogs, Map<String, Object> model) throws Exception {
+    private void readItems(List<FileItem> items, List<String> errorLogs, Map<String, Object> model) throws IOException {
         Map<String, InputStream> files = new HashMap<>();
 
         FileItem item = items.get(0);
@@ -216,7 +217,7 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
 
     /**
      * This method is used to read the workbook in xls file item.
-     * 
+     *
      * @param fileName fileName as input parameter
      * @param errorLogs on adding all incorrect entries, we can let user know what need to fixed.
      * @param model Map which stores key value (blacklist and append list data)
@@ -238,13 +239,13 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
             String error = "Error Occured While Reading File. Please check the format of the file.";
             errorLogs.add(error);
             model.put(BLACKLISTENTRIESDATA, errorLogs);
-            policyLogger.error(error , e);
+            policyLogger.error(error, e);
         }
     }
 
     /**
      * This method is used to read all the rows from imported Excel sheet and set to respective objects.
-     * 
+     *
      * @param rowIterator Excel Sheet rows are passed as input parameters.
      * @param blackListEntries the data is set to this object, which is going to be added.
      * @param appendBlackListEntries the data is set to this object which is going to be removed.
@@ -264,7 +265,7 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
 
     /**
      * This method is used to read all the cells in the row.
-     * 
+     *
      * @param cellIterator iterating the cells and will parse based on the cell type.
      * @param blackListEntries the data is set to this object, which is going to be added.
      * @param appendBlackListEntries the data is set to this object which is going to be removed.
@@ -288,7 +289,6 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
                 ReturnBlackList returnList = readBlackListCell(cell, lineNo, errorLogs);
                 blEntry = returnList.getEntryValue();
                 blackListCheck = returnList.isEntryCheck();
-                actionEntry = returnList.getActionValue();
             }
             lineNo++;
         }
@@ -299,7 +299,7 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
 
     /**
      * This method is used to read the Action cell entry.
-     * 
+     *
      * @param cell reading the action entry cell.
      * @param lineNo counts the number of the cell.
      * @param errorLogs on adding all incorrect entries, we can let user know what need to fixed.
@@ -326,9 +326,8 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
     }
 
     /**
-     * 
      * This method is used to read the BlackList cell entry.
-     * 
+     *
      * @param cell reading the blacklist entry cell.
      * @param lineNo counts the number of the cell.
      * @param errorLogs on adding all incorrect entries, we can let user know what need to fixed.
@@ -353,7 +352,7 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
 
     /**
      * This method is used to add the data to blacklist and append list after parsing each and every row.
-     * 
+     *
      * @param actionEntry it has the input to add or not and holds either 0 or 1.
      * @param blackListEntries list to add blacklist entries based on action entry = 1.
      * @param appendBlackListEntries list to add append list entries based on action entry = 0.
@@ -370,7 +369,7 @@ public class ExportAndImportDecisionBlackListEntries extends RestrictedBaseContr
 
     /**
      * This method is used to identify the header of the cell.
-     * 
+     *
      * @param cell Excel sheet cell is passed as input parameter.
      * @return the column header name value
      */