Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/XWikiContext.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/XWikiContext.java (revision 789) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/XWikiContext.java (working copy) @@ -42,6 +42,7 @@ public static final int MODE_PORTLET = 1; public static final int MODE_XMLRPC = 2; public static final int MODE_ATOM = 3; + public static final int MODE_PDF = 4; private boolean finished = false; private XWiki wiki; Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/xmlrpc/XWikiXMLRPCURLFactory.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/xmlrpc/XWikiXMLRPCURLFactory.java (revision 789) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/xmlrpc/XWikiXMLRPCURLFactory.java (working copy) @@ -28,8 +28,7 @@ import com.xpn.xwiki.web.XWikiServletURLFactory; public class XWikiXMLRPCURLFactory extends XWikiServletURLFactory { - public XWikiXMLRPCURLFactory(XWikiContext context) { - super(context); + public XWikiXMLRPCURLFactory() { } public String getURL(URL url, XWikiContext context) { Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/pdf/impl/PdfURLFactory.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/pdf/impl/PdfURLFactory.java (revision 789) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/pdf/impl/PdfURLFactory.java (working copy) @@ -57,14 +57,6 @@ } } - public PdfURLFactory(URL serverURL, String servletPath, String actionPath) { - super(serverURL, servletPath, actionPath); - } - - public PdfURLFactory(XWikiContext context) { - super(context); - } - public String getURL(URL url, XWikiContext context) { if (url==null) return ""; Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/XWiki.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/XWiki.java (revision 789) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/XWiki.java (working copy) @@ -114,6 +114,8 @@ import com.xpn.xwiki.web.XWikiMessageTool; import com.xpn.xwiki.web.XWikiRequest; import com.xpn.xwiki.web.XWikiURLFactory; +import com.xpn.xwiki.web.XWikiURLFactoryService; +import com.xpn.xwiki.web.XWikiURLFactoryServiceImpl; import com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString; public class XWiki implements XWikiDocChangeNotificationInterface, XWikiInterface { @@ -129,6 +131,7 @@ private XWikiGroupService groupService; private XWikiStatsService statsService; private XWikiCacheService cacheService; + private XWikiURLFactoryService urlFactoryService; private MetaClass metaclass = MetaClass.getMetaClass(); private boolean test = false; @@ -168,7 +171,7 @@ return configPath; } - public static XWiki getMainXWiki(XWikiContext context) throws XWikiException { + public static XWiki getMainXWiki(XWikiContext context) throws XWikiException { String xwikicfg = null; String xwikiname = "xwiki"; XWiki xwiki = null; @@ -2672,6 +2675,40 @@ return statsService; } + public XWikiURLFactoryService getURLFactoryService() + { + if (urlFactoryService == null) + { + + log.info("Initializing URLFactory Service..."); + + String urlFactoryServiceClass = Param("xwiki.urlfactory.serviceclass"); + + if (urlFactoryServiceClass != null) + { + try + { + if (log.isDebugEnabled()) log.debug("Using custom URLFactory Service Class " + urlFactoryServiceClass + "."); + urlFactoryService = (XWikiURLFactoryService) Class.forName(urlFactoryServiceClass).newInstance(); + urlFactoryService.init(this); + log.debug("Initialized URLFactory Service using Reflection."); + } + catch (Exception e) + { + urlFactoryService = null; + log.warn("Failed to initialize URLFactory Service " + urlFactoryServiceClass + " using Reflection, trying default implementation using 'new'.", e); + } + } + if (urlFactoryService == null) + { + if (log.isDebugEnabled()) log.debug("Using default URLFactory Service Class " + urlFactoryServiceClass + "."); + urlFactoryService = new XWikiURLFactoryServiceImpl(); + urlFactoryService.init(this); + } + } + return urlFactoryService; + } + public Object getService(String className) throws XWikiException { try { RootContainer manager = RootContainer.getInstance(); Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/Utils.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/Utils.java (revision 789) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/Utils.java (working copy) @@ -206,23 +206,18 @@ context.setAction(action); context.setDatabase(dbname); - + int mode = 0; if (request instanceof XWikiXMLRPCRequest) { - context.setMode(XWikiContext.MODE_XMLRPC); - XWikiURLFactory urlf = new XWikiXMLRPCURLFactory(context); - context.setURLFactory(urlf); + mode = XWikiContext.MODE_XMLRPC; } else if (request instanceof XWikiServletRequest) { - context.setMode(XWikiContext.MODE_SERVLET); - XWikiURLFactory urlf = new XWikiServletURLFactory(context); - context.setURLFactory(urlf); + mode = XWikiContext.MODE_SERVLET; } else if (request instanceof XWikiPortletRequest) { - context.setMode(XWikiContext.MODE_PORTLET); - XWikiURLFactory urlf = new XWikiPortletURLFactory(context); - context.setURLFactory(urlf); + mode = XWikiContext.MODE_PORTLET; } - + context.setMode(mode); + return context; } Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/PDFAction.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/PDFAction.java (revision 789) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/PDFAction.java (working copy) @@ -10,7 +10,8 @@ public class PDFAction extends XWikiAction { public String render(XWikiContext context) throws XWikiException { - context.setURLFactory(new PdfURLFactory(context)); + XWikiURLFactory urlf = context.getWiki().getURLFactoryService().createURLFactory(XWikiContext.MODE_PDF, context); + context.setURLFactory(urlf); PdfExportImpl pdfexport = new PdfExportImpl(); XWikiDocument doc = context.getDoc(); handleRevision(context); Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiPortletURLFactory.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiPortletURLFactory.java (revision 789) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiPortletURLFactory.java (working copy) @@ -36,11 +36,7 @@ private static final Log log = LogFactory.getLog(XWikiPortletURLFactory.class); - public XWikiPortletURLFactory(URL serverURL, String servletPath, String actionPath) { - super(serverURL, servletPath, actionPath); - } - - public XWikiPortletURLFactory(XWikiContext context) { + public void init(XWikiContext context) { URL url = context.getURL(); try { Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiAction.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiAction.java (revision 789) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiAction.java (working copy) @@ -1,245 +1,247 @@ -/** - * =================================================================== - * - * Copyright (c) 2003 Ludovic Dubost, All rights reserved. - * - * 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 2 - * 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, published at - * http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the - * root folder of this distribution. - * - * Created by - * User: Ludovic Dubost - * Date: 25 nov. 2003 - * Time: 21:20:04 - */ - - -package com.xpn.xwiki.web; - -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.log4j.MDC; -import org.apache.struts.action.Action; -import org.apache.struts.action.ActionForm; -import org.apache.struts.action.ActionForward; -import org.apache.struts.action.ActionMapping; -import org.apache.velocity.VelocityContext; - -import com.xpn.xwiki.XWiki; -import com.xpn.xwiki.XWikiContext; -import com.xpn.xwiki.XWikiException; -import com.xpn.xwiki.api.Document; -import com.xpn.xwiki.doc.XWikiDocument; -import com.xpn.xwiki.monitor.api.MonitorPlugin; -import com.xpn.xwiki.render.XWikiVelocityRenderer; - -/** - *

A simple action that handles the display and editing of an - * wiki page..

- * - *

The action support an action URL. The action in the URL - * controls what this action class does. The following values are supported:

- * - * - */ -public abstract class XWikiAction extends Action -{ - - // --------------------------------------------------------- Public Methods - /** - * Handle server requests. - * - * @param mapping The ActionMapping used to select this instance - * @param form The optional ActionForm bean for this request (if any) - * @param req The HTTP request we are processing - * @param resp The HTTP response we are creating - * - * @exception IOException if an input/output error occurs - * @exception ServletException if a servlet exception occurs - */ - public ActionForward execute(ActionMapping mapping, - ActionForm form, - HttpServletRequest req, - HttpServletResponse resp) - throws Exception, ServletException - { -// String action = mapping.getName(); - - MonitorPlugin monitor = null; - try { - XWikiRequest request = new XWikiServletRequest(req); - XWikiResponse response = new XWikiServletResponse(resp); +/** + * =================================================================== + * + * Copyright (c) 2003 Ludovic Dubost, All rights reserved. + * + * 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 2 + * 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, published at + * http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the + * root folder of this distribution. + * + * Created by + * User: Ludovic Dubost + * Date: 25 nov. 2003 + * Time: 21:20:04 + */ + + +package com.xpn.xwiki.web; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.log4j.MDC; +import org.apache.struts.action.Action; +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; +import org.apache.velocity.VelocityContext; + +import com.xpn.xwiki.XWiki; +import com.xpn.xwiki.XWikiContext; +import com.xpn.xwiki.XWikiException; +import com.xpn.xwiki.api.Document; +import com.xpn.xwiki.doc.XWikiDocument; +import com.xpn.xwiki.monitor.api.MonitorPlugin; +import com.xpn.xwiki.render.XWikiVelocityRenderer; + +/** + *

A simple action that handles the display and editing of an + * wiki page..

+ * + *

The action support an action URL. The action in the URL + * controls what this action class does. The following values are supported:

+ * + * + */ +public abstract class XWikiAction extends Action +{ + + // --------------------------------------------------------- Public Methods + /** + * Handle server requests. + * + * @param mapping The ActionMapping used to select this instance + * @param form The optional ActionForm bean for this request (if any) + * @param req The HTTP request we are processing + * @param resp The HTTP response we are creating + * + * @exception IOException if an input/output error occurs + * @exception ServletException if a servlet exception occurs + */ + public ActionForward execute(ActionMapping mapping, + ActionForm form, + HttpServletRequest req, + HttpServletResponse resp) + throws Exception, ServletException + { +// String action = mapping.getName(); + + MonitorPlugin monitor = null; + try { + XWikiRequest request = new XWikiServletRequest(req); + XWikiResponse response = new XWikiServletResponse(resp); XWikiContext context = Utils.prepareContext(mapping.getName(), request, response, - new XWikiServletContext(servlet.getServletContext())); - - // Add the form to the context - context.setForm((XWikiForm) form); - - XWiki xwiki = XWiki.getXWiki(context); - - // Any error before this will be treated using a redirection to an error page - - // Start monitoring timer - monitor = (MonitorPlugin) xwiki.getPlugin("monitor", context); - if (monitor!=null) - monitor.startRequest("", mapping.getName(), context.getURL()); - if (monitor!=null) - monitor.startTimer("request"); - - VelocityContext vcontext = null; - // Prepare velocity context - vcontext = XWikiVelocityRenderer.prepareContext(context); - - try { - // Prepare documents and put them in the context - if (xwiki.prepareDocuments(request, context, vcontext)==false) - return null; - - if (monitor!=null) - monitor.setWikiPage(context.getDoc().getFullName()); - - String renderResult = null; - - if (action(context)) { - renderResult = render(context); - } - - if (renderResult!=null) { - String page = Utils.getPage(request, renderResult); - Utils.parseTemplate(page, !page.equals("direct"), context); - } - return null; - } catch (Throwable e) { - if (!(e instanceof XWikiException)) { - e = new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_UNKNOWN, - "Uncaught exception", e); - } - - vcontext.put("exp", e); - try { - XWikiException xex = (XWikiException) e; - if (xex.getCode()==XWikiException.ERROR_XWIKI_ACCESS_DENIED) { - String page = Utils.getPage(request, "accessdenied"); - Utils.parseTemplate(page, context); - return null; - } else if (xex.getCode()==XWikiException.ERROR_XWIKI_USER_INACTIVE) { - String page = Utils.getPage(request, "userinactive"); - Utils.parseTemplate(page, context); - return null; - } - - Log log = LogFactory.getLog(XWikiAction.class); - if (log.isWarnEnabled()) { - log.warn("Uncaught exception: " + e.getMessage(), e); - } - Utils.parseTemplate(Utils.getPage(request, "exception"), context); - return null; - } catch (Exception e2) { - // I hope this never happens - e.printStackTrace(); - e2.printStackTrace(); - return null; - } - } finally { - - // Let's make sure we have flushed content and closed - try { - response.getWriter().flush(); - } catch (Throwable e) { - } - - if (monitor!=null) - monitor.endTimer("request"); - - if (monitor!=null) - monitor.startTimer("notify"); - - // Let's handle the notification and make sure it never fails - try { - xwiki.getNotificationManager().verify(context.getDoc(), mapping.getName(), context); - } catch (Throwable e) { - e.printStackTrace(); - } - - if (monitor!=null) - monitor.endTimer("notify"); - - // Make sure we cleanup database connections - // There could be cases where we have some - if ((context!=null)&&(xwiki!=null)) { - xwiki.getStore().cleanUp(context); - } - } - } finally { - // End request - if (monitor!=null) - monitor.endRequest(); - - MDC.remove("url"); - } - } - - public String getRealPath(String path) { - return servlet.getServletContext().getRealPath(path); - } - - // hook - public boolean action(XWikiContext context) throws XWikiException { - return true; - } - - // hook - public String render(XWikiContext context) throws XWikiException { - return null; - } - - protected void handleRevision(XWikiContext context) throws XWikiException { - String rev = context.getRequest().getParameter("rev"); - if (rev!=null) { - context.put("rev", rev); - XWikiDocument doc = (XWikiDocument) context.get("doc"); - XWikiDocument tdoc = (XWikiDocument) context.get("tdoc"); - XWikiDocument rdoc = context.getWiki().getDocument(doc, rev, context); - XWikiDocument rtdoc = context.getWiki().getDocument(tdoc, rev, context); - context.put("tdoc", rtdoc); - context.put("cdoc", rdoc); - context.put("doc", rdoc); - VelocityContext vcontext = (VelocityContext) context.get("vcontext"); - vcontext.put("doc", new Document(rdoc, context)); - vcontext.put("cdoc", vcontext.get("doc")); - vcontext.put("tdoc", new Document(rtdoc, context)); - } - } - - protected void sendRedirect(XWikiResponse response, String page) throws XWikiException { - try { - if (page!=null) { - response.sendRedirect(page); - } - } catch (IOException e) { - Object[] args = { page }; - throw new XWikiException(XWikiException.MODULE_XWIKI_APP, - XWikiException.ERROR_XWIKI_APP_REDIRECT_EXCEPTION, - "Exception while sending redirect to page {0}", e, args); - } - } -} + new XWikiServletContext(servlet.getServletContext())); + + // Add the form to the context + context.setForm((XWikiForm) form); + XWiki xwiki = XWiki.getXWiki(context); + + XWikiURLFactory urlf = xwiki.getURLFactoryService().createURLFactory(context.getMode(), context); + context.setURLFactory(urlf); + + // Any error before this will be treated using a redirection to an error page + + // Start monitoring timer + monitor = (MonitorPlugin) xwiki.getPlugin("monitor", context); + if (monitor!=null) + monitor.startRequest("", mapping.getName(), context.getURL()); + if (monitor!=null) + monitor.startTimer("request"); + + VelocityContext vcontext = null; + // Prepare velocity context + vcontext = XWikiVelocityRenderer.prepareContext(context); + + try { + // Prepare documents and put them in the context + if (xwiki.prepareDocuments(request, context, vcontext)==false) + return null; + + if (monitor!=null) + monitor.setWikiPage(context.getDoc().getFullName()); + + String renderResult = null; + + if (action(context)) { + renderResult = render(context); + } + + if (renderResult!=null) { + String page = Utils.getPage(request, renderResult); + Utils.parseTemplate(page, !page.equals("direct"), context); + } + return null; + } catch (Throwable e) { + if (!(e instanceof XWikiException)) { + e = new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_UNKNOWN, + "Uncaught exception", e); + } + + vcontext.put("exp", e); + try { + XWikiException xex = (XWikiException) e; + if (xex.getCode()==XWikiException.ERROR_XWIKI_ACCESS_DENIED) { + String page = Utils.getPage(request, "accessdenied"); + Utils.parseTemplate(page, context); + return null; + } else if (xex.getCode()==XWikiException.ERROR_XWIKI_USER_INACTIVE) { + String page = Utils.getPage(request, "userinactive"); + Utils.parseTemplate(page, context); + return null; + } + + Log log = LogFactory.getLog(XWikiAction.class); + if (log.isWarnEnabled()) { + log.warn("Uncaught exception: " + e.getMessage(), e); + } + Utils.parseTemplate(Utils.getPage(request, "exception"), context); + return null; + } catch (Exception e2) { + // I hope this never happens + e.printStackTrace(); + e2.printStackTrace(); + return null; + } + } finally { + + // Let's make sure we have flushed content and closed + try { + response.getWriter().flush(); + } catch (Throwable e) { + } + + if (monitor!=null) + monitor.endTimer("request"); + + if (monitor!=null) + monitor.startTimer("notify"); + + // Let's handle the notification and make sure it never fails + try { + xwiki.getNotificationManager().verify(context.getDoc(), mapping.getName(), context); + } catch (Throwable e) { + e.printStackTrace(); + } + + if (monitor!=null) + monitor.endTimer("notify"); + + // Make sure we cleanup database connections + // There could be cases where we have some + if ((context!=null)&&(xwiki!=null)) { + xwiki.getStore().cleanUp(context); + } + } + } finally { + // End request + if (monitor!=null) + monitor.endRequest(); + + MDC.remove("url"); + } + } + + public String getRealPath(String path) { + return servlet.getServletContext().getRealPath(path); + } + + // hook + public boolean action(XWikiContext context) throws XWikiException { + return true; + } + + // hook + public String render(XWikiContext context) throws XWikiException { + return null; + } + + protected void handleRevision(XWikiContext context) throws XWikiException { + String rev = context.getRequest().getParameter("rev"); + if (rev!=null) { + context.put("rev", rev); + XWikiDocument doc = (XWikiDocument) context.get("doc"); + XWikiDocument tdoc = (XWikiDocument) context.get("tdoc"); + XWikiDocument rdoc = context.getWiki().getDocument(doc, rev, context); + XWikiDocument rtdoc = context.getWiki().getDocument(tdoc, rev, context); + context.put("tdoc", rtdoc); + context.put("cdoc", rdoc); + context.put("doc", rdoc); + VelocityContext vcontext = (VelocityContext) context.get("vcontext"); + vcontext.put("doc", new Document(rdoc, context)); + vcontext.put("cdoc", vcontext.get("doc")); + vcontext.put("tdoc", new Document(rtdoc, context)); + } + } + + protected void sendRedirect(XWikiResponse response, String page) throws XWikiException { + try { + if (page!=null) { + response.sendRedirect(page); + } + } catch (IOException e) { + Object[] args = { page }; + throw new XWikiException(XWikiException.MODULE_XWIKI_APP, + XWikiException.ERROR_XWIKI_APP_REDIRECT_EXCEPTION, + "Exception while sending redirect to page {0}", e, args); + } + } +} Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiURLFactory.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiURLFactory.java (revision 792) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiURLFactory.java (working copy) @@ -27,6 +27,7 @@ import com.xpn.xwiki.XWikiContext; public interface XWikiURLFactory { + public void init(XWikiContext context); public URL createURL(String web, String name, XWikiContext context); public URL createURL(String web, String name, String action, XWikiContext context); public URL createURL(String web, String name, String action, boolean redirect, XWikiContext context); Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiURLFactoryServiceImpl.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiURLFactoryServiceImpl.java (revision 0) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiURLFactoryServiceImpl.java (revision 0) @@ -0,0 +1,89 @@ +/** + * =================================================================== + * + * Copyright (c) 2003,2004 Ludovic Dubost, All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * 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 Lesser General Public License for more details, published at + * http://www.gnu.org/copyleft/lesser.html or in lesser.txt in the + * root folder of this distribution. + + * Created by + * User: Matthew Conway + * Date: 9 Sep 2005 + */ +package com.xpn.xwiki.web; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.xpn.xwiki.XWiki; +import com.xpn.xwiki.XWikiContext; +import com.xpn.xwiki.pdf.impl.PdfURLFactory; +import com.xpn.xwiki.xmlrpc.XWikiXMLRPCURLFactory; + +public class XWikiURLFactoryServiceImpl implements XWikiURLFactoryService +{ + private static final Log log = LogFactory.getLog(XWikiURLFactoryService.class); + + private Map factoryMap; + + public XWikiURLFactoryServiceImpl() + { + } + + public void init(XWiki xwiki) + { + factoryMap = new HashMap(); + register(xwiki, XWikiContext.MODE_XMLRPC, XWikiXMLRPCURLFactory.class, "xwiki.urlfactory.xmlrpcclass"); + register(xwiki, XWikiContext.MODE_SERVLET, XWikiServletURLFactory.class, "xwiki.urlfactory.servletclass"); + register(xwiki, XWikiContext.MODE_PORTLET, XWikiPortletURLFactory.class, "xwiki.urlfactory.portletclass"); + register(xwiki, XWikiContext.MODE_PDF, PdfURLFactory.class, "xwiki.urlfactory.pdfclass"); + } + + protected void register(XWiki xwiki, int mode, Class defaultImpl, String propertyName) + { + Integer factoryMode = new Integer(mode); + factoryMap.put(factoryMode, defaultImpl); + String urlFactoryClassName = xwiki.Param(propertyName); + if (urlFactoryClassName != null) + { + try + { + log.debug("Using custom url factory: " + urlFactoryClassName); + Class urlFactoryClass = Class.forName(urlFactoryClassName); + factoryMap.put(factoryMode, urlFactoryClass); + } + catch (Exception e) + { + log.error("Faiiled to load custom url factory class: " + urlFactoryClassName); + } + } + } + + public XWikiURLFactory createURLFactory(int mode, XWikiContext context) + { + XWikiURLFactory urlf = null; + try + { + Class urlFactoryClass = (Class) factoryMap.get(new Integer(mode)); + urlf = (XWikiURLFactory) urlFactoryClass.newInstance(); + urlf.init(context); + } + catch (Exception e) + { + log.error("Failed to get construct url factory", e); + } + return urlf; + } +} Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiServletURLFactory.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiServletURLFactory.java (revision 789) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiServletURLFactory.java (working copy) @@ -35,13 +35,19 @@ public XWikiServletURLFactory() { } + // Used by tests public XWikiServletURLFactory(URL serverURL, String servletPath, String actionPath) { this.serverURL = serverURL; this.servletPath = servletPath; this.actionPath = actionPath; } + // Used by tests public XWikiServletURLFactory(XWikiContext context) { + init(context); + } + + public void init(XWikiContext context) { URL url = context.getURL(); String path = url.getPath(); servletPath = path.substring(0, path.indexOf('/', 1) + 1); Index: /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiURLFactoryService.java =================================================================== --- /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiURLFactoryService.java (revision 0) +++ /Users/conway/Documents/eclipse/workspace/xwiki/src/main/java/com/xpn/xwiki/web/XWikiURLFactoryService.java (revision 0) @@ -0,0 +1,31 @@ +/** + * =================================================================== + * + * Copyright (c) 2003,2004 Ludovic Dubost, All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * 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 Lesser General Public License for more details, published at + * http://www.gnu.org/copyleft/lesser.html or in lesser.txt in the + * root folder of this distribution. + + * Created by + * User: Matthew Conway + * Date: 9 Sep 2005 + */ +package com.xpn.xwiki.web; + +import com.xpn.xwiki.XWiki; +import com.xpn.xwiki.XWikiContext; + +public interface XWikiURLFactoryService +{ + public void init(XWiki context); + public XWikiURLFactory createURLFactory(int mode, XWikiContext context); +}