@Override protected String getMediaTypeKey(NativeWebRequest webRequest) { HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); if (request == null) { logger.warn("An HttpServletRequest is required to determine the media type key"); return null; } String path = PATH_HELPER.getLookupPathForRequest(request); String filename = WebUtils.extractFullFilenameFromUrlPath(path); String extension = StringUtils.getFilenameExtension(filename); return (StringUtils.hasText(extension)) ? extension.toLowerCase(Locale.ENGLISH) : null; }
/** * Lookup the CMS Content Page for this request. * * @param request The request * @return the CMS content page */ protected ContentPageModel getContentPageForRequest(final HttpServletRequest request) { // Get the path for this request. // Note that the path begins with a '/' final String lookupPathForRequest = urlPathHelper.getLookupPathForRequest(request); try { // Lookup the CMS Content Page by label. Note that the label value must begin with a '/'. return getCmsPageService().getPageForLabel(lookupPathForRequest); } catch (final CMSItemNotFoundException ignore) { // Ignore exception } return null; }
public static boolean matchAll( HttpServletRequest request, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, String[] patterns) { if (ArrayUtils.isNotEmpty(patterns)) { String lookupPath = urlPathHelper.getLookupPathForRequest(request); for (String pattern : patterns) { if (!pathMatcher.match(pattern, lookupPath)) { return false; } } } return true; }
@Override protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception { String lookupPath = urlPathHelper.getLookupPathForRequest(request); if (logger.isDebugEnabled()) { logger.debug("Looking up handler method for path " + lookupPath); } HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request); if (logger.isDebugEnabled()) { if (handlerMethod != null) { logger.debug("Returning handler method [" + handlerMethod + "]"); } else { logger.debug("Did not find handler method for [" + lookupPath + "]"); } } return (handlerMethod != null) ? handlerMethod.createWithResolvedBean() : null; }
/** * Determines the list of {@link MediaType} for the given {@link HttpServletRequest}. * * <p>The default implementation invokes {@link #getMediaTypeFromFilename(String)} if {@linkplain * #setFavorPathExtension favorPathExtension} property is <code>true</code>. If the property is * <code>false</code>, or when a media type cannot be determined from the request path, this * method will inspect the {@code Accept} header of the request. * * <p>This method can be overridden to provide a different algorithm. * * @param request the current servlet request * @return the list of media types requested, if any */ protected List<MediaType> getMediaTypes(HttpServletRequest request) { if (this.favorPathExtension) { String requestUri = urlPathHelper.getLookupPathForRequest(request); String filename = WebUtils.extractFullFilenameFromUrlPath(requestUri); MediaType mediaType = getMediaTypeFromFilename(filename); if (mediaType != null) { if (logger.isDebugEnabled()) { logger.debug( "Requested media type is '" + mediaType + "' (based on filename '" + filename + "')"); } return Collections.singletonList(mediaType); } } if (this.favorParameter) { if (request.getParameter(this.parameterName) != null) { String parameterValue = request.getParameter(this.parameterName); MediaType mediaType = getMediaTypeFromParameter(parameterValue); if (mediaType != null) { if (logger.isDebugEnabled()) { logger.debug( "Requested media type is '" + mediaType + "' (based on parameter '" + this.parameterName + "'='" + parameterValue + "')"); } return Collections.singletonList(mediaType); } } } if (!this.ignoreAcceptHeader) { String acceptHeader = request.getHeader(ACCEPT_HEADER); if (StringUtils.hasText(acceptHeader)) { try { List<MediaType> acceptableMediaTypes = MediaType.parseMediaTypes(acceptHeader); List<MediaType> producibleMediaTypes = getProducibleMediaTypes(request); Set<MediaType> compatibleMediaTypes = new LinkedHashSet<MediaType>(); for (MediaType acceptable : acceptableMediaTypes) { for (MediaType producible : producibleMediaTypes) { if (acceptable.isCompatibleWith(producible)) { compatibleMediaTypes.add(getMostSpecificMediaType(acceptable, producible)); } } } List<MediaType> mediaTypes = new ArrayList<MediaType>(compatibleMediaTypes); MediaType.sortByQualityValue(mediaTypes); if (logger.isDebugEnabled()) { logger.debug( "Requested media types are " + mediaTypes + " based on Accept header types " + "and producible media types " + producibleMediaTypes + ")"); } return mediaTypes; } catch (IllegalArgumentException ex) { if (logger.isDebugEnabled()) { logger.debug( "Could not parse accept header [" + acceptHeader + "]: " + ex.getMessage()); } return null; } } } if (this.defaultContentType != null) { if (logger.isDebugEnabled()) { logger.debug( "Requested media types is " + this.defaultContentType + " (based on defaultContentType property)"); } return Collections.singletonList(this.defaultContentType); } else { return Collections.emptyList(); } }