Example usage for com.google.common.net HttpHeaders IF_NONE_MATCH

List of usage examples for com.google.common.net HttpHeaders IF_NONE_MATCH

Introduction

In this page you can find the example usage for com.google.common.net HttpHeaders IF_NONE_MATCH.

Prototype

String IF_NONE_MATCH

To view the source code for com.google.common.net HttpHeaders IF_NONE_MATCH.

Click Source Link

Document

The HTTP If-None-Match header field name.

Usage

From source file:com.cnksi.core.web.utils.Servlets.java

/**
 * ?? If-None-Match Header, Etag?./*  ww  w  . ja v  a 2  s .c  o  m*/
 * 
 * Etag, checkIfNoneMatchfalse, 304 not modify status.
 * 
 * @param etag ETag.
 */
public static boolean checkIfNoneMatchEtag(HttpServletRequest request, HttpServletResponse response,
        String etag) {

    String headerValue = request.getHeader(HttpHeaders.IF_NONE_MATCH);
    if (headerValue != null) {
        boolean conditionSatisfied = false;
        if (!"*".equals(headerValue)) {
            StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ",");

            while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
                String currentToken = commaTokenizer.nextToken();
                if (currentToken.trim().equals(etag)) {
                    conditionSatisfied = true;
                }
            }
        } else {
            conditionSatisfied = true;
        }

        if (conditionSatisfied) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            response.setHeader(HttpHeaders.ETAG, etag);
            return false;
        }
    }
    return true;
}

From source file:com.googlesource.gerrit.plugins.lfs.fs.LfsFsContentServlet.java

@Override
protected void doHead(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
    String verifyId = req.getHeader(HttpHeaders.IF_NONE_MATCH);
    if (Strings.isNullOrEmpty(verifyId)) {
        doGet(req, rsp);//from   w w  w  .  j a va  2 s. c  om
        return;
    }

    Optional<AnyLongObjectId> obj = validateGetRequest(req, rsp);
    if (obj.isPresent() && obj.get().getName().equalsIgnoreCase(verifyId)) {
        rsp.addHeader(HttpHeaders.ETAG, obj.get().getName());
        rsp.setStatus(HttpStatus.SC_NOT_MODIFIED);
        return;
    }

    getObject(req, rsp, obj);
}

From source file:com.trc.core2.web.Servlets.java

/**
 * ?? If-None-Match Header, Etag?.//  w  w w. j ava2 s  . co  m
 * 
 * Etag, checkIfNoneMatchfalse, 304 not modify status.
 * 
 * @param etag ETag.
 */
public static boolean checkIfNoneMatchEtag(HttpServletRequest request, HttpServletResponse response,
        String etag) {
    String headerValue = request.getHeader(HttpHeaders.IF_NONE_MATCH);
    if (headerValue != null) {
        boolean conditionSatisfied = false;
        if (!"*".equals(headerValue)) {
            StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ",");

            while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
                String currentToken = commaTokenizer.nextToken();
                if (currentToken.trim().equals(etag)) {
                    conditionSatisfied = true;
                }
            }
        } else {
            conditionSatisfied = true;
        }

        if (conditionSatisfied) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            response.setHeader(HttpHeaders.ETAG, etag);
            return false;
        }
    }
    return true;
}

From source file:com.emaxcore.emaxdata.common.web.Servlets.java

/**
 * ?? If-None-Match Header, Etag?./*  ww w  .java  2s. co m*/
 *
 * Etag, checkIfNoneMatchfalse, 304 not modify status.
 *
 * @param etag ETag.
 */
public static boolean checkIfNoneMatchEtag(HttpServletRequest request, HttpServletResponse response,
        String etag) {
    String headerValue = request.getHeader(HttpHeaders.IF_NONE_MATCH);
    if (headerValue != null) {
        boolean conditionSatisfied = false;
        if (!"*".equals(headerValue)) {
            StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ",");
            while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
                String currentToken = commaTokenizer.nextToken();
                if (currentToken.trim().equals(etag)) {
                    conditionSatisfied = true;
                }
            }
        } else {
            conditionSatisfied = true;
        }

        if (conditionSatisfied) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            response.setHeader(HttpHeaders.ETAG, etag);
            return false;
        }
    }
    return true;
}

