public StorageFileItemBody( StorageFileItem file ) throws IOException { this.file = file; this.channel = Channels.newChannel( file.getInputStream() ); }
/** * Reads up a hash as string from StorageFileItem pointing to .sha1/.md5 files. * * @param inputFileItem * @return * @throws IOException */ public static String readDigestFromFileItem( final StorageFileItem inputFileItem ) throws IOException { return readDigestFromStream( inputFileItem.getInputStream() ); } }
@Override public InputStream retrieveContent( String path ) throws IOException { StorageItem item = getStorageItem( path, false ); if ( item instanceof StorageFileItem ) { return ( (StorageFileItem) item ).getInputStream(); } else { return null; } }
private ContentLocator gunzipContentLocator(StorageFileItem item) throws IOException { try (InputStream in = item.getInputStream()) { ByteArrayInputStream gzipped = IOUtil.toGunzipped(in); return new PreparedContentLocator(gzipped, "application/x-marshal-ruby", gzipped.available()); } }
/** * Read content of repomd.xml. * * @param repository repository containing repomd.xml * @return parsed repomd.xml */ private static Document parseRepoMD(final Repository repository) throws Exception { StorageFileItem repoMDItem = (StorageFileItem) repository.retrieveItem( false, new ResourceStoreRequest("/" + PATH_OF_REPOMD_XML) ); try (InputStream in = repoMDItem.getInputStream()) { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); return documentBuilder.parse(in); } }
@Override public void write(OutputStream outputStream) throws IOException { try(final InputStream is = getStorageItem().getInputStream()) { StreamSupport.copy(is, outputStream, OUTPUT_BUFFER_SIZE); } catch (IOException e) { if ("EofException".equals(e.getClass().getSimpleName())) { // This is for Jetty's org.eclipse.jetty.io.EofException // https://issues.sonatype.org/browse/NEXUS-217 } else if (e instanceof SocketException) { // https://issues.sonatype.org/browse/NEXUS-217 } else { throw e; } } } }
private static Xpp3Dom parseXmlItem(final StorageFileItem item) throws IOException, XmlPullParserException { try (InputStream is = item.getInputStream()) { return Xpp3DomBuilder.build(new XmlStreamReader(is)); } }
private Xpp3Dom getMirrorsDom(final StorageFileItem mirrorsItem) throws IOException, XmlPullParserException { try (InputStream is = mirrorsItem.getInputStream()) { return Xpp3DomBuilder.build(new XmlStreamReader(is)); } }
private static Xpp3Dom parseJarItem(final StorageFileItem item, final String jarPath) throws IOException, XmlPullParserException { final File file = File.createTempFile("p2file", "zip"); try { try (InputStream is = item.getInputStream()) { FileUtils.copyInputStreamToFile(is, file); try (ZipFile z = new ZipFile(file)) { final ZipEntry ze = z.getEntry(jarPath); if (ze == null) { throw new LocalStorageException("Corrupted P2 metadata jar " + jarPath); } try (InputStream zis = z.getInputStream(ze)) { return Xpp3DomBuilder.build(new XmlStreamReader(zis)); } } } } finally { file.delete(); } } }
private StorageItem mergeSpecsIndex(SpecsIndexZippedFile file, List<StorageItem> items) throws Exception { log.debug("mergeSpecsIndex :: {} :: {}", file, items); MergeSpecsHelper specs = gateway.newMergeSpecsHelper(); for (StorageItem item : items) { try (InputStream is = ((StorageFileItem) item).getInputStream()) { specs.add(new GZIPInputStream(is)); } } try (InputStream is = specs.getInputStream(file.specsType() == SpecsIndexType.LATEST)) { return storeSpecsIndex(file, is); } }
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; }
@Override public InputStream getInputStream(RubygemsFile file) throws IOException { if (file.get() == null) { retrieve(file); } return ((StorageFileItem) file.get()).getInputStream(); }
@Override public ContentLocator generateContent( Repository repository, String path, StorageFileItem item ) throws IllegalOperationException, ItemNotFoundException, LocalStorageException { InputStreamReader isr = null; try { StringWriter sw = new StringWriter(); VelocityContext vctx = new VelocityContext( item.getItemContext() ); isr = new InputStreamReader( item.getInputStream(), "UTF-8" ); velocity.getEngine().evaluate( vctx, sw, item.getRepositoryItemUid().toString(), isr ); return new StringContentLocator( sw.toString() ); } catch ( Exception e ) { throw new LocalStorageException( "Could not expand the template: " + item.getRepositoryItemUid().toString(), e ); } finally { IOUtil.close( isr ); } } }
@Override public FileTypeValidity isExpectedFileType(final StorageFileItem file) { // only check content from p2 repositories if (file.getRepositoryItemUid().getRepository().adaptToFacet(P2Repository.class) == null) { return FileTypeValidity.NEUTRAL; } if (file.getRepositoryItemUid().getPath().endsWith(".pack.gz")) { try (InputStream input = file.getInputStream();) { final byte[] magicBytes = new byte[4]; if (input.read(magicBytes) > 0) { if (Arrays.equals(magicBytes, PACK200_MAGIC) // real pack.gz || Arrays.equals(magicBytes, JAR_MAGIC)) // plain jar works too { return FileTypeValidity.VALID; } } } catch (final IOException e) { log.error("Unable to read pack200 magic bytes", e); } return FileTypeValidity.INVALID; } return super.isExpectedFileType(file); } }
@Override public void processItem(WalkerContext context, StorageItem item) { if (item instanceof StorageFileItem) { try { if (item.getName().endsWith(ApiV1DependenciesCuba.RUBY)) { try(InputStream is = ((StorageFileItem) item).getInputStream()) { rubygems.newDependencyHelper().add(is); } catch(Exception e){ repository.deleteItem(true, new ResourceStoreRequest(item)); } } else if (item.getName().endsWith(QuickMarshalCuba.GEMSPEC_RZ)) { try(InputStream is = ((StorageFileItem) item).getInputStream()) { rubygems.newGemspecHelper(is); } catch(Exception e){ repository.deleteItem(true, new ResourceStoreRequest(item)); } } else if (item.getName().endsWith(".gems")) { repository.deleteItem(true, new ResourceStoreRequest(item)); } } catch (Exception e) { logger.warn("Error occurred while processing item '" + item.getPath() + "'.", e); } } } }
public InputStream retrieve(String name) throws IOException { TaskUtil.checkInterruption(); ResourceStoreRequest req = new ResourceStoreRequest(PUBLISHING_PATH_PREFIX + "/" + name); try { StorageFileItem item = null; // XXX: ensure it goes to remote only and throws FileNotFoundException if nothing found on remote // kinda turn off transparent proxying for this method // We need to use ProxyRepository and get it's RemoteStorage stuff to completely // avoid "transparent" proxying, and even the slightest possibility to return // some stale file from cache to the updater. if (ISPROXY(repository) && REMOTEACCESSALLOWED(repository)) { item = (StorageFileItem) repository.getRemoteStorage() .retrieveItem(repository, req, repository.getRemoteUrl()); } else { throw new ItemNotFoundException(req, repository); } return item.getInputStream(); } catch (ItemNotFoundException ex) { final FileNotFoundException fne = new FileNotFoundException(name + " (remote item not found)"); fne.initCause(ex); throw fne; } } });
private void copyIndexPropertiesToTempDir( Repository repository, File tempDir ) { InputStream is = null; try { // Need to use RepositoryUID to get around security ResourceStoreRequest req = new ResourceStoreRequest( "/.index/" + IndexingContext.INDEX_FILE + ".properties" ); req.setRequestLocalOnly( true ); StorageFileItem item = (StorageFileItem) repository.retrieveItem( true, req ); // Hack to make sure that group properties isn't retrieved from child repo if ( repository.getId().equals( item.getRepositoryId() ) ) { is = item.getInputStream(); // FileUtils.copyStreamToFile closes the stream! FileUtils.copyStreamToFile( new RawInputStreamFacade( is ), new File( tempDir, IndexingContext.INDEX_FILE + ".properties" ) ); } } catch ( Exception e ) { getLogger().debug( "Unable to copy index properties file, continuing without it", e ); } }
@Override public String renderStatus() { if (isConfigured()) { try { final Repository repository = repositoryRegistry.getRepository(getConfig().repository()); final StorageItem storageItem = repository.retrieveItem( new ResourceStoreRequest(YumConfigContentGenerator.configFilePath(repository.getId()), true) ); if (storageItem instanceof StorageFileItem) { try (InputStream in = ((StorageFileItem) storageItem).getInputStream()) { return "<b>Example Yum configuration file:</b><br/><br/>" + "<pre>" + IOUtils.toString(in) + "</pre>"; } } } catch (Exception e) { return null; } } return null; }
/** * Converts a POM file into an Artifact that holds the GAV data for the pom. * * @param item * StorageFileItem representing a POM file * @return an Artifact containing the GAV information in the file * @throws IOException */ GAV getArtifactForStorageItem(StorageFileItem item) throws IOException { try { PomArtifactManager mgr = new PomArtifactManager( this.nexusConfig.getTemporaryDirectory()); mgr.storeTempPomFile(item.getInputStream()); ArtifactCoordinate ac = mgr.getArtifactCoordinateFromTempPomFile(); return new GAV(ac.getGroupId(), ac.getArtifactId(), ac.getVersion()); } catch (Exception e) { getLogger().warn( "Error processing POM file for artifact usage data.", ExceptionUtils.getRootCause(e)); return null; } }
private StorageItem mergeDependency(DependencyFile file, List<StorageItem> dependencies) throws Exception { log.debug("mergeDependency :: {} :: {}", file, dependencies); DependencyHelper deps = gateway.newDependencyHelper(); for (StorageItem item : dependencies) { try (InputStream is = ((StorageFileItem) item).getInputStream()) { deps.add(is); } } ContentLocator cl = new PreparedContentLocator(deps.getInputStream(true), file.type().mime(), PreparedContentLocator.UNKNOWN_LENGTH); DefaultStorageFileItem item = new DefaultStorageFileItem(repository, new ResourceStoreRequest(file.storagePath()), true, true, cl); repository.storeItem(false, item); return item; }