diff options
Diffstat (limited to 'src/main/java/org/onap/sdc/toscaparser/api/prereq/CSAR.java')
-rw-r--r-- | src/main/java/org/onap/sdc/toscaparser/api/prereq/CSAR.java | 765 |
1 files changed, 375 insertions, 390 deletions
diff --git a/src/main/java/org/onap/sdc/toscaparser/api/prereq/CSAR.java b/src/main/java/org/onap/sdc/toscaparser/api/prereq/CSAR.java index 92d5194..4ada267 100644 --- a/src/main/java/org/onap/sdc/toscaparser/api/prereq/CSAR.java +++ b/src/main/java/org/onap/sdc/toscaparser/api/prereq/CSAR.java @@ -7,9 +7,9 @@ * 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. @@ -50,204 +50,199 @@ import org.yaml.snakeyaml.Yaml; public class CSAR { - private static Logger log = LoggerFactory.getLogger(CSAR.class.getName()); - private static final ArrayList<String> META_PROPERTIES_FILES = new ArrayList<>(Arrays.asList("TOSCA-Metadata/TOSCA.meta", "csar.meta")); + private static Logger log = LoggerFactory.getLogger(CSAR.class.getName()); + private static final ArrayList<String> META_PROPERTIES_FILES = new ArrayList<>(Arrays.asList("TOSCA-Metadata/TOSCA.meta", "csar.meta")); - private String path; + private String path; private boolean isFile; private boolean isValidated; private boolean errorCaught; private String csar; private String tempDir; -// private Metadata metaData; + // private Metadata metaData; private File tempFile; - private LinkedHashMap<String, LinkedHashMap<String, Object>> metaProperties; + private LinkedHashMap<String, LinkedHashMap<String, Object>> metaProperties; - public CSAR(String csarPath, boolean aFile) { - path = csarPath; - isFile = aFile; + public CSAR(String csarPath, boolean aFile) { + path = csarPath; + isFile = aFile; isValidated = false; errorCaught = false; csar = null; tempDir = null; tempFile = null; - metaProperties = new LinkedHashMap<>(); - } + metaProperties = new LinkedHashMap<>(); + } + + public boolean validate() throws JToscaException { + isValidated = true; - public boolean validate() throws JToscaException { - isValidated = true; - //validate that the file or URL exists - - if(isFile) { - File f = new File(path); - if (!f.isFile()) { - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE220", String.format("\"%s\" is not a file", path))); - return false; - } - else { - this.csar = path; - } - } - else { - if(!UrlUtils.validateUrl(path)) { - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE221", String.format("ImportError: \"%s\" does not exist",path))); - return false; - } - // get it to a local file - try { - File tempFile = File.createTempFile("csartmp",".csar"); - Path ptf = Paths.get(tempFile.getPath()); - URL webfile = new URL(path); - InputStream in = webfile.openStream(); - Files.copy(in,ptf,StandardCopyOption.REPLACE_EXISTING); - } - catch(Exception e) { - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE222", "ImportError: failed to load CSAR from " + path)); - return false; - } - - log.debug("CSAR - validate - currently only files are supported"); - return false; - } - - _parseAndValidateMetaProperties(); - - if(errorCaught) { - return false; - } - + + if (isFile) { + File f = new File(path); + if (!f.isFile()) { + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE220", String.format("\"%s\" is not a file", path))); + return false; + } else { + this.csar = path; + } + } else { + if (!UrlUtils.validateUrl(path)) { + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE221", String.format("ImportError: \"%s\" does not exist", path))); + return false; + } + // get it to a local file + try { + File tempFile = File.createTempFile("csartmp", ".csar"); + Path ptf = Paths.get(tempFile.getPath()); + URL webfile = new URL(path); + InputStream in = webfile.openStream(); + Files.copy(in, ptf, StandardCopyOption.REPLACE_EXISTING); + } catch (Exception e) { + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE222", "ImportError: failed to load CSAR from " + path)); + return false; + } + + log.debug("CSAR - validate - currently only files are supported"); + return false; + } + + _parseAndValidateMetaProperties(); + + if (errorCaught) { + return false; + } + // validate that external references in the main template actually exist and are accessible _validateExternalReferences(); - + return !errorCaught; - } - - private void _parseAndValidateMetaProperties() throws JToscaException { - - ZipFile zf = null; - - try { - - // validate that it is a valid zip file - RandomAccessFile raf = new RandomAccessFile(csar, "r"); - long n = raf.readInt(); - raf.close(); - // check if Zip's magic number - if (n != 0x504B0304) { - String errorString = String.format("\"%s\" is not a valid zip file", csar); - log.error(errorString); - throw new JToscaException(errorString , JToscaErrorCodes.INVALID_CSAR_FORMAT.getValue()); - } - - // validate that it contains the metadata file in the correct location - zf = new ZipFile(csar); - ZipEntry ze = zf.getEntry("TOSCA-Metadata/TOSCA.meta"); - if (ze == null) { - - String errorString = String.format( - "\"%s\" is not a valid CSAR as it does not contain the " + - "required file \"TOSCA.meta\" in the folder \"TOSCA-Metadata\"", csar); - log.error(errorString); - throw new JToscaException(errorString, JToscaErrorCodes.MISSING_META_FILE.getValue()); - } - - //Going over expected metadata files and parsing them - for (String metaFile: META_PROPERTIES_FILES) { - - byte ba[] = new byte[4096]; - ze = zf.getEntry(metaFile); - if (ze != null) { - InputStream inputStream = zf.getInputStream(ze); - n = inputStream.read(ba, 0, 4096); - String md = new String(ba); - md = md.substring(0, (int) n); - - String errorString = String.format( - "The file \"%s\" in the" + - " CSAR \"%s\" does not contain valid YAML content", ze.getName(), csar); - - try { - Yaml yaml = new Yaml(); - Object mdo = yaml.load(md); - if (!(mdo instanceof LinkedHashMap)) { - log.error(errorString); - throw new JToscaException(errorString, JToscaErrorCodes.INVALID_META_YAML_CONTENT.getValue()); - } - - String[] split = ze.getName().split("/"); - String fileName = split[split.length - 1]; - - if (!metaProperties.containsKey(fileName)) { - metaProperties.put(fileName, (LinkedHashMap<String, Object>) mdo); - } - } - catch(Exception e) { - log.error(errorString); - throw new JToscaException(errorString, JToscaErrorCodes.INVALID_META_YAML_CONTENT.getValue()); - } - } - } - - // verify it has "Entry-Definition" - String edf = _getMetadata("Entry-Definitions"); - if (edf == null) { - String errorString = String.format( - "The CSAR \"%s\" is missing the required metadata " + - "\"Entry-Definitions\" in \"TOSCA-Metadata/TOSCA.meta\"", csar); - log.error(errorString); - throw new JToscaException(errorString, JToscaErrorCodes.ENTRY_DEFINITION_NOT_DEFINED.getValue()); - } - - //validate that "Entry-Definitions' metadata value points to an existing file in the CSAR - boolean foundEDF = false; - Enumeration<? extends ZipEntry> entries = zf.entries(); - while (entries.hasMoreElements()) { - ze = entries.nextElement(); - if (ze.getName().equals(edf)) { - foundEDF = true; - break; - } - } - if (!foundEDF) { - String errorString = String.format( - "The \"Entry-Definitions\" file defined in the CSAR \"%s\" does not exist", csar); - log.error(errorString); - throw new JToscaException(errorString, JToscaErrorCodes.MISSING_ENTRY_DEFINITION_FILE.getValue()); - } - } catch (JToscaException e) { - //ThreadLocalsHolder.getCollector().appendCriticalException(e.getMessage()); - throw e; - } catch (Exception e) { - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE223", "ValidationError: " + e.getMessage())); - errorCaught = true; - } - - try { - if (zf != null) { - zf.close(); - } - } catch (IOException e) { - } - } - - public void cleanup() { - try { - if(tempFile != null) { - tempFile.delete(); - } - } - catch(Exception e) { - } - } - + } + + private void _parseAndValidateMetaProperties() throws JToscaException { + + ZipFile zf = null; + + try { + + // validate that it is a valid zip file + RandomAccessFile raf = new RandomAccessFile(csar, "r"); + long n = raf.readInt(); + raf.close(); + // check if Zip's magic number + if (n != 0x504B0304) { + String errorString = String.format("\"%s\" is not a valid zip file", csar); + log.error(errorString); + throw new JToscaException(errorString, JToscaErrorCodes.INVALID_CSAR_FORMAT.getValue()); + } + + // validate that it contains the metadata file in the correct location + zf = new ZipFile(csar); + ZipEntry ze = zf.getEntry("TOSCA-Metadata/TOSCA.meta"); + if (ze == null) { + + String errorString = String.format( + "\"%s\" is not a valid CSAR as it does not contain the " + + "required file \"TOSCA.meta\" in the folder \"TOSCA-Metadata\"", csar); + log.error(errorString); + throw new JToscaException(errorString, JToscaErrorCodes.MISSING_META_FILE.getValue()); + } + + //Going over expected metadata files and parsing them + for (String metaFile : META_PROPERTIES_FILES) { + + byte ba[] = new byte[4096]; + ze = zf.getEntry(metaFile); + if (ze != null) { + InputStream inputStream = zf.getInputStream(ze); + n = inputStream.read(ba, 0, 4096); + String md = new String(ba); + md = md.substring(0, (int) n); + + String errorString = String.format( + "The file \"%s\" in the" + + " CSAR \"%s\" does not contain valid YAML content", ze.getName(), csar); + + try { + Yaml yaml = new Yaml(); + Object mdo = yaml.load(md); + if (!(mdo instanceof LinkedHashMap)) { + log.error(errorString); + throw new JToscaException(errorString, JToscaErrorCodes.INVALID_META_YAML_CONTENT.getValue()); + } + + String[] split = ze.getName().split("/"); + String fileName = split[split.length - 1]; + + if (!metaProperties.containsKey(fileName)) { + metaProperties.put(fileName, (LinkedHashMap<String, Object>) mdo); + } + } catch (Exception e) { + log.error(errorString); + throw new JToscaException(errorString, JToscaErrorCodes.INVALID_META_YAML_CONTENT.getValue()); + } + } + } + + // verify it has "Entry-Definition" + String edf = _getMetadata("Entry-Definitions"); + if (edf == null) { + String errorString = String.format( + "The CSAR \"%s\" is missing the required metadata " + + "\"Entry-Definitions\" in \"TOSCA-Metadata/TOSCA.meta\"", csar); + log.error(errorString); + throw new JToscaException(errorString, JToscaErrorCodes.ENTRY_DEFINITION_NOT_DEFINED.getValue()); + } + + //validate that "Entry-Definitions' metadata value points to an existing file in the CSAR + boolean foundEDF = false; + Enumeration<? extends ZipEntry> entries = zf.entries(); + while (entries.hasMoreElements()) { + ze = entries.nextElement(); + if (ze.getName().equals(edf)) { + foundEDF = true; + break; + } + } + if (!foundEDF) { + String errorString = String.format( + "The \"Entry-Definitions\" file defined in the CSAR \"%s\" does not exist", csar); + log.error(errorString); + throw new JToscaException(errorString, JToscaErrorCodes.MISSING_ENTRY_DEFINITION_FILE.getValue()); + } + } catch (JToscaException e) { + //ThreadLocalsHolder.getCollector().appendCriticalException(e.getMessage()); + throw e; + } catch (Exception e) { + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE223", "ValidationError: " + e.getMessage())); + errorCaught = true; + } + + try { + if (zf != null) { + zf.close(); + } + } catch (IOException e) { + } + } + + public void cleanup() { + try { + if (tempFile != null) { + tempFile.delete(); + } + } catch (Exception e) { + } + } + private String _getMetadata(String key) throws JToscaException { - if(!isValidated) { - validate(); - } - Object value = _getMetaProperty("TOSCA.meta").get(key); - return value != null ? value.toString() : null; + if (!isValidated) { + validate(); + } + Object value = _getMetaProperty("TOSCA.meta").get(key); + return value != null ? value.toString() : null; } public String getAuthor() throws JToscaException { @@ -258,276 +253,266 @@ public class CSAR { return _getMetadata("CSAR-Version"); } - public LinkedHashMap<String, LinkedHashMap<String, Object>> getMetaProperties() { - return metaProperties; - } - - private LinkedHashMap<String, Object> _getMetaProperty(String propertiesFile) { - return metaProperties.get(propertiesFile); - } - - public String getMainTemplate() throws JToscaException { - String entryDef = _getMetadata("Entry-Definitions"); - ZipFile zf; - boolean ok = false; - try { - zf = new ZipFile(path); - ok = (zf.getEntry(entryDef) != null); - zf.close(); - } - catch(IOException e) { - if(!ok) { - log.error("CSAR - getMainTemplate - failed to open {}", path); - } - } - if(ok) { - return entryDef; - } - else { - return null; - } + public LinkedHashMap<String, LinkedHashMap<String, Object>> getMetaProperties() { + return metaProperties; + } + + private LinkedHashMap<String, Object> _getMetaProperty(String propertiesFile) { + return metaProperties.get(propertiesFile); + } + + public String getMainTemplate() throws JToscaException { + String entryDef = _getMetadata("Entry-Definitions"); + ZipFile zf; + boolean ok = false; + try { + zf = new ZipFile(path); + ok = (zf.getEntry(entryDef) != null); + zf.close(); + } catch (IOException e) { + if (!ok) { + log.error("CSAR - getMainTemplate - failed to open {}", path); + } + } + if (ok) { + return entryDef; + } else { + return null; + } } - @SuppressWarnings("unchecked") - public LinkedHashMap<String,Object> getMainTemplateYaml() throws JToscaException { - String mainTemplate = tempDir + File.separator + getMainTemplate(); - if(mainTemplate != null) { - try (InputStream input = new FileInputStream(new File(mainTemplate));){ - Yaml yaml = new Yaml(); - Object data = yaml.load(input); - if(!(data instanceof LinkedHashMap)) { - throw new IOException(); - } - return (LinkedHashMap<String,Object>)data; - } - catch(Exception e) { - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE224", String.format( - "The file \"%s\" in the CSAR \"%s\" does not " + - "contain valid TOSCA YAML content", - mainTemplate,csar))); - } - } - return null; + @SuppressWarnings("unchecked") + public LinkedHashMap<String, Object> getMainTemplateYaml() throws JToscaException { + String mainTemplate = tempDir + File.separator + getMainTemplate(); + if (mainTemplate != null) { + try (InputStream input = new FileInputStream(new File(mainTemplate));) { + Yaml yaml = new Yaml(); + Object data = yaml.load(input); + if (!(data instanceof LinkedHashMap)) { + throw new IOException(); + } + return (LinkedHashMap<String, Object>) data; + } catch (Exception e) { + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE224", String.format( + "The file \"%s\" in the CSAR \"%s\" does not " + + "contain valid TOSCA YAML content", + mainTemplate, csar))); + } + } + return null; } - + public String getDescription() throws JToscaException { String desc = _getMetadata("Description"); - if(desc != null) { + if (desc != null) { return desc; } - Map<String, Object> metaData = metaProperties.get("TOSCA.meta"); - metaData.put("Description", getMainTemplateYaml().get("description")); - return _getMetadata("Description"); + Map<String, Object> metaData = metaProperties.get("TOSCA.meta"); + metaData.put("Description", getMainTemplateYaml().get("description")); + return _getMetadata("Description"); } public String getTempDir() { - return tempDir; + return tempDir; } - + public void decompress() throws IOException, JToscaException { - if(!isValidated) { + if (!isValidated) { validate(); } - - if(tempDir == null || tempDir.isEmpty()) { - tempDir = Files.createTempDirectory("JTP").toString(); - unzip(path,tempDir); + + if (tempDir == null || tempDir.isEmpty()) { + tempDir = Files.createTempDirectory("JTP").toString(); + unzip(path, tempDir); } } - - private void _validateExternalReferences() throws JToscaException { + + private void _validateExternalReferences() throws JToscaException { // Extracts files referenced in the main template - // These references are currently supported: + // These references are currently supported: // * imports // * interface implementations // * artifacts try { decompress(); String mainTplFile = getMainTemplate(); - if(mainTplFile == null) { + if (mainTplFile == null) { return; } - - LinkedHashMap<String,Object> mainTpl = getMainTemplateYaml(); - if(mainTpl.get("imports") != null) { - // this loads the imports - ImportsLoader il = new ImportsLoader((ArrayList<Object>)mainTpl.get("imports"), - tempDir + File.separator + mainTplFile, - (Object)null, - (LinkedHashMap<String,Object>)null); + + LinkedHashMap<String, Object> mainTpl = getMainTemplateYaml(); + if (mainTpl.get("imports") != null) { + // this loads the imports + ImportsLoader il = new ImportsLoader((ArrayList<Object>) mainTpl.get("imports"), + tempDir + File.separator + mainTplFile, + (Object) null, + (LinkedHashMap<String, Object>) null); } - - if(mainTpl.get("topology_template") != null) { - LinkedHashMap<String,Object> topologyTemplate = - (LinkedHashMap<String,Object>)mainTpl.get("topology_template"); - - if(topologyTemplate.get("node_templates") != null) { - LinkedHashMap<String,Object> nodeTemplates = - (LinkedHashMap<String,Object>)topologyTemplate.get("node_templates"); - for(String nodeTemplateKey: nodeTemplates.keySet()) { - LinkedHashMap<String,Object> nodeTemplate = - (LinkedHashMap<String,Object>)nodeTemplates.get(nodeTemplateKey); - if(nodeTemplate.get("artifacts") != null) { - LinkedHashMap<String,Object> artifacts = - (LinkedHashMap<String,Object>)nodeTemplate.get("artifacts"); - for(String artifactKey: artifacts.keySet()) { - Object artifact = artifacts.get(artifactKey); - if(artifact instanceof String) { - _validateExternalReference(mainTplFile,(String)artifact,true); - } - else if(artifact instanceof LinkedHashMap) { - String file = (String)((LinkedHashMap<String,Object>)artifact).get("file"); - if(file != null) { - _validateExternalReference(mainTplFile,file,true); - } - } - else { + + if (mainTpl.get("topology_template") != null) { + LinkedHashMap<String, Object> topologyTemplate = + (LinkedHashMap<String, Object>) mainTpl.get("topology_template"); + + if (topologyTemplate.get("node_templates") != null) { + LinkedHashMap<String, Object> nodeTemplates = + (LinkedHashMap<String, Object>) topologyTemplate.get("node_templates"); + for (String nodeTemplateKey : nodeTemplates.keySet()) { + LinkedHashMap<String, Object> nodeTemplate = + (LinkedHashMap<String, Object>) nodeTemplates.get(nodeTemplateKey); + if (nodeTemplate.get("artifacts") != null) { + LinkedHashMap<String, Object> artifacts = + (LinkedHashMap<String, Object>) nodeTemplate.get("artifacts"); + for (String artifactKey : artifacts.keySet()) { + Object artifact = artifacts.get(artifactKey); + if (artifact instanceof String) { + _validateExternalReference(mainTplFile, (String) artifact, true); + } else if (artifact instanceof LinkedHashMap) { + String file = (String) ((LinkedHashMap<String, Object>) artifact).get("file"); + if (file != null) { + _validateExternalReference(mainTplFile, file, true); + } + } else { ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE225", String.format( - "ValueError: Unexpected artifact definition for \"%s\"", - artifactKey))); - errorCaught = true; - } - } - } - if(nodeTemplate.get("interfaces") != null) { - LinkedHashMap<String,Object> interfaces = - (LinkedHashMap<String,Object>)nodeTemplate.get("interfaces"); - for(String interfaceKey: interfaces.keySet()) { - LinkedHashMap<String,Object> _interface = - (LinkedHashMap<String,Object>)interfaces.get(interfaceKey); - for(String operationKey: _interface.keySet()) { - Object operation = _interface.get(operationKey); - if(operation instanceof String) { - _validateExternalReference(mainTplFile,(String)operation,false); - } - else if(operation instanceof LinkedHashMap) { - String imp = (String)((LinkedHashMap<String,Object>)operation).get("implementation"); - if(imp != null) { - _validateExternalReference(mainTplFile,imp,true); - } - } - } - } - } - } - } + "ValueError: Unexpected artifact definition for \"%s\"", + artifactKey))); + errorCaught = true; + } + } + } + if (nodeTemplate.get("interfaces") != null) { + LinkedHashMap<String, Object> interfaces = + (LinkedHashMap<String, Object>) nodeTemplate.get("interfaces"); + for (String interfaceKey : interfaces.keySet()) { + LinkedHashMap<String, Object> _interface = + (LinkedHashMap<String, Object>) interfaces.get(interfaceKey); + for (String operationKey : _interface.keySet()) { + Object operation = _interface.get(operationKey); + if (operation instanceof String) { + _validateExternalReference(mainTplFile, (String) operation, false); + } else if (operation instanceof LinkedHashMap) { + String imp = (String) ((LinkedHashMap<String, Object>) operation).get("implementation"); + if (imp != null) { + _validateExternalReference(mainTplFile, imp, true); + } + } + } + } + } + } + } } + } catch (IOException e) { + errorCaught = true; + } finally { + // delete tempDir (only here?!?) + File fdir = new File(tempDir); + deleteDir(fdir); + tempDir = null; } - catch(IOException e) { - errorCaught = true; - } - finally { - // delete tempDir (only here?!?) - File fdir = new File(tempDir); - deleteDir(fdir); - tempDir = null; + } + + public static void deleteDir(File fdir) { + try { + if (fdir.isDirectory()) { + for (File c : fdir.listFiles()) + deleteDir(c); + } + fdir.delete(); + } catch (Exception e) { } - } - - public static void deleteDir(File fdir) { - try { - if (fdir.isDirectory()) { - for (File c : fdir.listFiles()) - deleteDir(c); - } - fdir.delete(); - } - catch(Exception e) { - } - } - - private void _validateExternalReference(String tplFile,String resourceFile,boolean raiseExc) { + } + + private void _validateExternalReference(String tplFile, String resourceFile, boolean raiseExc) { // Verify that the external resource exists // If resource_file is a URL verify that the URL is valid. // If resource_file is a relative path verify that the path is valid // considering base folder (self.temp_dir) and tpl_file. // Note that in a CSAR resource_file cannot be an absolute path. - if(UrlUtils.validateUrl(resourceFile)) { - String msg = String.format("URLException: The resource at \"%s\" cannot be accessed",resourceFile); + if (UrlUtils.validateUrl(resourceFile)) { + String msg = String.format("URLException: The resource at \"%s\" cannot be accessed", resourceFile); try { - if(UrlUtils.isUrlAccessible(resourceFile)) { + if (UrlUtils.isUrlAccessible(resourceFile)) { return; - } - else { - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE226", msg)); + } else { + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE226", msg)); errorCaught = true; } - } - catch (Exception e) { - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE227", msg)); + } catch (Exception e) { + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE227", msg)); } } - String dirPath = Paths.get(tplFile).getParent().toString(); - String filePath = tempDir + File.separator + dirPath + File.separator + resourceFile; - File f = new File(filePath); - if(f.isFile()) { - return; - } - - if(raiseExc) { - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE228", String.format( - "ValueError: The resource \"%s\" does not exist",resourceFile))); - } - errorCaught = true; - } - + String dirPath = Paths.get(tplFile).getParent().toString(); + String filePath = tempDir + File.separator + dirPath + File.separator + resourceFile; + File f = new File(filePath); + if (f.isFile()) { + return; + } + + if (raiseExc) { + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE228", String.format( + "ValueError: The resource \"%s\" does not exist", resourceFile))); + } + errorCaught = true; + } + private void unzip(String zipFilePath, String destDirectory) throws IOException { File destDir = new File(destDirectory); if (!destDir.exists()) { destDir.mkdir(); } - try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));){ - ZipEntry entry = zipIn.getNextEntry(); - // iterates over entries in the zip file - while (entry != null) { - // create all directories needed for nested items - String[] parts = entry.getName().split("/"); - String s = destDirectory + File.separator ; - for(int i=0; i< parts.length-1; i++) { - s += parts[i]; - File idir = new File(s); - if(!idir.exists()) { - idir.mkdir(); - } - s += File.separator; - } - String filePath = destDirectory + File.separator + entry.getName(); - if (!entry.isDirectory()) { - // if the entry is a file, extracts it - extractFile(zipIn, filePath); - } else { - // if the entry is a directory, make the directory - File dir = new File(filePath); - dir.mkdir(); - } - zipIn.closeEntry(); - entry = zipIn.getNextEntry(); - } - } + try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));) { + ZipEntry entry = zipIn.getNextEntry(); + // iterates over entries in the zip file + while (entry != null) { + // create all directories needed for nested items + String[] parts = entry.getName().split("/"); + String s = destDirectory + File.separator; + for (int i = 0; i < parts.length - 1; i++) { + s += parts[i]; + File idir = new File(s); + if (!idir.exists()) { + idir.mkdir(); + } + s += File.separator; + } + String filePath = destDirectory + File.separator + entry.getName(); + if (!entry.isDirectory()) { + // if the entry is a file, extracts it + extractFile(zipIn, filePath); + } else { + // if the entry is a directory, make the directory + File dir = new File(filePath); + dir.mkdir(); + } + zipIn.closeEntry(); + entry = zipIn.getNextEntry(); + } + } } - + /** * Extracts a zip entry (file entry) + * * @param zipIn * @param filePath * @throws IOException */ private static final int BUFFER_SIZE = 4096; - + private void extractFile(ZipInputStream zipIn, String filePath) throws IOException { //BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); - try (FileOutputStream fos = new FileOutputStream(filePath); - BufferedOutputStream bos = new BufferedOutputStream(fos);){ - byte[] bytesIn = new byte[BUFFER_SIZE]; - int read = 0; - while ((read = zipIn.read(bytesIn)) != -1) { - bos.write(bytesIn, 0, read); - } - } + try (FileOutputStream fos = new FileOutputStream(filePath); + BufferedOutputStream bos = new BufferedOutputStream(fos);) { + byte[] bytesIn = new byte[BUFFER_SIZE]; + int read = 0; + while ((read = zipIn.read(bytesIn)) != -1) { + bos.write(bytesIn, 0, read); + } + } } } |