Example usage for org.springframework.web.servlet View getContentType

List of usage examples for org.springframework.web.servlet View getContentType

Introduction

In this page you can find the example usage for org.springframework.web.servlet View getContentType.

Prototype

@Nullable
default String getContentType() 

Source Link

Document

Return the content type of the view, if predetermined.

Usage

From source file:architecture.ee.web.servlet.ViewRendererServlet.java

protected View resolvingView(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse) {
    /**//from  ww  w .  jav a2s.c om
     * ? VIEW    ContentType ?  ?? ?  .
     */
    View view = (View) httpservletrequest.getAttribute(WebApplicatioinConstants.VIEW_ATTRIBUTE);
    if (view == null) {
        try {
            String viewName = translator.getViewName(httpservletrequest);
            ViewResolver viewresolver = getViewResolver(viewResolverName);
            view = viewresolver.resolveViewName(viewName, httpservletrequest.getLocale());
        } catch (Exception exception) {
            log.error(exception);
        }

        if (view != null && view instanceof AbstractView) {

            OutputFormat format = ServletUtils.getOutputFormat(httpservletrequest, httpservletresponse);
            if (log.isDebugEnabled())
                log.debug("architecture.ee.web.community.content: " + format);
            if (format == OutputFormat.XML) {
                httpservletresponse.setContentType(null);
                ((AbstractView) view).setContentType("text/xml;charset=UTF-8");
            } else if (format == OutputFormat.HTML) {
                httpservletresponse.setContentType(null);
                ((AbstractView) view).setContentType("text/html;charset=UTF-8");
            }
        }
    }

    if (log.isDebugEnabled()) {
        log.debug(
                "response architecture.ee.web.community.content type: " + httpservletresponse.getContentType());
        log.debug("architecture.ee.web.community.content type : " + view.getContentType());
    }
    return view;
}

From source file:org.beadle.framework.view.ReturnTypeViewResolver.java

private View getBestView(List<View> candidateViews, List<MediaType> requestedMediaTypes,
        RequestAttributes attrs) {/*from   w  w  w  .  jav  a 2s .c om*/
    HttpServletRequest request = ((ServletRequestAttributes) attrs).getRequest();
    for (View candidateView : candidateViews) {
        if (StringUtils.hasText(candidateView.getContentType())) {
            ModelAndView mv = (ModelAndView) request.getAttribute("ModelAndView");
            //message...
            Object result = mv.getModelMap().get("message");
            MediaType candidateContentType = MediaType.parseMediaType(candidateView.getContentType());
            if (result == null && candidateContentType.isCompatibleWith(MediaType.TEXT_HTML)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Returning [" + candidateView + "] based on requested media type '"
                            + candidateContentType + "'");
                }
                attrs.setAttribute(View.SELECTED_CONTENT_TYPE, candidateContentType,
                        RequestAttributes.SCOPE_REQUEST);
                return candidateView;
            } else if (result != null && candidateContentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Returning [" + candidateView + "] based on requested media type '"
                            + candidateContentType + "'");
                }
                attrs.setAttribute(View.SELECTED_CONTENT_TYPE, candidateContentType,
                        RequestAttributes.SCOPE_REQUEST);
                return candidateView;
            }
        }
    }
    return null;
}

From source file:org.springframework.web.portlet.DispatcherPortlet.java

/**
 * Render the given ModelAndView. This is the last stage in handling a request.
 * It may involve resolving the view by name.
 * @param mv the ModelAndView to render// ww  w .  j  av a 2 s . c om
 * @param request current portlet render request
 * @param response current portlet render response
 * @throws Exception if there's a problem rendering the view
 */
protected void render(ModelAndView mv, PortletRequest request, MimeResponse response) throws Exception {
    View view;
    if (mv.isReference()) {
        // We need to resolve the view name.
        view = resolveViewName(mv.getViewName(), mv.getModelInternal(), request);
        if (view == null) {
            throw new PortletException("Could not resolve view with name '" + mv.getViewName()
                    + "' in portlet with name '" + getPortletName() + "'");
        }
    } else {
        // No need to lookup: the ModelAndView object contains the actual View object.
        Object viewObject = mv.getView();
        if (viewObject == null) {
            throw new PortletException("ModelAndView [" + mv + "] neither contains a view name nor a "
                    + "View object in portlet with name '" + getPortletName() + "'");
        }
        if (!(viewObject instanceof View)) {
            throw new PortletException("View object [" + viewObject
                    + "] is not an instance of [org.springframework.web.servlet.View] - "
                    + "DispatcherPortlet does not support any other view types");
        }
        view = (View) viewObject;
    }

    // Set the content type on the response if needed and if possible.
    // The Portlet spec requires the content type to be set on the RenderResponse;
    // it's not sufficient to let the View set it on the ServletResponse.
    if (response.getContentType() != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Portlet response content type already set to [" + response.getContentType() + "]");
        }
    } else {
        // No Portlet content type specified yet -> use the view-determined type.
        String contentType = view.getContentType();
        if (contentType != null) {
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "Setting portlet response content type to view-determined type [" + contentType + "]");
            }
            response.setContentType(contentType);
        }
    }

    doRender(view, mv.getModelInternal(), request, response);
}

From source file:org.springframework.web.servlet.view.ContentNegotiatingViewResolver.java

private View getBestView(List<View> candidateViews, List<MediaType> requestedMediaTypes,
        RequestAttributes attrs) {/*from  www .j ava 2 s .com*/
    for (View candidateView : candidateViews) {
        if (candidateView instanceof SmartView) {
            SmartView smartView = (SmartView) candidateView;
            if (smartView.isRedirectView()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Returning redirect view [" + candidateView + "]");
                }
                return candidateView;
            }
        }
    }
    for (MediaType mediaType : requestedMediaTypes) {
        for (View candidateView : candidateViews) {
            if (StringUtils.hasText(candidateView.getContentType())) {
                MediaType candidateContentType = MediaType.parseMediaType(candidateView.getContentType());
                if (mediaType.isCompatibleWith(candidateContentType)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Returning [" + candidateView + "] based on requested media type '"
                                + mediaType + "'");
                    }
                    attrs.setAttribute(View.SELECTED_CONTENT_TYPE, mediaType, RequestAttributes.SCOPE_REQUEST);
                    return candidateView;
                }
            }
        }
    }
    return null;
}