joerghoh commented on code in PR #106:
URL:
https://github.com/apache/sling-org-apache-sling-engine/pull/106#discussion_r4542237569
##########
src/main/java/org/apache/sling/engine/impl/DefaultErrorHandler.java:
##########
@@ -133,6 +150,27 @@ 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 is treated as HTML if its Accept header matches {@code
text/html},
+ * including through {@code text/*}, and does not contain {@code *}{@code
/*}.
+ * A missing or blank Accept header, or any header containing {@code
*}{@code /*},
+ * is treated as non-HTML, even if it also explicitly accepts {@code
text/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 JakartaMediaRangeList acceptList =
JakartaMediaRangeList.fromRequest(request);
+ return !acceptList.matchesAll() &&
acceptList.prefer(JakartaMediaRangeList.CONTENT_TYPE_HTML) != null;
Review Comment:
is that logic correct?
##########
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 = {JakartaErrorHandler.PROPERTY_NAME + "=" +
JakartaErrorHandler.NAME_NONHTML})
+public class DefaultNonHtmlErrorHandler implements JakartaErrorHandler {
+
+ @Override
+ public void handleError(
+ int status,
+ String message,
+ SlingJakartaHttpServletRequest request,
+ SlingJakartaHttpServletResponse response)
+ throws IOException {
+ response.setStatus(status);
+ response.setContentLength(0);
Review Comment:
shouldn't we do a ``response.resetBuffer()`` as well?
--
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]