Thread: [Ejtools-cvs] CVS: libraries/common/src/main/net/sourceforge/ejtools/servlet/http CharResponseWrappe
Brought to you by:
letiemble
From: Laurent E. <let...@us...> - 2002-05-02 19:25:57
|
Update of /cvsroot/ejtools/libraries/common/src/main/net/sourceforge/ejtools/servlet/http In directory usw-pr-cvs1:/tmp/cvs-serv1397/common/src/main/net/sourceforge/ejtools/servlet/http Modified Files: CharResponseWrapper.java StringResponseWrapper.java XSLFilter.java Log Message: Add some JavaDocs Index: CharResponseWrapper.java =================================================================== RCS file: /cvsroot/ejtools/libraries/common/src/main/net/sourceforge/ejtools/servlet/http/CharResponseWrapper.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** CharResponseWrapper.java 30 Apr 2002 21:07:38 -0000 1.2 --- CharResponseWrapper.java 2 May 2002 19:25:54 -0000 1.3 *************** *** 1,7 **** /* ! * EJTools, the Enterprise Java Tools * ! * Distributable under LGPL license. ! * See terms of license at gnu.org. */ package net.sourceforge.ejtools.servlet.http; --- 1,7 ---- /* ! * EJTools, the Enterprise Java Tools * ! * Distributable under LGPL license. ! * See terms of license at gnu.org. */ package net.sourceforge.ejtools.servlet.http; *************** *** 14,40 **** /** ! * Description of the Interface * ! * @author laurent ! * @created 11 avril 2002 */ public class CharResponseWrapper extends HttpServletResponseWrapper { ! protected CharArrayWriter output = null; - public CharResponseWrapper(HttpServletResponse response) - { - super(response); - output = new CharArrayWriter(); - } ! public String toString() ! { ! return output.toString(); ! } ! public PrintWriter getWriter() ! { ! return new PrintWriter(output); ! } } --- 14,61 ---- /** ! * A Response wrapper that allows the manipulation of the Servlet Response. ! * Create a buffer based on a Char array. * ! * @author letiemble ! * @created 11 avril 2002 ! * @version $Revision$ */ public class CharResponseWrapper extends HttpServletResponseWrapper { ! /** Buffer used to store content */ ! protected CharArrayWriter output = null; ! /** ! * Constructor for the CharResponseWrapper object ! * ! * @param response Response stream to wrap ! */ ! public CharResponseWrapper(HttpServletResponse response) ! { ! super(response); ! output = new CharArrayWriter(); ! } ! ! /** ! * Return a writer that wraps the buffered content ! * ! * @return The writer of the wrapper ! */ ! public PrintWriter getWriter() ! { ! return new PrintWriter(output); ! } ! ! ! /** ! * Return the content of the buffer ! * ! * @return The content as String ! */ ! public String toString() ! { ! return output.toString(); ! } } Index: StringResponseWrapper.java =================================================================== RCS file: /cvsroot/ejtools/libraries/common/src/main/net/sourceforge/ejtools/servlet/http/StringResponseWrapper.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** StringResponseWrapper.java 30 Apr 2002 21:07:38 -0000 1.2 --- StringResponseWrapper.java 2 May 2002 19:25:54 -0000 1.3 *************** *** 1,7 **** /* ! * EJTools, the Enterprise Java Tools * ! * Distributable under LGPL license. ! * See terms of license at gnu.org. */ package net.sourceforge.ejtools.servlet.http; --- 1,7 ---- /* ! * EJTools, the Enterprise Java Tools * ! * Distributable under LGPL license. ! * See terms of license at gnu.org. */ package net.sourceforge.ejtools.servlet.http; *************** *** 14,40 **** /** ! * Description of the Interface * ! * @author laurent ! * @created 11 avril 2002 */ public class StringResponseWrapper extends HttpServletResponseWrapper { ! protected StringWriter output = null; - public StringResponseWrapper(HttpServletResponse response) - { - super(response); - output = new StringWriter(); - } ! public String toString() ! { ! return output.toString(); ! } ! public PrintWriter getWriter() ! { ! return new PrintWriter(output); ! } } --- 14,61 ---- /** ! * A Response wrapper that allows the manipulation of the Servlet Response. ! * Create a buffer based on a String. * ! * @author letiemble ! * @created 11 avril 2002 ! * @version $Revision$ */ public class StringResponseWrapper extends HttpServletResponseWrapper { ! /** Buffer used to store content */ ! protected StringWriter output = null; ! /** ! * Constructor for the StringResponseWrapper object ! * ! * @param response Response stream to wrap ! */ ! public StringResponseWrapper(HttpServletResponse response) ! { ! super(response); ! output = new StringWriter(); ! } ! ! /** ! * Return a writer that wraps the buffered content ! * ! * @return The writer of the wrapper ! */ ! public PrintWriter getWriter() ! { ! return new PrintWriter(output); ! } ! ! ! /** ! * Return the content of the buffer ! * ! * @return The content as String ! */ ! public String toString() ! { ! return output.toString(); ! } } Index: XSLFilter.java =================================================================== RCS file: /cvsroot/ejtools/libraries/common/src/main/net/sourceforge/ejtools/servlet/http/XSLFilter.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** XSLFilter.java 30 Apr 2002 21:07:38 -0000 1.3 --- XSLFilter.java 2 May 2002 19:25:54 -0000 1.4 *************** *** 1,7 **** /* ! * EJTools, the Enterprise Java Tools * ! * Distributable under LGPL license. ! * See terms of license at gnu.org. */ package net.sourceforge.ejtools.servlet.http; --- 1,7 ---- /* ! * EJTools, the Enterprise Java Tools * ! * Distributable under LGPL license. ! * See terms of license at gnu.org. */ package net.sourceforge.ejtools.servlet.http; *************** *** 25,97 **** import javax.xml.transform.stream.StreamSource; /** ! * Description of the Interface * ! * @author laurent ! * @created 11 avril 2002 */ public class XSLFilter implements Filter { ! protected Transformer transformer = null; ! protected String stylePath = null; ! protected String contentType = null; ! protected FilterConfig config = null; - public void init(FilterConfig config) throws ServletException - { - this.config = config; ! this.contentType = config.getInitParameter("mime-type"); ! System.out.println("Acquiring parameter mime-type=" + contentType); ! String styleSheet = config.getInitParameter("stylesheet"); ! System.out.println("Acquiring parameter stylesheet=" + styleSheet); ! String stylePath = config.getServletContext().getRealPath(styleSheet); - try - { - System.out.println("Compiling stylesheet..."); - TransformerFactory tFactory = TransformerFactory.newInstance(); - transformer = tFactory.newTransformer(new StreamSource(stylePath)); - } - catch (Exception e) - { - System.out.println(e.toString()); - } - } ! public void destroy() ! { ! this.config = null; ! } ! public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException ! { ! response.setContentType(contentType); ! PrintWriter out = response.getWriter(); ! StringResponseWrapper wrapper = new StringResponseWrapper((HttpServletResponse) response); ! chain.doFilter(request, wrapper); ! StringReader reader = new StringReader(wrapper.toString()); ! Source source = new StreamSource(reader); ! try ! { ! System.out.println("Creating Writer..."); ! StringWriter writer = new StringWriter(); ! StreamResult result = new StreamResult(writer); ! System.out.println("Applying stylesheet..."); ! transformer.transform(source, result); ! System.out.println("Ouputing..."); ! response.setContentLength(writer.toString().length()); ! out.println(writer.toString()); ! } ! catch (Exception e) ! { ! out.println(e.toString()); ! out.println(wrapper.toString()); ! } ! } } --- 25,140 ---- import javax.xml.transform.stream.StreamSource; + import org.apache.log4j.Category; + /** ! * HTTP Servlet filter that applies a XSL stylesheet on a XML stream. * ! * @author letiemble ! * @created 11 avril 2002 ! * @version $Revision$ */ public class XSLFilter implements Filter { ! /** Filter configuration */ ! protected FilterConfig config = null; ! /** Content type of the output */ ! protected String contentType = null; ! /** Absolute path the stylesheet */ ! protected String stylePath = null; ! /** XSL transformer */ ! protected Transformer transformer = null; ! /** Log4j Logger */ ! protected static Category logger = Category.getInstance(XSLFilter.class); ! /** Destroy the filter */ ! public void destroy() ! { ! this.config = null; ! } ! /** ! * Main method of the filter ! * ! * @param request Incoming request ! * @param response Outgoing response ! * @param chain Filter chain to forward the request and the ! * response ! * @exception IOException In case of error ! * @exception ServletException In case of error ! */ ! public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException ! { ! // Set content type and get the original response ! response.setContentType(contentType); ! PrintWriter out = response.getWriter(); ! // Wrap the response with a buffer and follow the chain ! StringResponseWrapper wrapper = new StringResponseWrapper((HttpServletResponse) response); ! chain.doFilter(request, wrapper); ! // Create a reader with the result ! StringReader reader = new StringReader(wrapper.toString()); ! Source source = new StreamSource(reader); ! try ! { ! // Create the final response writer ! logger.debug("Creating Writer..."); ! StringWriter writer = new StringWriter(); ! StreamResult result = new StreamResult(writer); ! // Transform according to XSL stylesheet ! logger.debug("Applying stylesheet..."); ! transformer.transform(source, result); ! // Print out the result ! logger.debug("Ouputing..."); ! response.setContentLength(writer.toString().length()); ! out.println(writer.toString()); ! } ! catch (Exception e) ! { ! logger.error("Error during transformation " + e.getMessage()); ! out.println(wrapper.toString()); ! } ! } ! ! /** ! * Performs the initialization of the filter ! * ! * @param config The filter configuration ! * @exception ServletException Exception thrown if the filter was not ! * properly configured ! */ ! public void init(FilterConfig config) throws ServletException ! { ! this.config = config; ! ! // Get the content type ! this.contentType = config.getInitParameter("mime-type"); ! logger.debug("Acquiring parameter mime-type=" + contentType); ! ! // Get the stylesheet ! String styleSheet = config.getInitParameter("stylesheet"); ! logger.debug("Acquiring parameter stylesheet=" + styleSheet); ! ! // Set the absolute position of the stylesheet ! String stylePath = config.getServletContext().getRealPath(styleSheet); ! ! try ! { ! // Prepare the XSL transformer ! logger.debug("Compiling stylesheet..."); ! TransformerFactory tFactory = TransformerFactory.newInstance(); ! transformer = tFactory.newTransformer(new StreamSource(stylePath)); ! } ! catch (Exception e) ! { ! logger.fatal("Failed to init the filter " + e.getMessage()); ! throw new ServletException(e.getMessage()); ! } ! } } |