This is an automated email from the ASF dual-hosted git repository.

snoopdave pushed a commit to branch atompub-roller-servlet
in repository https://gitbox.apache.org/repos/asf/roller.git

commit 83baa8618336f995b46a812d888dda2790e92dcb
Author: David M. Johnson <[email protected]>
AuthorDate: Tue Sep 29 17:03:42 2026 -0400

    Serve AtomPub through a Roller servlet
    
    Map /roller-services/app/* to a small RollerAtomServlet that extends
    Propono's AtomServlet:
    
    - Answer 404 while webservices.enableAtomPub is off, matching the XML-RPC
      endpoint. Previously only the service document checked the setting.
    - Read Atom entry bodies (POST of Atom content, PUT to an entry URI) with
      Roller's standard XML parser configuration, cap them at 10 MB, and hand
      Propono a buffered copy. Unparseable entries get 400; oversized ones 413.
    
    RollerAtomHandler's entry-URI test moves into a static helper so the
    servlet and handler use the same rule.
---
 .../atomprotocol/RollerAtomHandler.java            |  13 +-
 .../atomprotocol/RollerAtomServlet.java            | 193 +++++++++++++++++
 app/src/main/webapp/WEB-INF/web.xml                |   2 +-
 .../atomprotocol/RollerAtomServletTest.java        | 240 +++++++++++++++++++++
 4 files changed, 446 insertions(+), 2 deletions(-)

diff --git 
a/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java
 
b/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java
index d1bdbb202..52c8336c8 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java
@@ -327,7 +327,18 @@ public class RollerAtomHandler implements AtomHandler {
      */
     @Override
     public boolean isEntryURI(AtomRequest areq) {
-        String[] pathInfo = StringUtils.split(areq.getPathInfo(),"/");
+        return isEntryPath(areq.getPathInfo());
+    }
+
+    /**
+     * True if the path info names an entry. Shared with RollerAtomServlet so
+     * both agree on which requests carry an entry body.
+     */
+    static boolean isEntryPath(String path) {
+        String[] pathInfo = StringUtils.split(path, "/");
+        if (pathInfo == null) {
+            return false;
+        }
         if (pathInfo.length > 2 && pathInfo[1].equals("entry")) {
             return true;
         }
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomServlet.java
 
b/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomServlet.java
new file mode 100644
index 000000000..b01cb7603
--- /dev/null
+++ 
b/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomServlet.java
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  The ASF licenses this file to You
+ * under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.weblogger.webservices.atomprotocol;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import javax.servlet.ReadListener;
+import javax.servlet.ServletException;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+import javax.servlet.http.HttpServletResponse;
+
+import com.rometools.propono.atom.server.AtomServlet;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
+import org.apache.roller.weblogger.util.SafeSAXBuilder;
+import org.jdom2.JDOMException;
+
+/**
+ * Roller's AtomPub endpoint. It answers only while
+ * <code>webservices.enableAtomPub</code> is on, and it reads each Atom entry
+ * body with Roller's shared XML parser settings before the Propono servlet
+ * handles the request.
+ */
+public class RollerAtomServlet extends AtomServlet {
+
+    private static final long serialVersionUID = 1L;
+
+    private static final Log LOG = LogFactory.getLog(RollerAtomServlet.class);
+
+    /** Largest Atom entry body accepted, in bytes. Media uploads are not 
affected. */
+    static final int MAX_ENTRY_BYTES = 10 * 1024 * 1024;
+
+    private static final String ATOM_CONTENT_TYPE = "application/atom+xml";
+
+    @Override
+    protected void service(HttpServletRequest req, HttpServletResponse res)
+            throws ServletException, IOException {
+
+        if 
(!WebloggerRuntimeConfig.getBooleanProperty("webservices.enableAtomPub")) {
+            LOG.debug("AtomPub service is disabled; rejecting request");
+            sendText(res, HttpServletResponse.SC_NOT_FOUND, "AtomPub service 
is disabled");
+            return;
+        }
+
+        if (!carriesEntry(req)) {
+            forward(req, res);
+            return;
+        }
+
+        byte[] body = readBody(req.getInputStream());
+        if (body == null) {
+            sendText(res, HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, 
"Entry is too large");
+            return;
+        }
+        try {
+            // Propono reads the entry as UTF-8 text, so check the same text.
+            new SafeSAXBuilder().build(new InputStreamReader(
+                    new ByteArrayInputStream(body), StandardCharsets.UTF_8));
+        } catch (JDOMException e) {
+            LOG.debug("Rejecting Atom entry that could not be parsed", e);
+            sendText(res, HttpServletResponse.SC_BAD_REQUEST, "Invalid Atom 
entry");
+            return;
+        }
+        forward(new BufferedBodyRequest(req, body), res);
+    }
+
+    /** Hands the request to the Propono servlet. */
+    protected void forward(HttpServletRequest req, HttpServletResponse res)
+            throws ServletException, IOException {
+        super.service(req, res);
+    }
+
+    /**
+     * True when Propono would parse the request body as an Atom entry: a POST
+     * of Atom content, or a PUT to an entry URI.
+     */
+    static boolean carriesEntry(HttpServletRequest req) {
+        String method = req.getMethod();
+        if ("POST".equalsIgnoreCase(method)) {
+            String contentType = req.getContentType();
+            return contentType != null && 
contentType.startsWith(ATOM_CONTENT_TYPE);
+        }
+        if ("PUT".equalsIgnoreCase(method)) {
+            return RollerAtomHandler.isEntryPath(req.getPathInfo());
+        }
+        return false;
+    }
+
+    /** Reads the whole body, or returns null when it exceeds the limit. */
+    private static byte[] readBody(InputStream in) throws IOException {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        byte[] buffer = new byte[8192];
+        int total = 0;
+        int read;
+        while ((read = in.read(buffer)) != -1) {
+            total += read;
+            if (total > MAX_ENTRY_BYTES) {
+                return null;
+            }
+            out.write(buffer, 0, read);
+        }
+        return out.toByteArray();
+    }
+
+    private static void sendText(HttpServletResponse res, int status, String 
message)
+            throws IOException {
+        res.setStatus(status);
+        res.setContentType("text/plain;charset=UTF-8");
+        res.getWriter().write(message);
+    }
+
+    /** A request whose body has already been read into memory. */
+    static final class BufferedBodyRequest extends HttpServletRequestWrapper {
+
+        private final byte[] body;
+
+        BufferedBodyRequest(HttpServletRequest request, byte[] body) {
+            super(request);
+            this.body = body;
+        }
+
+        @Override
+        public ServletInputStream getInputStream() {
+            final ByteArrayInputStream in = new ByteArrayInputStream(body);
+            return new ServletInputStream() {
+                @Override
+                public int read() {
+                    return in.read();
+                }
+
+                @Override
+                public int read(byte[] b, int off, int len) {
+                    return in.read(b, off, len);
+                }
+
+                @Override
+                public boolean isFinished() {
+                    return in.available() == 0;
+                }
+
+                @Override
+                public boolean isReady() {
+                    return true;
+                }
+
+                @Override
+                public void setReadListener(ReadListener listener) {
+                    throw new UnsupportedOperationException();
+                }
+            };
+        }
+
+        @Override
+        public BufferedReader getReader() {
+            return new BufferedReader(new InputStreamReader(
+                    new ByteArrayInputStream(body), StandardCharsets.UTF_8));
+        }
+
+        @Override
+        public int getContentLength() {
+            return body.length;
+        }
+
+        @Override
+        public long getContentLengthLong() {
+            return body.length;
+        }
+    }
+}
diff --git a/app/src/main/webapp/WEB-INF/web.xml 
b/app/src/main/webapp/WEB-INF/web.xml
index 8b1b550e3..70f239932 100644
--- a/app/src/main/webapp/WEB-INF/web.xml
+++ b/app/src/main/webapp/WEB-INF/web.xml
@@ -286,7 +286,7 @@
 
     <servlet>
         <servlet-name>AtomServlet</servlet-name>
-        
<servlet-class>com.rometools.propono.atom.server.AtomServlet</servlet-class>
+        
<servlet-class>org.apache.roller.weblogger.webservices.atomprotocol.RollerAtomServlet</servlet-class>
     </servlet>
 
     <servlet>
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomServletTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomServletTest.java
new file mode 100644
index 000000000..9fe5954a8
--- /dev/null
+++ 
b/app/src/test/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomServletTest.java
@@ -0,0 +1,240 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  The ASF licenses this file to You
+ * under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.weblogger.webservices.atomprotocol;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import javax.servlet.ReadListener;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class RollerAtomServletTest {
+
+    private static final String ENTRY =
+            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            + "<entry xmlns=\"http://www.w3.org/2005/Atom\";>"
+            + "<title>Hello</title>"
+            + "<id>urn:uuid:00000000-0000-0000-0000-000000000001</id>"
+            + "<updated>2026-01-01T00:00:00Z</updated>"
+            + "<content type=\"text\">Body</content>"
+            + "</entry>";
+
+    private static final String ENTRY_WITH_DOCTYPE =
+            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            + "<!DOCTYPE entry>\n"
+            + "<entry 
xmlns=\"http://www.w3.org/2005/Atom\";><title>Hello</title></entry>";
+
+    private MockedStatic<WebloggerRuntimeConfig> config;
+    private RecordingServlet servlet;
+    private HttpServletResponse response;
+    private StringWriter responseBody;
+
+    @BeforeEach
+    void setUp() throws IOException {
+        config = mockStatic(WebloggerRuntimeConfig.class);
+        config.when(() -> 
WebloggerRuntimeConfig.getBooleanProperty("webservices.enableAtomPub"))
+                .thenReturn(true);
+        servlet = new RecordingServlet();
+        response = mock(HttpServletResponse.class);
+        responseBody = new StringWriter();
+        when(response.getWriter()).thenReturn(new PrintWriter(responseBody));
+    }
+
+    @AfterEach
+    void tearDown() {
+        config.close();
+    }
+
+    @Test
+    void disabledServiceAnswersNotFoundWithoutReadingTheBody() throws 
Exception {
+        config.when(() -> 
WebloggerRuntimeConfig.getBooleanProperty("webservices.enableAtomPub"))
+                .thenReturn(false);
+        HttpServletRequest request = request("POST", "/blog/entries", 
"application/atom+xml", ENTRY);
+
+        servlet.service(request, response);
+
+        verify(response).setStatus(HttpServletResponse.SC_NOT_FOUND);
+        verify(request, never()).getInputStream();
+        assertNull(servlet.forwarded);
+    }
+
+    @Test
+    void disabledServiceAlsoRefusesReads() throws Exception {
+        config.when(() -> 
WebloggerRuntimeConfig.getBooleanProperty("webservices.enableAtomPub"))
+                .thenReturn(false);
+
+        servlet.service(request("GET", "/blog/entries", null, ""), response);
+
+        verify(response).setStatus(HttpServletResponse.SC_NOT_FOUND);
+        assertNull(servlet.forwarded);
+    }
+
+    @Test
+    void readsPassThroughUnchanged() throws Exception {
+        HttpServletRequest request = request("GET", "/blog/entries", null, "");
+
+        servlet.service(request, response);
+
+        assertSame(request, servlet.forwarded);
+    }
+
+    @Test
+    void wellFormedEntryIsForwardedWithTheSameBody() throws Exception {
+        servlet.service(request("POST", "/blog/entries", 
"application/atom+xml;type=entry", ENTRY), response);
+
+        assertNotNull(servlet.forwarded);
+        assertArrayEquals(ENTRY.getBytes(StandardCharsets.UTF_8), 
servlet.forwardedBody);
+    }
+
+    @Test
+    void postedEntryWithDoctypeIsRefused() throws Exception {
+        servlet.service(request("POST", "/blog/entries", 
"application/atom+xml", ENTRY_WITH_DOCTYPE), response);
+
+        verify(response).setStatus(HttpServletResponse.SC_BAD_REQUEST);
+        assertNull(servlet.forwarded);
+    }
+
+    @Test
+    void entryUpdateWithDoctypeIsRefusedWhateverItsContentType() throws 
Exception {
+        servlet.service(request("PUT", "/blog/entry/abc", "text/plain", 
ENTRY_WITH_DOCTYPE), response);
+
+        verify(response).setStatus(HttpServletResponse.SC_BAD_REQUEST);
+        assertNull(servlet.forwarded);
+    }
+
+    @Test
+    void mediaUploadIsForwardedWithoutParsing() throws Exception {
+        HttpServletRequest request = request("POST", "/blog/resources", 
"image/png", "not xml");
+
+        servlet.service(request, response);
+
+        assertSame(request, servlet.forwarded);
+        verify(request, never()).getInputStream();
+    }
+
+    @Test
+    void mediaUpdateIsForwardedWithoutParsing() throws Exception {
+        HttpServletRequest request = request("PUT", 
"/blog/resource/photo.png", "image/png", "not xml");
+
+        servlet.service(request, response);
+
+        assertSame(request, servlet.forwarded);
+        verify(request, never()).getInputStream();
+    }
+
+    @Test
+    void oversizedEntryIsRefused() throws Exception {
+        byte[] big = new byte[RollerAtomServlet.MAX_ENTRY_BYTES + 1];
+        Arrays.fill(big, (byte) ' ');
+        HttpServletRequest request = request("POST", "/blog/entries", 
"application/atom+xml", "");
+        when(request.getInputStream()).thenReturn(stream(big));
+
+        servlet.service(request, response);
+
+        
verify(response).setStatus(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
+        assertNull(servlet.forwarded);
+    }
+
+    @Test
+    void entryPathsMatchTheHandler() {
+        assertTrue(RollerAtomHandler.isEntryPath("/blog/entry/abc"));
+        
assertTrue(RollerAtomHandler.isEntryPath("/blog/resource/photo.png.media-link"));
+        assertFalse(RollerAtomHandler.isEntryPath("/blog/resource/photo.png"));
+        assertFalse(RollerAtomHandler.isEntryPath("/blog/entries"));
+        assertFalse(RollerAtomHandler.isEntryPath(null));
+    }
+
+    private static HttpServletRequest request(String method, String pathInfo,
+                                              String contentType, String body) 
throws IOException {
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        when(request.getMethod()).thenReturn(method);
+        when(request.getPathInfo()).thenReturn(pathInfo);
+        when(request.getContentType()).thenReturn(contentType);
+        
when(request.getInputStream()).thenReturn(stream(body.getBytes(StandardCharsets.UTF_8)));
+        return request;
+    }
+
+    private static ServletInputStream stream(byte[] bytes) {
+        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
+        return new ServletInputStream() {
+            @Override
+            public int read() {
+                return in.read();
+            }
+
+            @Override
+            public int read(byte[] b, int off, int len) {
+                return in.read(b, off, len);
+            }
+
+            @Override
+            public boolean isFinished() {
+                return in.available() == 0;
+            }
+
+            @Override
+            public boolean isReady() {
+                return true;
+            }
+
+            @Override
+            public void setReadListener(ReadListener listener) {
+                throw new UnsupportedOperationException();
+            }
+        };
+    }
+
+    /** Records what would have been handed to the Propono servlet. */
+    private static final class RecordingServlet extends RollerAtomServlet {
+        private static final long serialVersionUID = 1L;
+        HttpServletRequest forwarded;
+        byte[] forwardedBody;
+
+        @Override
+        protected void forward(HttpServletRequest req, HttpServletResponse 
res) throws IOException {
+            forwarded = req;
+            if (req instanceof BufferedBodyRequest) {
+                forwardedBody = req.getInputStream().readAllBytes();
+            }
+        }
+    }
+}

Reply via email to