Make slingshot sample work with JR2 again

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1704900 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/slingshot/src/main/java/org/apache/sling/sample/slingshot/comments/impl/CommentPostServlet.java b/slingshot/src/main/java/org/apache/sling/sample/slingshot/comments/impl/CommentPostServlet.java
index d540a97..6cd65bc 100644
--- a/slingshot/src/main/java/org/apache/sling/sample/slingshot/comments/impl/CommentPostServlet.java
+++ b/slingshot/src/main/java/org/apache/sling/sample/slingshot/comments/impl/CommentPostServlet.java
@@ -17,6 +17,8 @@
 package org.apache.sling.sample.slingshot.comments.impl;
 
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.servlet.ServletException;
 
@@ -33,6 +35,7 @@
 import org.apache.sling.sample.slingshot.comments.Comment;
 import org.apache.sling.sample.slingshot.comments.CommentsService;
 import org.apache.sling.sample.slingshot.comments.CommentsUtil;
+import org.apache.sling.sample.slingshot.impl.InternalConstants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,7 +67,12 @@
         // save comment
         ResourceResolver resolver = null;
         try {
-            resolver = factory.getServiceResourceResolver(null);
+            // TODO - switch to service user with Oak
+            final Map<String, Object> authInfo = new HashMap<String, Object>();
+            authInfo.put(ResourceResolverFactory.USER, InternalConstants.SERVICE_USER_NAME);
+            authInfo.put(ResourceResolverFactory.PASSWORD, InternalConstants.SERVICE_USER_NAME.toCharArray());
+            resolver = factory.getResourceResolver(authInfo);
+//          resolver = factory.getServiceResourceResolver(null);
 
             final Resource reqResource = resolver.getResource(request.getResource().getPath());
 
@@ -83,9 +91,9 @@
         }
 
         // send redirect at the end
-        final String path = request.getResource().getParent().getPath();
+        final String path = request.getResource().getPath();
 
-        response.sendRedirect(request.getContextPath() + path + ".html");
+        response.sendRedirect(resolver.map(request.getContextPath() + path + ".html"));
     }
 
 }
diff --git a/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/SetupService.java b/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/SetupService.java
index 92a6d16..27c90a6 100644
--- a/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/SetupService.java
+++ b/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/SetupService.java
@@ -92,20 +92,14 @@
 
         // create default slingshot users
         for(final String principalId : USERS) {
+            // user home
             final String resourcePath = SlingshotConstants.APP_ROOT_PATH + "/users/" + principalId;
-
-            final Map<String, String> privileges = new HashMap<String, String>();
-            privileges.put(Privilege.JCR_ALL, "granted");
-
             modifyAce(session, resourcePath, principalId, Privilege.JCR_ALL, true);
 
             // ugc path
             final String ugcPath = resourcePath + "/ugc";
-            privileges.clear();
-            privileges.put(Privilege.JCR_ALL, "granted");
-
-            modifyAce(session, ugcPath, InternalConstants.SERVICE_USER_NAME,
-                    Privilege.JCR_ALL, true);
+            modifyAce(session, ugcPath,
+                    InternalConstants.SERVICE_USER_NAME, Privilege.JCR_ALL, true);
         }
     }
 
diff --git a/slingshot/src/main/java/org/apache/sling/sample/slingshot/ratings/impl/RatingPostServlet.java b/slingshot/src/main/java/org/apache/sling/sample/slingshot/ratings/impl/RatingPostServlet.java
index 7c1e200..a5768c8 100644
--- a/slingshot/src/main/java/org/apache/sling/sample/slingshot/ratings/impl/RatingPostServlet.java
+++ b/slingshot/src/main/java/org/apache/sling/sample/slingshot/ratings/impl/RatingPostServlet.java
@@ -18,6 +18,8 @@
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.servlet.ServletException;
 
@@ -26,10 +28,12 @@
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.api.servlets.SlingAllMethodsServlet;
 import org.apache.sling.sample.slingshot.SlingshotConstants;
+import org.apache.sling.sample.slingshot.impl.InternalConstants;
 import org.apache.sling.sample.slingshot.ratings.RatingsService;
 import org.apache.sling.sample.slingshot.ratings.RatingsUtil;
 import org.slf4j.Logger;
