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) {
91 //Create a loop to read every single entry in TAR file
93 while ((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) {
107 //Copy files to Directorys
108 public static void copyFileToLocation(TarArchiveInputStream extractFile, TarArchiveEntry entry, ArrayList<String> xacmlFiles, Set<String> finalScopes, Boolean superadminValue ) throws IOException{
109 String individualFiles = "";
112 // Get the name of the file
114 individualFiles = entry.getName();
116 for(int i =0; i< finalScopes.size(); i++){
117 if(entry.getName().startsWith(finalScopes.toArray()[i].toString())){
118 individualFiles = entry.getName();
123 if(individualFiles.endsWith(".xls")){
124 if(individualFiles.contains("\\")){
125 individualFiles = individualFiles.replace("\\", File.separator);
126 }else if(individualFiles.contains("/")){
127 individualFiles = individualFiles.replace("/", File.separator);
132 individualFiles = individualFiles.replace("/", File.separator);
133 individualFiles = individualFiles.replace("\\", File.separator);
135 //Create the path with the entry name
139 // Get Size of the file and create a byte array for the size
140 byte[] content = new byte[(int) entry.getSize()];
143 LOGGER.info("Size of the File is: " + entry.getSize());
144 // Read file from the archive into byte array
145 extractFile.read(content, offset, content.length - offset);
147 // Use IOUtiles to write content of byte array to physical file
148 IOUtils.write(content,outputFile);
150 // Close Output Stream
153 } catch (IOException e) {
154 LOGGER.info("IOException:" +e);
160 public Path getUploadedFile() {
161 if (this.succeeded) {