use of org.glassfish.jersey.server.mvc.spi.ResolvedViewable in project jersey by jersey.
the class ViewableMessageBodyWriter method resolve.
/**
* Resolve the given {@link org.glassfish.jersey.server.mvc.Viewable viewable} using
* {@link org.glassfish.jersey.server.mvc.spi.ViewableContext}.
*
* @param viewable viewable to be resolved.
* @return resolved viewable or {@code null}, if the viewable cannot be resolved.
*/
private ResolvedViewable resolve(final Viewable viewable) {
if (viewable instanceof ResolvedViewable) {
return (ResolvedViewable) viewable;
} else {
final ViewableContext viewableContext = getViewableContext();
final Set<TemplateProcessor> templateProcessors = getTemplateProcessors();
final List<MediaType> producibleMediaTypes = TemplateHelper.getProducibleMediaTypes(requestProvider.get(), extendedUriInfoProvider.get(), null);
final Class<?> resourceClass = resourceInfoProvider.get().getResourceClass();
if (viewable instanceof ImplicitViewable) {
// Template Names.
final ImplicitViewable implicitViewable = (ImplicitViewable) viewable;
for (final String templateName : implicitViewable.getTemplateNames()) {
final Viewable simpleViewable = new Viewable(templateName, viewable.getModel());
final ResolvedViewable resolvedViewable = resolve(simpleViewable, producibleMediaTypes, implicitViewable.getResolvingClass(), viewableContext, templateProcessors);
if (resolvedViewable != null) {
return resolvedViewable;
}
}
} else {
return resolve(viewable, producibleMediaTypes, resourceClass, viewableContext, templateProcessors);
}
return null;
}
}
use of org.glassfish.jersey.server.mvc.spi.ResolvedViewable in project jersey by jersey.
the class ViewableMessageBodyWriter method writeTo.
@Override
public void writeTo(final Viewable viewable, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException {
try {
final ResolvedViewable resolvedViewable = resolve(viewable);
if (resolvedViewable == null) {
final String message = LocalizationMessages.TEMPLATE_NAME_COULD_NOT_BE_RESOLVED(viewable.getTemplateName());
throw new WebApplicationException(new ProcessingException(message), Response.Status.NOT_FOUND);
}
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, resolvedViewable.getMediaType());
resolvedViewable.writeTo(entityStream, httpHeaders);
} catch (ViewableContextException vce) {
throw new NotFoundException(vce);
}
}
use of org.glassfish.jersey.server.mvc.spi.ResolvedViewable in project jersey by jersey.
the class JspTemplateProcessor method writeTo.
@Override
public void writeTo(final String templateReference, final Viewable viewable, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream out) throws IOException {
if (!(viewable instanceof ResolvedViewable)) {
// This should not happen with default MVC message body writer implementation
throw new IllegalArgumentException(LocalizationMessages.ERROR_VIEWABLE_INCORRECT_INSTANCE());
}
// SPI could supply instance of ResolvedViewable but we would like to keep the backward
// compatibility, so the cast is here.
final ResolvedViewable resolvedViewable = (ResolvedViewable) viewable;
final TracingLogger tracingLogger = TracingLogger.getInstance(containerRequestProvider.get().getPropertiesDelegate());
if (tracingLogger.isLogEnabled(MvcJspEvent.JSP_FORWARD)) {
tracingLogger.log(MvcJspEvent.JSP_FORWARD, templateReference, resolvedViewable.getModel());
}
final RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(templateReference);
if (dispatcher == null) {
throw new ContainerException(LocalizationMessages.NO_REQUEST_DISPATCHER_FOR_RESOLVED_PATH(templateReference));
}
final RequestDispatcher wrapper = new RequestDispatcherWrapper(dispatcher, getBasePath(), resolvedViewable);
// OutputStream and Writer for HttpServletResponseWrapper.
final ServletOutputStream responseStream = new ServletOutputStream() {
@Override
public void write(final int b) throws IOException {
out.write(b);
}
};
final PrintWriter responseWriter = new PrintWriter(new OutputStreamWriter(responseStream, getEncoding()));
try {
wrapper.forward(requestProviderRef.get().get(), new HttpServletResponseWrapper(responseProviderRef.get().get()) {
@Override
public ServletOutputStream getOutputStream() throws IOException {
return responseStream;
}
@Override
public PrintWriter getWriter() throws IOException {
return responseWriter;
}
});
} catch (final Exception e) {
throw new ContainerException(e);
} finally {
responseWriter.flush();
}
}
Aggregations