/**
 * @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.auth.*;
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));
        }

        User user = (User) session.getContext().get("user");
        X    x    = session.getContext().put(
            "logger",
            new PrefixLogger(
                new Object[] { user == null ? "" : user.getId() + " - " + user.label(), "[Service]", spec.getName() },
                (Logger) session.getContext().get("logger")));

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

        if ( user != null ) sessionDAO.put(session);

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

        if ( authenticate_ && ! auth.check(session.getContext(), "service." + spec.getName()) ) {
          DAO    groupDAO = (DAO) x.get("groupDAO");
          Group  group    = (Group) groupDAO.find(user.getGroup());
          Logger logger   = (Logger) x.get("logger");
          logger.debug("missing permission", group.getId(), "service." + spec.getName());
          // msg.replyWithException(new NoPermissionException("No permission"));
          // return;
        }

        msg.getLocalAttributes().put("x", x);
      }
    } catch (Throwable t) {
      t.printStackTrace();
    }

    getDelegate().send(msg);
  }
}

@@ -1874,62 +1874,8 @@
 );%0A%0A
-        if ( user != null ) sessionDAO.put(session);%0A%0A
     
@@ -2505,32 +2505,86 @@
 urn;%0A        %7D%0A%0A
+        if ( user != null ) sessionDAO.put(session);%0A%0A
         msg.getL

Move sessionDAO.put after test for login