SLING-6715 : SlingInfoServlet is overengineered
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1788844 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 551a29f..10f3764 100644
--- a/pom.xml
+++ b/pom.xml
@@ -86,7 +86,7 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.api</artifactId>
- <version>2.0.8</version>
+ <version>2.7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SessionInfoProvider.java b/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SessionInfoProvider.java
deleted file mode 100644
index bc5f154..0000000
--- a/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SessionInfoProvider.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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.servlets.get.impl.impl.info;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.jcr.Session;
-
-import org.apache.sling.api.SlingHttpServletRequest;
-
-public class SessionInfoProvider implements SlingInfoProvider {
-
- static final String PROVIDER_LABEL = "sessionInfo";
-
- public Map<String, String> getInfo(SlingHttpServletRequest request) {
- final Map<String, String> result = new HashMap<String, String>();
-
- final Session s = request.getResourceResolver().adaptTo(Session.class);
-
- result.put("workspace",s.getWorkspace().getName());
- result.put("userID",s.getUserID());
-
- if (request.getAuthType() != null) {
- result.put("authType", request.getAuthType());
- }
-
- return result;
- }
-
-}
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoProvider.java b/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoProvider.java
deleted file mode 100644
index a1086f5..0000000
--- a/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoProvider.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * 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.servlets.get.impl.impl.info;
-
-import java.util.Map;
-
-import org.apache.sling.api.SlingHttpServletRequest;
-
-public interface SlingInfoProvider {
-
- Map<String, String> getInfo(SlingHttpServletRequest request);
-
-}
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java
index e76b23d..6e6600b 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java
@@ -29,10 +29,11 @@
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.request.ResponseUtil;
+import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
-import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
/**
@@ -47,40 +48,49 @@
})
public class SlingInfoServlet extends SlingSafeMethodsServlet {
- /**
- *
- */
private static final String CACHE_CONTROL_HEADER = "Cache-Control";
private static final String CACHE_CONTROL_HEADER_VALUE =
"private, no-store, no-cache, max-age=0, must-revalidate";
- private Map<String, SlingInfoProvider> infoProviders = new HashMap<>();
+ static final String PROVIDER_LABEL = "sessionInfo";
+
+ private Map<String, String> getInfo(final SlingHttpServletRequest request) {
+ final Map<String, String> result = new HashMap<>();
+
+ final ResourceResolver resolver = request.getResourceResolver();
+
+ result.put("userID", resolver.getUserID());
+
+ if (request.getAuthType() != null) {
+ result.put("authType", request.getAuthType());
+ }
+
+ return result;
+ }
@Override
- protected void doGet(SlingHttpServletRequest request,
- SlingHttpServletResponse response) throws IOException {
+ protected void doGet(final SlingHttpServletRequest request,
+ final SlingHttpServletResponse response) throws IOException {
Map<String, String> data = null;
if (request.getRequestPathInfo().getSelectors().length > 0) {
- String label = request.getRequestPathInfo().getSelectors()[0];
- SlingInfoProvider uip = infoProviders.get(label);
- if (uip != null) {
- data = uip.getInfo(request);
+ final String label = request.getRequestPathInfo().getSelectors()[0];
+ if ( PROVIDER_LABEL.equals(label) ) {
+ data = this.getInfo(request);
}
}
if (data == null) {
- // listOptions(response);
response.sendError(HttpServletResponse.SC_NOT_FOUND,
"Unknown Info Request");
} else {
response.setHeader(CACHE_CONTROL_HEADER, CACHE_CONTROL_HEADER_VALUE);
- String extension = request.getRequestPathInfo().getExtension();
+ final String extension = request.getRequestPathInfo().getExtension();
if ("json".equals(extension)) {
renderJson(response, data);
} else if ("txt".equals(extension)) {
@@ -91,39 +101,9 @@
}
}
-/*
- private void listOptions(SlingHttpServletResponse response)
- throws IOException {
- // render data in JSON format
- response.setContentType("text/html");
- response.setCharacterEncoding("UTF-8");
-
- final PrintWriter out = response.getWriter();
-
- out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
- out.println("<html><head><title>Sling Info Providers</title></head>");
- out.println("<body><h1>Select from the following Providers</h1>");
-
- out.println("<table>");
- for (String label : infoProviders.keySet()) {
- out.print("<tr><td>");
- out.print("<a href='sling.");
- out.print(label);
- out.print(".html'>");
- out.print(label);
- out.print("</a>");
- out.println("</td></tr>");
- }
- out.println("</table>");
-
- out.println("</body>");
- out.flush();
-
- }
-*/
- private void renderJson(SlingHttpServletResponse response,
- Map<String, String> data) throws IOException {
+ private void renderJson(final SlingHttpServletResponse response,
+ final Map<String, String> data) throws IOException {
// render data in JSON format
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
@@ -133,7 +113,7 @@
try {
w.object();
- for (Map.Entry<String, String> e : data.entrySet()) {
+ for (final Map.Entry<String, String> e : data.entrySet()) {
w.key(e.getKey());
w.value(e.getValue());
}
@@ -147,8 +127,8 @@
}
}
- private void renderHtml(SlingHttpServletResponse response,
- Map<String, String> data) throws IOException {
+ private void renderHtml(final SlingHttpServletResponse response,
+ final Map<String, String> data) throws IOException {
// render data in JSON format
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
@@ -156,15 +136,15 @@
final PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
- out.println("<html><head><title>Sling Info</title></head>");
- out.println("<body><h1>Sling Info</h1>");
+ out.println("<html><head><title>Apche Sling Info</title></head>");
+ out.println("<body><h1>Apache Sling Info</h1>");
out.println("<table>");
- for (Map.Entry<String, String> e : data.entrySet()) {
+ for (final Map.Entry<String, String> e : data.entrySet()) {
out.print("<tr><td>");
- out.print(e.getKey());
+ out.print(ResponseUtil.escapeXml(e.getKey()));
out.print("</td><td>");
- out.print(e.getValue());
+ out.print(ResponseUtil.escapeXml(e.getValue()));
out.println("</td></tr>");
}
out.println("</table>");
@@ -173,8 +153,8 @@
out.flush();
}
- private void renderPlainText(SlingHttpServletResponse response,
- Map<String, String> data) throws IOException {
+ private void renderPlainText(final SlingHttpServletResponse response,
+ final Map<String, String> data) throws IOException {
// render data in JSON format
response.setContentType("text/plain");
@@ -182,7 +162,7 @@
final PrintWriter out = response.getWriter();
- for (Map.Entry<String, String> e : data.entrySet()) {
+ for (final Map.Entry<String, String> e : data.entrySet()) {
out.print(e.getKey());
out.print(": ");
out.println(e.getValue());
@@ -190,16 +170,4 @@
out.flush();
}
-
- // --------- SCR integration -----------------------------------------------
-
- @Activate
- protected void activate() {
- try {
- infoProviders.put(SessionInfoProvider.PROVIDER_LABEL,
- new SessionInfoProvider());
- } catch ( final Throwable t) {
- // if no JCR API is available the above might throw an exception
- }
- }
}
\ No newline at end of file