private StorageItem storeSpecsIndex(SpecsIndexZippedFile file, InputStream content) throws Exception { ByteArrayOutputStream gzipped = new ByteArrayOutputStream(); try (OutputStream out = new GZIPOutputStream(gzipped)) { IOUtil.copy(content, out); } ContentLocator cl = new PreparedContentLocator(new ByteArrayInputStream(gzipped.toByteArray()), "application/x-gzip", gzipped.size()); DefaultStorageFileItem item = new DefaultStorageFileItem(repository, new ResourceStoreRequest(file.storagePath()), true, true, cl); repository.storeItem(false, item); return item; }
private void importFile(final File baseDir, final String relPath) throws StorageException, UnsupportedStorageOperationException, IllegalOperationException { final File source = new File(baseDir, relPath); final ResourceStoreRequest request = new ResourceStoreRequest(relPath); final DefaultStorageFileItem file = new DefaultStorageFileItem(this, request, source.canRead(), source.canWrite(), new FileContentLocator( source, getMimeSupport().guessMimeTypeFromPath(source.getName()))); file.setModified(source.lastModified()); file.setCreated(source.lastModified()); storeItem(false, file); }
private void putStorageItem( final String path, final ContentLocator contentLocator ) throws IOException { try { ResourceStoreRequest req = new ResourceStoreRequest( path ); DefaultStorageFileItem mdFile = new DefaultStorageFileItem( repository, req, true, true, contentLocator ); repository.storeItem( false, mdFile ); // TODO: why? storeItem() already does this!!! repository.removeFromNotFoundCache( req ); } catch ( Exception e ) { throw new IOException( e ); } }
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; }
@Deprecated protected AbstractStorageItem createStorageItem( ResourceStoreRequest request, byte[] bytes ) { ContentLocator content = new ByteArrayContentLocator( bytes, getMimeUtil().getMimeType( request.getRequestPath() ) ); DefaultStorageFileItem result = new DefaultStorageFileItem( this, request, true /* isReadable */, false /* isWritable */, content ); result.setLength( bytes.length ); return result; }
/** * Store repository item. * * @param repository containing item to be stored * @param path of item to be stored * @param content of item to be stored * @param mimeType of item to be stored */ private static void storeItem(final Repository repository, final String path, final byte[] content, final String mimeType) throws Exception { log.debug("Storing {}:{}", repository.getId(), path); DefaultStorageFileItem item = new DefaultStorageFileItem( repository, new ResourceStoreRequest("/" + path), true, true, new PreparedContentLocator(new ByteArrayInputStream(content), mimeType, ContentLocator.UNKNOWN_LENGTH) ); repository.storeItem(false, item); }
private void createVirtualYumConfigFile(final MavenRepository repository) { DefaultStorageFileItem file = new DefaultStorageFileItem( repository, new ResourceStoreRequest(YumConfigContentGenerator.configFilePath(repository.getId())), true, false, new StringContentLocator(YumConfigContentGenerator.ID) ); file.setContentGeneratorId(YumConfigContentGenerator.ID); try { repository.storeItem(false, file); } catch (Exception e) { LOG.warn("Could not install yum.repo file '{}' due to {}/{}", file, e.getClass().getName(), e.getMessage()); } }
protected void writeMetadata( RepositoryItemUid uid, Map<String, Object> ctx, Metadata md ) throws IllegalOperationException, UnsupportedStorageOperationException, MetadataException, IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); MetadataBuilder.write( md, outputStream ); String mdString = outputStream.toString( "UTF-8" ); outputStream.close(); DefaultStorageFileItem file = new DefaultStorageFileItem( uid.getRepository(), new ResourceStoreRequest( uid.getPath() ), true, true, new StringContentLocator( mdString ) ); ( (MavenRepository) uid.getRepository() ).storeItemWithChecksums( false, file ); }
public void writeRawData( String path, byte[] data ) throws Exception { DefaultStorageFileItem file = new DefaultStorageFileItem( repository, new ResourceStoreRequest( path ), true, true, new ByteArrayContentLocator( data, "text/xml" ) ); repository.storeItem( false, file ); lastWriteFile = file; }
public static void storeItem(final Repository repository, final ResourceStoreRequest request, final InputStream in, final String mimeType, final Map<String, String> userAttributes) throws Exception { final DefaultStorageFileItem fItem = new DefaultStorageFileItem(repository, request, true, true, new PreparedContentLocator(in, mimeType, ContentLocator.UNKNOWN_LENGTH)); if (userAttributes != null) { fItem.getRepositoryItemAttributes().putAll(userAttributes); } repository.storeItem(false, fItem); }
private void storeItemFromFile(final String path, final File file, final P2ProxyRepository repository) throws LocalStorageException, UnsupportedStorageOperationException { final ContentLocator content = new FileContentLocator(file, "text/xml"); final DefaultStorageFileItem storageItem = new DefaultStorageFileItem(repository, new ResourceStoreRequest(path), true /* isReadable */, false /* isWritable */, content); repository.getLocalStorage().storeItem(repository, storageItem); } }
public StorageItem newStorageItem(RubygemsFile file, InputStream is){ ResourceStoreRequest request = new ResourceStoreRequest(file.storagePath()); ContentLocator contentLocator = new PreparedContentLocator(is, file.type().mime(), ContentLocator.UNKNOWN_LENGTH); return new DefaultStorageFileItem(repository, request, true, true, contentLocator); } }
protected void storeMergedMetadataItemDigest( ResourceStoreRequest request, String digest, List<StorageItem> sources, String algorithm ) throws IOException, UnsupportedStorageOperationException, IllegalOperationException { String digestFileName = request.getRequestPath() + "." + algorithm.toLowerCase(); String mimeType = getMimeSupport().guessMimeTypeFromPath( getMimeRulesSource(), digestFileName ); byte[] bytes = ( digest + '\n' ).getBytes( "UTF-8" ); ContentLocator contentLocator = new ByteArrayContentLocator( bytes, mimeType ); ResourceStoreRequest req = new ResourceStoreRequest( digestFileName ); req.getRequestContext().setParentContext( request.getRequestContext() ); // Metadata checksum files are not composite ones, they are derivatives of the Metadata (and metadata file _is_ // composite one) DefaultStorageFileItem digestFileItem = new DefaultStorageFileItem( this, req, true, false, contentLocator ); storeItem( false, digestFileItem ); }
@Override public void update(InputStream is, RubygemsFile file) { log.debug("update :: {}", file); ResourceStoreRequest request = new ResourceStoreRequest(file.storagePath()); ContentLocator contentLocator = new PreparedContentLocator(is, file.type().mime(), ContentLocator.UNKNOWN_LENGTH); DefaultStorageFileItem fileItem = new DefaultStorageFileItem(repository, request, true, true, contentLocator); try { // we need to bypass access control here !!! repository.storeItem(false, fileItem); file.set(fileItem); } catch (IOException | UnsupportedStorageOperationException | IllegalOperationException e) { file.setException(e); } }
protected void storeIndexItem(Repository repository, File file, IndexingContext context) { String path = PUBLISHING_PATH_PREFIX + "/" + file.getName(); try { ResourceStoreRequest request = new ResourceStoreRequest(path); DefaultStorageFileItem fItem = new DefaultStorageFileItem(repository, request, true, true, new FileContentLocator(file, mimeSupport.guessMimeTypeFromPath(repository.getMimeRulesSource(), file.getAbsolutePath()))); if (context.getTimestamp() == null) { fItem.setModified(0); fItem.setCreated(0); } else { fItem.setModified(context.getTimestamp().getTime()); fItem.setCreated(context.getTimestamp().getTime()); } if (repository instanceof MavenRepository) { // this is maven repo, so use the checksumming facility ((MavenRepository) repository).storeItemWithChecksums(false, fItem); } else { // simply store it repository.storeItem(false, fItem); } } catch (Exception e) { log.error("Cannot store index file " + path, e); } }
private void memory(InputStream data, RubygemsFile file, long length) { ContentLocator cl = new PreparedContentLocator(data, file.type().mime(), length); file.set(new DefaultStorageFileItem(repository, new ResourceStoreRequest(file.storagePath()), true, false, cl)); }
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; }
@Override public void retrieve(SpecsIndexFile specs) { log.debug("retrieve :: {}", specs); SpecsIndexZippedFile source = specs.zippedSpecsIndexFile(); try { StorageFileItem item = (StorageFileItem) repository.retrieveDirectItem(new ResourceStoreRequest(source.storagePath())); DefaultStorageFileItem unzippedItem = new DefaultStorageFileItem(repository, new ResourceStoreRequest(specs.storagePath()), true, false, gunzipContentLocator(item)); unzippedItem.setModified(item.getModified()); specs.set(unzippedItem); } catch (ItemNotFoundException e) { specs.markAsNotExists(); } catch (IOException | IllegalOperationException e) { specs.setException(e); } }
public void storeItem( ResourceStoreRequest request, InputStream is, Map<String, String> userAttributes ) throws UnsupportedStorageOperationException, IllegalOperationException, StorageException, AccessDeniedException { if ( !checkConditions( request, getResultingActionOnWrite( request ) ) ) { throw new AccessDeniedException( request, "Operation does not fills needed requirements!" ); } DefaultStorageFileItem fItem = new DefaultStorageFileItem( this, request, true, true, new PreparedContentLocator( is, getMimeSupport().guessMimeTypeFromPath( getMimeRulesSource(), request.getRequestPath() ) ) ); if ( userAttributes != null ) { fItem.getRepositoryItemAttributes().putAll( userAttributes ); } storeItem( false, fItem ); }
protected static StorageFileItem createMetadataItem(final Repository repository, final String path, final Xpp3Dom metadata, final String hack, final RequestContext context) throws IOException { // this is a special one: once cached (hence consumed), temp file get's deleted final FileContentLocator fileContentLocator = new FileContentLocator("text/xml"); try (OutputStream buffer = fileContentLocator.getOutputStream()) { final MXSerializer mx = new MXSerializer(); mx.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " "); mx.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n"); final String encoding = "UTF-8"; mx.setOutput(buffer, encoding); mx.startDocument(encoding, null); if (hack != null) { mx.processingInstruction(hack); } metadata.writeToSerializer(null, mx); mx.flush(); } final ResourceStoreRequest request = new ResourceStoreRequest(path); request.getRequestContext().setParentContext(context); final DefaultStorageFileItem result = new DefaultStorageFileItem(repository, request, true /* isReadable */, false /* isWritable */, fileContentLocator); return result; }