Search in sources :

Example 1 with HttpExchangeMetadata

use of org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata in project indy by Commonjava.

the class ContentAccessHandler method handleMissingContentQuery.

private Response handleMissingContentQuery(final StoreKey sk, final String path, final Consumer<ResponseBuilder> builderModifier) {
    Response response = null;
    logger.trace("Transfer not found: {}/{}", sk, path);
    if (StoreType.remote == sk.getType()) {
        logger.trace("Transfer was from remote repo. Trying to get HTTP metadata for: {}/{}", sk, path);
        try {
            final HttpExchangeMetadata metadata = contentController.getHttpMetadata(sk, path);
            if (metadata != null) {
                logger.trace("Using HTTP metadata to formulate response status for: {}/{}", sk, path);
                response = formatResponseFromMetadata(metadata, builderModifier);
            } else {
                logger.trace("No HTTP metadata found!");
            }
        } catch (final IndyWorkflowException e) {
            logger.error(String.format("Error retrieving status metadata for: %s from: %s. Reason: %s", path, sk.getName(), e.getMessage()), e);
            response = formatResponse(e, builderModifier);
        }
    }
    if (response == null) {
        response = formatResponse(ApplicationStatus.NOT_FOUND, null, "Path " + path + " is not available in store " + sk + ".", builderModifier);
    }
    return response;
}
Also used : ResponseUtils.formatResponse(org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse) Response(javax.ws.rs.core.Response) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) HttpExchangeMetadata(org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata)

Example 2 with HttpExchangeMetadata

use of org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata 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;
}
Also used : StoreType(org.commonjava.indy.model.core.StoreType) ResponseUtils.formatResponse(org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse) Response(javax.ws.rs.core.Response) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) AcceptInfo(org.commonjava.indy.util.AcceptInfo) StoreKey(org.commonjava.indy.model.core.StoreKey) HttpExchangeMetadata(org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata) Date(java.util.Date)

Example 3 with HttpExchangeMetadata

use of org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata in project indy by Commonjava.

the class HttpConduitWrapper method writeNotFoundTransfer.

