Search in sources :

Example 1 with ResolvedViewable

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;
    }
}
Also used : ResolvedViewable(org.glassfish.jersey.server.mvc.spi.ResolvedViewable) TemplateProcessor(org.glassfish.jersey.server.mvc.spi.TemplateProcessor) Viewable(org.glassfish.jersey.server.mvc.Viewable) ResolvedViewable(org.glassfish.jersey.server.mvc.spi.ResolvedViewable) MediaType(javax.ws.rs.core.MediaType) ViewableContext(org.glassfish.jersey.server.mvc.spi.ViewableContext)

Example 2 with ResolvedViewable

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);
    }
}
Also used : ResolvedViewable(org.glassfish.jersey.server.mvc.spi.ResolvedViewable) WebApplicationException(javax.ws.rs.WebApplicationException) NotFoundException(javax.ws.rs.NotFoundException) ViewableContextException(org.glassfish.jersey.server.mvc.spi.ViewableContextException) ProcessingException(javax.ws.rs.ProcessingException)

Example 3 with ResolvedViewable

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();
    }
}
Also used : ResolvedViewable(org.glassfish.jersey.server.mvc.spi.ResolvedViewable) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) TracingLogger(org.glassfish.jersey.message.internal.TracingLogger) IOException(java.io.IOException) RequestDispatcher(javax.servlet.RequestDispatcher) IOException(java.io.IOException) ContainerException(org.glassfish.jersey.server.ContainerException) ContainerException(org.glassfish.jersey.server.ContainerException) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Aggregations

ResolvedViewable (org.glassfish.jersey.server.mvc.spi.ResolvedViewable)3 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)1 NotFoundException (javax.ws.rs.NotFoundException)1 ProcessingException (javax.ws.rs.ProcessingException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 MediaType (javax.ws.rs.core.MediaType)1 TracingLogger (org.glassfish.jersey.message.internal.TracingLogger)1 ContainerException (org.glassfish.jersey.server.ContainerException)1 Viewable (org.glassfish.jersey.server.mvc.Viewable)1 TemplateProcessor (org.glassfish.jersey.server.mvc.spi.TemplateProcessor)1 ViewableContext (org.glassfish.jersey.server.mvc.spi.ViewableContext)1 ViewableContextException (org.glassfish.jersey.server.mvc.spi.ViewableContextException)1