/**
 * @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.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);
        HttpServletRequest req          = getX().get(HttpServletRequest.class);
        AuthService        auth         = (AuthService) getX().get("auth");
        DAO                sessionDAO   = (DAO)         getX().get("sessionDAO");
        DAO                groupDAO     = (DAO)         getX().get("groupDAO");
        Session            session      = (Session)     sessionDAO.find(sessionID);

        if ( session == null ) {
          session = new Session();
          session.setId(sessionID);
          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 ( authenticate_ && session.getUserId() == 0 ) {
          msg.replyWithException(new AuthenticationException());
          return;
        }

        if ( user != null ) {
          Group group    = (Group) groupDAO.find(user.getGroup());

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

          x = x.put("appConfig", group.getAppConfig(getX()));

          sessionDAO.put(session);
        }

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

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

    getDelegate().send(msg);
  }
}

@@ -2678,62 +2678,8 @@
  %7D%0A%0A
-        if ( user != null ) sessionDAO.put(session);%0A%0A
     

remove duplicate code