ViewableWriter implements MessageBodyWriter<Viewable> { @Override public void writeTo(Viewable viewable, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> headers, OutputStream out) throws IOException, WebApplicationException { final ViewEngine engine = engineFinder.find(viewable); if (engine == null) { throw new ServerErrorException(messages.get("NoViewEngine", viewable), INTERNAL_SERVER_ERROR); } HttpServletRequest request = unwrap(injectedRequest, HttpServletRequest.class); HttpServletResponse response = unwrap(injectedResponse, HttpServletResponse.class); final ServletOutputStream responseStream = new DelegatingServletOutputStream(out); final PrintWriter responseWriter = new PrintWriter(new OutputStreamWriter(responseStream, getCharset(headers))); final HttpServletResponse responseWrapper = new MvcHttpServletResponse(response, responseStream, responseWriter); try { Models models = viewable.getModels(); if (models == null) { models = modelsInstance.get(); } models.put("mvc", mvc); if (OzarkCdiExtension.isEventObserved(BeforeProcessViewEvent.class)) { final BeforeProcessViewEventImpl event = new BeforeProcessViewEventImpl(); event.setEngine(engine.getClass()); event.setView(viewable.getView()); dispatcher.fire(event); } engine.processView(new ViewEngineContextImpl(viewable.getView(), models, request, responseWrapper, headers, responseStream, mediaType, uriInfo, resourceInfo, config, mvc.getLocale())); if (OzarkCdiExtension.isEventObserved(AfterProcessViewEvent.class)) { final AfterProcessViewEventImpl event = new AfterProcessViewEventImpl(); event.setEngine(engine.getClass()); event.setView(viewable.getView()); dispatcher.fire(event); } } catch (ViewEngineException e) { throw new ServerErrorException(INTERNAL_SERVER_ERROR, e); } finally { responseWriter.flush(); } }  @Override boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType); @Override long getSize(Viewable viewable, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType); @Override void writeTo(Viewable viewable, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType,
                        MultivaluedMap<String, Object> headers, OutputStream out); static final String CONTENT_TYPE; static final Charset UTF8; }
@Test public void testWriteTo() throws Exception { ViewableWriter writer = new ViewableWriter(); Field mvcField = writer.getClass().getDeclaredField("mvc"); mvcField.setAccessible(true); mvcField.set(writer, new MvcContextImpl()); ViewEngineFinder finder = EasyMock.createStrictMock(ViewEngineFinder.class); Field finderField = writer.getClass().getDeclaredField("engineFinder"); finderField.setAccessible(true); finderField.set(writer, finder); HttpServletRequest request = EasyMock.createStrictMock(HttpServletRequest.class); Field requestField = writer.getClass().getDeclaredField("injectedRequest"); requestField.setAccessible(true); requestField.set(writer, request); Event<MvcEvent> dispatcher = EasyMock.createStrictMock(Event.class); Field dispatcherField = writer.getClass().getDeclaredField("dispatcher"); dispatcherField.setAccessible(true); dispatcherField.set(writer, dispatcher); ViewEngine viewEngine = EasyMock.createStrictMock(ViewEngine.class); HttpServletResponse response = EasyMock.createStrictMock(HttpServletResponse.class); Field responseField = writer.getClass().getDeclaredField("injectedResponse"); responseField.setAccessible(true); responseField.set(writer, response); Configuration config = EasyMock.createStrictMock(Configuration.class); Field configField = writer.getClass().getDeclaredField("config"); configField.setAccessible(true); configField.set(writer, config); MultivaluedHashMap map = new MultivaluedHashMap(); ArrayList<MediaType> contentTypes = new ArrayList<>(); contentTypes.add(MediaType.TEXT_HTML_TYPE); map.put("Content-Type", contentTypes); Viewable viewable = new Viewable("myview"); viewable.setModels(new ModelsImpl()); expect(finder.find(anyObject())).andReturn(viewEngine); viewEngine.processView((ViewEngineContext) anyObject()); replay(finder, request, viewEngine, response); writer.writeTo(viewable, null, null, new Annotation[] {}, MediaType.WILDCARD_TYPE, map, null); verify(finder, request, viewEngine, response); }