Example usage for java.util.jar JarInputStream JarInputStream

List of usage examples for java.util.jar JarInputStream JarInputStream

Introduction

In this page you can find the example usage for java.util.jar JarInputStream JarInputStream.

Prototype

public JarInputStream(InputStream in, boolean verify) throws IOException 

Source Link

Document

Creates a new JarInputStream and reads the optional manifest.

Usage

From source file:com.sketchy.server.action.GetCurrentVersion.java

@Override
public JSONServletResult execute(HttpServletRequest request) throws Exception {
    JSONServletResult jsonServletResult = new JSONServletResult(Status.SUCCESS);
    String version = "";
    try {//from ww  w .  j ava  2  s  . c  o  m
        File sketchyFile = new File("Sketchy.jar");
        if (!sketchyFile.exists()) {
            throw new Exception("Can't find Sketchy.jar file!");
        }
        JarInputStream jarInputStream = null;
        try {
            // check to make sure it's a Sketchy File with a Manifest File
            jarInputStream = new JarInputStream(new FileInputStream(sketchyFile), true);
            Manifest manifest = jarInputStream.getManifest();
            if (manifest == null) {
                throw new Exception("Manifest file not found.");
            }
            Attributes titleAttributes = manifest.getMainAttributes();
            version = titleAttributes.getValue("Implementation-Version");
        } catch (Exception e) {
            throw new Exception("Invalid Upgrade File!");
        } finally {
            IOUtils.closeQuietly(jarInputStream);
        }

        jsonServletResult.put("currentVersion", version);
    } catch (Throwable t) {
        jsonServletResult = new JSONServletResult(Status.ERROR,
                "Error getting Current Version! " + t.getMessage());
    }
    return jsonServletResult;
}

From source file:com.adeptj.runtime.osgi.BundleInstaller.java

Stream<Bundle> install(ClassLoader cl, String bundlesDir) throws IOException {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    Pattern pattern = Pattern.compile("^bundles.*\\.jar$");
    return ((JarURLConnection) this.getClass().getResource(bundlesDir).openConnection()).getJarFile().stream()
            .filter(jarEntry -> pattern.matcher(jarEntry.getName()).matches())
            .map(jarEntry -> cl.getResource(jarEntry.getName())).filter(Objects::nonNull).map(url -> {
                logger.debug("Installing Bundle from location: [{}]", url);
                Bundle bundle = null;/*from  w  ww. j  av  a  2s  .c  o m*/
                try (JarInputStream jis = new JarInputStream(url.openStream(), false)) {
                    if (StringUtils
                            .isEmpty(jis.getManifest().getMainAttributes().getValue(BUNDLE_SYMBOLIC_NAME))) {
                        logger.warn("Artifact [{}] is not a Bundle, skipping install!!", url);
                    } else {
                        bundle = BundleContextHolder.getInstance().getBundleContext()
                                .installBundle(url.toExternalForm());
                        this.installationCount.incrementAndGet();
                    }
                } catch (BundleException | IllegalStateException | SecurityException | IOException ex) {
                    logger.error("Exception while installing Bundle: [{}]. Cause:", url, ex);
                }
                return bundle;
            }).filter(Objects::nonNull).sorted(Comparator.comparingLong(Bundle::getBundleId));
}

From source file:com.sketchy.server.UpgradeUploadServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    JSONServletResult jsonServletResult = new JSONServletResult(Status.SUCCESS);
    try {/*from   w  w w  . j  a va 2s.  c om*/

        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {

            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setRepository(FileUtils.getTempDirectory());
            factory.setSizeThreshold(MAX_SIZE);

            ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
            List<FileItem> files = servletFileUpload.parseRequest(request);
            String version = "";
            for (FileItem fileItem : files) {
                String uploadFileName = fileItem.getName();
                if (StringUtils.isNotBlank(uploadFileName)) {

                    JarInputStream jarInputStream = null;
                    try {
                        // check to make sure it's a Sketchy File with a Manifest File
                        jarInputStream = new JarInputStream(fileItem.getInputStream(), true);
                        Manifest manifest = jarInputStream.getManifest();
                        if (manifest == null) {
                            throw new Exception("Invalid Upgrade File!");
                        }
                        Attributes titleAttributes = manifest.getMainAttributes();
                        if ((titleAttributes == null) || (!StringUtils.containsIgnoreCase(
                                titleAttributes.getValue("Implementation-Title"), "Sketchy"))) {
                            throw new Exception("Invalid Upgrade File!");
                        }
                        version = titleAttributes.getValue("Implementation-Version");
                    } catch (Exception e) {
                        throw new Exception("Invalid Upgrade File!");
                    } finally {
                        IOUtils.closeQuietly(jarInputStream);
                    }
                    // save new .jar file as "ready"
                    fileItem.write(new File("Sketchy.jar.ready"));
                    jsonServletResult.put("version", version);
                }
            }
        }
    } catch (Exception e) {
        jsonServletResult = new JSONServletResult(Status.ERROR, e.getMessage());
    }
    response.setContentType("text/html");
    response.setStatus(HttpServletResponse.SC_OK);
    response.getWriter().print(jsonServletResult.toJSONString());
}

