All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.bootique.mvc.AbstractViewWriter Maven / Gradle / Ivy

There is a newer version: 3.0.M2
Show newest version
package io.bootique.mvc;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Objects;

import javax.inject.Provider;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;

import io.bootique.mvc.renderer.TemplateRendererFactory;
import io.bootique.mvc.resolver.TemplateResolver;

public class AbstractViewWriter implements MessageBodyWriter {

	private TemplateResolver templateResolver;
	private TemplateRendererFactory templateRendererFactory;

	// TODO dirty ... other properties are set in constructor, but this one
	// awaits injection. So the object is in a partial state for the part of its
	// lifecycle...
	@Context
	private Provider resourceInfoProvider;

	public AbstractViewWriter(TemplateResolver templateResolver, TemplateRendererFactory templateRendererFactory) {
		this.templateResolver = templateResolver;
		this.templateRendererFactory = templateRendererFactory;
	}

	@Override
	public long getSize(AbstractView t, Class type, Type genericType, Annotation[] annotations,
			MediaType mediaType) {
		return -1;
	}

	@Override
	public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
		return AbstractView.class.isAssignableFrom(type);
	}

	@Override
	public void writeTo(AbstractView t, Class type, Type genericType, Annotation[] annotations, MediaType mediaType,
			MultivaluedMap httpHeaders, OutputStream entityStream)
					throws IOException, WebApplicationException {

		// doublecheck injection....
		Objects.requireNonNull(resourceInfoProvider, "'resourceInfoProvider' was not injected");

		Writer out = new OutputStreamWriter(entityStream, t.getEncoding());
		Template template = templateResolver.resolve(t.getTemplateName(), t.getClass());
		templateRendererFactory.getRenderer(template).render(out, template, t);

		// flush but do not close the underlying stream
		out.flush();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy