Example usage for io.netty.handler.codec.http HttpHeaderNames IF_NONE_MATCH

List of usage examples for io.netty.handler.codec.http HttpHeaderNames IF_NONE_MATCH

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpHeaderNames IF_NONE_MATCH.

Prototype

AsciiString IF_NONE_MATCH

To view the source code for io.netty.handler.codec.http HttpHeaderNames IF_NONE_MATCH.

Click Source Link

Document

"if-none-match"

Usage

From source file:asset.pipeline.ratpack.internal.ProductionAssetHandler.java

License:Apache License

@Override
public void handle(Context ctx) throws Exception {
    final Properties manifest = AssetPipelineConfigHolder.manifest;
    final AssetProperties props = ctx.get(AssetProperties.class);
    final ProductionAssetCache fileCache = ctx.get(ProductionAssetCache.class);
    Response response = ctx.getResponse();

    final String manifestPath = manifest.getProperty(props.getPath(), props.getPath());
    final Path asset = ctx.file(ASSET_BASE_PATH + manifestPath);
    final AssetPipelineResponseBuilder responseBuilder = new AssetPipelineResponseBuilder(props.getPath(),
            ctx.getRequest().getHeaders().get(HttpHeaderNames.IF_NONE_MATCH));

    //Is this in the attribute cache?
    AssetAttributes attributeCache = fileCache.get(manifestPath);
    if (attributeCache != null) {
        if (attributeCache.exists()) {
            response.contentTypeIfNotSet(props.getFormat());
            if (responseBuilder.headers != null) {
                for (Map.Entry<String, String> cursor : responseBuilder.headers.entrySet()) {
                    response.getHeaders().set(cursor.getKey(), cursor.getValue());
                }/*w w  w .  j a  v a  2  s .  com*/
            }
            if (responseBuilder.statusCode != null) {
                response.status(responseBuilder.statusCode);
            }

            if (responseBuilder.statusCode == null || responseBuilder.statusCode != 304) {
                if (acceptsGzip(ctx) && attributeCache.gzipExists()) {
                    Path gzipFile = ctx.file(ASSET_BASE_PATH + manifestPath + ".gz");
                    response.getHeaders().set("Content-Encoding", "gzip");
                    response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH,
                            Long.toString(attributeCache.getGzipFileSize()));
                    response.sendFile(gzipFile);
                } else {
                    response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH,
                            Long.toString(attributeCache.getFileSize()));
                    response.noCompress().sendFile(asset);
                }
            } else {
                response.send();
            }
        } else if (attributeCache.isDirectory()) {
            doIndexFileNext(ctx, props);
        } else {
            ctx.next();
        }
    } else {
        readAttributes(asset, attributes -> {
            if (attributes == null || !attributes.isRegularFile()) {

                if (props.getIndexedPath() != null && attributes != null) {
                    fileCache.put(manifestPath, new AssetAttributes(false, false, true, null, null));
                    doIndexFileNext(ctx, props);
                } else {
                    fileCache.put(manifestPath, new AssetAttributes(false, false, false, null, null));
                    ctx.next();
                }
            } else {
                response.contentTypeIfNotSet(props.getFormat());
                if (responseBuilder.headers != null) {
                    for (Map.Entry<String, String> cursor : responseBuilder.headers.entrySet()) {
                        response.getHeaders().set(cursor.getKey(), cursor.getValue());
                    }
                }

                if (responseBuilder.statusCode != null) {
                    response.status(responseBuilder.statusCode);
                }

                if (responseBuilder.statusCode == null || responseBuilder.statusCode != 304) {
                    Path gzipFile = ctx.file(ASSET_BASE_PATH + manifestPath + ".gz");
                    if (acceptsGzip(ctx)) {
                        readAttributes(gzipFile, gzipAttributes -> {
                            if (gzipAttributes == null || !gzipAttributes.isRegularFile()) {
                                response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH,
                                        Long.toString(attributes.size()));
                                fileCache.put(manifestPath,
                                        new AssetAttributes(true, false, false, attributes.size(), null));
                                response.noCompress().sendFile(asset);
                            } else {
                                response.getHeaders().set("Content-Encoding", "gzip");
                                response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH,
                                        Long.toString(gzipAttributes.size()));
                                fileCache.put(manifestPath, new AssetAttributes(true, true, false,
                                        attributes.size(), gzipAttributes.size()));
                                response.sendFile(gzipFile);
                            }
                        });
                    } else {
                        response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH,
                                Long.toString(attributes.size()));
                        response.noCompress().sendFile(asset);
                        readAttributes(gzipFile, gzipAttributes -> {
                            if (gzipAttributes == null || !gzipAttributes.isRegularFile()) {
                                fileCache.put(manifestPath,
                                        new AssetAttributes(true, false, false, attributes.size(), null));
                            } else {
                                fileCache.put(manifestPath, new AssetAttributes(true, true, false,
                                        attributes.size(), gzipAttributes.size()));
                            }
                        });
                    }
                } else {
                    response.send();
                }
            }
        });
    }
}

From source file:ratpack.file.internal.DefaultFileRenderer.java

License:Apache License

public static void sendFile(Context context, Path file, BasicFileAttributes attributes) {
    if (!context.getRequest().getMethod().isGet()) {
        context.clientError(405);//from  w  w  w.  j  ava2  s  .c  om
        return;
    }

    Date date = new Date(attributes.lastModifiedTime().toMillis());

    context.lastModified(date, () -> {
        final String ifNoneMatch = context.getRequest().getHeaders().get(HttpHeaderNames.IF_NONE_MATCH);
        Response response = context.getResponse();
        if (ifNoneMatch != null && ifNoneMatch.trim().equals("*")) {
            response.status(NOT_MODIFIED.code()).send();
            return;
        }

        response.contentTypeIfNotSet(
                () -> context.get(MimeTypes.class).getContentType(file.getFileName().toString()));

        try {
            response.sendFile(attributes, file);
        } catch (Exception e) {
            throw Exceptions.uncheck(e);
        }
    });
}

From source file:ratpack.file.internal.FileRenderer.java

License:Apache License

public static void sendFile(Context context, Path file, BasicFileAttributes attributes) {
    Date date = new Date(attributes.lastModifiedTime().toMillis());

    context.lastModified(date, () -> {
        final String ifNoneMatch = context.getRequest().getHeaders().get(HttpHeaderNames.IF_NONE_MATCH);
        Response response = context.getResponse();
        if (ifNoneMatch != null && ifNoneMatch.trim().equals("*")) {
            response.status(NOT_MODIFIED.code()).send();
            return;
        }//from w  ww.j a va2  s.  c om

        response.contentTypeIfNotSet(
                () -> context.get(MimeTypes.class).getContentType(file.getFileName().toString()));
        response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH, Long.toString(attributes.size()));
        try {
            response.sendFile(file);
        } catch (Exception e) {
            throw Exceptions.uncheck(e);
        }
    });
}