Example usage for javax.servlet.http HttpServletResponse SC_NOT_MODIFIED

List of usage examples for javax.servlet.http HttpServletResponse SC_NOT_MODIFIED

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_NOT_MODIFIED.

Prototype

int SC_NOT_MODIFIED

To view the source code for javax.servlet.http HttpServletResponse SC_NOT_MODIFIED.

Click Source Link

Document

Status code (304) indicating that a conditional GET operation found that the resource was available and not modified.

Usage

From source file:com.taobao.diamond.server.controller.ConfigController.java

public String getConfig(HttpServletRequest request, HttpServletResponse response, String dataId, String group) {
    response.setHeader("Content-Type", "text/html;charset=GBK");
    final String address = getRemortIP(request);
    if (address == null) {
        // 400//  ww  w . j a  v  a2s. c o m
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return "400";
    }

    if (GlobalCounter.getCounter().decrementAndGet() >= 0) {
        response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        return "503";
    }

    String md5 = this.configService.getContentMD5(dataId, group);
    if (md5 == null) {
        return "404";
    }

    response.setHeader(Constants.CONTENT_MD5, md5);

    // 304double-check
    if (diskService.isModified(dataId, group)) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return "304";
    }
    String path = configService.getConfigInfoPath(dataId, group);
    // 
    if (diskService.isModified(dataId, group)) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return "304";
    }
    // 
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Cache-Control", "no-cache,no-store");
    return "forward:" + path;
}

From source file:net.java.dev.weblets.impl.servlets.WebletResponseImpl.java

public void setStatus(int statusCode) {
    switch (statusCode) {
    case WebletResponse.SC_ACCEPTED:
        setResponseStatus(HttpServletResponse.SC_ACCEPTED);
        break;//  w ww .  j  av  a  2 s  .  c  o m
    case WebletResponse.SC_NOT_FOUND:
        setResponseStatus(HttpServletResponse.SC_NOT_FOUND);
        break;
    case WebletResponse.SC_NOT_MODIFIED:
        setResponseStatus(HttpServletResponse.SC_NOT_MODIFIED);
        break;
    default:
        throw new IllegalArgumentException();
    }
}

From source file:org.kuali.mobility.writer.controllers.WriterMediaController.java

@RequestMapping(value = "/{mediaId}", method = RequestMethod.GET)
public void getMedia(@PathVariable int mediaId,
        @RequestParam(value = "thumb", required = false, defaultValue = "false") boolean thumb,
        HttpServletRequest request, HttpServletResponse response) throws IOException {

    long dateChanged = request.getDateHeader("If-Modified-Since") / 1000;

    File mediaFile = service.getMediaFile(mediaId, thumb);
    long mediaChanged = mediaFile.lastModified() / 1000;
    if (dateChanged == mediaChanged) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;/*from   w  ww . j ava2  s.c  om*/
    }
    Media media = service.getMedia(mediaId);
    response.setContentType(media.getMimeType());
    response.setDateHeader("Last-Modified", mediaFile.lastModified());

    int bytesWritten = IOUtils.copy(new FileInputStream(mediaFile), response.getOutputStream());
    response.setContentLength(bytesWritten);

}

From source file:com.starit.diamond.server.controller.ConfigController.java

/**
 * ???//from w w  w .j  a  v  a 2  s .  c o  m
 * 
 * @param request
 * @param dataId
 * @param clientGroup
 *            ?
 * @return
 */
@RequestMapping(method = RequestMethod.GET)
public String getConfig(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(Constants.DATAID) String dataId,
        @RequestParam(value = Constants.GROUP, required = false) String group) {
    response.setHeader("Content-Type", "text/plain;charset=UTF-8");
    final String address = getRemortIP(request);
    if (address == null) {
        // ?400
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return "400";

    }

    if (group == null) {
        group = Constants.DEFAULT_GROUP;
    }

    String md5 = this.configService.getContentMD5(dataId, group);
    if (md5 != null) {
        response.setHeader(Constants.CONTENT_MD5, md5);

        // Md5304
        String requestMd5 = request.getHeader(Constants.CONTENT_MD5);
        if (md5.equals(requestMd5)) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return "304";
        }
    } else {
        // 404.jsp, 404??, ???404.jsp
        return "404";
    }
    // 304???double-check??
    if (diskService.isModified(dataId, group)) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return "304";
    }
    String path = configService.getConfigInfoPath(dataId, address, group);
    // ?
    if (diskService.isModified(dataId, group)) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return "304";
    }
    // ?
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Cache-Control", "no-cache,no-store");
    return "forward:" + path;
}

From source file:com.mengka.diamond.server.controller.ConfigController.java

/**
 * ???/*  w  w w .  jav  a  2  s. com*/
 * 
 * @param request
 * @param dataId
 * @param group
 *            ?
 * @return
 */
@RequestMapping(method = RequestMethod.GET)
public String getConfig(HttpServletRequest request, HttpServletResponse response, String dataId, String group) {
    response.setHeader("Content-Type", "text/html;charset=utf-8");
    final String address = getRemortIP(request);
    if (address == null) {
        // ?400
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return "400";
    }

    if (GlobalCounter.getCounter().decrementAndGet() >= 0) {
        response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        return "503";
    }

    String md5 = this.configService.getContentMD5(dataId, group);
    if (md5 == null) {
        return "404";
    }

    response.setHeader(Constants.CONTENT_MD5, md5);

    // 304???double-check??
    if (diskService.isModified(dataId, group)) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return "304";
    }
    String path = configService.getConfigInfoPath(dataId, group);
    // ?
    if (diskService.isModified(dataId, group)) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return "304";
    }
    // ?
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Cache-Control", "no-cache,no-store");
    return "forward:" + path;
}

From source file:org.shredzone.commons.gravatar.GravatarCacheServlet.java

@Override
protected void doService(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Matcher m = HASH_PATTERN.matcher(req.getPathInfo());
    if (!m.matches()) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;//from www .  j a  v  a2  s .c om
    }

    String hash = m.group(1);

    GravatarService gs = getWebApplicationContext().getBean("gravatarService", GravatarService.class);
    File gravatarFile = gs.fetchGravatar(hash);

    long modifiedSinceTs = -1;
    try {
        modifiedSinceTs = req.getDateHeader("If-Modified-Since");
    } catch (IllegalArgumentException ex) {
        // As stated in RFC2616 Sec. 14.25, an invalid date will just be ignored.
    }

    if (modifiedSinceTs >= 0 && (modifiedSinceTs / 1000L) == (gravatarFile.lastModified() / 1000L)) {
        // The image has not been modified since last request
        resp.sendError(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }

    long size = gravatarFile.length();
    if (size > 0 && size <= Integer.MAX_VALUE) {
        // Cast to int is safe
        resp.setContentLength((int) size);
    }

    resp.setContentType("image/png");
    resp.setDateHeader("Date", System.currentTimeMillis());
    resp.setDateHeader("Last-Modified", gravatarFile.lastModified());

    try (InputStream in = new FileInputStream(gravatarFile)) {
        FileCopyUtils.copy(in, resp.getOutputStream());
    }
}

From source file:org.bedework.webcommon.location.FetchLocationsAction.java

@Override
public int doAction(final BwRequest request, final BwActionFormBase form) throws Throwable {
    if (debug) {/*from   w  ww.  ja  va2 s  . c  o m*/
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    }

    final DateFormat df = new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'");

    mapper.setDateFormat(df);

    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    final Client cl = request.getClient();
    form.setNocache(false);
    final String changeToken = cl.getCurrentChangeToken();

    final String ifNoneMatch = request.getRequest().getHeader("if-none-match");

    if ((changeToken != null) && changeToken.equals(ifNoneMatch)) {
        request.getResponse().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return forwardNull;
    }

    /* Add an etag */
    request.getResponse().addHeader("etag", changeToken);

    final BwSession sess = request.getSess();

    final Collection<BwLocation> vals;
    final String attrName;

    final String kind = request.getReqPar("kind", "owners");

    switch (kind) {
    case "owners":
        attrName = BwRequest.bwLocationsListName;
        vals = sess.getLocations(request, BwSession.ownersEntity, true);
        break;
    case "editable":
        attrName = BwRequest.bwEditableLocationsListName;

        vals = sess.getLocations(request, BwSession.editableEntity, false);
        //    } else if (kind.equals("preferred")) {
        //      attrName = BwRequest.bwPreferredLocationsListName;
        //
        //      vals = curAuthUserPrefs.getLocationPrefs().getPreferred();
        break;
    default:
        request.getResponse().setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return forwardNull;
    }

    request.getResponse().setContentType("text/json; charset=UTF-8");

    writeJson(request.getResponse(), vals);
    request.getResponse().getOutputStream().close();

    return forwardNull;
}

From source file:net.sf.ehcache.constructs.web.ResponseUtil.java

