use of org.commonjava.maven.galley.event.EventMetadata in project indy by Commonjava.
the class ContentBrowseResource method processHead.
// @ApiOperation( "Retrieve directory content under the given artifact store (type/name) and directory path." )
// @ApiResponses( { @ApiResponse( code = 404, message = "Content is not available" ),
// @ApiResponse( code = 200, response = String.class, message = "Rendered content listing" ) } )
// @HEAD
// @Path( "/{path: (.*)}" )
// public Response headForDirectory(
// final @ApiParam( allowableValues = "maven,npm", required = true ) @PathParam( "packageType" )
// String packageType,
// final @ApiParam( allowableValues = "hosted,group,remote", required = true ) @PathParam( "type" )
// String type, final @ApiParam( required = true ) @PathParam( "name" ) String name,
// final @PathParam( "path" ) String path, final UriInfo uriInfo,
// @Context final HttpServletRequest request )
// {
// return processHead( packageType, type, name, path, uriInfo, request );
// }
//
// @ApiOperation( "Retrieve directory content under the given artifact store (type/name) and directory path." )
// @ApiResponses( { @ApiResponse( code = 404, message = "Content is not available" ),
// @ApiResponse( code = 200, response = String.class, message = "Rendered content listing" ) } )
// @HEAD
// @Path( "/" )
// public Response headForRoot(
// final @ApiParam( allowableValues = "maven,npm", required = true ) @PathParam( "packageType" )
// String packageType,
// final @ApiParam( allowableValues = "hosted,group,remote", required = true ) @PathParam( "type" )
// String type, final @ApiParam( required = true ) @PathParam( "name" ) String name,
// final @PathParam( "path" ) String path, @Context final UriInfo uriInfo,
// @Context final HttpServletRequest request )
// {
// return processHead( packageType, type, name, "", uriInfo, request );
// }
private Response processHead(final String packageType, final String type, final String name, final String path, final UriInfo uriInfo, final HttpServletRequest request) {
if (!PackageTypes.contains(packageType)) {
return Response.status(400).build();
}
Response response;
ContentBrowseResult result = null;
try {
result = getBrowseResult(packageType, type, name, path, uriInfo);
final AcceptInfo acceptInfo = jaxRsRequestHelper.findAccept(request, ApplicationContent.application_json);
final String content = mapper.writeValueAsString(result);
Response.ResponseBuilder builder = Response.ok().header(ApplicationHeader.content_type.key(), acceptInfo.getRawAccept()).header(ApplicationHeader.content_length.key(), Long.toString(content.length())).header(ApplicationHeader.last_modified.key(), HttpUtils.formatDateHeader(new Date())).header(ApplicationHeader.md5.key(), contentDigester.digest(result.getStoreKey(), path, new EventMetadata()).getDigests().get(ContentDigest.MD5).toUpperCase()).header(ApplicationHeader.sha1.key(), contentDigester.digest(result.getStoreKey(), path, new EventMetadata()).getDigests().get(ContentDigest.SHA_1).toUpperCase());
;
response = builder.build();
} catch (IndyWorkflowException e) {
logger.error(String.format("Failed to list content: %s from: %s. Reason: %s", StringUtils.isBlank(path) ? "/" : path, name, e.getMessage()), e);
response = responseHelper.formatResponse(e);
} catch (JsonProcessingException e) {
response = responseHelper.formatResponse(e, "Failed to serialize DTO to JSON: " + result);
}
return response;
}
use of org.commonjava.maven.galley.event.EventMetadata in project indy by Commonjava.
the class ContentAccessHandler method doHead.
public Response doHead(final String packageType, final String type, final String name, final String path, final Boolean cacheOnly, final String baseUri, final HttpServletRequest request, EventMetadata eventMetadata, final Consumer<ResponseBuilder> builderModifier) {
setContext(PACKAGE_TYPE, packageType);
setContext(PATH, path);
if (!PackageTypes.contains(packageType)) {
ResponseBuilder builder = Response.status(400);
if (builderModifier != null) {
builderModifier.accept(builder);
}
return builder.build();
}
final StoreType st = StoreType.get(type);
final StoreKey sk = new StoreKey(packageType, st, name);
eventMetadata = eventMetadata.set(ContentManager.ENTRY_POINT_STORE, sk);
setContext(CONTENT_ENTRY_POINT, sk.toString());
Response response = null;
if (isDirectoryPath(path, request)) {
response = RequestUtils.redirectContentListing(packageType, type, name, path, request, builderModifier);
} else {
try {
Transfer item = null;
logger.info("Checking existence of: {}:{} (cache only? {})", sk, path, cacheOnly);
boolean exists;
if (Boolean.TRUE.equals(cacheOnly)) {
logger.debug("Calling getTransfer()");
item = contentController.getTransfer(sk, path, TransferOperation.DOWNLOAD);
exists = item != null && item.exists();
SpecialPathInfo spi = specialPathManager.getSpecialPathInfo(item, packageType);
setContext(METADATA_CONTENT, Boolean.toString(spi != null && spi.isMetadata()));
logger.debug("Got transfer reference: {}", item);
} else {
// Use exists for remote repo to avoid downloading file. Use getTransfer for everything else (hosted, cache-only).
// Response will be composed of metadata by getHttpMetadata which get metadata from .http-metadata.json (because HTTP transport always writes a .http-metadata.json
// file when it makes a request). This file stores the HTTP response status code and headers regardless exist returning true or false.
logger.debug("Calling remote exists()");
exists = contentController.exists(sk, path);
logger.debug("Got remote exists: {}", exists);
}
if (exists) {
HttpExchangeMetadata httpMetadata = item != null ? contentController.getHttpMetadata(item) : contentController.getHttpMetadata(sk, path);
// For hosted / group repo, artifacts will also have metadata generated. This will fetch the item by content get method.
if (item == null) {
logger.debug("Retrieving: {}:{} for existence test", sk, path);
item = contentController.get(sk, path, eventMetadata);
logger.debug("Got retrieved transfer reference: {}", item);
}
if (MDC.get(METADATA_CONTENT) != null) {
SpecialPathInfo spi = specialPathManager.getSpecialPathInfo(item, packageType);
setContext(METADATA_CONTENT, Boolean.toString(spi != null && spi.isMetadata()));
}
logger.trace("Building 200 response. Using HTTP metadata: {}", httpMetadata);
setContext(HTTP_STATUS, String.valueOf(200));
final ResponseBuilder builder = Response.ok();
// restrict the npm header contentType with json to avoid some parsing error
String contentType = packageType.equals(NPM_PKG_KEY) ? MediaType.APPLICATION_JSON : contentController.getContentType(path);
responseHelper.setInfoHeaders(builder, item, sk, path, true, contentType, httpMetadata);
if (!path.endsWith("/")) {
// Content hashing headers
builder.header(ApplicationHeader.md5.key(), contentDigester.digest(sk, path, new EventMetadata()).getDigests().get(ContentDigest.MD5));
builder.header(ApplicationHeader.sha1.key(), contentDigester.digest(sk, path, new EventMetadata()).getDigests().get(ContentDigest.SHA_1));
}
if (builderModifier != null) {
builderModifier.accept(builder);
}
response = builder.build();
} else {
logger.trace("Building 404 (or error) response...");
if (StoreType.remote == st) {
final HttpExchangeMetadata metadata = contentController.getHttpMetadata(sk, path);
if (metadata != null) {
logger.trace("Using HTTP metadata to build negative response.");
response = responseHelper.formatResponseFromMetadata(metadata);
}
}
if (response == null) {
logger.debug("No HTTP metadata; building generic 404 response.");
setContext(HTTP_STATUS, String.valueOf(404));
ResponseBuilder builder = Response.status(Status.NOT_FOUND);
if (builderModifier != null) {
builderModifier.accept(builder);
}
response = builder.build();
}
}
} catch (final IndyWorkflowException e) {
logger.error(String.format("Failed to download artifact: %s from: %s. Reason: %s", path, name, e.getMessage()), e);
response = responseHelper.formatResponse(e, builderModifier);
}
}
return response;
}
use of org.commonjava.maven.galley.event.EventMetadata in project indy by Commonjava.
the class StoreEnablementManager method handleDisableTimeout.
private void handleDisableTimeout(String payload) {
Logger logger = LoggerFactory.getLogger(getClass());
String keystr = payload;
StoreKey key = null;
try {
key = objectMapper.readValue(keystr, StoreKey.class);
} catch (IOException e) {
logger.warn("Failed to read StoreKey from JSON string: '{}' in event payload.", keystr);
}
logger.debug("Read key: {} from JSON string: '{}' in event payload.", key, keystr);
if (key != null) {
try {
ArtifactStore store = storeDataManager.getArtifactStore(key);
if (store == null) {
logger.warn("Attempt to re-enable missing repository! Skipping.");
return;
}
store = store.copyOf();
if (store.isDisabled()) {
store.setDisabled(false);
storeDataManager.storeArtifactStore(store, new ChangeSummary(ChangeSummary.SYSTEM_USER, "Re-enabling " + key), false, true, new EventMetadata());
}
} catch (IndyDataException e) {
logger.error(String.format("Failed to re-enable %s", key), e);
}
}
}
use of org.commonjava.maven.galley.event.EventMetadata in project indy by Commonjava.
the class DefaultContentDigester method digest.
public TransferMetadata digest(final StoreKey key, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
final Transfer transfer = directContentAccess.getTransfer(key, path);
if (transfer == null || !transfer.exists()) {
logger.error("No transfer to digest, store: {}, path: {}, transfer: {}", key, path, transfer);
return new TransferMetadata(Collections.emptyMap(), 0L);
}
TransferMetadata meta = getContentMetadata(transfer);
if (meta != null) {
logger.debug("Get transferMetadata: {}", meta);
return meta;
}
String cacheKey = generateCacheKey(transfer);
logger.debug("TransferMetadata missing for: {}. Re-reading with FORCE_CHECKSUM now to calculate it.", cacheKey);
EventMetadata forcedEventMetadata = new EventMetadata(eventMetadata).set(FORCE_CHECKSUM, Boolean.TRUE);
try (InputStream stream = transfer.openInputStream(false, forcedEventMetadata)) {
// depend on ChecksummingTransferDecorator to calculate / store metadata as this gets read, using
// the FORCE_CHECKSUM metadata key to control its generation.
IOUtils.toByteArray(stream);
} catch (IOException e) {
throw new IndyWorkflowException("Failed to calculate checksums (MD5, SHA-256, etc.) for: %s. Reason: %s", e, transfer, e.getMessage());
}
logger.debug("Retrying TransferMetadata retrieval from cache for: {} after recalculating", cacheKey);
return getContentMetadata(transfer);
}
use of org.commonjava.maven.galley.event.EventMetadata in project indy by Commonjava.
the class GroupConsistencyListener method processChanged.
private void processChanged(final ArtifactStore store) {
final StoreKey key = store.getKey();
try {
final Set<Group> groups = storeDataManager.query().getGroupsContaining(key);
logger.trace("For repo: {}, containing groups are: {}", key, groups);
for (final Group group : groups) {
logger.debug("Removing {} from membership of group: {}", key, group.getKey());
Group g = group.copyOf();
g.removeConstituent(key);
storeDataManager.storeArtifactStore(g, new ChangeSummary(ChangeSummary.SYSTEM_USER, "Auto-update groups containing: " + key + " (to maintain consistency)"), false, false, new EventMetadata().set(StoreDataManager.EVENT_ORIGIN, GROUP_CONSISTENCY_ORIGIN));
}
} catch (final IndyDataException e) {
logger.error(String.format("Failed to remove group constituent listings for: %s. Error: %s", key, e.getMessage()), e);
}
}
Aggregations