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

com.manydesigns.portofino.pageactions.rest.messagebodywriters.XhtmlFragmentMessageBodyWriter Maven / Gradle / Ivy

package com.manydesigns.portofino.pageactions.rest.messagebodywriters;

import com.manydesigns.elements.xml.XhtmlBuffer;
import com.manydesigns.elements.xml.XhtmlFragment;

import javax.ws.rs.ConstrainedTo;
import javax.ws.rs.RuntimeType;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

/**
 * @author Angelo Lupo          - [email protected]
 * @author Giampiero Granatella - [email protected]
 * @author Emanuele Poggi       - [email protected]
 * @author Alessio Stalla       - [email protected]
 */
@Provider
@ConstrainedTo(RuntimeType.SERVER)
public class XhtmlFragmentMessageBodyWriter implements MessageBodyWriter {

    public static final String copyright =
            "Copyright (C) 2005-2017 ManyDesigns srl";

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

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

    @Override
    public void writeTo(
            XhtmlFragment fragment, Class type, Type genericType, Annotation[] annotations, MediaType mediaType,
            final MultivaluedMap httpHeaders, final OutputStream entityStream)
            throws IOException, WebApplicationException {
        OutputStreamWriter writer = new OutputStreamWriter(entityStream);
        XhtmlBuffer buffer = new XhtmlBuffer(writer);
        fragment.toXhtml(buffer);
        writer.flush();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy