Package com.composum.ai.aem.core.impl
Class BulkReplaceServlet
- java.lang.Object
-
- javax.servlet.GenericServlet
-
- org.apache.sling.api.servlets.SlingSafeMethodsServlet
-
- org.apache.sling.api.servlets.SlingAllMethodsServlet
-
- com.composum.ai.aem.core.impl.BulkReplaceServlet
-
- All Implemented Interfaces:
Serializable,javax.servlet.Servlet,javax.servlet.ServletConfig
public class BulkReplaceServlet extends org.apache.sling.api.servlets.SlingAllMethodsServletServlet 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classBulkReplaceServlet.Changedprotected static classBulkReplaceServlet.Matchprotected static classBulkReplaceServlet.ReplacePageResponseprotected static classBulkReplaceServlet.ReplaceRequestprotected static classBulkReplaceServlet.SearchPageResponseprotected static classBulkReplaceServlet.SearchParamsprotected static classBulkReplaceServlet.SummaryResponseprotected static classBulkReplaceServlet.Target
-
Constructor Summary
Constructors Constructor Description BulkReplaceServlet()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected StringabbreviateSurroundings(String text, Pattern termPattern, int surroundingChars)Creates an excerpt from the given text around the search term.protected booleancontainsCJKCharacters(String text)Checks if a string contains non-Latin characters that may not be properly tokenized by Lucene.protected StringcreateExcerpt(String text, Pattern termPattern, int surroundingChars)Creates an excerpt from the given text around the search term.protected voiddoGet(org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response)Handles GET requests to stream search results: the second step that does the actual search.protected voiddoPost(org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response)Handles POST requests for search and replace operations.protected StringtoPlaintext(String text)Use Jsoup to create plaintext from HTML.protected PatternwhitespaceLenientPattern(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".-
Methods inherited from class org.apache.sling.api.servlets.SlingAllMethodsServlet
doDelete, doPut, getAllowedRequestMethods, isMethodValid, mayService
-
Methods inherited from class org.apache.sling.api.servlets.SlingSafeMethodsServlet
doGeneric, doHead, doOptions, doTrace, getServletInfo, handleMethodNotImplemented, service, service
-
-
-
-
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:
doPostin classorg.apache.sling.api.servlets.SlingAllMethodsServlet- Parameters:
request- the SlingHttpServletRequest, not nullresponse- 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:
doGetin classorg.apache.sling.api.servlets.SlingSafeMethodsServlet- Parameters:
request- the SlingHttpServletRequest, not nullresponse- 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 nulltermPattern- 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 nulltermPattern- 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.
-
containsCJKCharacters
protected boolean containsCJKCharacters(@Nonnull String text)
Checks if a string contains non-Latin characters that may not be properly tokenized by Lucene. Includes: CJK (Chinese, Japanese, Korean), Arabic, Hebrew, Thai, and other languages.
-
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.
-
-