/** * Method that will create the DOM for given P2 metadata file. It handles files like "artifacts.xml" and * "content.xml", but also thier JAR counterparts, like "artifacts.jar" and "content.jar" by cranking them up, * getting the entry with same name but with modified extension to ".xml". */ public static Xpp3Dom getMetadataXpp3Dom(final StorageFileItem item) throws IOException, XmlPullParserException { // TODO: if we ever want to have the DOM reused, this method could put the parsed DOM // into item context (to not have it reparsed). For now, this call always parses as // currently we'd go rather to P2 "eat CPU" then "eat heap", as P2 metadata DOM // objects might get very large final Xpp3Dom dom; if (item.getName().endsWith(".jar")) { dom = parseJarItem(item, item.getName().replace(".jar", ".xml")); } else if (item.getName().endsWith(".xml")) { dom = parseXmlItem(item); } else { throw new IOException("Cannot parse the DOM for metadata in item " + item.getRepositoryItemUid()); } return dom; }
private static StorageFileItem compressMetadataItem(final Repository repository, final String path, final StorageFileItem metadataXml) throws IOException { final Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); // this is a special one: once cached (hence consumed), temp file gets deleted final FileContentLocator fileContentLocator = new FileContentLocator("application/java-archive"); try (OutputStream buffer = fileContentLocator.getOutputStream(); ZipOutputStream out = new ZipOutputStream(buffer); InputStream in = metadataXml.getInputStream()) { out.putNextEntry(new JarEntry(metadataXml.getName())); IOUtils.copy(in, out); } final DefaultStorageFileItem result = new DefaultStorageFileItem( repository, new ResourceStoreRequest(path), true /* isReadable */, false /* isWritable */, fileContentLocator ); return result; }
result.setDownloadName(file.getName());
result.setDownloadName( file.getName() );