Example usage for java.net JarURLConnection getContentLength

List of usage examples for java.net JarURLConnection getContentLength

Introduction

In this page you can find the example usage for java.net JarURLConnection getContentLength.

Prototype

public int getContentLength() 

Source Link

Document

Returns the value of the content-length header field.

Usage

From source file:org.apache.cxf.management.web.browser.bootstrapping.BootstrapStorage.java

@GET
@Path("{resource:.*}")
public Response getResource(@Context final MessageContext mc, @PathParam("resource") final String resource) {
    if (isLastModifiedRequest(mc)) {
        return Response.notModified().build();
    }/*from w  w w  . j a v  a  2 s  .  co  m*/

    try {
        URL url;
        URL jar = getClass().getProtectionDomain().getCodeSource().getLocation();

        url = new URL(String.format("jar:%s!/static-content/logbrowser/%s", jar, resource));

        JarURLConnection connection = (JarURLConnection) url.openConnection();
        if (connection.getContentLength() == -1 || connection.getJarEntry() == null) {
            return Response.status(Status.NOT_FOUND).build();
        } else if (connection.getJarEntry().isDirectory()) {
            return Response.status(Status.FORBIDDEN).build();
        } else { // correct
            MediaType mime = getMimeType(mc, resource);
            StaticFile staticFile = new StaticFile(url, acceptsGzip(mc), mime);

            Response.ResponseBuilder builder = Response.ok(staticFile);
            builder.variant(new Variant(mime, (Locale) null, staticFile.isGzipEnabled() ? "gzip" : null));

            return builder.build();
        }
    } catch (MalformedURLException e) {
        return Response.status(Status.BAD_REQUEST).build();
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, "Error occur while retrieve static file", e);
        return Response.serverError().build();
    }
}