From source file:org.ardverk.dropwizard.assets.AssetsDirectoryServlet.java

/**
 * Returns {@code true} if the client has an up-to-date version of the asset.
 *//* w w w  .ja  v  a2  s.  com*/
private static boolean isCurrent(HttpServletRequest request, AssetsDirectory.Entry entry) {
    String etag = entry.getETag();
    long lastModified = entry.getLastModified();

    return etag.equals(request.getHeader(HttpHeaders.IF_NONE_MATCH))
            || (request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE) >= lastModified);
}

From source file:com.sector91.wit.http.ETag.java

public boolean matches(Request request) {
    final List<String> etags = request.getValues(HttpHeaders.IF_NONE_MATCH);
    for (String etag : etags) {
        if (str.equals(etag)) {
            return true;
        }/*w  w  w. ja v a 2 s  .  c  o  m*/
    }
    return false;
}

From source file:com.google.gitiles.doc.DocServlet.java

@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
    Config cfg = getAccess(req).getConfig();
    if (!cfg.getBoolean("markdown", "render", true)) {
        res.setStatus(SC_NOT_FOUND);/*from w  w w  .  ja  va2 s  .com*/
        return;
    }

    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);
    RevWalk rw = new RevWalk(repo);
    try {
        String path = view.getPathPart();
        RevTree root;
        try {
            root = rw.parseTree(view.getRevision().getId());
        } catch (IncorrectObjectTypeException e) {
            res.setStatus(SC_NOT_FOUND);
            return;
        }

        SourceFile srcmd = findFile(rw, root, path);
        if (srcmd == null) {
            res.setStatus(SC_NOT_FOUND);
            return;
        }

        SourceFile navmd = findFile(rw, root, NAVBAR_MD);
        String reqEtag = req.getHeader(HttpHeaders.IF_NONE_MATCH);
        String curEtag = etag(srcmd, navmd);
        if (reqEtag != null && reqEtag.equals(curEtag)) {
            res.setStatus(SC_NOT_MODIFIED);
            return;
        }

        view = view.toBuilder().setPathPart(srcmd.path).build();
        int inputLimit = cfg.getInt("markdown", "inputLimit", 5 << 20);
        RootNode doc = GitilesMarkdown.parseFile(view, srcmd.path,
                srcmd.read(rw.getObjectReader(), inputLimit));
        if (doc == null) {
            res.sendRedirect(GitilesView.show().copyFrom(view).toUrl());
            return;
        }

        RootNode nav = null;
        if (navmd != null) {
            nav = GitilesMarkdown.parseFile(view, navmd.path, navmd.read(rw.getObjectReader(), inputLimit));
            if (nav == null) {
                res.setStatus(SC_INTERNAL_SERVER_ERROR);
                return;
            }
        }

        int imageLimit = cfg.getInt("markdown", "imageLimit", 256 << 10);
        ImageLoader img = null;
        if (imageLimit > 0) {
            img = new ImageLoader(rw.getObjectReader(), view, root, srcmd.path, imageLimit);
        }

        res.setHeader(HttpHeaders.ETAG, curEtag);
        showDoc(req, res, view, cfg, img, nav, doc);
    } finally {
        rw.release();
    }
}

From source file:com.sector91.wit.responders.Page.java