/**
 * Performs a number of checks to ensure response saneness according to the rules of RFC2616:
 * <ol>// ww  w . j av  a  2  s  . c  o  m
 * <li>If the response code is {@link javax.servlet.http.HttpServletResponse#SC_NO_CONTENT} then it is illegal for the body
 * to contain anything. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5
 * <li>If the response code is {@link javax.servlet.http.HttpServletResponse#SC_NOT_MODIFIED} then it is illegal for the body
 * to contain anything. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
 * </ol>
 *
 * @param request         the client HTTP request
 * @param responseStatus         the responseStatus
 * @return true if the response should be 0, even if it is isn't.
 */
public static boolean shouldBodyBeZero(HttpServletRequest request, int responseStatus) {

    //Check for NO_CONTENT
    if (responseStatus == HttpServletResponse.SC_NO_CONTENT) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(request.getRequestURL() + " resulted in a " + HttpServletResponse.SC_NO_CONTENT
                    + " response. Removing message body in accordance with RFC2616.");
        }
        return true;
    }

    //Check for NOT_MODIFIED
    if (responseStatus == HttpServletResponse.SC_NOT_MODIFIED) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(request.getRequestURL() + " resulted in a " + HttpServletResponse.SC_NOT_MODIFIED
                    + " response. Removing message body in accordance with RFC2616.");
        }
        return true;
    }

    return false;
}

From source file:org.broadleafcommerce.cms.web.file.StaticAssetView.java

@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    String cacheFilePath = (String) model.get("cacheFilePath");
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(cacheFilePath));
    try {/*  w w w  . j a v a2  s.  co  m*/
        String mimeType = (String) model.get("mimeType");
        response.setContentType(mimeType);
        if (!browserAssetCachingEnabled) {
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
        } else {
            response.setHeader("Cache-Control", "public");
            response.setHeader("Pragma", "cache");
            if (!StringUtils.isEmpty(request.getHeader("If-Modified-Since"))) {
                long lastModified = request.getDateHeader("If-Modified-Since");
                Calendar last = Calendar.getInstance();
                last.setTime(new Date(lastModified));
                Calendar check = Calendar.getInstance();
                check.add(Calendar.SECOND, -2 * new Long(cacheSeconds).intValue());
                if (check.compareTo(last) < 0) {
                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                    return;
                }
            } else {
                Calendar check = Calendar.getInstance();
                check.add(Calendar.SECOND, -1 * new Long(cacheSeconds).intValue());
                response.setDateHeader("Last-Modified", check.getTimeInMillis());
            }
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.SECOND, new Long(cacheSeconds).intValue());
            response.setDateHeader("Expires", cal.getTimeInMillis());
        }
        OutputStream os = response.getOutputStream();
        boolean eof = false;
        while (!eof) {
            int temp = bis.read();
            if (temp < 0) {
                eof = true;
            } else {
                os.write(temp);
            }
        }
        os.flush();
    } catch (Exception e) {
        if (e.getCause() instanceof SocketException) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Unable to stream asset", e);
            }
        } else {
            LOG.error("Unable to stream asset", e);
            throw e;
        }
    } finally {
        try {
            bis.close();
        } catch (Throwable e) {
            //do nothing
        }
    }
}

From source file:org.shredzone.cilla.view.ResourceView.java

/**
 * Streams a resource./*  ww w  . j ava2s .co  m*/
 */
@View(pattern = "/resource/${#package}/${#name}", name = "resource")
public void resourceView(@PathPart("#package") String pack, @PathPart("#name") String name,
        HttpServletRequest req, HttpServletResponse resp) throws ViewException {
    if (pack.indexOf("..") > -1 || pack.indexOf('/') > -1 || name.indexOf("..") > -1
            || name.indexOf('/') > -1) {
        throw new PageNotFoundException("resource '" + pack + '/' + name + "' not found");
    }

    try {
        String key = setup(pack, name);

        String resourceEtag = etagMap.get(key);
        resp.setHeader("ETag", resourceEtag);

        String headerEtag = req.getHeader("If-None-Match");

        if (headerEtag != null && headerEtag.equals(resourceEtag)) {
            throw new ErrorResponseException(HttpServletResponse.SC_NOT_MODIFIED);
        }

        if (isNotModifiedSince(req, lastModified)) {
            throw new ErrorResponseException(HttpServletResponse.SC_NOT_MODIFIED);
        }

        resp.setContentLength(sizeMap.get(key));
        resp.setDateHeader("Last-Modified", lastModified.getTime());

        try (InputStream in = ResourceView.class.getResourceAsStream("/public/" + pack + '/' + name)) {
            FileCopyUtils.copy(in, resp.getOutputStream());
        }

    } catch (IOException ex) {
        throw new PageNotFoundException("resource '" + pack + '/' + name + "' not found");
    }
}