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.components;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.util.ArrayList;
35 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
36 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
37 import org.apache.commons.io.IOUtils;
38 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
39 import org.openecomp.policy.common.logging.flexlogger.Logger;
40 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
44 public class PolicyImportWindow{
46 private static final Logger LOGGER = FlexLogger.getLogger(PolicyImportWindow.class);
47 private Path newfile = null;
48 private boolean succeeded = false;
49 private Boolean superadmin = false;
50 private ArrayList<String> xacmlFiles = new ArrayList<String>();
51 private static FileOutputStream outputFile;
53 * The constructor should first build the main layout, set the
54 * composition root and then do any custom initialization.
56 * The constructor will not be automatically regenerated by the
60 public OutputStream receiveUpload(String filename, String mimeType) {
63 // Create its new full path
67 // Does it already exist?
69 if (Files.exists(this.newfile)) {
73 // Try to create the output stream
76 return new FileOutputStream(this.newfile.toFile());
77 } catch (FileNotFoundException e) {
78 LOGGER.error("Failed to create uploaded file", e);
84 TarArchiveEntry entry = null;
85 TarArchiveInputStream extractFile = null;
87 extractFile = new TarArchiveInputStream (new FileInputStream(this.newfile.toFile()));
88 } catch (FileNotFoundException e1) {
89 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception while Importing Polcies"+e1);
91 //Create a loop to read every single entry in TAR file
93 while (extractFile!=null && (entry = extractFile.getNextTarEntry()) != null) {
94 this.superadmin = true;
96 copyFileToLocation(extractFile, entry, xacmlFiles, null, superadmin);
98 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Exception while Importing Polcies"+e);
101 } catch (IOException e) {
102 LOGGER.error("Exception Occured"+e);
105 if(extractFile != null){
108 } catch (IOException e) {
109 LOGGER.error("Exception Occured"+e);
115 //Copy files to Directorys
116 public static void copyFileToLocation(TarArchiveInputStream extractFile, TarArchiveEntry entry, ArrayList<String> xacmlFiles, Set<String> finalScopes, Boolean superadminValue ) throws IOException{
117 String individualFiles = "";
120 // Get the name of the file
122 individualFiles = entry.getName();
124 for(int i =0; i< finalScopes.size(); i++){
125 if(entry.getName().startsWith(finalScopes.toArray()[i].toString())){
126 individualFiles = entry.getName();
131 if(individualFiles.endsWith(".xls")){
132 if(individualFiles.contains("\\")){
133 individualFiles = individualFiles.replace("\\", File.separator);
134 }else if(individualFiles.contains("/")){
135 individualFiles = individualFiles.replace("/", File.separator);
140 individualFiles = individualFiles.replace("/", File.separator);
141 individualFiles = individualFiles.replace("\\", File.separator);
143 //Create the path with the entry name
147 // Get Size of the file and create a byte array for the size
148 byte[] content = new byte[(int) entry.getSize()];
151 LOGGER.info("Size of the File is: " + entry.getSize());
152 // Read file from the archive into byte array
153 extractFile.read(content, offset, content.length - offset);
155 // Use IOUtiles to write content of byte array to physical file
156 IOUtils.write(content,outputFile);
158 // Close Output Stream
160 if(outputFile != null){
163 } catch (IOException e) {
164 LOGGER.info("IOException:" +e);
165 LOGGER.error("Exception Occured"+e);
170 public Path getUploadedFile() {
171 if (this.succeeded) {