@Override
public final void respond(Zero params, Request request, Response response) throws IOException {
    boolean gzipSupported = request.getValue(HttpHeaders.ACCEPT_ENCODING).contains("gzip");
    byte[] data = gzipSupported ? compressed : uncompressed;

    response.setValue(HttpHeaders.ETAG, etag);
    response.setDate(HttpHeaders.LAST_MODIFIED, timestamp);

    long ifModifiedSince = request.getDate(HttpHeaders.IF_MODIFIED_SINCE);
    if (ifModifiedSince >= timestamp || etag.equals(request.getValue(HttpHeaders.IF_NONE_MATCH))) {
        response.setStatus(Status.NOT_MODIFIED);
        response.getOutputStream().close();
        return;//w w w. j a  v  a 2  s  .c om
    }

    try (OutputStream stream = response.getOutputStream()) {
        response.setContentType(contentType);
        response.setContentLength(data.length);
        if (gzipSupported)
            response.setValue(HttpHeaders.CONTENT_ENCODING, "gzip");
        if (!request.getMethod().equals("HEAD"))
            stream.write(data);
    }
}

From source file:com.eucalyptus.tokens.oidc.OidcDiscoveryCache.java

private OidcDiscoveryCachedResource fetchResource(final String url, final long timeNow,
        final OidcDiscoveryCachedResource cached) throws IOException {
    final URL location = new URL(url);
    final OidcResource oidcResource;
    { // setup url connection and resolve
        final HttpURLConnection conn = (HttpURLConnection) location.openConnection();
        conn.setAllowUserInteraction(false);
        conn.setInstanceFollowRedirects(false);
        conn.setConnectTimeout(CONNECT_TIMEOUT);
        conn.setReadTimeout(READ_TIMEOUT);
        conn.setUseCaches(false);//from w ww  .  j  a v a  2s.  com
        if (cached != null) {
            if (cached.lastModified.isDefined()) {
                conn.setRequestProperty(HttpHeaders.IF_MODIFIED_SINCE, cached.lastModified.get());
            }
            if (cached.etag.isDefined()) {
                conn.setRequestProperty(HttpHeaders.IF_NONE_MATCH, cached.etag.get());
            }
        }
        oidcResource = resolve(conn);
    }

    // build cache entry from resource
    if (oidcResource.statusCode == 304) {
        return new OidcDiscoveryCachedResource(timeNow, cached);
    } else {
        return new OidcDiscoveryCachedResource(timeNow, Option.of(oidcResource.lastModifiedHeader),
                Option.of(oidcResource.etagHeader), ImmutableList.copyOf(oidcResource.certs), url,
                new String(oidcResource.content, StandardCharsets.UTF_8));
    }
}

From source file:com.sector91.wit.responders.CachedResponder.java

@Override
public final void respond(P params, Request request, Response response) throws HttpException, IOException {
    final CachedResponse cachedResponse;
    try {/*  w  w w.  j  a v  a 2 s .c om*/
        localRequest.set(request);
        localResponse.set(response);
        cachedResponse = cache.get(params);
    } catch (ExecutionException ex) {
        try {
            throw ex.getCause();
        } catch (TooLargeForCacheException ex2) {
            Log.debug(TAG, ex2.getMessage());
            return;
        } catch (HttpException | IOException | RuntimeException ex2) {
            throw ex2;
        } catch (Throwable ex2) {
            throw new HttpException(Status.INTERNAL_SERVER_ERROR, ex2);
        }
    } finally {
        localRequest.set(null);
        localResponse.set(null);
    }
    if (!response.isCommitted()) {
        if (useETags) {
            final List<String> etags = request.getValues(HttpHeaders.IF_NONE_MATCH);
            if (etags.contains(cachedResponse.etag)) {
                Log.trace(TAG, "Response not modified for URL '" + request.getPath() + "', with params "
                        + params + ".");
                response.setStatus(Status.NOT_MODIFIED);
                response.setValue(HttpHeaders.ETAG, cachedResponse.etag);
                response.close();
                return;
            }
        } else {
            final long ifModifiedSince = request.getDate(HttpHeaders.IF_MODIFIED_SINCE);
            if (ifModifiedSince >= (cachedResponse.timestamp / 1000) * 1000) {
                Log.trace(TAG, "Response not modified for URL '" + request.getPath() + "', with params "
                        + params + ".");
                response.setStatus(Status.NOT_MODIFIED);
                response.close();
                return;
            }
        }
        cachedResponse.writeTo(response);
    }
}