public synchronized void onClose() {
   sessionState.onClose(this);
   setSessionState(new StateDiconnected());
   if (logger.isTraceEnabled()) {
     logger.trace("Connection closed for device '" + getDeviceId() + "'");
   }
 }
 protected void sendSyncMessage(byte[] notificationMessage) {
   Sync sync = new Sync();
   sync.setNotificationMessage(notificationMessage);
   Message replyMessage = new Message(CTPProtocolConstants.CTP_PROTOCOL_1_0, sync);
   write(replyMessage);
   if (logger.isInfoEnabled()) {
     logger.info("Sending notification message to the device '" + getDeviceId() + "'");
   }
 }
  /**
   * Forward the authentication request to the Authentication Manager
   *
   * @return
   * @throws com.funambol.ctp.server.authentication.AuthenticationException
   */
  protected AuthorizationResponse forwardAuthenticationRequest() throws AuthenticationException {

    String username = (String) session.getAttribute(ATTRIBUTE_USERNAME);
    String deviceId = (String) session.getAttribute(ATTRIBUTE_DEVID);
    String credential = (String) session.getAttribute(ATTRIBUTE_CRED);
    if (logger.isTraceEnabled()) {
      logger.trace("Authenticating user '" + username + "', deviceId: '" + deviceId + "'");
    }
    AuthorizationResponse result =
        authenticationManager.authenticate(username, deviceId, credential);
    return result;
  }
  protected void forwardSubscription() {
    String deviceId = (String) session.getAttribute(ATTRIBUTE_DEVID);
    if (deviceId == null || "".equals(deviceId)) {
      logger.error("Unable to retrieve device id from session attributes");
      closeSession();
      return;
    }

    dispatcher.subscribe(deviceId, this);
    if (logger.isTraceEnabled()) {
      logger.trace("Subscribed notification for device " + deviceId);
    }
  }
  /**
   * Called by Status implementations to retrieve the pending notifications and send them to the
   * connected client device
   */
  protected void managePendingNotificationMessage() {
    try {

      String username = (String) session.getAttribute(ATTRIBUTE_USERNAME);
      String deviceId = (String) session.getAttribute(ATTRIBUTE_DEVID);
      if (logger.isTraceEnabled()) {
        logger.trace(
            "Retrieving pending notifications for user '"
                + username
                + "', deviceId: '"
                + deviceId
                + "'");
      }

      com.funambol.framework.notification.Message message =
          pendingNotificationManager.getMessageFromPendingNotifications(username, deviceId);

      if (message == null) {
        if (logger.isTraceEnabled()) {
          logger.trace("No pending notification messages present.");
        }
      } else {
        sendSyncMessage(message.getMessageContent());

        pendingNotificationManager.deletePendingNotifications(
            username, deviceId, message.getSyncSources());
        if (logger.isTraceEnabled()) {
          logger.trace("Pending notifications sent to device " + deviceId);
        }
      }
    } catch (PendingNotificationException ex) {
      logger.error("Error while retrieving pending notification messages", ex);
    }
  }
  private void setLogPrefix(final String userName, final String deviceId, final IoSession session) {

    StringBuilder prefix = new StringBuilder();
    prefix.append("[").append(session.getRemoteAddress()).append("] ");
    prefix.append("[").append(getSessionId()).append("] ");
    prefix.append("[").append(deviceId).append("] ");
    prefix.append("[").append(userName != null ? userName : "").append("] ");
    session.setAttribute(SessionLog.PREFIX, prefix.toString());
    logger.setPrefix(prefix.toString());
  }
  private void setSessionState(State sessionState) {

    if (this.sessionState.deepequals(sessionState)) {
      return;
    }
    if (this.sessionState.deepequals(new StateDiconnected())
        && !(sessionState.deepequals(new StateDiconnected()))) {
      logger.error(
          "status transition from "
              + this.sessionState.getName()
              + " to "
              + sessionState.getName()
              + " in not allowed");
      return;
    }
    if (logger.isTraceEnabled()) {
      logger.trace(
          "Changing status from " + this.sessionState.getName() + " to " + sessionState.getName());
    }
    this.sessionState = sessionState;
  }
  /**
   * Creates a new instance of <CODE>SessionManager</CODE>
   *
   * <p>The authentication manager is retrieved from the CTPServerConfiguration using parameter
   * specifide in file config/com/funambol/ctp/server/authentication/AuthenticationManager.xml
   *
   * @param session The related IoSession
   * @param dispatcher The NotificationProvider where to subscribe for notification messages.
   */
  public SessionManager(IoSession session, NotificationProvider dispatcher) {
    this.session = session;
    this.dispatcher = dispatcher;
    this.sessionState = new StateConnected();
    this.sessionId = createSessionId();
    lastClientEventTime = System.currentTimeMillis();

    this.authenticationManager =
        CTPServerConfiguration.getCTPServerConfiguration().getAuthenticationManager();

    this.pendingNotificationManager =
        CTPServerConfiguration.getCTPServerConfiguration().getPendingNotificationManager();

    session.setAttribute(SessionLog.LOGGER, logger);

    String prefix = "[" + session.getRemoteAddress() + "] ";
    session.setAttribute(SessionLog.PREFIX, prefix);
    logger.setPrefix(prefix);
  }
 protected void closeSession() {
   logger.warn("Closing session");
   session.close();
 }
Example #10
0
 /**
  * Callback called when another <CODE>SessionManager</CODE> subscribes using the same key
  * (deviceId). If another <CODE>SessionManager</CODE> subscribes using the same key, the
  * previously subscribed <CODE>SessionManager</CODE> closes the connection.
  */
 public void onUnsubscription() {
   logger.error("Found previously established connection for the same device id");
   closeSession();
 }