Search in sources :

Example 61 with JarInputStream

use of java.util.jar.JarInputStream in project sling by apache.

the class InstallServlet method installBasedOnUploadedJar.

private void installBasedOnUploadedJar(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    InstallationResult result = null;
    try {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // try to hold even largish bundles in memory to potentially improve performance
        factory.setSizeThreshold(UPLOAD_IN_MEMORY_SIZE_THRESHOLD);
        ServletFileUpload upload = new ServletFileUpload();
        upload.setFileItemFactory(factory);
        @SuppressWarnings("unchecked") List<FileItem> items = upload.parseRequest(req);
        if (items.size() != 1) {
            logAndWriteError("Found " + items.size() + " items to process, but only updating 1 bundle is supported", resp);
            return;
        }
        FileItem item = items.get(0);
        JarInputStream jar = null;
        InputStream rawInput = null;
        try {
            jar = new JarInputStream(item.getInputStream());
            Manifest manifest = jar.getManifest();
            if (manifest == null) {
                logAndWriteError("Uploaded jar file does not contain a manifest", resp);
                return;
            }
            final String symbolicName = manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
            if (symbolicName == null) {
                logAndWriteError("Manifest does not have a " + Constants.BUNDLE_SYMBOLICNAME, resp);
                return;
            }
            final String version = manifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
            // the JarInputStream is used only for validation, we need a fresh input stream for updating
            rawInput = item.getInputStream();
            Bundle found = getBundle(symbolicName);
            try {
                installOrUpdateBundle(found, rawInput, "inputstream:" + symbolicName + "-" + version + ".jar");
                result = new InstallationResult(true, null);
                resp.setStatus(200);
                result.render(resp.getWriter());
                return;
            } catch (BundleException e) {
                logAndWriteError("Unable to install/update bundle " + symbolicName, e, resp);
                return;
            }
        } finally {
            IOUtils.closeQuietly(jar);
            IOUtils.closeQuietly(rawInput);
        }
    } catch (FileUploadException e) {
        logAndWriteError("Failed parsing uploaded bundle", e, resp);
        return;
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Bundle(org.osgi.framework.Bundle) Manifest(java.util.jar.Manifest) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) BundleException(org.osgi.framework.BundleException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 62 with JarInputStream

use of java.util.jar.JarInputStream in project sling by apache.

the class ModelArchiveReader method read.

/**
     * Read a model archive.
     * The input stream is not closed. It is up to the caller to close the input stream.
     * @param in The input stream to read from.
     * @return The model
     * @throws IOException If anything goes wrong
     */
@SuppressWarnings("resource")
public static Model read(final InputStream in, final ArtifactConsumer consumer) throws IOException {
    Model model = null;
    final JarInputStream jis = new JarInputStream(in);
    // check manifest
    final Manifest manifest = jis.getManifest();
    if (manifest == null) {
        throw new IOException("Not a model archive - manifest is missing.");
    }
    // check manifest header
    final String version = manifest.getMainAttributes().getValue(ModelArchiveWriter.MANIFEST_HEADER);
    if (version == null) {
        throw new IOException("Not a model archive - manifest header is missing.");
    }
    // validate manifest header
    try {
        final int number = Integer.valueOf(version);
        if (number < 1 || number > ModelArchiveWriter.ARCHIVE_VERSION) {
            throw new IOException("Not a model archive - invalid manifest header value: " + version);
        }
    } catch (final NumberFormatException nfe) {
        throw new IOException("Not a model archive - invalid manifest header value: " + version);
    }
    // read contents
    JarEntry entry = null;
    while ((entry = jis.getNextJarEntry()) != null) {
        if (ModelArchiveWriter.MODEL_NAME.equals(entry.getName())) {
            model = ModelUtility.getEffectiveModel(ModelReader.read(new InputStreamReader(jis, "UTF-8"), null));
        } else if (!entry.isDirectory() && entry.getName().startsWith(ModelArchiveWriter.ARTIFACTS_PREFIX)) {
            // artifact
            final Artifact artifact = Artifact.fromMvnUrl("mvn:" + entry.getName().substring(ModelArchiveWriter.ARTIFACTS_PREFIX.length()));
            consumer.consume(artifact, jis);
        }
        jis.closeEntry();
    }
    if (model == null) {
        throw new IOException("Not a model archive - model file is missing.");
    }
    return model;
}
Also used : InputStreamReader(java.io.InputStreamReader) JarInputStream(java.util.jar.JarInputStream) Model(org.apache.sling.provisioning.model.Model) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) Artifact(org.apache.sling.provisioning.model.Artifact)

Example 63 with JarInputStream

use of java.util.jar.JarInputStream in project sling by apache.

the class PreparePackageMojoTest method compareJarContents.

private static void compareJarContents(File orgJar, File actualJar) throws IOException {
    try (JarInputStream jis1 = new JarInputStream(new FileInputStream(orgJar));
        JarInputStream jis2 = new JarInputStream(new FileInputStream(actualJar))) {
        JarEntry je1 = null;
        while ((je1 = jis1.getNextJarEntry()) != null) {
            if (je1.isDirectory())
                continue;
            JarEntry je2 = null;
            while ((je2 = jis2.getNextJarEntry()) != null) {
                if (!je2.isDirectory())
                    break;
            }
            assertEquals(je1.getName(), je2.getName());
            assertEquals(je1.getSize(), je2.getSize());
            try {
                byte[] buf1 = IOUtils.toByteArray(jis1);
                byte[] buf2 = IOUtils.toByteArray(jis2);
                assertArrayEquals("Contents not equal: " + je1.getName(), buf1, buf2);
            } finally {
                jis1.closeEntry();
                jis2.closeEntry();
            }
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream)

Example 64 with JarInputStream

use of java.util.jar.JarInputStream in project sling by apache.

the class SourceReferencesServlet method collectMavenSourceRerefences.

private void collectMavenSourceRerefences(JSONWriter w, URL entry) throws IOException {
    InputStream wrappedIn = entry.openStream();
    try {
        JarInputStream jarIs = new JarInputStream(wrappedIn);
        JarEntry jarEntry;
        while ((jarEntry = jarIs.getNextJarEntry()) != null) {
            String entryName = jarEntry.getName();
            if (entryName.startsWith("META-INF/maven/") && entryName.endsWith("/pom.properties")) {
                writeMavenGav(w, jarIs);
            }
        }
    } finally {
        IOUtils.closeQuietly(wrappedIn);
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) JarEntry(java.util.jar.JarEntry)

Example 65 with JarInputStream

use of java.util.jar.JarInputStream in project bnd by bndtools.

the class BuilderTest method testNoManifest.

public static void testNoManifest() throws Exception {
    Builder b = new Builder();
    try {
        b.setProperty("-nomanifest", "true");
        b.setProperty(Constants.BUNDLE_CLASSPATH, "WEB-INF/classes");
        b.setProperty("Include-Resource", "WEB-INF/classes=@jar/asm.jar");
        Jar jar = b.build();
        assertTrue(b.check());
        File f = new File("tmp.jar");
        f.deleteOnExit();
        jar.write(f);
        JarInputStream jin = new JarInputStream(new FileInputStream(f));
        Manifest m = jin.getManifest();
        assertNull(m);
    } finally {
        b.close();
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) Builder(aQute.bnd.osgi.Builder) Jar(aQute.bnd.osgi.Jar) Manifest(java.util.jar.Manifest) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

JarInputStream (java.util.jar.JarInputStream)185 JarEntry (java.util.jar.JarEntry)82 IOException (java.io.IOException)73 FileInputStream (java.io.FileInputStream)66 Manifest (java.util.jar.Manifest)56 File (java.io.File)48 InputStream (java.io.InputStream)45 ZipEntry (java.util.zip.ZipEntry)34 JarOutputStream (java.util.jar.JarOutputStream)29 Test (org.junit.Test)29 FileOutputStream (java.io.FileOutputStream)26 ByteArrayInputStream (java.io.ByteArrayInputStream)24 URL (java.net.URL)20 ArrayList (java.util.ArrayList)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 OutputStream (java.io.OutputStream)14 JarFile (java.util.jar.JarFile)14 BufferedInputStream (java.io.BufferedInputStream)11 Attributes (java.util.jar.Attributes)11 HashSet (java.util.HashSet)9