/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.box;

import foam.core.X;
import foam.dao.DAO;
import foam.nanos.auth.AuthService;
import foam.nanos.boot.NSpec;
import foam.nanos.logger.*;
import foam.nanos.session.Session;
import java.security.AccessControlException;
import java.util.Date;
import javax.naming.NoPermissionException;
import javax.servlet.http.HttpServletRequest;

public class SessionServerBox
  extends ProxyBox
{
  protected boolean authenticate_;

  public SessionServerBox(X x, Box delegate, boolean authenticate) {
    super(x, delegate);
    authenticate_ = authenticate;
  }

  public void send(Message msg) {
    String sessionID = (String) msg.getAttributes().get("sessionId");

    try {
      if ( sessionID != null ) {
        NSpec       spec       = getX().get(NSpec.class);
        AuthService auth       = (AuthService) getX().get("auth");
        DAO         sessionDAO = (DAO)         getX().get("sessionDAO");
        Session     session    = (Session)     sessionDAO.find(sessionID);

        if ( session == null ) {
          session = new Session();
          session.setId(sessionID);

          HttpServletRequest req = getX().get(HttpServletRequest.class);
          session.setRemoteHost(req.getRemoteHost());
          session.setContext(getX().put(Session.class, session));
        }

        session.setLastUsed(new Date());
        session.setUses(session.getUses()+1);

        sessionDAO.put(session);

        if ( authenticate_ && session.getUserId() == 0 ) {
          msg.replyWithException(new AccessControlException("Not logged in"));
          return;
        }

        /*
        Temporarily work around service check.
        if ( authenticate_ && ! auth.check(session.getContext(), "service." + spec.getName()) ) {
          msg.replyWithException(new NoPermissionException("No permission"));
          return;
        }
        */

        X x = session.getContext().put(
          "logger",
          new PrefixLogger(new Object[] { "[Service]", spec.getName() }, (Logger) session.getContext().get("logger")));
        msg.getLocalAttributes().put("x", x);
      }
    } catch (Throwable t) {
      t.printStackTrace();
    }

    getDelegate().send(msg);
  }
}

@@ -213,16 +213,46 @@
 ervice;%0A
+import foam.nanos.auth.Group;%0A
 import f
@@ -1436,32 +1436,250 @@
 n));%0A        %7D%0A%0A
+        X x = session.getContext().put(%0A            %22logger%22,%0A            new PrefixLogger(%0A                new Object%5B%5D %7B %22%5BService%5D%22, spec.getName() %7D,%0A                (Logger) session.getContext().get(%22logger%22)));%0A%0A
         session.
@@ -1955,66 +1955,8 @@
  %7D%0A%0A
-        /*%0A        Temporarily work around service check.%0A
     
@@ -2063,230 +2063,134 @@
     
-msg.replyWithException(new NoPermissionException(%22No permission%22));%0A          return;%0A        %7D%0A        */%0A%0A        X x = session.getContext().put(%0A          %22logger%22,%0A          new PrefixLogger(new Object%5B%5D %7B %22%5BS
+Group group = x.get(Group.class);%0A          ((Logger) x.get(%22logger%22)).debug(%22missing permission%22, group.getId(), %22s
 ervice
-%5D%22,
+.%22 +
  spe
@@ -2204,58 +2204,123 @@
 me()
- %7D, (Logger) session.getContext().get(%22logger%22)));
+);%0A          // msg.replyWithException(new NoPermissionException(%22No permission%22));%0A          // return;%0A        %7D%0A
 %0A   

Add warning when accessing service without permission.