Copilot commented on code in PR #106:
URL:
https://github.com/apache/sling-org-apache-sling-engine/pull/106#discussion_r4537312788
##########
src/main/java/org/apache/sling/engine/impl/DefaultErrorHandler.java:
##########
@@ -48,14 +54,31 @@ public class DefaultErrorHandler implements
JakartaErrorHandler {
/** default log */
private final Logger log = LoggerFactory.getLogger(getClass());
+ // TODO - move this to API
+ private static final String HTML_CONTENT_TYPE = "text/html";
+
+ private static final String ALL_CONTENT_TYPES = "*/*";
+
+ // TODO - move this to API
+ static final String NON_HTML = "nonhtml";
+
/** Use this if not null, and if that fails output a report about that
failure */
private volatile JakartaErrorHandler delegate;
+ /** Use this if not null for non html responses */
+ private volatile JakartaErrorHandler nonHtmlDelegate;
+
private volatile JakartaErrorHandler errorHandler;
private volatile JakartaErrorHandler jakartaErrorHandler;
private volatile ServiceReference<?> errorHandlerRef;
private volatile ServiceReference<?> jakartaErrorHandlerRef;
+ private final Map<ServiceReference<JakartaErrorHandler>,
JakartaErrorHandler> jakartaErrorHandlers =
+ new TreeMap<>(Comparator.reverseOrder());
Review Comment:
`ServiceReference` ordering is already defined by OSGi `compareTo()` to
reflect service ranking and service id tie-breaking. Using
`Comparator.reverseOrder()` here risks inverting the intended priority (e.g.,
selecting the lowest-ranked or newest service first), which would break the
'highest ranked wins' and 'older wins on tie' behavior described/tested. Prefer
relying on the natural `ServiceReference` ordering (e.g., `new TreeMap<>()`) or
use an explicit comparator that matches the OSGi ordering rules (ranking desc,
service id asc).
##########
src/main/java/org/apache/sling/engine/impl/DefaultNonHtmlErrorHandler.java:
##########
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.sling.engine.impl;
+
+import java.io.IOException;
+
+import jakarta.servlet.http.HttpServletResponse;
+import org.apache.sling.api.SlingJakartaHttpServletRequest;
+import org.apache.sling.api.SlingJakartaHttpServletResponse;
+import org.apache.sling.api.servlets.JakartaErrorHandler;
+import org.osgi.service.component.annotations.Component;
+
+@Component(
+ service = JakartaErrorHandler.class,
+ property = {SlingRequestProcessorImpl.ERROR_HANDLER_NAME + "=" +
DefaultErrorHandler.NON_HTML})
+public class DefaultNonHtmlErrorHandler implements JakartaErrorHandler {
+
Review Comment:
This component (and `DefaultErrorHandler`) depend on
`SlingRequestProcessorImpl.ERROR_HANDLER_NAME`, which couples error handlers to
the request processor implementation class. To reduce cross-class coupling and
make future migration to the Sling API easier, consider introducing an internal
constants holder (e.g., `ErrorHandlerConstants` in `impl`), and reference that
from both the processor and handlers.
##########
src/main/java/org/apache/sling/engine/impl/DefaultErrorHandler.java:
##########
@@ -263,4 +309,56 @@ private void sendError(
// close the response (SLING-2724)
pw.close();
}
+
+ private boolean matchesName(final ServiceReference<JakartaErrorHandler>
ref, final String name) {
+ final String[] names = Converters.standardConverter()
+
.convert(ref.getProperty(SlingRequestProcessorImpl.ERROR_HANDLER_NAME))
+ .to(String[].class);
+
+ for (final String n : names) {
+ if (name.equals(n)) {
+ return true;
+ }
+ }
+ return false;
+ }
Review Comment:
`Converters...to(String[].class)` can yield `null` if the property is
missing or cannot be converted, which would cause a `NullPointerException` in
the enhanced for-loop. Please make this null-safe (e.g., default to an empty
array / early return false when `names == null`).
##########
src/main/java/org/apache/sling/engine/impl/DefaultErrorHandler.java:
##########
@@ -133,6 +156,29 @@ private void delegateFailed(
sendError(originalStatus, originalMessage, null, request, response);
}
+ protected JakartaErrorHandler getErrorHandler(final
SlingJakartaHttpServletRequest request) {
+ final JakartaErrorHandler nonHtml = this.nonHtmlDelegate;
+ if (this.enableAcceptHeaderCheck && nonHtml != null &&
!acceptsHtml(request)) {
+ return nonHtml;
+ }
+ return this.delegate;
+ }
+
+ /**
+ * A request accepts html if it explicitly accepts {@code text/html} or
{@code *}{@code /*}.
+ * A missing accept header and partial wildcards like {@code text/*} are
not considered html.
+ * Only the accept header is evaluated: request parameters are not read,
as parameter
+ * parsing might itself be the cause of the error being handled.
+ */
+ private static boolean acceptsHtml(final SlingJakartaHttpServletRequest
request) {
+ final String accept =
request.getHeader(JakartaMediaRangeList.HEADER_ACCEPT);
+ if (accept == null || accept.trim().isEmpty()) {
+ return false;
+ }
+ // TODO - do we want to treat */* as html?
Review Comment:
The TODO contradicts the documented behavior in the PR description (and the
current implementation), which treats `*/*` as HTML and routes to the default
handler. Please remove or reword this comment to reflect the final, intended
behavior so it doesn't create ambiguity for future maintenance.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]