You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/02/26 11:53:48 UTC

svn commit: r631174 - in /incubator/sling/trunk/launchpad/launchpad-servlets/src/main: java/org/apache/sling/launchpad/renderers/ java/org/apache/sling/launchpad/servlets/ java/org/apache/sling/ujax/ java/org/apache/sling/ujax/info/ java/org/apache/sli...

Author: fmeschbe
Date: Tue Feb 26 02:53:45 2008
New Revision: 631174

URL: http://svn.apache.org/viewvc?rev=631174&view=rev
Log:
SLING-268 removing serlvets and renderes moved to new sling/servlets-default
project, define scr-tags for UjaxPostServlet and make the ujax info provider
a first class servlet.
This is an intermediary checkin before moving this whole project to
sling/ujax

Added:
    incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/
    incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/SessionInfoProvider.java
    incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/UjaxInfoProvider.java
    incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/UjaxInfoServlet.java
Removed:
    incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/launchpad/renderers/
    incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/launchpad/servlets/
    incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/usling/
    incubator/sling/trunk/launchpad/launchpad-servlets/src/main/resources/OSGI-INF/
Modified:
    incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/UjaxPostServlet.java

Modified: incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/UjaxPostServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/UjaxPostServlet.java?rev=631174&r1=631173&r2=631174&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/UjaxPostServlet.java (original)
+++ incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/UjaxPostServlet.java Tue Feb 26 02:53:45 2008
@@ -30,6 +30,29 @@
 
 /**
  * POST servlet that implements the ujax "protocol"
+ * 
+ * @scr.service
+ *  interface="javax.servlet.Servlet"
+ *  
+ * @scr.component 
+ *  immediate="true" 
+ *  metatype="false"
+ *  
+ * @scr.property 
+ *  name="service.description"
+ *  value="ujax Post Servlet"
+ *  
+ * @scr.property 
+ *  name="service.vendor" 
+ *  value="The Apache Software Foundation"
+ *
+ * Use this as the default servlet for POST requests for Sling 
+ * @scr.property 
+ *  name="sling.servlet.resourceTypes" 
+ *  value="sling/servlet/default"
+ * @scr.property
+ *  name="sling.servlet.methods"
+ *  value="POST"
  */
 public class UjaxPostServlet extends SlingAllMethodsServlet {
 

Added: incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/SessionInfoProvider.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/SessionInfoProvider.java?rev=631174&view=auto
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/SessionInfoProvider.java (added)
+++ incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/SessionInfoProvider.java Tue Feb 26 02:53:45 2008
@@ -0,0 +1,45 @@
+/*
+ * 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.ujax.info;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.sling.api.SlingException;
+import org.apache.sling.api.SlingHttpServletRequest;
+
+public class SessionInfoProvider implements UjaxInfoProvider {
+
+    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());
+
+        return result;
+    }
+
+}

Added: incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/UjaxInfoProvider.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/UjaxInfoProvider.java?rev=631174&view=auto
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/UjaxInfoProvider.java (added)
+++ incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/UjaxInfoProvider.java Tue Feb 26 02:53:45 2008
@@ -0,0 +1,29 @@
+/*
+ * 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.ujax.info;
+
+import java.util.Map;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+
+public interface UjaxInfoProvider {
+
+    Map<String, String> getInfo(SlingHttpServletRequest request);
+    
+}

Added: incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/UjaxInfoServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/UjaxInfoServlet.java?rev=631174&view=auto
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/UjaxInfoServlet.java (added)
+++ incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/ujax/info/UjaxInfoServlet.java Tue Feb 26 02:53:45 2008
@@ -0,0 +1,189 @@
+/*
+ * 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.ujax.info;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+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.ComponentContext;
+
+/**
+ * The <code>UjaxInfoServlet</code> TODO
+ * 
+ * @scr.service interface="javax.servlet.Servlet"
+ * @scr.component immediate="true" metatype="false"
+ * @scr.property name="service.description" value="ujax Info Servlet"
+ * @scr.property name="service.vendor" value="The Apache Software Foundation"
+ * @scr.property name="sling.servlet.paths" value="/ujax"
+ */
+public class UjaxInfoServlet extends SlingSafeMethodsServlet {
+
+    private Map<String, UjaxInfoProvider> infoProviders = new HashMap<String, UjaxInfoProvider>();
+
+    @Override
+    protected void doGet(SlingHttpServletRequest request,
+            SlingHttpServletResponse response) throws IOException {
+
+        Map<String, String> data = null;
+
+        if (request.getRequestPathInfo().getSelectors().length > 0) {
+            String label = request.getRequestPathInfo().getSelectors()[0];
+            UjaxInfoProvider uip = infoProviders.get(label);
+            if (uip != null) {
+                data = uip.getInfo(request);
+            }
+        }
+
+        if (data == null) {
+
+            // listOptions(response);
+            response.sendError(HttpServletResponse.SC_NOT_FOUND,
+                "Unknown Info Request");
+
+        } else {
+
+            String extension = request.getRequestPathInfo().getExtension();
+            if ("json".equals(extension)) {
+                renderJson(response, data);
+            } else if ("txt".equals(extension)) {
+                renderPlainText(response, data);
+            } else { // default to html
+                renderHtml(response, data);
+            }
+
+        }
+    }
+
+    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>Ujax 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='ujax.");
+            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 {
+        // render data in JSON format
+        response.setContentType("application/json");
+        response.setCharacterEncoding("UTF-8");
+
+        final Writer out = response.getWriter();
+        final JSONWriter w = new JSONWriter(out);
+
+        try {
+            w.object();
+            for (Map.Entry<String, String> e : data.entrySet()) {
+                w.key(e.getKey());
+                w.value(e.getValue());
+            }
+            w.endObject();
+
+        } catch (JSONException jse) {
+            out.write(jse.toString());
+
+        } finally {
+            out.flush();
+        }
+    }
+
+    private void renderHtml(SlingHttpServletResponse response,
+            Map<String, String> data) 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>Ujax Info</title></head>");
+        out.println("<body><h1>Ujax Info</h1>");
+
+        out.println("<table>");
+        for (Map.Entry<String, String> e : data.entrySet()) {
+            out.print("<tr><td>");
+            out.print(e.getKey());
+            out.print("</td><td>");
+            out.print(e.getValue());
+            out.println("</td></tr>");
+        }
+        out.println("</table>");
+
+        out.println("</body>");
+        out.flush();
+    }
+
+    private void renderPlainText(SlingHttpServletResponse response,
+            Map<String, String> data) throws IOException {
+
+        // render data in JSON format
+        response.setContentType("text/plain");
+        response.setCharacterEncoding("UTF-8");
+
+        final PrintWriter out = response.getWriter();
+
+        for (Map.Entry<String, String> e : data.entrySet()) {
+            out.print(e.getKey());
+            out.print(": ");
+            out.println(e.getValue());
+        }
+
+        out.flush();
+    }
+
+    // --------- SCR integration -----------------------------------------------
+
+    protected void activate(ComponentContext context) {
+        infoProviders.put(SessionInfoProvider.PROVIDER_LABEL,
+            new SessionInfoProvider());
+    }
+}
\ No newline at end of file