@Nullable @Override public String getContentType() { return payload.getContentType(); }
@Override public InputStream openInputStream() throws IOException { return payload.openInputStream(); }
@Override public long getSize() { return payload.getSize(); }
@TransactionalStoreBlob public Asset ingestAsset(Payload body) throws IOException, PGPException { StorageFacet storageFacet = facet(StorageFacet.class); try (TempBlob tempBlob = storageFacet.createTempBlob(body, FacetHelper.hashAlgorithms)) { ControlFile control = AptPackageParser.parsePackage(tempBlob); if (control == null) { throw new IllegalOperationException("Invalid Debian package supplied"); } return ingestAsset(control, tempBlob, body.getSize(), body.getContentType()); } }
@Override public void close() throws IOException { payload.close(); }
@Nullable @Override public String getContentType() { return payload.getContentType(); } }
private Map<String, Object> parseJson(final Payload payload) throws IOException { try (InputStream in = payload.openInputStream()) { TypeReference<Map<String, Object>> typeReference = new TypeReference<Map<String, Object>>() { }; return mapper.readValue(in, typeReference); } } }
/** * Rewrites the provider JSON so that source entries are removed and dist entries are pointed back to Nexus. */ public Payload rewriteProviderJson(final Repository repository, final Payload payload) throws IOException { Map<String, Object> json = parseJson(payload); if (json.get(PACKAGES_KEY) instanceof Map) { Map<String, Object> packagesMap = (Map<String, Object>) json.get(PACKAGES_KEY); for (String packageName : packagesMap.keySet()) { Map<String, Object> packageVersions = (Map<String, Object>) packagesMap.get(packageName); for (String packageVersion : packageVersions.keySet()) { // TODO: Make this more robust, right now it makes a lot of assumptions and doesn't deal with bad things well Map<String, Object> versionInfo = (Map<String, Object>) packageVersions.get(packageVersion); versionInfo.remove(SOURCE_KEY); // TODO: For now don't allow sources, probably should make this configurable? Map<String, Object> distInfo = (Map<String, Object>) versionInfo.get(DIST_KEY); if (distInfo != null && ZIP_TYPE.equals(distInfo.get(TYPE_KEY))) { versionInfo.put(DIST_KEY, buildDistInfo(repository, packageName, packageVersion, (String) distInfo.get(REFERENCE_KEY), (String) distInfo.get(SHASUM_KEY), ZIP_TYPE)); } } } } return new StringPayload(mapper.writeValueAsString(json), payload.getContentType()); }
@Override public TempBlob createTempBlob(final Payload payload, final Iterable<HashAlgorithm> hashAlgorithms) { try (InputStream inputStream = payload.openInputStream()) { return createTempBlob(inputStream, hashAlgorithms); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Mutate the response into one that returns part of the payload. */ private Response partialResponse(final Response response, final Payload payload, final Range<Long> requestedRange) { Response.Builder builder = new Response.Builder() .copy(response) .status(Status.success(HttpStatus.PARTIAL_CONTENT)); Payload partialPayload = new PartialPayload(payload, requestedRange); builder.payload(partialPayload); // ResponseSender takes care of Content-Length header, via payload.size builder.header(HttpHeaders.CONTENT_RANGE, "bytes " + requestedRange.lowerEndpoint() + "-" + requestedRange.upperEndpoint() + "/" + payload.getSize()); return builder.build(); }
/** * Save an asset and create blob. * * @return blob content */ public Content saveAsset(final StorageTx tx, final Asset asset, final Supplier<InputStream> contentSupplier, final Payload payload) throws IOException { AttributesMap contentAttributes = null; String contentType = null; if (payload instanceof Content) { contentAttributes = ((Content) payload).getAttributes(); contentType = payload.getContentType(); } return saveAsset(tx, asset, contentSupplier, contentType, contentAttributes); }
@Override public InputStream openInputStream() throws IOException { final InputStream payloadStream = payload.openInputStream(); payloadStream.skip(rangeToSend.lowerEndpoint()); return limit(payloadStream, partialSize); }
@TransactionalStoreBlob protected Content doPut(final MavenPath path, final Payload payload, final TempBlob tempBlob) throws IOException { final StorageTx tx = UnitOfWork.currentTx(); final AssetBlob assetBlob = tx.createBlob( path.getPath(), tempBlob, null, payload.getContentType(), false ); AttributesMap contentAttributes = null; if (payload instanceof Content) { contentAttributes = ((Content) payload).getAttributes(); } return doPutAssetBlob(path, contentAttributes, tx, assetBlob); }
/** * Parses a search response, returning the marshaled {@link NpmSearchResponse}. */ @Nullable private NpmSearchResponse parseSearchResponse(final Repository repository, final Response response) { Payload payload = response.getPayload(); if (response.getStatus().getCode() == HttpStatus.OK && payload != null) { try (InputStream in = payload.openInputStream()) { return npmSearchResponseMapper.readFromInputStream(in); } catch (IOException e) { if (log.isDebugEnabled()) { log.warn("Unable to process search response for repository {}, skipping", repository.getName(), e); } else { log.warn("Unable to process search response for repository {}, cause: {}, skipping", repository.getName(), e.getMessage()); } } } return null; } }
@TransactionalStoreBlob protected Content doPutContent(final String path, final TempBlob tempBlob, final Payload payload) throws IOException { StorageTx tx = UnitOfWork.currentTx(); Asset asset = getOrCreateAsset(getRepository(), path, RawCoordinatesHelper.getGroup(path), path); AttributesMap contentAttributes = null; if (payload instanceof Content) { contentAttributes = ((Content) payload).getAttributes(); } Content.applyToAsset(asset, Content.maintainLastModified(asset, contentAttributes)); AssetBlob assetBlob = tx.setBlob( asset, path, tempBlob, null, payload.getContentType(), false ); tx.saveAsset(asset); return toContent(asset, assetBlob.getBlob()); }
/** * This method MUST NOT be called from within a TX, as it dispatches a new request! It fails with * {@code java.lang.IllegalStateException}: "Transaction already in progress" otherwise! */ private NestedAttributesMap retrievePackageRoot(final NpmPackageId packageId, final Context context) throws IOException { try { Request getRequest = new Request.Builder().action(GET).path("/" + packageId.id()).build(); Response response = getRepository().facet(ViewFacet.class).dispatch(getRequest, context); if (response.getPayload() == null) { throw new IOException("Could not retrieve package " + packageId); } final InputStream packageRootIn = response.getPayload().openInputStream(); return NpmFacetUtils.parse(() -> packageRootIn); } catch (IOException e) { throw e; } catch (Exception e) { Throwables.throwIfUnchecked(e); throw new IOException(e); } }
@TransactionalStoreBlob protected Content doPutMetadata(final String path, final TempBlob tempBlob, final Payload payload, final AssetKind assetKind) throws IOException { StorageTx tx = UnitOfWork.currentTx(); Asset asset = getOrCreateAsset(path); asset.formatAttributes().set(P_ASSET_KIND, assetKind.toString()); if (payload instanceof Content) { Content.applyToAsset(asset, Content.maintainLastModified(asset, ((Content) payload).getAttributes())); } AssetBlob assetBlob = tx.setBlob( asset, path, tempBlob, null, payload.getContentType(), false ); tx.saveAsset(asset); return toContent(asset, assetBlob.getBlob()); }
private Response handleSnapshotAdminRequest(Context context, String id) throws Exception { String method = context.getRequest().getAction(); Repository repository = context.getRepository(); AptSnapshotFacet snapshotFacet = repository.facet(AptSnapshotFacet.class); switch (method) { case MKCOL: { snapshotFacet.createSnapshot(id, new AllSnapshotComponentSelector()); return HttpResponses.created(); } case PUT: { try (InputStream is = context.getRequest().getPayload().openInputStream()) { ControlFile settings = new ControlFileParser().parseControlFile(is); snapshotFacet.createSnapshot(id, new FilteredSnapshotComponentSelector(settings)); } return HttpResponses.created(); } case DELETE: { snapshotFacet.deleteSnapshot(id); return HttpResponses.noContent(); } default: return HttpResponses.methodNotAllowed(method, DELETE, MKCOL, PUT); } }
tempBlob, null, payload.getContentType(), false );