Example usage for org.springframework.web.context.request NativeWebRequest checkNotModified

List of usage examples for org.springframework.web.context.request NativeWebRequest checkNotModified

Introduction

In this page you can find the example usage for org.springframework.web.context.request NativeWebRequest checkNotModified.

Prototype

boolean checkNotModified(@Nullable String etag, long lastModifiedTimestamp);

Source Link

Document

Check whether the requested resource has been modified given the supplied ETag (entity tag) and last-modified timestamp, as determined by the application.

Usage

From source file:org.fao.geonet.api.records.formatters.FormatterApi.java

@RequestMapping(value = { "/api/records/{metadataUuid}/formatters/{formatterId}",
        "/api/" + API.VERSION_0_1
                + "/records/{metadataUuid}/formatters/{formatterId}" }, method = RequestMethod.GET, produces = {
                        MediaType.TEXT_HTML_VALUE, MediaType.APPLICATION_XHTML_XML_VALUE, "application/pdf",
                        MediaType.ALL_VALUE
        // TODO: PDF
})
@ApiOperation(value = "Get a formatted metadata record", nickname = "getRecordFormattedBy")
@ResponseBody/*from w w w . jav a 2s.  co  m*/
public void getRecordFormattedBy(
        @ApiParam(value = "Formatter type to use.") @RequestHeader(value = HttpHeaders.ACCEPT, defaultValue = MediaType.TEXT_HTML_VALUE) String acceptHeader,
        @PathVariable(value = "formatterId") final String formatterId,
        @ApiParam(value = API_PARAM_RECORD_UUID, required = true) @PathVariable String metadataUuid,
        @RequestParam(value = "width", defaultValue = "_100") final FormatterWidth width,
        @RequestParam(value = "mdpath", required = false) final String mdPath,
        @RequestParam(value = "output", required = false) FormatType formatType,
        @ApiIgnore final NativeWebRequest request, final HttpServletRequest servletRequest) throws Exception {

    ApplicationContext applicationContext = ApplicationContextHolder.get();
    Locale locale = languageUtils.parseAcceptLanguage(servletRequest.getLocales());

    // TODO :
    // if text/html > xsl_view
    // if application/pdf > xsl_view and PDF output
    // if application/x-gn-<formatterId>+(xml|html|pdf|text)
    // Force PDF ouutput when URL parameter is set.
    // This is useful when making GET link to PDF which
    // can not use headers.
    if (MediaType.ALL_VALUE.equals(acceptHeader)) {
        acceptHeader = MediaType.TEXT_HTML_VALUE;
    }
    if (formatType == null) {
        formatType = FormatType.find(acceptHeader);
    }
    if (formatType == null) {
        formatType = FormatType.xml;
    }

    final String language = LanguageUtils.locale2gnCode(locale.getISO3Language());
    final ServiceContext context = createServiceContext(language, formatType,
            request.getNativeRequest(HttpServletRequest.class));
    AbstractMetadata metadata = ApiUtils.canViewRecord(metadataUuid, servletRequest);

    Boolean hideWithheld = true;
    //        final boolean hideWithheld = Boolean.TRUE.equals(hide_withheld) ||
    //            !context.getBean(AccessManager.class).canEdit(context, resolvedId);
    Key key = new Key(metadata.getId(), language, formatType, formatterId, hideWithheld, width);
    final boolean skipPopularityBool = false;

    ISODate changeDate = metadata.getDataInfo().getChangeDate();

    Validator validator;
    if (changeDate != null) {
        final long changeDateAsTime = changeDate.toDate().getTime();
        long roundedChangeDate = changeDateAsTime / 1000 * 1000;
        if (request.checkNotModified(language, roundedChangeDate)
                && context.getBean(CacheConfig.class).allowCaching(key)) {
            if (!skipPopularityBool) {
                context.getBean(DataManager.class).increasePopularity(context,
                        String.valueOf(metadata.getId()));
            }
            return;
        }
        validator = new ChangeDateValidator(changeDateAsTime);
    } else {
        validator = new NoCacheValidator();
    }
    final FormatMetadata formatMetadata = new FormatMetadata(context, key, request);

    byte[] bytes;
    if (hasNonStandardParameters(request)) {
        // the http headers can cause a formatter to output custom output due to the parameters.
        // because it is not known how the parameters may affect the output then we have two choices
        // 1. make a unique cache for each configuration of parameters
        // 2. don't cache anything that has extra parameters beyond the standard parameters used to
        //    create the key
        // #1 has a major flaw because an attacker could simply make new requests always changing the parameters
        // and completely swamp the cache.  So we go with #2.  The formatters are pretty fast so it is a fine solution
        bytes = formatMetadata.call().data;
    } else {
        bytes = context.getBean(FormatterCache.class).get(key, validator, formatMetadata, false);
    }
    if (bytes != null) {
        if (!skipPopularityBool) {
            context.getBean(DataManager.class).increasePopularity(context, String.valueOf(metadata.getId()));
        }
        writeOutResponse(context, metadataUuid, locale.getISO3Language(),
                request.getNativeResponse(HttpServletResponse.class), formatType, bytes);
    }
}