Java tutorial
/* * Copyright (c) 2018, vindell (https://github.com/vindell). * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package org.docx4j.template.jsp.engine; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.Map; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; import org.apache.commons.io.IOUtils; /** * * TODO * @author <a href="https://github.com/vindell">vindell</a> */ public class JspTemplateImpl implements JspTemplate { protected final JspEngine engine; protected final JspConfig config; protected final String name; protected HttpServletRequest request = null; protected HttpServletResponse response = null; public JspTemplateImpl(JspEngine engine, HttpServletRequest request, HttpServletResponse response, String name) { this.engine = engine; this.config = engine.getConfig(); this.name = name; this.request = request; this.response = response; } @Override public void render(String requestURL, Map<String, Object> variables, Writer out) throws IOException, ServletException { ByteArrayInputStream input = null; ByteArrayOutputStream output = null; try { output = new ByteArrayOutputStream(); doInterpret(requestURL, variables, output); input = new ByteArrayInputStream(output.toByteArray()); IOUtils.copy(input, out, config.getInputEncoding()); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(input); } } @Override public void render(String requestURL, Map<String, Object> variables, OutputStream out) throws IOException, ServletException { doInterpret(requestURL, variables, out); } private void doInterpret(String requestURL, Map<String, Object> variables, OutputStream output) throws IOException, ServletException { /** * ServletContext?RequestDispatcher */ ServletContext sc = request.getSession().getServletContext(); /** * ???reqeustDispatcher */ RequestDispatcher rd = sc.getRequestDispatcher(requestURL); /** * ByteArrayOutputStream?,?? */ final ByteArrayOutputStream baos = new ByteArrayOutputStream(); /** * ServletOutputStreamwrite */ final ServletOutputStream outputStream = new ServletOutputStream() { public void write(int b) throws IOException { /** * ? */ baos.write(b); } @SuppressWarnings("unused") public boolean isReady() { return false; } }; /** * OutputStream PrintWriter * OutputStreamWriter ????? charset ???? */ final PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, config.getOutputEncoding()), true); /** * ?HttpServletResponse??response */ HttpServletResponse resp = new HttpServletResponseWrapper(response) { /** * getOutputStream(ServletResponse)ServletOutputStream * ?response * ?byteArrayOutputStream */ public ServletOutputStream getOutputStream() { return outputStream; } /** * ?getWriter(ServletResponse)PrintWriter * ??? */ public PrintWriter getWriter() { return pw; } }; /** * ?jsp RequestDispatcher.include(ServletRequest request, * ServletResponse response) RequestDispatcher??response */ rd.include(request, resp); pw.flush(); /** * ByteArrayOutputStreamwriteTo?????ByteArray */ baos.writeTo(output); } @Override public String getName() { return name; } }