You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2022/09/09 08:33:32 UTC
[sling-org-apache-sling-launchpad-test-services] branch master updated: SLING-11569 test servlets to use RequestDispatcher to include content from other servlets
This is an automated email from the ASF dual-hosted git repository.
sseifert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-launchpad-test-services.git
The following commit(s) were added to refs/heads/master by this push:
new 0455b28 SLING-11569 test servlets to use RequestDispatcher to include content from other servlets
0455b28 is described below
commit 0455b2865ddf07a678b0fa51ff0a736ec3e892ef
Author: Stefan Seifert <st...@users.noreply.github.com>
AuthorDate: Thu Sep 8 13:11:35 2022 +0200
SLING-11569 test servlets to use RequestDispatcher to include content from other servlets
---
.../requestdispatcher/OriginalResponseServlet.java | 54 ++++++++++++++++
.../RequestDispatcherIncludeBufferedServlet.java | 72 ++++++++++++++++++++++
.../RequestDispatcherIncludeDirectServlet.java | 61 ++++++++++++++++++
3 files changed, 187 insertions(+)
diff --git a/src/main/java/org/apache/sling/launchpad/testservices/servlets/requestdispatcher/OriginalResponseServlet.java b/src/main/java/org/apache/sling/launchpad/testservices/servlets/requestdispatcher/OriginalResponseServlet.java
new file mode 100644
index 0000000..96e0de0
--- /dev/null
+++ b/src/main/java/org/apache/sling/launchpad/testservices/servlets/requestdispatcher/OriginalResponseServlet.java
@@ -0,0 +1,54 @@
+/*
+ * 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.launchpad.testservices.servlets.requestdispatcher;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.jetbrains.annotations.NotNull;
+import org.osgi.service.component.annotations.Component;
+
+/**
+ * Simple servlet delivering a static string - used for testing request dispatcher include.
+ */
+@Component(
+ immediate=true,
+ service = javax.servlet.Servlet.class,
+ property = {
+ "service.description:String=Paths Test Servlet",
+ "service.vendor:String=The Apache Software Foundation",
+ "sling.servlet.paths:String=" + OriginalResponseServlet.PATH
+ })
+@SuppressWarnings("serial")
+public class OriginalResponseServlet extends SlingSafeMethodsServlet {
+
+ static final String PATH = "/testing/requestDispatcher/originalResponse";
+
+ @Override
+ protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response)
+ throws ServletException, IOException {
+ response.setContentType("text/plain;charset=UTF-8");
+ response.getWriter().write("OriginalResponse");
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/launchpad/testservices/servlets/requestdispatcher/RequestDispatcherIncludeBufferedServlet.java b/src/main/java/org/apache/sling/launchpad/testservices/servlets/requestdispatcher/RequestDispatcherIncludeBufferedServlet.java
new file mode 100644
index 0000000..72912e7
--- /dev/null
+++ b/src/main/java/org/apache/sling/launchpad/testservices/servlets/requestdispatcher/RequestDispatcherIncludeBufferedServlet.java
@@ -0,0 +1,72 @@
+/*
+ * 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.launchpad.testservices.servlets.requestdispatcher;
+
+import java.io.IOException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.request.builder.Builders;
+import org.apache.sling.api.request.builder.SlingHttpServletResponseResult;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.jetbrains.annotations.NotNull;
+import org.osgi.service.component.annotations.Component;
+
+/**
+ * Servlet using RequestDispatcher.include to include output of another servlet in it's response.
+ * This time the inclusion is not done directly on the response, but the include is
+ * done into a "synthetic response" created via Sling Builders and the output than written to the real response.
+ */
+@Component(
+ immediate=true,
+ service = javax.servlet.Servlet.class,
+ property = {
+ "service.description:String=Paths Test Servlet",
+ "service.vendor:String=The Apache Software Foundation",
+ "sling.servlet.paths:String=" + RequestDispatcherIncludeBufferedServlet.PATH
+ })
+@SuppressWarnings("serial")
+public class RequestDispatcherIncludeBufferedServlet extends SlingSafeMethodsServlet {
+
+ static final String PATH = "/testing/requestDispatcher/includeBuffered";
+
+ @Override
+ protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response)
+ throws ServletException, IOException {
+ response.setContentType("text/plain;charset=UTF-8");
+
+ response.getWriter().write("includeBuffered(");
+
+ // build "synthetic response"
+ SlingHttpServletResponseResult syntheticResponse = Builders.newResponseBuilder().build();
+
+ // write output of other servlet to this response to allow to post-process or validate it
+ RequestDispatcher requestDispatcher = request.getRequestDispatcher(OriginalResponseServlet.PATH);
+ requestDispatcher.include(request, syntheticResponse);
+
+ // write generated output to response
+ response.getWriter().write(syntheticResponse.getOutputAsString());
+
+ response.getWriter().write(")");
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/launchpad/testservices/servlets/requestdispatcher/RequestDispatcherIncludeDirectServlet.java b/src/main/java/org/apache/sling/launchpad/testservices/servlets/requestdispatcher/RequestDispatcherIncludeDirectServlet.java
new file mode 100644
index 0000000..8261d63
--- /dev/null
+++ b/src/main/java/org/apache/sling/launchpad/testservices/servlets/requestdispatcher/RequestDispatcherIncludeDirectServlet.java
@@ -0,0 +1,61 @@
+/*
+ * 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.launchpad.testservices.servlets.requestdispatcher;
+
+import java.io.IOException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.jetbrains.annotations.NotNull;
+import org.osgi.service.component.annotations.Component;
+
+/**
+ * Servlet using RequestDispatcher.include to include output of another servlet in it's response.
+ */
+@Component(
+ immediate=true,
+ service = javax.servlet.Servlet.class,
+ property = {
+ "service.description:String=Paths Test Servlet",
+ "service.vendor:String=The Apache Software Foundation",
+ "sling.servlet.paths:String=" + RequestDispatcherIncludeDirectServlet.PATH
+ })
+@SuppressWarnings("serial")
+public class RequestDispatcherIncludeDirectServlet extends SlingSafeMethodsServlet {
+
+ static final String PATH = "/testing/requestDispatcher/includeDirect";
+
+ @Override
+ protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response)
+ throws ServletException, IOException {
+ response.setContentType("text/plain;charset=UTF-8");
+
+ response.getWriter().write("includeDirect(");
+
+ RequestDispatcher requestDispatcher = request.getRequestDispatcher(OriginalResponseServlet.PATH);
+ requestDispatcher.include(request, response);
+
+ response.getWriter().write(")");
+ }
+
+}