public void writeNotFoundTransfer(RemoteRepository repo, String path) throws IOException, IndyWorkflowException {
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("No transfer found.");
    final HttpExchangeMetadata metadata = contentController.getHttpMetadata(repo.getKey(), path);
    if (metadata == null) {
        logger.debug("No transfer metadata.");
        writeStatus(ApplicationStatus.NOT_FOUND);
    } else {
        logger.debug("Writing metadata from http exchange with upstream.");
        if (metadata.getResponseStatusCode() == 500) {
            logger.debug("Translating 500 error upstream into 502");
            writeStatus(502, "Bad Gateway");
        } else {
            logger.debug("Passing through upstream status: " + metadata.getResponseStatusCode());
            writeStatus(metadata.getResponseStatusCode(), metadata.getResponseStatusMessage());
        }
        writeHeader(ApplicationHeader.content_type, contentController.getContentType(path));
        for (final Map.Entry<String, List<String>> headerSet : metadata.getResponseHeaders().entrySet()) {
            final String key = headerSet.getKey();
            if (ApplicationHeader.content_type.upperKey().equals(key)) {
                continue;
            }
            for (final String value : headerSet.getValue()) {
                writeHeader(headerSet.getKey(), value);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) Logger(org.slf4j.Logger) HttpExchangeMetadata(org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata) Map(java.util.Map)

Example 4 with HttpExchangeMetadata

use of org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata in project indy by Commonjava.

the class HttpConduitWrapper method writeExistingTransfer.

public void writeExistingTransfer(Transfer txfr, boolean writeBody, String path, EventMetadata eventMetadata) throws IOException {
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("Valid transfer found.");
    try (InputStream in = txfr.openInputStream(true, eventMetadata)) {
        final HttpExchangeMetadata metadata = contentController.getHttpMetadata(txfr);
        logger.trace("Got HTTP metadata: {} for transfer: {}", metadata, txfr);
        writeStatus(ApplicationStatus.OK);
        Long headerContentLength = metadata != null ? metadata.getContentLength() : null;
        long bytes = metadata != null && headerContentLength != null ? metadata.getContentLength() : txfr.length();
        if (bytes < 1) {
            bytes = txfr.length();
        }
        if (bytes > 0) {
            writeHeader(ApplicationHeader.content_length, String.valueOf(bytes));
        }
        String lastMod = metadata != null ? metadata.getLastModified() : null;
        if (lastMod == null) {
            lastMod = HttpUtils.formatDateHeader(txfr.lastModified());
        }
        if (lastMod != null) {
            writeHeader(ApplicationHeader.last_modified, lastMod);
        }
        writeHeader(ApplicationHeader.content_type, contentController.getContentType(path));
        if (writeBody) {
            sinkChannel.write(ByteBuffer.wrap("\r\n".getBytes()));
            int capacity = 16384;
            ByteBuffer bbuf = ByteBuffer.allocate(capacity);
            byte[] buf = new byte[capacity];
            int read = -1;
            while ((read = in.read(buf)) > -1) {
                bbuf.clear();
                bbuf.put(buf, 0, read);
                bbuf.flip();
                int written = 0;
                do {
                    written += sinkChannel.write(bbuf);
                } while (written < read);
            }
        }
    } catch (IndyWorkflowException e) {
        logger.error(String.format("Failed to retrieve http-metadata.json file for: %s. Reason: %s", txfr, e.getMessage()), e);
    } finally {
        cacheProvider.cleanupCurrentThread();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Logger(org.slf4j.Logger) HttpExchangeMetadata(org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata) ByteBuffer(java.nio.ByteBuffer)

Example 5 with HttpExchangeMetadata

use of org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata in project galley by Commonjava.

the class AbstractHttpJob method writeMetadata.

private void writeMetadata(final Transfer target, final ObjectMapper mapper) {
    if (target == null || request == null || response == null) {
        logger.debug("Cannot write HTTP exchange metadata. Request: {}. Response: {}. Transfer: {}", request, response, target);
        return;
    }
    logger.debug("Writing HTTP exchange metadata. Request: {}. Response: {}", request, response);
    Transfer metaTxfr = target.getSiblingMeta(HttpExchangeMetadata.FILE_EXTENSION);
    if (metaTxfr == null) {
        if (target.isDirectory()) {
            logger.debug("DIRECTORY. Using HTTP exchange metadata file INSIDE directory called: {}", HttpExchangeMetadata.FILE_EXTENSION);
            metaTxfr = target.getChild(HttpExchangeMetadata.FILE_EXTENSION);
        } else {
            logger.debug("SKIP: Cannot retrieve HTTP exchange metadata Transfer instance for: {}", target);
            return;
        }
    }
    final HttpExchangeMetadata metadata = new HttpExchangeMetadata(request, response);
    OutputStream out = null;
    try {
        out = metaTxfr.openOutputStream(TransferOperation.GENERATE, false);
        logger.debug("Writing HTTP exchange metadata:\n\n{}\n\n", new Object() {

            @Override
            public String toString() {
                try {
                    return mapper.writeValueAsString(metadata);
                } catch (final JsonProcessingException e) {
                }
                return "ERROR RENDERING METADATA";
            }
        });
        out.write(mapper.writeValueAsBytes(metadata));
    } catch (final IOException e) {
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Failed to write metadata for HTTP exchange to: %s. Reason: %s", metaTxfr, e.getMessage()), e);
        } else {
            logger.warn("Failed to write metadata for HTTP exchange to: {}. Reason: {}", metaTxfr, e.getMessage());
        }
    } finally {
        IOUtils.closeQuietly(out);
    }
}
Also used : OutputStream(java.io.OutputStream) Transfer(org.commonjava.maven.galley.model.Transfer) IOException(java.io.IOException) HttpExchangeMetadata(org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

HttpExchangeMetadata (org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata)5 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)3 Response (javax.ws.rs.core.Response)2 ResponseUtils.formatResponse (org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse)2 Transfer (org.commonjava.maven.galley.model.Transfer)2 Logger (org.slf4j.Logger)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 StoreKey (org.commonjava.indy.model.core.StoreKey)1 StoreType (org.commonjava.indy.model.core.StoreType)1 AcceptInfo (org.commonjava.indy.util.AcceptInfo)1