Hi Lilianne, Sorry for the late response and thanks for this patch ! We love improvements to XWiki ! First I think we should go through Jira to submit patches. I've create a task at: http://jira.xwiki.org/jira/browse/XWIKI-36 You should subscribe and join in on this task. If you can add the patches as attachment in this task it would be great. Concerning the code, the main remark I have would be to put it in the com.xpn.xwiki.web.includeservlet package, since it is not specific to Velocity but adds a feature to the XWiki API. Finally, it would be great if you could add a TestCase which demonstrates the features and verifies that it works fine. You will probably need to add the test to ViewEditTest which means it involves a TomCat local install to make it run. Ludovic Lilianne E. Blaze a écrit :
Hello,
So far it works good. It still needs more testing with different character encodings.
Greetings, Lilianne E. Blaze
Changes in /template/macros.vm: Add: #macro( includeServlet $url) <!-- including $url --> $xwiki.includeServlet($url) <!-- included $url --> #end
Index: com/xpn/xwiki/api/XWiki.java =================================================================== --- com/xpn/xwiki/api/XWiki.java (revision 580) +++ com/xpn/xwiki/api/XWiki.java (working copy) @@ -253,6 +253,17 @@ public String parseTemplate(String template) { return xwiki.parseTemplate(template, context); } + + // LBlaze's + /** + * Includes dynamic content, such as Servlets or JSPs, inside Velocity + * templates. + * + * @author LBlaze + */ + public String includeServlet(String url) { + return xwiki.includeServlet(url, context); + }
public String getSkinFile(String filename) { return xwiki.getSkinFile(filename, context);
Index: com/xpn/xwiki/XWiki.java =================================================================== --- com/xpn/xwiki/XWiki.java (revision 580) +++ com/xpn/xwiki/XWiki.java (working copy) @@ -83,6 +83,7 @@ import com.xpn.xwiki.api.Api; import com.xpn.xwiki.cache.api.XWikiCacheService; import com.xpn.xwiki.cache.impl.OSCacheService; +import com.xpn.xwiki.velocity.includeservlet.VelocityIncludeServlet; import org.hibernate.HibernateException; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.StringUtils; @@ -102,6 +103,8 @@ import org.exoplatform.container.PortalContainer;
import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -830,6 +833,27 @@ return null; }
+ // LBlaze's + public String includeServlet(String url, XWikiContext xwikiContext) { + + HttpServletRequest servletRequest = xwikiContext.getRequest(); + HttpServletResponse servletResponse = xwikiContext.getResponse(); + + try + { + return VelocityIncludeServlet.includeServletAndReturnAsString( + url, + servletRequest, + servletResponse); + } + catch(Exception e) + { + log.warn("Exception including url: "+url, e); + return "Exception including \""+url+"\", see logs for details."; + } + + } + public String getSkinFile(String filename, XWikiContext context) { XWikiURLFactory urlf = context.getURLFactory();
------------------------------------ /* * BufferOutputStream.java * * Created on June 2, 2005, 2:45 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */
package com.xpn.xwiki.velocity.includeservlet;
import java.io.ByteArrayOutputStream; import java.io.IOException;
import javax.servlet.ServletOutputStream;
/** * * @author LBlaze */ public class BufferOutputStream extends ServletOutputStream {
protected ByteArrayOutputStream buffer;
/** Creates a new instance of BufferOutputStream */ public BufferOutputStream() { buffer = new ByteArrayOutputStream(); } public void write(int b) throws IOException { buffer.write(b); } public void write(byte b[]) throws IOException { buffer.write(b); } public void write(byte[] b, int off, int len) throws IOException { buffer.write(b, off, len); } public void flush() throws IOException { buffer.flush(); } public void close() throws IOException { buffer.close(); }
public byte[] getContentsAsByteArray() throws IOException { flush(); return buffer.toByteArray(); } } ----------------------------- /* * BufferedResponse.java * * Created on June 2, 2005, 2:15 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */
package com.xpn.xwiki.velocity.includeservlet;
import java.io.IOException; import java.io.PrintWriter; import java.io.OutputStreamWriter;
import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper;
/** * * @author LBlaze */ public class BufferedResponse extends HttpServletResponseWrapper {
protected HttpServletResponse internalResponse; protected BufferOutputStream outputStream; protected PrintWriter writer;
/** Creates a new instance of BufferedResponse */ public BufferedResponse(HttpServletResponse internalResponse) { super(internalResponse); this.internalResponse = internalResponse; }
public ServletOutputStream getOutputStream() throws IOException { if( outputStream == null ) { outputStream = new BufferOutputStream(); } return outputStream; }
public PrintWriter getWriter() throws IOException { if( writer == null ) { writer = new PrintWriter(new OutputStreamWriter(getOutputStream(), getCharacterEncoding())); } return writer; } public byte[] getBufferAsByteArray() throws IOException { if( writer != null ) { writer.flush(); } outputStream.flush(); return outputStream.getContentsAsByteArray(); }
} ------------------------------------ /* * VelocityIncludeServlet.java * * Created on June 2, 2005, 1:41 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */
package com.xpn.xwiki.velocity.includeservlet;
import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.RequestDispatcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
/** * * @author LBlaze */ public class VelocityIncludeServlet {
private static final Log log = LogFactory.getLog(VelocityIncludeServlet.class);
/** Creates a new instance of VelocityIncludeServlet */ private VelocityIncludeServlet() { } static public String includeServletAndReturnAsString(String url, HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException, ServletException { if( log.isDebugEnabled() ) { log.debug("Including url \""+url+"\"..."); } RequestDispatcher requestDispatcher = servletRequest.getRequestDispatcher(url); if( requestDispatcher == null ) { IllegalArgumentException iae = new IllegalArgumentException( "Failed to get RequestDispatcher for url: "+url); log.error(iae.getMessage(),iae); throw iae; } BufferedResponse bufferedResponse = new BufferedResponse(servletResponse); requestDispatcher.include(servletRequest, bufferedResponse); byte[] buffer = bufferedResponse.getBufferAsByteArray(); if( log.isDebugEnabled() ) { log.debug("Buffer returned with "+buffer.length+" bytes."); } String bufferString = new String(buffer, servletResponse.getCharacterEncoding()); return bufferString; }
}
------------------------------------------------------------------------
-- You receive this message as a subscriber of the [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help ObjectWeb mailing lists service home page: http://www.objectweb.org/wws
-- Ludovic Dubost XPertNet: http://www.xpertnet.fr/ Blog: http://www.ludovic.org/blog/ XWiki: http://www.xwiki.com Skype: ldubost AIM: nvludo Yahoo: ludovic