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;
}
}
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;
}
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();
}
}
}
}
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);
}
}
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();
}
}
Aggregations