@Test public void karExtractTest() throws Exception { File base = new File("target/test"); base.mkdirs(); Kar kar = new Kar(new URI("https: File repoDir = new File("target/test/framework-repo"); repoDir.mkdirs(); File resourcesDir = new File("target/test/framework-resources"); resourcesDir.mkdirs(); kar.extract(repoDir, resourcesDir); File[] repoDirFiles = repoDir.listFiles(); Assert.assertEquals(1, repoDirFiles.length); Assert.assertEquals("org", repoDirFiles[0].getName()); File[] resourceDirFiles = resourcesDir.listFiles(); Assert.assertEquals(6, resourceDirFiles.length); }
public void extract(File repoDir, File resourceDir) { InputStream is = null; JarInputStream zipIs = null; FeatureDetector featureDetector = new FeatureDetector(); this.featureRepos = new ArrayList<>(); this.shouldInstallFeatures = true; try { is = karUri.toURL().openStream(); repoDir.mkdirs(); if (!repoDir.isDirectory()) { throw new RuntimeException("The KAR file " + karUri + " is already installed"); } LOGGER.debug("Uncompress the KAR file {} into directory {}", karUri, repoDir); zipIs = new JarInputStream(is); boolean scanForRepos = true; Manifest manifest = zipIs.getManifest(); if (manifest != null) { Attributes attr = manifest.getMainAttributes(); String featureStartSt = (String)attr .get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_START)); if ("false".equals(featureStartSt)) { shouldInstallFeatures = false; } String featureReposAttr = (String)attr .get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_REPOS)); if (featureReposAttr != null) { featureRepos.add(new URI(featureReposAttr)); scanForRepos = false; } } ZipEntry entry = zipIs.getNextEntry(); while (entry != null) { if (entry.getName().contains("..") || entry.getName().contains("%2e%2e")) { LOGGER.warn("kar entry {} contains a .. relative path. For security reasons, it's not allowed.", entry.getName()); } else { if (entry.getName().startsWith("repository/")) { String path = entry.getName().substring("repository/".length()); File destFile = new File(repoDir, path); extract(zipIs, entry, destFile); if (scanForRepos && featureDetector.isFeaturesRepository(destFile)) { Map map = new HashMap<>(); String uri = Parser.pathToMaven(path, map); if (map.get("classifier") != null && ((String) map.get("classifier")).equalsIgnoreCase("features")) featureRepos.add(URI.create(uri)); else featureRepos.add(destFile.toURI()); } } if (entry.getName().startsWith("resources/")) { String path = entry.getName().substring("resources/".length()); File destFile = new File(resourceDir, path); extract(zipIs, entry, destFile); } } entry = zipIs.getNextEntry(); } } catch (Exception e) { throw new RuntimeException("Error extracting kar file " + karUri + " into dir " + repoDir + ": " + e.getMessage(), e); } finally { closeStream(zipIs); closeStream(is); } }
Kar { public void extract(File repoDir, File resourceDir) { InputStream is = null; JarInputStream zipIs = null; FeatureDetector featureDetector = new FeatureDetector(); this.featureRepos = new ArrayList<>(); this.shouldInstallFeatures = true; try { is = karUri.toURL().openStream(); repoDir.mkdirs(); if (!repoDir.isDirectory()) { throw new RuntimeException("The KAR file " + karUri + " is already installed"); } LOGGER.debug("Uncompress the KAR file {} into directory {}", karUri, repoDir); zipIs = new JarInputStream(is); boolean scanForRepos = true; Manifest manifest = zipIs.getManifest(); if (manifest != null) { Attributes attr = manifest.getMainAttributes(); String featureStartSt = (String)attr .get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_START)); if ("false".equals(featureStartSt)) { shouldInstallFeatures = false; } String featureReposAttr = (String)attr .get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_REPOS)); if (featureReposAttr != null) { featureRepos.add(new URI(featureReposAttr)); scanForRepos = false; } } ZipEntry entry = zipIs.getNextEntry(); while (entry != null) { if (entry.getName().contains("..") || entry.getName().contains("%2e%2e")) { LOGGER.warn("kar entry {} contains a .. relative path. For security reasons, it's not allowed.", entry.getName()); } else { if (entry.getName().startsWith("repository/")) { String path = entry.getName().substring("repository/".length()); File destFile = new File(repoDir, path); extract(zipIs, entry, destFile); if (scanForRepos && featureDetector.isFeaturesRepository(destFile)) { Map map = new HashMap<>(); String uri = Parser.pathToMaven(path, map); if (map.get("classifier") != null && ((String) map.get("classifier")).equalsIgnoreCase("features")) featureRepos.add(URI.create(uri)); else featureRepos.add(destFile.toURI()); } } if (entry.getName().startsWith("resources/")) { String path = entry.getName().substring("resources/".length()); File destFile = new File(resourceDir, path); extract(zipIs, entry, destFile); } } entry = zipIs.getNextEntry(); } } catch (Exception e) { throw new RuntimeException("Error extracting kar file " + karUri + " into dir " + repoDir + ": " + e.getMessage(), e); } finally { closeStream(zipIs); closeStream(is); } } }
Kar { public void extract(File repoDir, File resourceDir) { InputStream is = null; JarInputStream zipIs = null; FeatureDetector featureDetector = new FeatureDetector(); this.featureRepos = new ArrayList<>(); this.shouldInstallFeatures = true; try { is = karUri.toURL().openStream(); repoDir.mkdirs(); if (!repoDir.isDirectory()) { throw new RuntimeException("The KAR file " + karUri + " is already installed"); } LOGGER.debug("Uncompress the KAR file {} into directory {}", karUri, repoDir); zipIs = new JarInputStream(is); boolean scanForRepos = true; Manifest manifest = zipIs.getManifest(); if (manifest != null) { Attributes attr = manifest.getMainAttributes(); String featureStartSt = (String)attr .get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_START)); if ("false".equals(featureStartSt)) { shouldInstallFeatures = false; } String featureReposAttr = (String)attr .get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_REPOS)); if (featureReposAttr != null) { featureRepos.add(new URI(featureReposAttr)); scanForRepos = false; } } ZipEntry entry = zipIs.getNextEntry(); while (entry != null) { if (entry.getName().contains("..") || entry.getName().contains("%2e%2e")) { LOGGER.warn("kar entry {} contains a .. relative path. For security reasons, it's not allowed.", entry.getName()); } else { if (entry.getName().startsWith("repository/")) { String path = entry.getName().substring("repository/".length()); File destFile = new File(repoDir, path); extract(zipIs, entry, destFile); if (scanForRepos && featureDetector.isFeaturesRepository(destFile)) { Map map = new HashMap<>(); String uri = Parser.pathToMaven(path, map); if (map.get("classifier") != null && ((String) map.get("classifier")).equalsIgnoreCase("features")) featureRepos.add(URI.create(uri)); else featureRepos.add(destFile.toURI()); } } if (entry.getName().startsWith("resources/")) { String path = entry.getName().substring("resources/".length()); File destFile = new File(resourceDir, path); extract(zipIs, entry, destFile); } } entry = zipIs.getNextEntry(); } } catch (Exception e) { throw new RuntimeException("Error extracting kar file " + karUri + " into dir " + repoDir + ": " + e.getMessage(), e); } finally { closeStream(zipIs); closeStream(is); } } Kar(URI karUri); }
Kar { public void extract(File repoDir, File resourceDir) { InputStream is = null; JarInputStream zipIs = null; FeatureDetector featureDetector = new FeatureDetector(); this.featureRepos = new ArrayList<>(); this.shouldInstallFeatures = true; try { is = karUri.toURL().openStream(); repoDir.mkdirs(); if (!repoDir.isDirectory()) { throw new RuntimeException("The KAR file " + karUri + " is already installed"); } LOGGER.debug("Uncompress the KAR file {} into directory {}", karUri, repoDir); zipIs = new JarInputStream(is); boolean scanForRepos = true; Manifest manifest = zipIs.getManifest(); if (manifest != null) { Attributes attr = manifest.getMainAttributes(); String featureStartSt = (String)attr .get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_START)); if ("false".equals(featureStartSt)) { shouldInstallFeatures = false; } String featureReposAttr = (String)attr .get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_REPOS)); if (featureReposAttr != null) { featureRepos.add(new URI(featureReposAttr)); scanForRepos = false; } } ZipEntry entry = zipIs.getNextEntry(); while (entry != null) { if (entry.getName().contains("..") || entry.getName().contains("%2e%2e")) { LOGGER.warn("kar entry {} contains a .. relative path. For security reasons, it's not allowed.", entry.getName()); } else { if (entry.getName().startsWith("repository/")) { String path = entry.getName().substring("repository/".length()); File destFile = new File(repoDir, path); extract(zipIs, entry, destFile); if (scanForRepos && featureDetector.isFeaturesRepository(destFile)) { Map map = new HashMap<>(); String uri = Parser.pathToMaven(path, map); if (map.get("classifier") != null && ((String) map.get("classifier")).equalsIgnoreCase("features")) featureRepos.add(URI.create(uri)); else featureRepos.add(destFile.toURI()); } } if (entry.getName().startsWith("resources/")) { String path = entry.getName().substring("resources/".length()); File destFile = new File(resourceDir, path); extract(zipIs, entry, destFile); } } entry = zipIs.getNextEntry(); } } catch (Exception e) { throw new RuntimeException("Error extracting kar file " + karUri + " into dir " + repoDir + ": " + e.getMessage(), e); } finally { closeStream(zipIs); closeStream(is); } } Kar(URI karUri); void extract(File repoDir, File resourceDir); String getKarName(); URI getKarUri(); boolean isShouldInstallFeatures(); List<URI> getFeatureRepos(); }
Kar { public void extract(File repoDir, File resourceDir) { InputStream is = null; JarInputStream zipIs = null; FeatureDetector featureDetector = new FeatureDetector(); this.featureRepos = new ArrayList<>(); this.shouldInstallFeatures = true; try { is = karUri.toURL().openStream(); repoDir.mkdirs(); if (!repoDir.isDirectory()) { throw new RuntimeException("The KAR file " + karUri + " is already installed"); } LOGGER.debug("Uncompress the KAR file {} into directory {}", karUri, repoDir); zipIs = new JarInputStream(is); boolean scanForRepos = true; Manifest manifest = zipIs.getManifest(); if (manifest != null) { Attributes attr = manifest.getMainAttributes(); String featureStartSt = (String)attr .get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_START)); if ("false".equals(featureStartSt)) { shouldInstallFeatures = false; } String featureReposAttr = (String)attr .get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_REPOS)); if (featureReposAttr != null) { featureRepos.add(new URI(featureReposAttr)); scanForRepos = false; } } ZipEntry entry = zipIs.getNextEntry(); while (entry != null) { if (entry.getName().contains("..") || entry.getName().contains("%2e%2e")) { LOGGER.warn("kar entry {} contains a .. relative path. For security reasons, it's not allowed.", entry.getName()); } else { if (entry.getName().startsWith("repository/")) { String path = entry.getName().substring("repository/".length()); File destFile = new File(repoDir, path); extract(zipIs, entry, destFile); if (scanForRepos && featureDetector.isFeaturesRepository(destFile)) { Map map = new HashMap<>(); String uri = Parser.pathToMaven(path, map); if (map.get("classifier") != null && ((String) map.get("classifier")).equalsIgnoreCase("features")) featureRepos.add(URI.create(uri)); else featureRepos.add(destFile.toURI()); } } if (entry.getName().startsWith("resources/")) { String path = entry.getName().substring("resources/".length()); File destFile = new File(resourceDir, path); extract(zipIs, entry, destFile); } } entry = zipIs.getNextEntry(); } } catch (Exception e) { throw new RuntimeException("Error extracting kar file " + karUri + " into dir " + repoDir + ": " + e.getMessage(), e); } finally { closeStream(zipIs); closeStream(is); } } Kar(URI karUri); void extract(File repoDir, File resourceDir); String getKarName(); URI getKarUri(); boolean isShouldInstallFeatures(); List<URI> getFeatureRepos(); static final Logger LOGGER; static final String MANIFEST_ATTR_KARAF_FEATURE_START; static final String MANIFEST_ATTR_KARAF_FEATURE_REPOS; }