Index: src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcHandler.java =================================================================== --- src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcHandler.java (revision 9563) +++ src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcHandler.java (working copy) @@ -25,9 +25,6 @@ import java.util.List; import java.util.Map; import java.util.Vector; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -42,67 +39,68 @@ import org.xwiki.xmlrpc.model.XWikiObject; import org.xwiki.xmlrpc.model.XWikiPage; import org.xwiki.xmlrpc.model.XWikiPageSummary; + import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.XWikiException; import com.xpn.xwiki.api.Document; import com.xpn.xwiki.api.XWiki; import com.xpn.xwiki.doc.XWikiAttachment; import com.xpn.xwiki.doc.XWikiDocument; -import com.xpn.xwiki.web.XWikiRequest; -import com.xpn.xwiki.web.XWikiResponse; -import com.xpn.xwiki.web.XWikiServletContext; /** * The class containing the implementation of the XML-RPC API. Methods tagged with the ConfluenceAPI * are compatible with Confluence. - * + * * @author fmancinelli */ public class XWikiXmlRpcHandler { - private XWikiRequest xwikiRequest; + private XWikiXmlRpcHttpRequestConfig xwikiXmlRpcHttpRequestConfig; - private XWikiResponse xwikiResponse; + private static final Log log = LogFactory.getLog(XWikiXmlRpcHandler.class); - private XWikiServletContext xwikiServletContext; + public String test() throws Exception + { + System.out.format("Test\n"); - private static final Log log = LogFactory.getLog(XWikiXmlRpcHandler.class); + return "test"; + } /** * Initialize the XML-RPC handler with respect to the current HTTP request. - * + * * @param servlet The servlet requesting the XML-RPC call handling. * @param httpRequest The current HTTP request. */ - public void init(HttpServlet servlet, HttpServletRequest httpRequest) + public void init(XWikiXmlRpcHttpRequestConfig requestConfig) { - xwikiRequest = new XWikiXmlRpcRequest(httpRequest); - xwikiResponse = - new XWikiXmlRpcResponse((XWikiResponse) XWikiUtils.mock(XWikiResponse.class)); - ServletContext servletContext = servlet.getServletContext(); - xwikiServletContext = new XWikiServletContext(servletContext); + this.xwikiXmlRpcHttpRequestConfig = requestConfig; } /** * Login. - * + * * @return A token to be used in subsequent calls as an identification. * @throws XmlRpcException If authentication fails. * @category ConfluenceAPI */ public String login(String userName, String password) throws XWikiException, XmlRpcException { - XWikiContext context = - XWikiUtils.getXWikiContext(xwikiRequest, xwikiResponse, xwikiServletContext); - com.xpn.xwiki.XWiki xwiki = context.getWiki(); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + // XWikiContext context = + // XWikiUtils.getXWikiContext(xwikiRequest, xwikiResponse, xwikiServletContext); + // com.xpn.xwiki.XWiki xwiki = context.getWiki(); + + com.xpn.xwiki.XWiki xwiki = xwikiXmlRpcContext.getBaseXWiki(); String token; - if (xwiki.getAuthService().authenticate(userName, password, context) != null) { + if (xwikiXmlRpcContext.getBaseXWiki().getAuthService().authenticate(userName, password, + xwikiXmlRpcContext.getXWikiContext()) != null) { // Generate "unique" token using a random number token = xwiki.generateValidationKey(128); - String ip = context.getRequest().getRemoteAddr(); + String ip = xwikiXmlRpcContext.getXWikiContext().getRequest().getRemoteAddr(); - XWikiUtils.getTokens(context).put(token, + XWikiUtils.getTokens(xwikiXmlRpcContext.getXWikiContext()).put(token, new XWikiXmlRpcUser(String.format("XWiki.%s", userName), ip)); return token; @@ -114,7 +112,7 @@ /** * Logout. - * + * * @param token The authentication token. * @return True is logout was successful. * @throws XmlRpcException An invalid token is provided. @@ -122,16 +120,15 @@ */ public Boolean logout(String token) throws XWikiException, XmlRpcException { - XWikiContext context = - XWikiUtils.getXWikiContext(xwikiRequest, xwikiResponse, xwikiServletContext); - XWikiUtils.checkToken(token, context); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); - return XWikiUtils.getTokens(context).remove(token) != null; + return XWikiUtils.getTokens(xwikiXmlRpcContext.getXWikiContext()).remove(token) != null; } /** * Get server information. - * + * * @param token The authentication token. * @return The server information * @throws XmlRpcException An invalid token is provided. @@ -139,12 +136,10 @@ */ public Map getServerInfo(String token) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getServerInfo()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called getServerInfo()", user.getName())); String version = xwiki.getVersion(); Integer majorVersion = null; @@ -157,10 +152,10 @@ String[] components = version.split("\\."); majorVersion = new Integer(components[0]); serverInfo.setMajorVersion(majorVersion); - if(components[1].indexOf('-') != -1) { + if (components[1].indexOf('-') != -1) { // Removing possible suffixes (-SNAPSHOT for example) - minorVersion = new Integer(components[1].substring(0, - components[1].indexOf('-'))); + minorVersion = + new Integer(components[1].substring(0, components[1].indexOf('-'))); } else { minorVersion = new Integer(components[1]); } @@ -185,12 +180,10 @@ */ public List getSpaces(String token) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getSpaces()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called getSpaces()", user.getName())); List result = new ArrayList(); List spaceKeys = xwiki.getSpaces(); @@ -220,17 +213,15 @@ * @param token The authentication token. * @return A map representing a Space object. * @throws XmlRpcException An invalid token is provided or the user doesn't have enough rights - * to access the space. + * to access the space. * @category ConfluenceAPI */ public Map getSpace(String token, String spaceKey) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getSpace()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called getSpace()", user.getName())); if (!xwiki.getSpaces().contains(spaceKey)) { throw new XmlRpcException(String.format("[Space '%s' does not exist]", spaceKey)); @@ -257,22 +248,20 @@ /** * Add a new space. It basically creates a SpaceKey.WebHome page with no content and the space * title as its title. - * + * * @param token The authentication token. * @param spaceMap The map representing a Space object. * @return The newly created space as a Space object. * @throws XmlRpcException Space cannot be created or it already exists and the user has not the - * rights to modify it + * rights to modify it * @category ConfluenceAPI */ public Map addSpace(String token, Map spaceMap) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called addSpace()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called addSpace()", user.getName())); Space space = new Space(spaceMap); @@ -292,14 +281,14 @@ } else { throw new XmlRpcException(String .format( - "[Space cannot be created or it already exists and user '%s' has not the right to modify it]", - xwikiXmlRpcContext.getUser().getName())); + "[Space cannot be created or it already exists and user '%s' has not the right to modify it]", + user.getName())); } } /** * Removes a space by deleting every page in it. - * + * * @param token The authentication token. * @return True if the space has been successfully deleted. * @category ConfluenceAPI @@ -307,12 +296,10 @@ public Boolean removeSpace(String token, String spaceKey) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called removeSpace()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called removeSpace()", user.getName())); if (!xwiki.getSpaces().contains(spaceKey)) { throw new XmlRpcException(String.format("[Space '%s' does not exist.]", spaceKey)); @@ -359,12 +346,10 @@ */ public List getPages(String token, String spaceKey) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getPages()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called getPages()", user.getName())); List result = new ArrayList(); List pageNames = xwiki.getSpaceDocsName(spaceKey); @@ -392,7 +377,7 @@ /** * Retrieves a page. - * + * * @param token The authentication token. * @param pageId The pageId in the 'Space.Page' format. * @param language The language id for the translation @@ -401,16 +386,14 @@ * @return A map representing a Page object containing information about the page at version * 'version.minorVersion' * @throws XmlRpcException If the user has not the right to access the page or the page does not - * exist at version 'version.minorVersion'. + * exist at version 'version.minorVersion'. */ public Map getPage(String token, String pageId) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getPage()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called getPage()", user.getName())); /* Extract all needed information from the extended xwiki id */ XWikiExtendedId extendedId = new XWikiExtendedId(pageId); @@ -472,7 +455,7 @@ /** * Store a page or create it if it doesn't exist. - * + * * @param token The authentication token. * @param pageMap A map representing the Page object to be stored. * @return A map representing a Page object with the updated information. @@ -480,12 +463,10 @@ */ public Map storePage(String token, Map pageMap) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called storePage()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called storePage()", user.getName())); XWikiPage page = new XWikiPage(pageMap); @@ -517,8 +498,7 @@ } if (!page.getLanguage().equals("") - && !page.getLanguage().equals(doc.getDefaultLanguage())) - { + && !page.getLanguage().equals(doc.getDefaultLanguage())) { /* * Try to get the document in the translation specified in the page parameter... */ @@ -555,17 +535,15 @@ * @param token The authentication token. * @param pageId The pageId in the 'Space.Page' format. * @throws XmlRpcException If the page does not exist or the user has not the right to access - * it. + * it. * @category ConfluenceAPI */ public Boolean removePage(String token, String pageId) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called removePage()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called removePage()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(pageId); @@ -575,7 +553,7 @@ if (doc.getLocked()) { throw new XmlRpcException(String.format( "Unable to remove attachment. Document '%s' locked by '%s'", doc - .getName(), doc.getLockingUser())); + .getName(), doc.getLockingUser())); } doc.delete(); @@ -596,18 +574,16 @@ * @param pageId The pageId in the 'Space.Page' format. * @return A list of maps representing PageHistorySummary objects. * @throws XmlRpcException If the page does not exist or the user has not the right to access - * it. + * it. * @category ConfluenceAPI */ public List getPageHistory(String token, String pageId) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called removePage()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called removePage()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(pageId); @@ -640,26 +616,24 @@ /** * Render a page or content in HTML. - * + * * @param token The authentication token. * @param space Ignored * @param pageId The page id in the form of Space.Page * @param content The content to be rendered. If content == "" then the page content is - * rendered. + * rendered. * @return The rendered content. * @throws XmlRpcException XmlRpcException If the page does not exist or the user has not the - * right to access it. + * right to access it. * @category ConfluenceAPI */ public String renderContent(String token, String space, String pageId, String content) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called removePage()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called removePage()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(pageId); @@ -701,17 +675,15 @@ * @param pageId The pageId in the 'Space.Page' format. * @return A list of maps representing Comment objects. * @throws XmlRpcException If the page does not exist or the user has not the right to access - * it. + * it. * @category ConfluenceAPI */ public List getComments(String token, String pageId) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getComments()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called getComments()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(pageId); @@ -742,12 +714,10 @@ public Map getComment(String token, String commentId) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getComments()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called getComments()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(commentId); int commentNumericalId = Integer.parseInt(extendedId.getParameter("commentId")); @@ -758,7 +728,7 @@ if (doc.getLocked()) { throw new XmlRpcException(String.format( "Unable to remove attachment. Document '%s' locked by '%s'", doc - .getName(), doc.getLockingUser())); + .getName(), doc.getLockingUser())); } com.xpn.xwiki.api.Object commentObject = @@ -781,17 +751,15 @@ * @param commentMap A map representing a Comment object. * @return A map representing a Comment object with updated information. * @throws XmlRpcException If the page does not exist or the user has not the right to access - * it. + * it. * @category ConfluenceAPI */ public Map addComment(String token, Map commentMap) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called addComment()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called addComment()", user.getName())); Comment comment = new Comment((Map) commentMap); XWikiExtendedId extendedId = new XWikiExtendedId(comment.getPageId()); @@ -801,7 +769,7 @@ if (doc != null) { int id = doc.createNewObject("XWiki.XWikiComments"); com.xpn.xwiki.api.Object commentObject = doc.getObject("XWiki.XWikiComments", id); - commentObject.set("author", xwikiXmlRpcContext.getUser().getName()); + commentObject.set("author", user.getName()); commentObject.set("date", new Date()); commentObject.set("comment", comment.getContent()); @@ -823,18 +791,16 @@ * @param pageId The pageId in the 'Space.Page' format. * @return True if the comment has been successfully removed. * @throws XmlRpcException If the page does not exist or the user has not the right to access - * it. + * it. * @category ConfluenceAPI */ public Boolean removeComment(String token, String commentId) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called removeComment()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called removeComment()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(commentId); int commentNumericalId = Integer.parseInt(extendedId.getParameter("commentId")); @@ -845,7 +811,7 @@ if (doc.getLocked()) { throw new XmlRpcException(String.format( "Unable to remove attachment. Document '%s' locked by '%s'", doc - .getName(), doc.getLockingUser())); + .getName(), doc.getLockingUser())); } com.xpn.xwiki.api.Object commentObject = @@ -869,18 +835,16 @@ * @param pageId The pageId in the 'Space.Page' format. * @return A list of maps representing Attachment objects. * @throws XmlRpcException If the page does not exist or the user has not the right to access - * it. + * it. * @category ConfluenceAPI */ public List getAttachments(String token, String pageId) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getAttachments()", xwikiXmlRpcContext - .getUser().getName())); + log.info(String.format("User %s has called getAttachments()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(pageId); @@ -908,22 +872,20 @@ * @param token The authentication token. * @param contentId Ignored * @param attachment The Attachment object used to identify the page id, and attachment - * metadata. + * metadata. * @param attachmentData The actual attachment data. * @return An Attachment object describing the newly added attachment. * @throws XmlRpcException If the page does not exist or the user has not the right to access - * it. + * it. * @category ConfluenceAPI */ public Map addAttachment(String token, Integer contentId, Map attachmentMap, byte[] attachmentData) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called addAttachment()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called addAttachment()", user.getName())); Attachment attachment = new Attachment((Map) attachmentMap); XWikiExtendedId extendedId = new XWikiExtendedId(attachment.getPageId()); @@ -958,7 +920,7 @@ } xwikiBaseAttachment.setContent(attachmentData); xwikiBaseAttachment.setFilename(attachment.getFileName()); - xwikiBaseAttachment.setAuthor(xwikiXmlRpcContext.getUser().getName()); + xwikiBaseAttachment.setAuthor(user.getName()); xwikiBaseAttachment.setDoc(xwikiDocument); xwikiDocument.saveAttachmentContent(xwikiBaseAttachment, xwikiXmlRpcContext @@ -987,18 +949,16 @@ * @param versionNumber (Ignored) * @return An array of bytes with the actual attachment content. * @throws XmlRpcException If the page does not exist or the user has not the right to access it - * or the attachment with the given fileName does not exist on the given page. + * or the attachment with the given fileName does not exist on the given page. * @category ConfluenceAPI */ public byte[] getAttachmentData(String token, String pageId, String fileName, String versionNumber) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getAttachmentData()", xwikiXmlRpcContext - .getUser().getName())); + log.info(String.format("User %s has called getAttachmentData()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(pageId); @@ -1011,7 +971,7 @@ } else { throw new XmlRpcException(String.format( "Attachment '%s' does not exist on page '%s'", fileName, extendedId - .getBasePageId())); + .getBasePageId())); } } else { throw new XmlRpcException(String.format("Page '%s' cannot be accessed", @@ -1028,18 +988,16 @@ * @param pageId The pageId in the 'Space.Page' format. * @return True if the attachment has been removed. * @throws XmlRpcException If the page does not exist or the user has not the right to access it - * or the attachment with the given fileName does not exist on the given page. + * or the attachment with the given fileName does not exist on the given page. * @category ConfluenceAPI */ public Boolean removeAttachment(String token, String pageId, String fileName) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called removeAttachment()", xwikiXmlRpcContext - .getUser().getName())); + log.info(String.format("User %s has called removeAttachment()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(pageId); @@ -1049,7 +1007,7 @@ if (doc.getLocked()) { throw new XmlRpcException(String.format( "Unable to remove attachment. Document '%s' locked by '%s'", doc - .getName(), doc.getLockingUser())); + .getName(), doc.getLockingUser())); } com.xpn.xwiki.api.Attachment xwikiAttachment = doc.getAttachment(fileName); @@ -1072,7 +1030,7 @@ } else { throw new XmlRpcException(String.format( "Attachment '%s' does not exist on page '%s'", fileName, extendedId - .getBasePageId())); + .getBasePageId())); } } else { throw new XmlRpcException(String.format("Page '%s' cannot be accessed", @@ -1092,12 +1050,10 @@ */ public List getClasses(String token) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getClasses()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called getClasses()", user.getName())); List result = new ArrayList(); @@ -1116,12 +1072,10 @@ */ public Map getClass(String token, String className) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getClass()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called getClass()", user.getName())); if (!xwiki.exists(className)) { throw new XmlRpcException(String.format("Class '%s' does not exist", className)); @@ -1139,16 +1093,14 @@ * @param pageId The pageId in the 'Space.Page' format. * @return A list of maps representing XWikiObject objects. * @throws XmlRpcException If the page does not exist or the user has not the right to access - * it. + * it. */ public List getObjects(String token, String pageId) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getObjects()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called getObjects()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(pageId); @@ -1184,7 +1136,7 @@ * The getObject function will return an XWikiObject where only non-null properties are included * in the mapping 'field' -> 'value' In order to know all the available fields and their * respective types and attributes, clients should refer to the object's class. - * + * * @param token The authentication token. * @param pageId The pageId in the 'Space.Page' format. * @param className The class of the object. @@ -1192,17 +1144,15 @@ * @return The XWikiObject containing the information about all the properties contained in the * selected object. * @throws XmlRpcException If the page does not exist or the user has not the right to access it - * or no object with the given id exist in the page. + * or no object with the given id exist in the page. */ public Map getObject(String token, String pageId, String className, Integer id) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called getObject()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called getObject()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(pageId); @@ -1230,21 +1180,19 @@ /** * Update the object or create a new one if it doesn't exist. - * + * * @param token The authentication token. * @param objectMap A map representing the XWikiObject to be updated/created. * @return A map representing the XWikiObject with the updated information. * @throws XmlRpcException If the page does not exist or the user has not the right to access - * it. + * it. */ public Map storeObject(String token, Map objectMap) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called storeObject()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called storeObject()", user.getName())); XWikiObject object = new XWikiObject(objectMap); XWikiExtendedId extendedId = new XWikiExtendedId(object.getPageId()); @@ -1303,17 +1251,15 @@ * @param id The object's id. * @return True if the object has been successfully deleted. * @throws XmlRpcException If the page does not exist or the user has not the right to access it - * or no object with the given id exist in the page. + * or no object with the given id exist in the page. */ public Boolean removeObject(String token, String pageId, String className, Integer id) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called removeComment()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called removeComment()", user.getName())); XWikiExtendedId extendedId = new XWikiExtendedId(pageId); @@ -1323,7 +1269,7 @@ if (doc.getLocked()) { throw new XmlRpcException(String.format( "Unable to remove attachment. Document '%s' locked by '%s'", doc - .getName(), doc.getLockingUser())); + .getName(), doc.getLockingUser())); } com.xpn.xwiki.api.Object commentObject = doc.getObject(className, id); @@ -1333,7 +1279,7 @@ } else { throw new XmlRpcException(String.format( "Object %s[%d] on page '%s' does not exist", className, id, extendedId - .getBasePageId())); + .getBasePageId())); } } else { throw new XmlRpcException(String.format("Page '%s' cannot be accessed", @@ -1350,18 +1296,16 @@ /** * @param token The authentication token. * @param query The string to be looked for. If it is "__ALL_PAGES__" the search will return all - * the page ids available in the Wiki. + * the page ids available in the Wiki. * @return A list of SearchResults */ public List search(String token, String query, int maxResults) throws XWikiException, XmlRpcException { - XWikiXmlRpcContext xwikiXmlRpcContext = - XWikiUtils.getXWikiXmlRpcContext(token, xwikiRequest, xwikiResponse, - xwikiServletContext); + XWikiXmlRpcContext xwikiXmlRpcContext = xwikiXmlRpcHttpRequestConfig.getXmlRpcContext(); + XWikiXmlRpcUser user = XWikiUtils.checkToken(token, xwikiXmlRpcContext.getXWikiContext()); XWiki xwiki = xwikiXmlRpcContext.getXWiki(); - log.info(String.format("User %s has called removeComment()", xwikiXmlRpcContext.getUser() - .getName())); + log.info(String.format("User %s has called removeComment()", user.getName())); com.xpn.xwiki.XWiki baseXWiki = xwikiXmlRpcContext.getBaseXWiki(); Index: src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcHttpRequestConfig.java =================================================================== --- src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcHttpRequestConfig.java (revision 9563) +++ src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcHttpRequestConfig.java (working copy) @@ -20,6 +20,7 @@ */ package com.xpn.xwiki.xmlrpc; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl; @@ -27,15 +28,20 @@ /** * This is an helper class for storing the current HTTP request coming from an XMLRPC client * interacting with the XMLRPC endpoint servlet - * + * * @author fmancinelli */ public class XWikiXmlRpcHttpRequestConfig extends XmlRpcHttpRequestConfigImpl { private HttpServletRequest request; - public XWikiXmlRpcHttpRequestConfig(HttpServletRequest request) + private HttpServlet servlet; + + private XWikiXmlRpcContext xmlRpcContext; + + public XWikiXmlRpcHttpRequestConfig(HttpServlet servlet, HttpServletRequest request) { + this.servlet = servlet; this.request = request; } @@ -43,4 +49,20 @@ { return request; } + + public HttpServlet getServlet() + { + return servlet; + } + + public void setXmlRpcContext(XWikiXmlRpcContext xmlRpcContext) + { + this.xmlRpcContext = xmlRpcContext; + } + + public XWikiXmlRpcContext getXmlRpcContext() + { + return xmlRpcContext; + } + } Index: src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcUser.java =================================================================== --- src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcUser.java (revision 9563) +++ src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcUser.java (working copy) @@ -22,7 +22,7 @@ /** * This is an helper class for storing XMLRPC user information. - * + * * @author fmancinelli */ public class XWikiXmlRpcUser Index: src/main/java/com/xpn/xwiki/xmlrpc/DomainObjectFactory.java =================================================================== --- src/main/java/com/xpn/xwiki/xmlrpc/DomainObjectFactory.java (revision 9563) +++ src/main/java/com/xpn/xwiki/xmlrpc/DomainObjectFactory.java (working copy) @@ -44,14 +44,14 @@ /** * This class contains utility methods for building xmlrpc domain objects. - * + * * @author fmancinelli */ public class DomainObjectFactory { /** * Create a space summary - * + * * @return The SpaceSummary representing the space. */ public static SpaceSummary createSpaceSummary(String spaceKey) @@ -66,7 +66,7 @@ /** * Create a space summary starting from the space Web home. - * + * * @return The SpaceSummary representing the space. */ public static SpaceSummary createSpaceSummary(Document spaceWebHome) @@ -88,7 +88,7 @@ /** * Create a space description. - * + * * @return A Space object containing all the information about the space. */ public static Space createSpace(String spaceKey) @@ -105,7 +105,7 @@ /** * Create a space from its WebHome document. - * + * * @return A Space object containing all the information about the space. */ public static Space createSpace(Document spaceWebHome) @@ -122,7 +122,7 @@ /** * Create a page summary description from an XWiki document. - * + * * @return An XWikiPageSummary with the information. * @throws XWikiException If there is a problem getting page translations. */ @@ -148,10 +148,10 @@ /** * Create a page description from an XWiki document. - * + * * @param useExtendedPageId true if the id should contain additional information concerning the - * version, language etc. In this case the pageId will be in the form - * Space.Page?param=value¶m=value&... + * version, language etc. In this case the pageId will be in the form + * Space.Page?param=value¶m=value&... * @return An XWikiPage object representing the page. * @throws XWikiException If there is a problem getting page translations. */ @@ -195,7 +195,7 @@ /** * Create a page history summary containing revision information about a document. - * + * * @return An XWikiPageHistorySummary object containing the revision information. */ public static XWikiPageHistorySummary createXWikiPageHistorySummary(Document document) @@ -217,7 +217,7 @@ /** * Create a comment object containing all the information concerning a document comment. - * + * * @param commentObject The XWiki object of type "XWiki.Comment" containing the actual comment. * @return A Comment Object containing comment information. */ @@ -247,7 +247,7 @@ /** * Create an Attachment object containing information about an attachment. - * + * * @return An Attachment object containing all the information. */ public static Attachment createAttachment(com.xpn.xwiki.api.Attachment xwikiAttachment) @@ -273,7 +273,7 @@ /** * Create a summary of an XWiki class. - * + * * @return An XWikiClassSummary containing information about the class. */ public static XWikiClassSummary createXWikiClassSummary(String className) @@ -286,7 +286,7 @@ /** * Create an XWikiClass object with all the information about a given XWiki class - * + * * @return An XWikiClass object with all the information. */ public static XWikiClass createXWikiClass(com.xpn.xwiki.api.Class xwikiClass) @@ -322,7 +322,7 @@ /** * Create a summary of a given xwiki object. - * + * * @return An XWikiObjectSummary object containing all the information. */ public static XWikiObjectSummary createXWikiObjectSummary(Document document, @@ -345,7 +345,7 @@ /** * Create an XWikiObject containing all the information and attributed of a given xwiki object. - * + * * @return An XWikiObject containing all the information. */ public static XWikiObject createXWikiObject(Document document, com.xpn.xwiki.api.Object object) @@ -380,7 +380,7 @@ /** * Create a search result object. - * + * * @param pageId The page id representing the page associated with this result. * @return A SearchResult object containing the information. */ @@ -395,4 +395,4 @@ return result; } -} \ No newline at end of file +} Index: src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcContext.java =================================================================== --- src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcContext.java (revision 9563) +++ src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcContext.java (working copy) @@ -26,7 +26,7 @@ /** * This is an helper class that contains information about the XWiki context and the current XMLRPC * user. - * + * * @author fmancinelli */ public class XWikiXmlRpcContext @@ -42,20 +42,16 @@ */ private com.xpn.xwiki.XWiki baseXWiki; - private XWikiXmlRpcUser user; - /* * This XWikiContext is needed for performing operations with the low-level API. */ private XWikiContext xwikiContext; - public XWikiXmlRpcContext(XWikiContext xwikiContext, com.xpn.xwiki.XWiki baseXWiki, - XWiki xwiki, XWikiXmlRpcUser user) + public XWikiXmlRpcContext(XWikiContext xwikiContext) { this.xwikiContext = xwikiContext; - this.baseXWiki = baseXWiki; - this.xwiki = xwiki; - this.user = user; + this.baseXWiki = xwikiContext.getWiki(); + this.xwiki = new com.xpn.xwiki.api.XWiki(baseXWiki, xwikiContext); } public XWiki getXWiki() @@ -63,11 +59,6 @@ return xwiki; } - public XWikiXmlRpcUser getUser() - { - return user; - } - public XWikiContext getXWikiContext() { return xwikiContext; @@ -77,9 +68,4 @@ { return baseXWiki; } - - public void setBaseXWiki(com.xpn.xwiki.XWiki baseXWiki) - { - this.baseXWiki = baseXWiki; - } } Index: src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcServlet.java =================================================================== --- src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcServlet.java (revision 9563) +++ src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcServlet.java (working copy) @@ -21,21 +21,25 @@ package com.xpn.xwiki.xmlrpc; import java.io.IOException; +import java.lang.reflect.Method; import java.net.URL; + import javax.servlet.ServletConfig; import javax.servlet.http.HttpServletRequest; import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.XmlRpcHandler; import org.apache.xmlrpc.XmlRpcRequest; import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl; import org.apache.xmlrpc.server.PropertyHandlerMapping; import org.apache.xmlrpc.server.RequestProcessorFactoryFactory; +import org.apache.xmlrpc.server.RequestProcessorFactoryFactory.RequestProcessorFactory; import org.apache.xmlrpc.webserver.XmlRpcServlet; import org.apache.xmlrpc.webserver.XmlRpcServletServer; /** * This is the XMLRPC servlet that is used as a gateway for serving XMLRPC requests. - * + * * @author fmancinelli */ public class XWikiXmlRpcServlet extends XmlRpcServlet @@ -46,7 +50,44 @@ protected PropertyHandlerMapping newPropertyHandlerMapping(URL url) throws IOException, XmlRpcException { - PropertyHandlerMapping mapping = new PropertyHandlerMapping(); + + PropertyHandlerMapping mapping = new PropertyHandlerMapping() + { + /** + * This is almost a 1-to-1 copy of the implementation of + * org.apache.xmlrpc.server.PropertyHandlerMapping.newXmlRpcHandler() defined in its + * superclass AbstractReflectiveHandlerMapping. We had to do this because in the + * original implementation of the method newXmlRpcHandler in + * AbstractReflectiveHandlerMapping, the classes RelfectiveXmlRpcHandler and + * ReflectiveXmlRpcMetaDataHandler are hard-coded. + */ + @Override + protected XmlRpcHandler newXmlRpcHandler(Class pClass, Method[] pMethods) + throws XmlRpcException + { + + String[][] sig = getSignature(pMethods); + String help = getMethodHelp(pClass, pMethods); + RequestProcessorFactory factory = + getRequestProcessorFactoryFactory().getRequestProcessorFactory(pClass); + if (sig == null || help == null) { + return new XWikiReflectiveXmlRpcHandler(this, + getTypeConverterFactory(), + pClass, + factory, + pMethods); + } + return new XWikiReflectiveXmlRpcMetaDataHandler(this, + getTypeConverterFactory(), + pClass, + factory, + pMethods, + sig, + help); + } + + }; + mapping.setTypeConverterFactory(getXmlRpcServletServer().getTypeConverterFactory()); RequestProcessorFactoryFactory factory = @@ -62,7 +103,7 @@ HttpServletRequest request = ((XWikiXmlRpcHttpRequestConfig) pRequest.getConfig()).getRequest(); - handler.init(XWikiXmlRpcServlet.this, request); + handler.init((XWikiXmlRpcHttpRequestConfig) pRequest.getConfig()); } return proc; @@ -78,13 +119,16 @@ @Override protected XmlRpcServletServer newXmlRpcServer(ServletConfig config) throws XmlRpcException { - return new XmlRpcServletServer() + XmlRpcServletServer server = new XmlRpcServletServer() { + @Override protected XmlRpcHttpRequestConfigImpl newConfig(HttpServletRequest request) { - return new XWikiXmlRpcHttpRequestConfig(request); + return new XWikiXmlRpcHttpRequestConfig(XWikiXmlRpcServlet.this, request); } }; + + return server; } } Index: src/main/java/com/xpn/xwiki/xmlrpc/XWikiReflectiveXmlRpcHandler.java =================================================================== --- src/main/java/com/xpn/xwiki/xmlrpc/XWikiReflectiveXmlRpcHandler.java (revision 0) +++ src/main/java/com/xpn/xwiki/xmlrpc/XWikiReflectiveXmlRpcHandler.java (revision 0) @@ -0,0 +1,112 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This 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.1 of + * the License, or (at your option) any later version. + * + * This software 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. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + * + */ +package com.xpn.xwiki.xmlrpc; + +import java.lang.reflect.Method; + +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.XmlRpcRequest; +import org.apache.xmlrpc.common.TypeConverterFactory; +import org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping; +import org.apache.xmlrpc.server.ReflectiveXmlRpcHandler; +import org.apache.xmlrpc.server.RequestProcessorFactoryFactory.RequestProcessorFactory; + +import com.xpn.xwiki.XWiki; +import com.xpn.xwiki.XWikiContext; +import com.xpn.xwiki.XWikiException; +import com.xpn.xwiki.render.XWikiVelocityRenderer; +import com.xpn.xwiki.web.Utils; +import com.xpn.xwiki.web.XWikiRequest; +import com.xpn.xwiki.web.XWikiResponse; +import com.xpn.xwiki.web.XWikiServletContext; +import com.xpn.xwiki.web.XWikiURLFactory; + +/** + * This is a custom XmlRpcHandler for replacing the standard one in the Apache XMLRPC framework. The + * trick here is to redefine the execute() method in order to intercept all the XMLRPC method calls. + * In this way we can wrap method execution in a try/catch/finally in order to properly handle + * exceptions/cleanup. + * + * @author fmancinelli + */ +public class XWikiReflectiveXmlRpcHandler extends ReflectiveXmlRpcHandler +{ + public XWikiReflectiveXmlRpcHandler(AbstractReflectiveHandlerMapping mapping, + TypeConverterFactory typeConverterFactory, Class class1, RequestProcessorFactory factory, + Method[] methods) + { + super(mapping, typeConverterFactory, class1, factory, methods); + } + + /** + * Here we intercept all the method call and wrap them in a try/catch/finally block. + * + * @see ReflectiveXmlRpcHandler#execute(XmlRpcRequest) + */ + @Override + public Object execute(final XmlRpcRequest request) throws XmlRpcException + { + try { + /* + * Here we prepare the XWikiXmlRpcContext object and we put it in the config object so + * that XMLRPC methods can get it. The config object is the same that is passed to the + * XWikiXmlRpcHandler when it is initialized. So modifications made here are reflected + * in the config object that is available to XMLRPC methods through the field + * XWikiXmlRpcHandler.xwikiXmlRpcHttpRequestConfig. + */ + XWikiXmlRpcHttpRequestConfig config = + (XWikiXmlRpcHttpRequestConfig) request.getConfig(); + XWikiRequest xwikiRequest = new XWikiXmlRpcRequest(config.getRequest()); + XWikiResponse xwikiResponse = + new XWikiXmlRpcResponse((XWikiResponse) XWikiUtils.mock(XWikiResponse.class)); + XWikiServletContext xwikiServletContext = + new XWikiServletContext(config.getServlet().getServletContext()); + + XWikiContext context = + Utils.prepareContext("", xwikiRequest, xwikiResponse, xwikiServletContext); + XWiki xwiki = XWiki.getXWiki(context); + XWikiURLFactory urlf = + xwiki.getURLFactoryService().createURLFactory(context.getMode(), context); + context.setURLFactory(urlf); + XWikiVelocityRenderer.prepareContext(context); + + // XWikiXmlRpcContext xmlRpcContext = + // XWikiUtils + // .getXWikiXmlRpcContext(xwikiRequest, xwikiResponse, xwikiServletContext); + + /* Here we set the prepared context */ + config.setXmlRpcContext(new XWikiXmlRpcContext(context)); + + /* + * This performs the actual XMLRPC method invocation using all the logic we don't care + * of :) + */ + return super.execute(request); + } catch (XmlRpcException e) { + throw e; + } catch (XWikiException e) { + throw new XmlRpcException(e.getMessage(), e); + } finally { + // Put here all the cleanup code. + } + } + +} Index: src/main/java/com/xpn/xwiki/xmlrpc/XWikiReflectiveXmlRpcMetadataHandler.java =================================================================== --- src/main/java/com/xpn/xwiki/xmlrpc/XWikiReflectiveXmlRpcMetadataHandler.java (revision 0) +++ src/main/java/com/xpn/xwiki/xmlrpc/XWikiReflectiveXmlRpcMetadataHandler.java (revision 0) @@ -0,0 +1,59 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This 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.1 of + * the License, or (at your option) any later version. + * + * This software 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. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + * + */ +package com.xpn.xwiki.xmlrpc; + +import java.lang.reflect.Method; + +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.common.TypeConverterFactory; +import org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping; +import org.apache.xmlrpc.server.RequestProcessorFactoryFactory.RequestProcessorFactory; + +/* + * This is exactly the same of org.apache.xmlrpc.metadataReflectiveXmlRpcMetaDataHandler. We had to + * re-implement it because it must extend XWikiReflectiveXmlRpcMetadataHandler + */ +class XWikiReflectiveXmlRpcMetaDataHandler extends XWikiReflectiveXmlRpcHandler +{ + private String[][] signatures; + + private String methodHelp; + + public XWikiReflectiveXmlRpcMetaDataHandler(AbstractReflectiveHandlerMapping pMapping, + TypeConverterFactory pTypeConverterFactory, Class pClass, + RequestProcessorFactory pFactory, Method[] pMethods, String[][] pSignatures, + String pMethodHelp) + { + super(pMapping, pTypeConverterFactory, pClass, pFactory, pMethods); + signatures = pSignatures; + methodHelp = pMethodHelp; + } + + public String[][] getSignatures() throws XmlRpcException + { + return signatures; + } + + public String getMethodHelp() throws XmlRpcException + { + return methodHelp; + } +} Index: src/main/java/com/xpn/xwiki/xmlrpc/XWikiUtils.java =================================================================== --- src/main/java/com/xpn/xwiki/xmlrpc/XWikiUtils.java (revision 9563) +++ src/main/java/com/xpn/xwiki/xmlrpc/XWikiUtils.java (working copy) @@ -33,20 +33,13 @@ import java.util.Map; import org.apache.xmlrpc.XmlRpcException; -import com.xpn.xwiki.XWiki; + import com.xpn.xwiki.XWikiContext; -import com.xpn.xwiki.XWikiException; -import com.xpn.xwiki.render.XWikiVelocityRenderer; -import com.xpn.xwiki.web.Utils; -import com.xpn.xwiki.web.XWikiRequest; -import com.xpn.xwiki.web.XWikiResponse; -import com.xpn.xwiki.web.XWikiServletContext; -import com.xpn.xwiki.web.XWikiURLFactory; /** * This is an helper class containing some utility method for handling and setting up the XWiki and * XMLRPC data objects needed to serve XMLRPC requests. - * + * * @author fmancinelli */ public class XWikiUtils @@ -65,36 +58,11 @@ return null; } }; - Class[] interfaces = new Class[]{someClass}; + Class[] interfaces = new Class[] {someClass}; return Proxy.newProxyInstance(loader, interfaces, handler); } - public static XWikiXmlRpcContext getXWikiXmlRpcContext(String token, XWikiRequest request, - XWikiResponse response, XWikiServletContext servletContext) throws XWikiException, - XmlRpcException - { - - XWikiContext context = getXWikiContext(request, response, servletContext); - XWikiXmlRpcUser user = XWikiUtils.checkToken(token, context); - com.xpn.xwiki.api.XWiki xwiki = new com.xpn.xwiki.api.XWiki(context.getWiki(), context); - - return new XWikiXmlRpcContext(context, context.getWiki(), xwiki, user); - } - - public static XWikiContext getXWikiContext(XWikiRequest request, XWikiResponse response, - XWikiServletContext servletContext) throws XWikiException - { - XWikiContext context = Utils.prepareContext("", request, response, servletContext); - XWiki xwiki = XWiki.getXWiki(context); - XWikiURLFactory urlf = - xwiki.getURLFactoryService().createURLFactory(context.getMode(), context); - context.setURLFactory(urlf); - XWikiVelocityRenderer.prepareContext(context); - - return context; - } - public static Map getTokens(XWikiContext context) { Map tokens = (Map) context.getEngineContext().getAttribute("xmlrpc_tokens"); @@ -106,11 +74,6 @@ return tokens; } - public static XWikiXmlRpcUser getUserForToken(XWikiContext context, String token) - { - return (XWikiXmlRpcUser) getTokens(context).get(token); - } - public static XWikiXmlRpcUser checkToken(String token, XWikiContext context) throws XmlRpcException {