private View getBestView(List<View> candidateViews, List<MediaType> requestedMediaTypes) {
   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.includes(candidateContentType)) {
           if (logger.isDebugEnabled()) {
             logger.debug(
                 "Returning ["
                     + candidateView
                     + "] based on requested media type '"
                     + mediaType
                     + "'");
           }
           return candidateView;
         }
       }
     }
   }
   return null;
 }
  @Test
  public void resolveViewNameAcceptHeaderSortByQuality() throws Exception {
    request.addHeader("Accept", "text/plain;q=0.5, application/json");

    ViewResolver htmlViewResolver = createMock(ViewResolver.class);
    ViewResolver jsonViewResolver = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(htmlViewResolver, jsonViewResolver));

    View htmlView = createMock("text_html", View.class);
    View jsonViewMock = createMock("application_json", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(htmlViewResolver.resolveViewName(viewName, locale)).andReturn(htmlView);
    expect(jsonViewResolver.resolveViewName(viewName, locale)).andReturn(jsonViewMock);
    expect(htmlView.getContentType()).andReturn("text/html").anyTimes();
    expect(jsonViewMock.getContentType()).andReturn("application/json").anyTimes();
    replay(htmlViewResolver, jsonViewResolver, htmlView, jsonViewMock);

    viewResolver.setFavorPathExtension(false);
    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", jsonViewMock, result);

    verify(htmlViewResolver, jsonViewResolver, htmlView, jsonViewMock);
  }
  @Test
  public void resolveViewNoMatchUseUnacceptableStatus() throws Exception {
    viewResolver.setUseNotAcceptableStatusCode(true);
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");

    ViewResolver viewResolverMock = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

    View viewMock = createMock("application_xml", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(viewMock);
    expect(viewMock.getContentType()).andReturn("application/pdf").anyTimes();

    replay(viewResolverMock, viewMock);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertNotNull("Invalid view", result);
    MockHttpServletResponse response = new MockHttpServletResponse();
    result.render(null, request, response);
    assertEquals("Invalid status code set", 406, response.getStatus());

    verify(viewResolverMock, viewMock);
  }
  @Test
  public void resolveViewNameRedirectView() throws Exception {
    request.addHeader("Accept", "application/json");
    request.setRequestURI("/test");

    StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
    webAppContext.setServletContext(new MockServletContext());
    webAppContext.refresh();

    UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver();
    urlViewResolver.setApplicationContext(webAppContext);
    ViewResolver xmlViewResolver = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(xmlViewResolver, urlViewResolver));

    View xmlView = createMock("application_xml", View.class);
    View jsonView = createMock("application_json", View.class);
    viewResolver.setDefaultViews(Arrays.asList(jsonView));

    String viewName = "redirect:anotherTest";
    Locale locale = Locale.ENGLISH;

    expect(xmlViewResolver.resolveViewName(viewName, locale)).andReturn(xmlView);
    expect(jsonView.getContentType()).andReturn("application/json").anyTimes();

    replay(xmlViewResolver, xmlView, jsonView);

    View actualView = viewResolver.resolveViewName(viewName, locale);
    assertEquals("Invalid view", RedirectView.class, actualView.getClass());

    verify(xmlViewResolver, xmlView, jsonView);
  }
  @Test
  public void resolveViewNameFilename() throws Exception {
    request.setRequestURI("/test.html");

    ViewResolver viewResolverMock1 = createMock("viewResolver1", ViewResolver.class);
    ViewResolver viewResolverMock2 = createMock("viewResolver2", ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

    View viewMock1 = createMock("application_xml", View.class);
    View viewMock2 = createMock("text_html", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
    expect(viewResolverMock1.resolveViewName(viewName + ".html", locale)).andReturn(null);
    expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(null);
    expect(viewResolverMock2.resolveViewName(viewName + ".html", locale)).andReturn(viewMock2);
    expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
    expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();

    replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock2, result);

    verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);
  }
  @Test
  public void resolveViewNameWithRequestParameter() throws Exception {
    request.addParameter("format", "xls");

    viewResolver.setFavorParameter(true);
    viewResolver.setMediaTypes(Collections.singletonMap("xls", "application/vnd.ms-excel"));

    ViewResolver viewResolverMock = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

    View viewMock = createMock("application_xls", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(null);
    expect(viewResolverMock.resolveViewName(viewName + ".xls", locale)).andReturn(viewMock);
    expect(viewMock.getContentType()).andReturn("application/vnd.ms-excel").anyTimes();

    replay(viewResolverMock, viewMock);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock, result);

    verify(viewResolverMock, viewMock);
  }
  @Test
  public void resolveViewNameAcceptHeader() throws Exception {
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

    ViewResolver viewResolverMock1 = createMock(ViewResolver.class);
    ViewResolver viewResolverMock2 = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

    View viewMock1 = createMock("application_xml", View.class);
    View viewMock2 = createMock("text_html", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
    expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(viewMock2);
    expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
    expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();

    replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock2, result);

    verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);
  }
 @Test(expected = SoyTofuException.class)
 public void shouldThrowExceptionIfTemplateDoesntExist() throws Exception {
   View view =
       cachingClosureTemplateViewResolver.resolveViewName(
           "com.tomakehurst.doesntexist", Locale.UK);
   view.render(model, request, response);
 }
 private void assertRedirectToMainProfilePage(ModelAndView modelAndView) {
   View expectedViewReturnValue = new RedirectView("/player/profile", false, false, false);
   View actualViewReturnValue = modelAndView.getView();
   assertEquals(
       "Redirect should go to the expected URI",
       actualViewReturnValue.toString(),
       expectedViewReturnValue.toString());
 }
 private MockHttpServletResponse render(String viewName, Locale locale) throws Exception {
   GroovyTemplateViewResolver resolver = this.context.getBean(GroovyTemplateViewResolver.class);
   View view = resolver.resolveViewName(viewName, locale);
   assertThat(view, notNullValue());
   HttpServletRequest request = new MockHttpServletRequest();
   request.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
   MockHttpServletResponse response = new MockHttpServletResponse();
   view.render(null, request, response);
   return response;
 }
  @Test
  public void resolveViewNoMatch() throws Exception {
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");

    ViewResolver viewResolverMock = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

    View viewMock = createMock("application_xml", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(viewMock);
    expect(viewMock.getContentType()).andReturn("application/pdf").anyTimes();

    replay(viewResolverMock, viewMock);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertNull("Invalid view", result);

    verify(viewResolverMock, viewMock);
  }
  @Test
  public void resolveViewNameWithPathExtension() throws Exception {
    request.setRequestURI("/test.xls");

    ViewResolver viewResolverMock = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

    View viewMock = createMock("application_xls", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(null);
    expect(viewResolverMock.resolveViewName(viewName + ".xls", locale)).andReturn(viewMock);
    expect(viewMock.getContentType()).andReturn("application/vnd.ms-excel").anyTimes();

    replay(viewResolverMock, viewMock);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock, result);

    verify(viewResolverMock, viewMock);
  }
Ejemplo n.º 13
0
 protected void doRender(Map<String, ?> model) throws Exception {
   RequestContext context = getRequestContext();
   ExternalContext externalContext = context.getExternalContext();
   View view = getView();
   PortletContext portletContext = (PortletContext) externalContext.getNativeContext();
   PortletRequest request = (PortletRequest) externalContext.getNativeRequest();
   MimeResponse response = (MimeResponse) externalContext.getNativeResponse();
   if (response.getContentType() == null) {
     // No Portlet content type specified yet -> use the view-determined type.
     // (The Portlet spec requires the content type to be set on the RenderResponse)
     String contentType = view.getContentType();
     if (contentType != null) {
       response.setContentType(contentType);
     }
   }
   request.setAttribute(ViewRendererServlet.VIEW_ATTRIBUTE, view);
   request.setAttribute(ViewRendererServlet.MODEL_ATTRIBUTE, model);
   request.setAttribute(
       org.springframework.web.servlet.support.RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
       context.getActiveFlow().getApplicationContext());
   portletContext
       .getRequestDispatcher(DispatcherPortlet.DEFAULT_VIEW_RENDERER_URL)
       .include(request, response);
 }
  @Test
  public void resolveViewNameFilenameDefaultView() throws Exception {
    request.setRequestURI("/test.json");

    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", "application/json");
    viewResolver.setMediaTypes(mediaTypes);

    ViewResolver viewResolverMock1 = createMock(ViewResolver.class);
    ViewResolver viewResolverMock2 = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

    View viewMock1 = createMock("application_xml", View.class);
    View viewMock2 = createMock("text_html", View.class);
    View viewMock3 = createMock("application_json", View.class);

    List<View> defaultViews = new ArrayList<View>();
    defaultViews.add(viewMock3);
    viewResolver.setDefaultViews(defaultViews);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
    expect(viewResolverMock1.resolveViewName(viewName + ".json", locale)).andReturn(null);
    expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(viewMock2);
    expect(viewResolverMock2.resolveViewName(viewName + ".json", locale)).andReturn(null);
    expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
    expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();
    expect(viewMock3.getContentType()).andReturn("application/json").anyTimes();

    replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock3, result);

    verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);
  }
 private String render(View view) throws Exception {
   response = new MockHttpServletResponse();
   view.render(model, request, response);
   return response.getContentAsString();
 }