package woko.facets.builtin.auth;

import net.sourceforge.jfacets.annotations.FacetKey;
import net.sourceforge.jfacets.annotations.FacetKeyList;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StrictBinding;
import woko.facets.BaseResolutionFacet;
import woko.facets.builtin.WokoFacets;

import javax.servlet.http.HttpServletRequest;

/**
 * Facet that forces the user to log-in. This facet is assigned by default
 * to all CRUD facets for <code>guest</code> role, to that when a guest user tries to
 * reach a "login required" facet, (s)he is prompted to authenticate first.
 *
 * You can override your own facets for your own role so that guest users are
 * forced to login when they try to reach some facets, instead of getting a 404 (FacetNotFound).
 */
@FacetKeyList(keys ={
    @FacetKey(name = WokoFacets.view, profileId = "guest"),
    @FacetKey(name = WokoFacets.edit, profileId = "guest"),
    @FacetKey(name = WokoFacets.create, profileId = "guest"),
    @FacetKey(name = WokoFacets.delete, profileId = "guest"),
    @FacetKey(name = WokoFacets.save, profileId = "guest"),
    @FacetKey(name = WokoFacets.json, profileId = "guest"),
    @FacetKey(name = WokoFacets.find, profileId = "guest"),
    @FacetKey(name = WokoFacets.list, profileId = "guest"),
    @FacetKey(name = WokoFacets.search, profileId = "all"),
    @FacetKey(name = WokoFacets.studio, profileId = "guest")
})
@StrictBinding
public class LoginRequiredFacet extends BaseResolutionFacet {

  public Resolution getResolution(ActionBeanContext abc) {
    HttpServletRequest request = abc.getRequest();
    String requestedPage = request.getRequestURI();
    // strip context path if needed
    String contextPath = request.getContextPath();
    if (requestedPage.startsWith(contextPath)) {
      requestedPage = requestedPage.substring(contextPath.length());
    }
    String queryString = request.getQueryString();
    if (queryString != null) {
      requestedPage += "?" + queryString;
    }
    return new ForwardResolution("/login").addParameter("targetUrl", requestedPage);
  }


}

@@ -355,16 +355,132 @@
 inding;%0A
+import net.sourceforge.stripes.rpc.RpcResolutionWrapper;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A
 import w
@@ -549,16 +549,49 @@
 oFacets;
+%0Aimport woko.util.JsonResolution;
 %0A%0Aimport
@@ -1074,18 +1074,23 @@
 t(keys =
+ 
 %7B%0A
+    
     @Fac
@@ -1133,32 +1133,36 @@
 eId = %22guest%22),%0A
+    
     @FacetKey(na
@@ -1201,32 +1201,36 @@
 = %22guest%22),%0A    
+ 
+   
 @FacetKey(name =
@@ -1267,32 +1267,36 @@
 = %22guest%22),%0A    
+  
+  
 @FacetKey(name =
@@ -1333,32 +1333,36 @@
 = %22guest%22),%0A    
+   
+ 
 @FacetKey(name =
@@ -1393,32 +1393,36 @@
 eId = %22guest%22),%0A
+    
     @FacetKey(na
@@ -1457,32 +1457,36 @@
 eId = %22guest%22),%0A
+    
     @FacetKey(na
@@ -1525,32 +1525,36 @@
 = %22guest%22),%0A    
+ 
+   
 @FacetKey(name =
@@ -1589,32 +1589,36 @@
 = %22guest%22),%0A    
+  
+  
 @FacetKey(name =
@@ -1657,24 +1657,28 @@
 %22all%22),%0A    
+  
+  
 @FacetKey(na
@@ -1803,16 +1803,18 @@
 acet %7B%0A%0A
+  
   public
@@ -1839,16 +1839,22 @@
 olution(
+final 
 ActionBe
@@ -1866,24 +1866,28 @@
 text abc) %7B%0A
+    
     HttpServ
@@ -1925,24 +1925,28 @@
 uest();%0A    
+ 
+   
 String reque
@@ -1985,16 +1985,20 @@
 ();%0A    
+ 
+   
 // strip
@@ -2021,16 +2021,20 @@
  needed%0A
+    
     Stri
@@ -2076,24 +2076,28 @@
 Path();%0A    
+   
+ 
 if (requeste
@@ -2125,24 +2125,30 @@
 extPath)) %7B%0A
+      
       reques
@@ -2204,26 +2204,34 @@
 gth());%0A    
+  
-%7D%0A
+  %7D%0A    
     String q
@@ -2265,24 +2265,28 @@
 ryString();%0A
+    
     if (quer
@@ -2300,24 +2300,30 @@
  != null) %7B%0A
+      
       reques
@@ -2352,22 +2352,30 @@
 String;%0A
+    
     %7D%0A
+    
     retu
@@ -2381,16 +2381,41 @@
 urn new 
+RpcResolutionWrapper(new 
 ForwardR
@@ -2474,18 +2474,507 @@
 tedPage)
-;%0A
+) %7B%0A            @Override%0A            public Resolution getRpcResolution() %7B%0A                JSONObject result = new JSONObject();%0A                try %7B%0A                    result.put(%22error%22, true);%0A                    result.put(%22forbidden%22, true);%0A                %7D catch(JSONException e) %7B%0A                    throw new RuntimeException(e);%0A                %7D%0A                abc.getResponse().setStatus(403);%0A                return new JsonResolution(result);%0A            %7D%0A        %7D;%0A  
   %7D%0A%0A%0A%7D%0A

return 403 in RPC mode for LoginRequiredFacet