From source file:io.adeptj.runtime.osgi.BundleInstaller.java

private Bundle installBundle(URL bundleUrl, BundleContext systemBundleContext) {
    LOGGER.debug("Installing Bundle from location: [{}]", bundleUrl);
    Bundle bundle = null;/*from w ww  .j  a v  a2  s.c  o m*/
    try (JarInputStream jar = new JarInputStream(bundleUrl.openStream(), false)) {
        if (StringUtils.isEmpty(jar.getManifest().getMainAttributes().getValue(BUNDLE_SYMBOLIC_NAME))) {
            LOGGER.warn("Not an OSGi Bundle: {}", bundleUrl);
        } else {
            bundle = systemBundleContext.installBundle(bundleUrl.toExternalForm());
            this.installCount.incrementAndGet();
        }
    } catch (BundleException | IllegalStateException | SecurityException | IOException ex) {
        LOGGER.error("Exception while installing Bundle: [{}]. Cause:", bundleUrl, ex);
    }
    return bundle;
}

From source file:org.eclipse.packagedrone.repo.aspect.common.p2.internal.P2Unzipper.java

private void processZippedMetaData(final Context context, final ZipInputStream zis, final String filename,
        final String xpath) throws Exception {
    final JarInputStream jin = new JarInputStream(zis, false);
    ZipEntry entry;/*from ww  w  . ja v  a 2  s.com*/
    while ((entry = jin.getNextEntry()) != null) {
        if (entry.getName().equals(filename)) {
            processMetaData(context, jin, filename, xpath);
        }
    }
}

From source file:org.jahia.services.modulemanager.persistence.PersistentBundleInfoBuilder.java

private static boolean isTransformationRequired(Resource resource) throws IOException {
    try (JarInputStream is = new JarInputStream(new BufferedInputStream(resource.getInputStream()), false)) {
        return ModuleUtils.requiresTransformation(is.getManifest().getMainAttributes());
    }//from   www.j  a  v  a  2 s  .c  om
}

From source file:org.artifactory.repo.db.DbStoringRepoMixin.java

private void validateArtifactIfRequired(MutableVfsFile mutableFile, RepoPath repoPath)
        throws RepoRejectException {
    String pathId = repoPath.getId();
    InputStream jarStream = mutableFile.getStream();
    try {/*  w ww  . j  av a2s  . co  m*/
        log.info("Validating the content of '{}'.", pathId);
        JarInputStream jarInputStream = new JarInputStream(jarStream, true);
        JarEntry entry = jarInputStream.getNextJarEntry();

        if (entry == null) {
            if (jarInputStream.getManifest() != null) {
                log.trace("Found manifest validating the content of '{}'.", pathId);
                return;
            }
            throw new IllegalStateException("Could not find entries within the archive.");
        }
        do {
            log.trace("Found the entry '{}' validating the content of '{}'.", entry.getName(), pathId);
        } while ((jarInputStream.available() == 1) && (entry = jarInputStream.getNextJarEntry()) != null);

        log.info("Finished validating the content of '{}'.", pathId);
    } catch (Exception e) {
        String message = String.format("Failed to validate the content of '%s': %s", pathId, e.getMessage());
        if (log.isDebugEnabled()) {
            log.debug(message, e);
        } else {
            log.error(message);
        }
        throw new RepoRejectException(message, HttpStatus.SC_CONFLICT);
    } finally {
        IOUtils.closeQuietly(jarStream);
    }
}