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

org.debux.webmotion.server.render.RenderTemplate Maven / Gradle / Ivy

There is a newer version: 10.0.0
Show newest version
/*
 * #%L
 * Toolkit :: Server
 * %%
 * Copyright (C) 2017 - 2024 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */
package org.debux.webmotion.server.render;

import org.debux.webmotion.server.call.Call;
import org.debux.webmotion.server.call.HttpContext;
import org.debux.webmotion.server.mapping.Mapping;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequestWrapper;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Map;

/**
 * Render use to include a page, like jsp, html, .. in response.
 *
 * @author julien
 */
public class RenderTemplate extends Render {
    protected String view;
    protected Map model;

    public RenderTemplate(String view, Map model) {
        this.view = view;
        this.model = model;
    }

    public Map getModel() {
        return model;
    }

    public String getView() {
        return view;
    }

    @Override
    public void create(Mapping mapping, Call call) throws IOException, ServletException {
        HttpContext context = call.getContext();
        HttpServletResponse response = context.getResponse();
        HttpServletRequest request = context.getRequest();

        addModel(call, model);

        ServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request);
        ResponseWrapper responseWrapper = new ResponseWrapper(response);
        String path = getViewPath(mapping, call, view);
        request.getRequestDispatcher(path).include(requestWrapper, responseWrapper);

        String contentType = responseWrapper.getContentType();
        response.setContentType(contentType);

        PrintWriter out = context.getOut();
        String include = responseWrapper.getContent();
        out.write(include);
    }


    /**
     * Wrap response to get content. Use to manage template with AJAX call.
     */
    public static class ResponseWrapper extends HttpServletResponseWrapper {

        protected ByteArrayOutputStream stream;

        public ResponseWrapper(HttpServletResponse response) {
            super(response);
            this.stream = new ByteArrayOutputStream();
        }

        @Override
        public ServletOutputStream getOutputStream() {
            return new ServletOutputStreamWrapper(stream);
        }

        @Override
        public PrintWriter getWriter() {
            return new PrintWriter(stream);
        }

        @Override
        public void flushBuffer() throws IOException {
            stream.flush();
        }

        public String getContent() {
            return stream.toString();
        }
    }

    public static class ServletOutputStreamWrapper extends ServletOutputStream {
        protected OutputStream outputStream;

        public ServletOutputStreamWrapper(OutputStream outputStream) {
            this.outputStream = outputStream;
        }

        @Override
        public void write(int b) throws IOException {
            outputStream.write(b);
        }

        @Override
        public boolean isReady() {
            return false;
        }

        @Override
        public void setWriteListener(WriteListener writeListener) {

        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy