use of org.commonjava.indy.util.AcceptInfo in project indy by Commonjava.
the class JaxRsRequestHelper method findAccept.
public AcceptInfo findAccept(final HttpServletRequest request, final String defaultAccept) {
final List<AcceptInfo> accepts = parser.parse(request.getHeaders(ApplicationHeader.accept.key()));
AcceptInfo selectedAccept = null;
for (final AcceptInfo accept : accepts) {
final String sa = ApplicationContent.getStandardAccept(accept.getBaseAccept());
if (sa != null) {
selectedAccept = accept;
break;
}
}
if (selectedAccept == null) {
selectedAccept = new AcceptInfo(defaultAccept, defaultAccept, parser.getDefaultVersion());
}
return selectedAccept;
}
use of org.commonjava.indy.util.AcceptInfo 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) {
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);
final AcceptInfo acceptInfo = jaxRsRequestHelper.findAccept(request, ApplicationContent.text_html);
Response response = null;
if (path == null || path.equals("") || path.endsWith("/") || path.endsWith(LISTING_HTML_FILE)) {
try {
logger.info("Getting listing at: {}", path);
final String content = contentController.renderListing(acceptInfo.getBaseAccept(), sk, path, baseUri, uriFormatter);
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()));
if (builderModifier != null) {
builderModifier.accept(builder);
}
response = builder.build();
} catch (final IndyWorkflowException e) {
logger.error(String.format("Failed to list content: %s from: %s. Reason: %s", path, name, e.getMessage()), e);
response = formatResponse(e, builderModifier);
}
} else {
try {
Transfer item = null;
logger.info("Checking existence of: {}:{} (cache only? {})", sk, path, cacheOnly);
boolean exists = false;
if (Boolean.TRUE.equals(cacheOnly)) {
logger.debug("Calling getTransfer()");
item = contentController.getTransfer(sk, path, TransferOperation.DOWNLOAD);
exists = item != null && item.exists();
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 exists()");
exists = contentController.exists(sk, path);
logger.debug("Got exists: {}", exists);
}
if (exists) {
HttpExchangeMetadata httpMetadata = item != null ? contentController.getHttpMetadata(item) : contentController.getHttpMetadata(sk, path);
// TODO: For hosted repo, artifacts do not have metadata generated. Fall to get(). But we need a better fix later on.
if (httpMetadata == null) {
logger.info("Retrieving: {}:{} for existence test", sk, path);
item = contentController.get(sk, path, eventMetadata);
logger.debug("Got retrieved transfer reference: {}", item);
}
logger.debug("Building 200 response. Using HTTP metadata: {}", httpMetadata);
final ResponseBuilder builder = Response.ok();
setInfoHeaders(builder, item, sk, path, true, contentController.getContentType(path), httpMetadata);
if (builderModifier != null) {
builderModifier.accept(builder);
}
response = builder.build();
} else {
logger.debug("Building 404 (or error) response...");
if (StoreType.remote == st) {
final HttpExchangeMetadata metadata = contentController.getHttpMetadata(sk, path);
if (metadata != null) {
logger.debug("Using HTTP metadata to build negative response.");
response = formatResponseFromMetadata(metadata);
}
}
if (response == null) {
logger.debug("No HTTP metadata; building generic 404 response.");
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 = formatResponse(e, builderModifier);
}
}
return response;
}
use of org.commonjava.indy.util.AcceptInfo in project indy by Commonjava.
the class ContentAccessHandler method doGet.
public Response doGet(final String packageType, final String type, final String name, final String path, final String baseUri, final HttpServletRequest request, EventMetadata eventMetadata, final Consumer<ResponseBuilder> builderModifier) {
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);
final AcceptInfo acceptInfo = jaxRsRequestHelper.findAccept(request, ApplicationContent.text_html);
final String standardAccept = ApplicationContent.getStandardAccept(acceptInfo.getBaseAccept());
Response response = null;
logger.info("GET path: '{}' (RAW: '{}')\nIn store: '{}'\nUser addMetadata header is: '{}'\nStandard addMetadata header for that is: '{}'", path, request.getPathInfo(), sk, acceptInfo.getRawAccept(), standardAccept);
if (path == null || path.equals("") || request.getPathInfo().endsWith("/") || path.endsWith(LISTING_HTML_FILE)) {
try {
logger.info("Getting listing at: {}", path);
final String content = contentController.renderListing(standardAccept, st, name, path, baseUri, uriFormatter);
response = formatOkResponseWithEntity(content, acceptInfo.getRawAccept(), builderModifier);
} catch (final IndyWorkflowException e) {
logger.error(String.format("Failed to render content listing: %s from: %s. Reason: %s", path, name, e.getMessage()), e);
response = formatResponse(e, builderModifier);
}
} else {
try {
logger.info("START: retrieval of content: {}:{}", sk, path);
final Transfer item = contentController.get(sk, path, eventMetadata);
logger.info("HANDLE: retrieval of content: {}:{}", sk, path);
if (item == null) {
return handleMissingContentQuery(sk, path, builderModifier);
}
boolean handleLocking = false;
if (!item.isWriteLocked()) {
item.lockWrite();
handleLocking = true;
}
try {
if (!item.exists()) {
return handleMissingContentQuery(sk, path, builderModifier);
} else if (item.isDirectory()) {
try {
logger.info("Getting listing at: {}", path + "/");
final String content = contentController.renderListing(standardAccept, st, name, path + "/", baseUri, uriFormatter);
response = formatOkResponseWithEntity(content, acceptInfo.getRawAccept(), builderModifier);
} catch (final IndyWorkflowException e) {
logger.error(String.format("Failed to render content listing: %s from: %s. Reason: %s", path, name, e.getMessage()), e);
response = formatResponse(e, builderModifier);
}
} else {
logger.info("RETURNING: retrieval of content: {}:{}", sk, path);
// open the stream here to prevent deletion while waiting for the transfer back to the user to start...
InputStream in = item.openInputStream(true, eventMetadata);
final ResponseBuilder builder = Response.ok(new TransferStreamingOutput(in));
setInfoHeaders(builder, item, sk, path, true, contentController.getContentType(path), contentController.getHttpMetadata(item));
if (builderModifier != null) {
builderModifier.accept(builder);
}
response = builder.build();
}
} finally {
if (handleLocking) {
item.unlock();
}
}
} catch (final IOException | IndyWorkflowException e) {
logger.error(String.format("Failed to download artifact: %s from: %s. Reason: %s", path, name, e.getMessage()), e);
response = formatResponse(e, builderModifier);
}
}
logger.info("RETURNING RESULT: {}:{}", sk, path);
return response;
}
Aggregations