incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r598649 - in /incubator/sling/trunk/microsling/microsling-core/src: main/java/org/apache/sling/microsling/slingservlets/ main/webapp/microjax/ main/webapp/microjax/test/ test/java/org/apache/sling/microsling/integration/microjax/
Date Tue, 27 Nov 2007 14:12:30 GMT
Author: bdelacretaz
Date: Tue Nov 27 06:12:27 2007
New Revision: 598649

URL: http://svn.apache.org/viewvc?rev=598649&view=rev
Log:
SLING-92, microjax:sessionInfo added, with tests

Added:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxGetServlet.java
  (with props)
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxSessionInfoTest.java
  (with props)
Modified:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
    incubator/sling/trunk/microsling/microsling-core/src/main/webapp/microjax/microjax.js
    incubator/sling/trunk/microsling/microsling-core/src/main/webapp/microjax/test/microjax-test.html

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java?rev=598649&r1=598648&r2=598649&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
Tue Nov 27 06:12:27 2007
@@ -49,12 +49,16 @@
     private static final long serialVersionUID = -2259461041692895761L;
     private Map<String, Servlet> renderingServlets = new HashMap <String, Servlet>();
     private Servlet postServlet;
+    private Servlet microjaxGetServlet;
 
     @Override
     public void init() throws ServletException {
         postServlet = new MicrojaxPostServlet();
         postServlet.init(new MicroslingServletConfig("Microjax POST servlet",getServletContext()));
         
+        microjaxGetServlet = new MicrojaxGetServlet();
+        microjaxGetServlet.init(new MicroslingServletConfig("Microjax GET servlet",getServletContext()));
+        
         String contentType = null;
         final String ctSuffix = "; charset=UTF-8";
         
@@ -78,7 +82,11 @@
         if (Resource.RESOURCE_TYPE_NON_EXISTING.equals(r.getResourceType())) {
 
             String path = r.getURI();
-            if (path.startsWith("/WEB-INF") || path.startsWith("/META-INF")) {
+            if (path.startsWith(MicrojaxGetServlet.URI_PREFIX)) {
+                microjaxGetServlet.service(req, resp);
+                return;
+                
+            } else if (path.startsWith("/WEB-INF") || path.startsWith("/META-INF")) {
                 throw new HttpStatusCodeException(HttpServletResponse.SC_FORBIDDEN,
                         "Access to " + path + " denied");
             }

Added: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxGetServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxGetServlet.java?rev=598649&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxGetServlet.java
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxGetServlet.java
Tue Nov 27 06:12:27 2007
@@ -0,0 +1,105 @@
+/*
+ * 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.microsling.slingservlets;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.HttpStatusCodeException;
+import org.apache.sling.api.SlingException;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.io.JSONWriter;
+
+/** Servlet that provides some non-repository resources for microjax 
+ *  using GET requests, see SLING-92 */
+public class MicrojaxGetServlet extends SlingSafeMethodsServlet {
+    private static final long serialVersionUID = -8406117284723670902L;
+    
+    /** Handle GET requests starting with this prefix */
+    public static final String URI_PREFIX = "/microjax:";
+
+    @Override
+    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException,
+            IOException {
+        
+        Map <String, Object> data = null;
+        
+        if(request.getPathInfo().equals(URI_PREFIX + "sessionInfo.json")) {
+            try {
+                data = getSessionInfo(request);
+            } catch(RepositoryException re) {
+                throw new ServletException("RepositoryException in getSessionInfo(): " +
re,re);
+            }
+        }
+        
+        if(data== null) {
+            throw new HttpStatusCodeException(HttpServletResponse.SC_NOT_FOUND,request.getPathInfo());
+        } else {
+            // render data in JSON format
+            response.setContentType(getServletContext().getMimeType("dummy.json"));
+            final Writer out = new OutputStreamWriter(response.getOutputStream());
+            final JSONWriter w = new JSONWriter(out);
+            try {
+                w.object();
+                for(Map.Entry<String, Object> e : data.entrySet()) {
+                    w.key(e.getKey());
+                    w.value(e.getValue());
+                }
+                w.endObject();
+                
+            } catch (JSONException jse) {
+                out.write(jse.toString());
+                
+            } finally {
+                out.flush();
+            }
+        }
+    }
+    
+    protected Map<String, Object> getSessionInfo(SlingHttpServletRequest request) 
+    throws RepositoryException, HttpStatusCodeException, SlingException {
+        final Map<String, Object> result = new HashMap<String, Object>();
+        
+        // TODO not very convenient way to get a Session...
+        final Resource root = request.getResourceResolver().getResource("/");
+        final Node rootNode = (Node)root.getRawData();
+        if(rootNode == null) {
+            throw new HttpStatusCodeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Root
Node not found");
+        }
+        final Session s = rootNode.getSession();
+        
+        result.put("workspace",s.getWorkspace().getName());
+        result.put("userID",s.getUserID());
+        
+        return result;
+    }
+    
+    
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxGetServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxGetServlet.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/webapp/microjax/microjax.js
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/webapp/microjax/microjax.js?rev=598649&r1=598648&r2=598649&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/webapp/microjax/microjax.js
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/webapp/microjax/microjax.js
Tue Nov 27 06:12:27 2007
@@ -155,32 +155,31 @@
 		// but in tests IE6 tends to cache too much
 		var passThroughCacheParam = "?clock=" + new Date().getTime();
 	    var res=microjax.httpGet(path + passThroughCacheParam + (maxlevels?"&maxlevels="+maxlevels:""));
+	    return microjax.evalString(res);
+	}
 	
-	    // TODO clarify how we pass objects/arrays via JSON
-	    if(res.indexOf('[')==0) {
-		    eval("obj="+res);
-	    } else if(res.indexOf('{')==0) {
-	        eval("obj="+res);
+	/** eval str, accepting various object delimiters */
+	microjax.evalString = function(str) {
+		var obj = null;
+	    if(str.indexOf('[')==0) {
+		    eval("obj="+str);
+	    } else if(str.indexOf('{')==0) {
+	        eval("obj="+str);
 	    } else {
-		    eval("obj={"+res+"}");
+		    eval("obj={"+str+"}");
 		}
-	    return (obj);
-	    }
-	
+		return obj;
+	}
+	 
 	/** Get "session info" from repository. Mainly answers the question: "Who am I"
 	 *  and "Which workspace am I logged into.
 	 * @return An Object tree containing the session information
 	 * @type Object
 	 */
 	microjax.getSessionInfo = function() {
-	    /** TODO: automatically find the microjax URI without hardcoding, possibly based on
-	    * the assumption that the microjax servlet is deployed relative to the servlet.
-	    */
 	    var res=microjax.httpGet(microjax.baseurl+"/microjax:sessionInfo.json");
-	    var obj;
-	    eval("obj={"+res+"}");
-	    return (obj);
-	    }
+	    return microjax.evalString(res);
+	}
 	
 	/** Replace extension in a path */
 	microjax._replaceExtension = function(path,newExtension) {

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/webapp/microjax/test/microjax-test.html
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/webapp/microjax/test/microjax-test.html?rev=598649&r1=598648&r2=598649&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/webapp/microjax/test/microjax-test.html
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/webapp/microjax/test/microjax-test.html
Tue Nov 27 06:12:27 2007
@@ -95,12 +95,11 @@
 	  }
 	  
 	  function testGetSessionInfo() {
-        /* TODO not implemented yet
 	    var session = microjax.getSessionInfo();
-	    assertEquals("session.userId is a string","string",typeof(session.userId));
+	    assertEquals("session.userID is a string","string",typeof(session.userID));
+        assertEquals("session.userID == 'admin'","admin",session.userID);
 	    assertEquals("session.workspace is a string","string",typeof(session.workspace));
 	    assert("session.workspace contains 'default'",session.workspace.indexOf('default') >=
0);
-        */
 	  }
 	  
 	  function runTests() {

Added: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxSessionInfoTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxSessionInfoTest.java?rev=598649&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxSessionInfoTest.java
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxSessionInfoTest.java
Tue Nov 27 06:12:27 2007
@@ -0,0 +1,37 @@
+/*
+ * 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.microsling.integration.microjax;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.microsling.integration.MicroslingHttpTestBase;
+
+/** Test the microjax:sessionInfo resource */
+
+public class MicrojaxSessionInfoTest extends MicroslingHttpTestBase {
+    
+    public void testSessionInfo() throws IOException {
+        final String content = getContent(HTTP_BASE_URL + "/microjax:sessionInfo.json", CONTENT_TYPE_JSON);
+        assertJavascript("admin.default", content, "out.println(data.userID + '.' + data.workspace)");
+    }
+    
+    public void testNonexistentMicrojaxUrl() throws IOException {
+        assertHttpStatus(HTTP_BASE_URL + "/microjax:nothing", HttpServletResponse.SC_NOT_FOUND);
+    }
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxSessionInfoTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxSessionInfoTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message