@@ -64,9 +68,17 @@
         // save rating
         ResourceResolver resolver = null;
         try {
-            resolver = factory.getServiceResourceResolver(null);
+            // TODO - switch to service user with Oak
+            final Map<String, Object> authInfo = new HashMap<String, Object>();
+            authInfo.put(ResourceResolverFactory.USER, InternalConstants.SERVICE_USER_NAME);
+            authInfo.put(ResourceResolverFactory.PASSWORD, InternalConstants.SERVICE_USER_NAME.toCharArray());
+            resolver = factory.getResourceResolver(authInfo);
+//            resolver = factory.getServiceResourceResolver(null);
 
-            ratingsService.setRating(request.getResource(), userId, Integer.valueOf(rating));
+            final Resource reqResource = resolver.getResource(request.getResource().getPath());
+
+            ratingsService.setRating(reqResource, userId, Integer.valueOf(rating));
+
         } catch ( final LoginException le ) {
             throw new ServletException("Unable to login", le);
         } finally {
diff --git a/slingshot/src/main/java/org/apache/sling/sample/slingshot/ratings/impl/RatingsServiceImpl.java b/slingshot/src/main/java/org/apache/sling/sample/slingshot/ratings/impl/RatingsServiceImpl.java
index 50cd4d4..b1e6820 100644
--- a/slingshot/src/main/java/org/apache/sling/sample/slingshot/ratings/impl/RatingsServiceImpl.java
+++ b/slingshot/src/main/java/org/apache/sling/sample/slingshot/ratings/impl/RatingsServiceImpl.java
@@ -21,6 +21,7 @@
 
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.ModifiableValueMap;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -44,6 +45,7 @@
     /**
      * @see org.apache.sling.sample.slingshot.ratings.RatingsService#getRatingsResourcePath(org.apache.sling.api.resource.Resource)
      */
+    @Override
     public String getRatingsResourcePath(final Resource resource) {
         final String contentPath = SlingshotUtil.getContentPath(resource);
         if ( contentPath != null ) {
@@ -58,6 +60,7 @@
     /**
      * @see org.apache.sling.sample.slingshot.ratings.RatingsService#getRating(org.apache.sling.api.resource.Resource)
      */
+    @Override
     public int getRating(final Resource resource) {
         final String fullPath = getRatingsResourcePath(resource);
         int rating = 0;
@@ -82,6 +85,7 @@
     /**
      * @see org.apache.sling.sample.slingshot.ratings.RatingsService#getRating(org.apache.sling.api.resource.Resource, java.lang.String)
      */
+    @Override
     public int getRating(final Resource resource, final String userId) {
         final String fullPath = getRatingsResourcePath(resource);
         int rating = 0;
@@ -97,6 +101,7 @@
     /**
      * @see org.apache.sling.sample.slingshot.ratings.RatingsService#setRating(org.apache.sling.api.resource.Resource, java.lang.String, int)
      */
+    @Override
     public void setRating(final Resource resource, final String userId, final int rating)
     throws PersistenceException {
         final String ratingsPath = getRatingsResourcePath(resource) ;
@@ -106,10 +111,17 @@
         final Resource ratingsResource = ResourceUtil.getOrCreateResource(resource.getResourceResolver(),
                 ratingsPath, props, null, true);
 
-        props.clear();
-        props.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, RatingsUtil.RESOURCETYPE_RATING);
-        props.put(RatingsUtil.PROPERTY_RATING, rating);
-        ResourceUtil.getOrCreateResource(resource.getResourceResolver(),
-                ratingsResource.getPath() + "/" + userId, props, null, false);
+        final Resource ratingRsrc = resource.getResourceResolver().getResource(ratingsResource, userId);
+        if ( ratingRsrc == null ) {
+            props.clear();
+            props.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, RatingsUtil.RESOURCETYPE_RATING);
+            props.put(RatingsUtil.PROPERTY_RATING, rating);
+
+            resource.getResourceResolver().create(ratingsResource, userId, props);
+        } else {
+            final ModifiableValueMap mvm = ratingRsrc.adaptTo(ModifiableValueMap.class);
+            mvm.put(RatingsUtil.PROPERTY_RATING, rating);
+        }
+        resource.getResourceResolver().commit();
     }
 }
diff --git a/slingshot/src/main/resources/SLING-INF/content/libs/slingshot/Component/trail.html.jsp b/slingshot/src/main/resources/SLING-INF/content/libs/slingshot/Component/trail.html.jsp
index 605f38f..f36440d 100644
--- a/slingshot/src/main/resources/SLING-INF/content/libs/slingshot/Component/trail.html.jsp
+++ b/slingshot/src/main/resources/SLING-INF/content/libs/slingshot/Component/trail.html.jsp
@@ -37,9 +37,11 @@
         String prefix = "../";
         boolean continueProcessing = true;
         do {
-            final ValueMap parentAttr = parent.getValueMap();
-            final String parentName = parent.getName();
-            parents.add(new Object[] {prefix + parentName, ResponseUtil.escapeXml(parentAttr.get(SlingshotConstants.PROPERTY_TITLE, parentName))});
+            if ( !parent.isResourceType(SlingshotConstants.RESOURCETYPE_CONTENT) ) {
+                final ValueMap parentAttr = parent.getValueMap();
+                final String parentName = parent.getName();
+                parents.add(new Object[] {prefix + parentName, ResponseUtil.escapeXml(parentAttr.get(SlingshotConstants.PROPERTY_TITLE, parentName))});
+            }
             if ( parent.isResourceType(SlingshotConstants.RESOURCETYPE_USER) ) {
                 continueProcessing = false;
             } else {