Class BulkReplaceServlet

  • All Implemented Interfaces:
    Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

    public class BulkReplaceServlet
    extends org.apache.sling.api.servlets.SlingAllMethodsServlet
    Servlet with functionality for the bulk replace tool.

    Operations: 1. Search: - POST (operation=search) Parameters: • rootPath: absolute page path to start search (e.g. /content/site/en) • term: literal search text Response: 202 Accepted with JSON payload {"jobId": ""}. Note: The search parameters are stored in an internal LinkedHashMap (only the last 10 operations are kept).

    - GET (operation=search, jobId=) Streams search results as text/event-stream based on the stored parameters.

    2. Replace: - POST (operation=replace) Parameters: • rootPath, term, replacement, target (see bulkreplace.md) Response: JSON indicating replace result.

    See Also:
    Serialized Form
    • Constructor Detail

      • BulkReplaceServlet

        public BulkReplaceServlet()
    • Method Detail

      • doPost

        protected void doPost​(@Nonnull
                              org.apache.sling.api.SlingHttpServletRequest request,
                              @Nonnull
                              org.apache.sling.api.SlingHttpServletResponse response)
                       throws IOException
        Handles POST requests for search and replace operations.
        Overrides:
        doPost in class org.apache.sling.api.servlets.SlingAllMethodsServlet
        Parameters:
        request - the SlingHttpServletRequest, not null
        response - the SlingHttpServletResponse, not null
        Throws:
        IOException - if an I/O error occurs
      • doGet

        protected void doGet​(@Nonnull
                             org.apache.sling.api.SlingHttpServletRequest request,
                             @Nonnull
                             org.apache.sling.api.SlingHttpServletResponse response)
                      throws IOException
        Handles GET requests to stream search results: the second step that does the actual search.
        Overrides:
        doGet in class org.apache.sling.api.servlets.SlingSafeMethodsServlet
        Parameters:
        request - the SlingHttpServletRequest, not null
        response - the SlingHttpServletResponse, not null
        Throws:
        IOException - if an I/O error occurs
      • createExcerpt

        @Nonnull
        protected String createExcerpt​(@Nonnull
                                       String text,
                                       @Nonnull
                                       Pattern termPattern,
                                       int surroundingChars)
        Creates an excerpt from the given text around the search term.
        Parameters:
        text - the full text, not null
        termPattern - a pattern matching the search term, not null
        Returns:
        an excerpt of the text surrounding the term
      • abbreviateSurroundings

        @Nonnull
        protected String abbreviateSurroundings​(@Nonnull
                                                String text,
                                                @Nonnull
                                                Pattern termPattern,
                                                int surroundingChars)
        Creates an excerpt from the given text around the search term.
        Parameters:
        text - the full text, not null
        termPattern - a pattern matching the search term, not null
        Returns:
        an excerpt of the text surrounding the term
      • toPlaintext

        protected String toPlaintext​(String text)
        Use Jsoup to create plaintext from HTML. We assume it's HTML if it starts with < and ends with > , trimmed.
      • whitespaceLenientPattern

        protected Pattern whitespaceLenientPattern​(@Nonnull
                                                   String searchString)
        A pattern that matches the search String case sensitively but being lenient on whitespaces: the pattern made from "a b c" should also match "a b\nc". If there is a whitespace at the beginning or the end we treat it as word boundary.