Class XWikiServletURLFactory

    • Field Detail

      • daemon

        protected boolean daemon
      • originalURL

        protected URL originalURL
      • defaultURLs

        protected Map<String,​URL> defaultURLs
        This is the URL which was requested by the user possibly with the host modified if x-forwarded-host header is set or if xwiki.home parameter is set and we are viewing the main page.
      • contextPath

        protected String contextPath
    • Constructor Detail

      • XWikiServletURLFactory

        public XWikiServletURLFactory()
      • XWikiServletURLFactory

        public XWikiServletURLFactory​(URL defaultURL,
                                      String contextPath,
                                      String actionPath)
      • XWikiServletURLFactory

        public XWikiServletURLFactory​(XWikiContext context)
        Creates a new URL factory that uses the server URL and context path specified by the given XWiki context. This constructor should be used only in tests. Make sure XWikiContext.setURL(URL) is called before this constructor.
        Parameters:
        context -
    • Method Detail

      • setDefaultURL

        public void setDefaultURL​(String wikiId,
                                  URL baseURL)
        Parameters:
        wikiId - the wiki identifier to associate with this URL
        baseURL - the input URL to take into account, null to disable it
        Since:
        10.3
      • getContextPath

        public String getContextPath()
        Returns the part of the URL identifying the web application. In a normal install, that is xwiki/.
        Returns:
        The configured context path.
      • getServerURL

        public URL getServerURL​(String wikiId,
                                XWikiContext context)
                         throws MalformedURLException
        Get the url of the server EG: http://www.xwiki.org/ This function sometimes will return a URL with a trailing / and other times not. This is because the xwiki.home param is recommended to have a trailing / but this.serverURL never does.
        Parameters:
        wikiId - the identifier of the wiki, if null it is assumed to be the same as the wiki which we are currently displaying.
        context - the XWikiContext used to determine the current wiki and the value if the xwiki.home parameter if needed as well as access the xwiki server document if in virtual mode.
        Returns:
        a URL containing the protocol, host, and port (if applicable) of the server to use for the given database.
        Throws:
        MalformedURLException
      • createURL

        public URL createURL​(String spaces,
                             String name,
                             String action,
                             boolean redirect,
                             XWikiContext context)
        Parameters:
        spaces - a serialized space reference which can contain one or several spaces (e.g. "space1.space2"). If a space name contains a dot (".") it must be passed escaped as in "space1\.with\.dot.space2"
      • createURL

        public URL createURL​(String spaces,
                             String name,
                             String action,
                             String querystring,
                             String anchor,
                             String xwikidb,
                             XWikiContext context)
        Parameters:
        spaces - a serialized space reference which can contain one or several spaces (e.g. "space1.space2"). If a space name contains a dot (".") it must be passed escaped as in "space1\.with\.dot.space2"
        querystring - the URL-encoded Query String. It's important to realize that the implementation of this method cannot encode it automatically since the Query String is passed as a String (and it's not possible to differentiate between a '=' character that should be encoded and one that shouldn't. Imagine an input of 'a=&b=c' which can be understood either as 'a' = '&b=c' or as 'a' = '' and 'b' = 'c'). Ideally we would need an API signature that accepts a Map<String, String> for the Query String, for example
      • createExternalURL

        public URL createExternalURL​(String spaces,
                                     String name,
                                     String action,
                                     String querystring,
                                     String anchor,
                                     String xwikidb,
                                     XWikiContext context)
        Parameters:
        spaces - a serialized space reference which can contain one or several spaces (e.g. "space1.space2"). If a space name contains a dot (".") it must be passed escaped as in "space1\.with\.dot.space2"
        querystring - the URL-encoded Query String. It's important to realize that the implementation of this method cannot encode it automatically since the Query String is passed as a String (and it's not possible to differentiate between a '=' character that should be encoded and one that shouldn't. Imagine an input of 'a=&b=c' which can be understood either as 'a' = '&b=c' or as 'a' = '' and 'b' = 'c'). Ideally we would need an API signature that accepts a Map<String, String> for the Query String, for example
      • createSkinURL

        public URL createSkinURL​(String filename,
                                 String skin,
                                 XWikiContext context,
                                 Map<String,​Object> queryParameters)
        Description copied from interface: XWikiURLFactory
        Create an URL for the given filename in the given skin directory.
        Parameters:
        filename - the file to reach.
        skin - the skin where the file should be loaded
        context - current context
        queryParameters - the parameters to put at the end of the URL
        Returns:
        a URL to load the given file with the given query parameters.
      • createSkinURL

        public URL createSkinURL​(String filename,
                                 String spaces,
                                 String name,
                                 String xwikidb,
                                 XWikiContext context,
                                 Map<String,​Object> queryParameters)
        Description copied from interface: XWikiURLFactory
        Create an URL for the filename located in the spaces and with given repository.
        Parameters:
        filename - the path of the file to reach.
        spaces - the spaces where the file is located.
        name - the directory where the file is located.
        xwikidb - the wiki in which the file is located.
        context - current context.
        queryParameters - parameters to put at the end of the URL.
        Returns:
        an URL to load the given file.
      • createResourceURL

        public URL createResourceURL​(String filename,
                                     boolean forceSkinAction,
                                     XWikiContext context,
                                     Map<String,​Object> queryParameters)
        Description copied from interface: XWikiURLFactory
        Create an URL for the file resource.
        Parameters:
        filename - the path of the file to load.
        forceSkinAction - if true specify the skin directory in the URL.
        context - the current context.
        queryParameters - the parameters to put at the end of the URL.
        Returns:
        an URL to load the given file
      • createAttachmentURL

        public URL createAttachmentURL​(String filename,
                                       String spaces,
                                       String name,
                                       String action,
                                       String querystring,
                                       String xwikidb,
                                       XWikiContext context)
        spaces - a serialized space reference which can contain one or several spaces (e.g. "space1.space2"). If a space name contains a dot (".") it must be passed escaped as in "space1\.with\.dot.space2"
        querystring - the URL-encoded Query String. It's important to realize that the implementation of this method cannot encode it automatically since the Query String is passed as a String (and it's not possible to differentiate between a '=' character that should be encoded and one that shouldn't. Imagine an input of 'a=&b=c' which can be understood either as 'a' = '&b=c' or as 'a' = '' and 'b' = 'c'). Ideally we would need an API signature that accepts a Map<String, String> for the Query String, for example
      • isContextDoc

        protected boolean isContextDoc​(String wiki,
                                       String spaces,
                                       String name,
                                       XWikiContext context)
        Check if a document is the original context document. This is needed when generating attachment revision URLs, since only attachments of the context document should also be versioned.
        Parameters:
        wiki - the wiki name of the document to check
        spaces - the space names of the document to check
        name - the document name of the document to check
        context - the current request context
        Returns:
        true if the provided document is the same as the current context document, false otherwise
      • createAttachmentRevisionURL

        public URL createAttachmentRevisionURL​(String filename,
                                               String spaces,
                                               String name,
                                               String revision,
                                               String querystring,
                                               String xwikidb,
                                               XWikiContext context)
        spaces - a serialized space reference which can contain one or several spaces (e.g. "space1.space2"). If a space name contains a dot (".") it must be passed escaped as in "space1\.with\.dot.space2"
        querystring - the URL-encoded Query String. It's important to realize that the implementation of this method cannot encode it automatically since the Query String is passed as a String (and it's not possible to differentiate between a '=' character that should be encoded and one that shouldn't. Imagine an input of 'a=&b=c' which can be understood either as 'a' = '&b=c' or as 'a' = '' and 'b' = 'c'). Ideally we would need an API signature that accepts a Map<String, String> for the Query String, for example
      • getURL

        public String getURL​(URL url,
                             XWikiContext context)
        Converts a URL to a relative URL if it's a XWiki URL (keeping only the path + query string + anchor) and leave the URL unchanged if it's an external URL.

        An URL is considered to be external if its server component doesn't match the server of the current request URL. This means that URLs are made relative with respect to the current request URL rather than the current wiki set on the XWiki context. Let's take an example:

         
         request URL: http://playground.xwiki.org/xwiki/bin/view/Sandbox/TestURL
         current wiki: code (code.xwiki.org)
         URL (1): http://code.xwiki.org/xwiki/bin/view/Main/WebHome
         URL (2): http://playground.xwiki.org/xwiki/bin/view/Spage/Page
         
         The result will be:
         (1) http://code.xwiki.org/xwiki/bin/view/Main/WebHome
         (2) /xwiki/bin/view/Spage/Page
         
         
        Specified by:
        getURL in interface XWikiURLFactory
        Overrides:
        getURL in class XWikiDefaultURLFactory
        Parameters:
        url - the URL to convert
        Returns:
        the converted URL as a string
        See Also:
        XWikiDefaultURLFactory.getURL(java.net.URL, com.xpn.xwiki.XWikiContext)
      • normalizeURL

        protected static URL normalizeURL​(URL url,
                                          XWikiContext context)
                                   throws MalformedURLException
        Encodes the passed URL and offers the possibility for Servlet Filter to perform URL rewriting (this is used for example by Tuckey's URLRewriteFilter for rewriting outbound URLs, see http://platform.xwiki.org/xwiki/bin/view/Main/ShortURLs).

        However Servlet Container will also add a ";jsessionid=xxx" content to the URL while encoding the URL and we strip it since we don't want to have that in our URLs as it can cause issues with:

        • security
        • SEO
        • clients not expecting jsessionid in URL, for example RSS feed readers which will think that articles are different as they'll get different URLs everytime they call the XWiki server
        See why jsessionid are considered harmful here and here
        Parameters:
        url - the URL to encode and normalize
        context - the XWiki Context used to get access to the Response for encoding the URL
        Returns:
        the normalized URL
        Throws:
        MalformedURLException - if the passed URL is invalid
      • normalizeURL

        protected static URL normalizeURL​(String url,
                                          XWikiContext context)
                                   throws MalformedURLException
        Encodes the passed URL and offers the possibility for Servlet Filter to perform URL rewriting (this is used for example by Tuckey's URLRewriteFilter for rewriting outbound URLs, see http://platform.xwiki.org/xwiki/bin/view/Main/ShortURLs).

        However Servlet Container will also add a ";jsessionid=xxx" content to the URL while encoding the URL and we strip it since we don't want to have that in our URLs as it can cause issues with:

        • security
        • SEO
        • clients not expecting jsessionid in URL, for example RSS feed readers which will think that articles are different as they'll get different URLs everytime they call the XWiki server
        See why jsessionid are considered harmful here and here
        Parameters:
        url - the URL to encode and normalize
        context - the XWiki Context used to get access to the Response for encoding the URL
        Returns:
        the normalized URL
        Throws:
        MalformedURLException - if the passed URL is invalid