/**
  * {@inheritDoc}
  *
  * <p>Overridden to make it visible to other classes in this package.
  */
 @Override
 protected void unregisterSession(Endpoint endpoint, WsSession wsSession) {
   if (wsSession.getUserPrincipal() != null && wsSession.getHttpSessionId() != null) {
     unregisterAuthenticatedSession(wsSession, wsSession.getHttpSessionId());
   }
   super.unregisterSession(endpoint, wsSession);
 }
 /**
  * {@inheritDoc}
  *
  * <p>Overridden to make it visible to other classes in this package.
  */
 @Override
 protected void registerSession(Endpoint endpoint, WsSession wsSession) {
   super.registerSession(endpoint, wsSession);
   if (wsSession.isOpen()
       && wsSession.getUserPrincipal() != null
       && wsSession.getHttpSessionId() != null) {
     registerAuthenticatedSession(wsSession, wsSession.getHttpSessionId());
   }
 }
  public void closeAuthenticatedSession(String httpSessionId) {
    Set<WsSession> wsSessions = authenticatedSessions.remove(httpSessionId);

    if (wsSessions != null && !wsSessions.isEmpty()) {
      for (WsSession wsSession : wsSessions) {
        try {
          wsSession.close(AUTHENTICATED_HTTP_SESSION_CLOSED);
        } catch (IOException e) {
          // Any IOExceptions during close will have been caught and the
          // onError method called.
        }
      }
    }
  }
 @Override
 public final void onMessage(T message, boolean last) {
   if (params.length == 1 && params[0] instanceof DecodeException) {
     ((WsSession) session).getLocal().onError(session, (DecodeException) params[0]);
     return;
   }
   Object[] parameters = params.clone();
   if (indexBoolean != -1) {
     parameters[indexBoolean] = Boolean.valueOf(last);
   }
   if (indexSession != -1) {
     parameters[indexSession] = session;
   }
   if (convert) {
     parameters[indexPayload] = ((ByteBuffer) message).array();
   } else {
     parameters[indexPayload] = message;
   }
   Object result = null;
   try {
     result = method.invoke(pojo, parameters);
   } catch (IllegalAccessException e) {
     handlePojoMethodException(e);
   } catch (InvocationTargetException e) {
     handlePojoMethodException(e);
   }
   processResult(result);
 }
Esempio n. 5
0
 private void handleThrowableOnSend(Throwable t) throws WsIOException {
   ExceptionUtils.handleThrowable(t);
   wsSession.getLocal().onError(wsSession, t);
   CloseReason cr =
       new CloseReason(CloseCodes.CLOSED_ABNORMALLY, sm.getString("wsFrame.ioeTriggeredClose"));
   throw new WsIOException(cr);
 }
Esempio n. 6
0
 protected void processInputBuffer() throws IOException {
   while (true) {
     wsSession.updateLastActive();
     if (state == State.NEW_FRAME) {
       if (!processInitialHeader()) {
         break;
       }
       // If a close frame has been received, no further data should
       // have seen
       if (!open) {
         throw new IOException(sm.getString("wsFrame.closed"));
       }
     }
     if (state == State.PARTIAL_HEADER) {
       if (!processRemainingHeader()) {
         break;
       }
     }
     if (state == State.DATA) {
       if (!processData()) {
         break;
       }
     }
   }
 }
Esempio n. 7
0
 public WsFrameBase(WsSession wsSession, Transformation transformation) {
   inputBuffer = new byte[Constants.DEFAULT_BUFFER_SIZE];
   messageBufferBinary = ByteBuffer.allocate(wsSession.getMaxBinaryMessageBufferSize());
   messageBufferText = CharBuffer.allocate(wsSession.getMaxTextMessageBufferSize());
   this.wsSession = wsSession;
   Transformation finalTransformation;
   if (isMasked()) {
     finalTransformation = new UnmaskTransformation();
   } else {
     finalTransformation = new NoopTransformation();
   }
   if (transformation == null) {
     this.transformation = finalTransformation;
   } else {
     transformation.setNext(finalTransformation);
     this.transformation = transformation;
   }
 }
Esempio n. 8
0
  private boolean processDataControl() throws IOException {
    TransformationResult tr = transformation.getMoreData(opCode, fin, rsv, controlBufferBinary);
    if (TransformationResult.UNDERFLOW.equals(tr)) {
      return false;
    }
    // Control messages have fixed message size so
    // TransformationResult.OVERFLOW is not possible here

    controlBufferBinary.flip();
    if (opCode == Constants.OPCODE_CLOSE) {
      open = false;
      String reason = null;
      int code = CloseCodes.NORMAL_CLOSURE.getCode();
      if (controlBufferBinary.remaining() == 1) {
        controlBufferBinary.clear();
        // Payload must be zero or greater than 2
        throw new WsIOException(
            new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.oneByteCloseCode")));
      }
      if (controlBufferBinary.remaining() > 1) {
        code = controlBufferBinary.getShort();
        if (controlBufferBinary.remaining() > 0) {
          CoderResult cr = utf8DecoderControl.decode(controlBufferBinary, controlBufferText, true);
          if (cr.isError()) {
            controlBufferBinary.clear();
            controlBufferText.clear();
            throw new WsIOException(
                new CloseReason(
                    CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.invalidUtf8Close")));
          }
          // There will be no overflow as the output buffer is big
          // enough. There will be no underflow as all the data is
          // passed to the decoder in a single call.
          controlBufferText.flip();
          reason = controlBufferText.toString();
        }
      }
      wsSession.onClose(new CloseReason(Util.getCloseCode(code), reason));
    } else if (opCode == Constants.OPCODE_PING) {
      if (wsSession.isOpen()) {
        wsSession.getBasicRemote().sendPong(controlBufferBinary);
      }
    } else if (opCode == Constants.OPCODE_PONG) {
      MessageHandler.Whole<PongMessage> mhPong = wsSession.getPongMessageHandler();
      if (mhPong != null) {
        try {
          mhPong.onMessage(new WsPongMessage(controlBufferBinary));
        } catch (Throwable t) {
          handleThrowableOnSend(t);
        } finally {
          controlBufferBinary.clear();
        }
      }
    } else {
      // Should have caught this earlier but just in case...
      controlBufferBinary.clear();
      throw new WsIOException(
          new CloseReason(
              CloseCodes.PROTOCOL_ERROR,
              sm.getString("wsFrame.invalidOpCode", Integer.valueOf(opCode))));
    }
    controlBufferBinary.clear();
    newFrame();
    return true;
  }
Esempio n. 9
0
  /**
   * @return <code>true</code> if sufficient data was present to process all of the initial header
   */
  private boolean processInitialHeader() throws IOException {
    // Need at least two bytes of data to do this
    if (writePos - readPos < 2) {
      return false;
    }
    int b = inputBuffer[readPos++];
    fin = (b & 0x80) > 0;
    rsv = (b & 0x70) >>> 4;
    opCode = (byte) (b & 0x0F);
    if (!transformation.validateRsv(rsv, opCode)) {
      throw new WsIOException(
          new CloseReason(
              CloseCodes.PROTOCOL_ERROR,
              sm.getString("wsFrame.wrongRsv", Integer.valueOf(rsv), Integer.valueOf(opCode))));
    }

    if (Util.isControl(opCode)) {
      if (!fin) {
        throw new WsIOException(
            new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.controlFragmented")));
      }
      if (opCode != Constants.OPCODE_PING
          && opCode != Constants.OPCODE_PONG
          && opCode != Constants.OPCODE_CLOSE) {
        throw new WsIOException(
            new CloseReason(
                CloseCodes.PROTOCOL_ERROR,
                sm.getString("wsFrame.invalidOpCode", Integer.valueOf(opCode))));
      }
    } else {
      if (continuationExpected) {
        if (!Util.isContinuation(opCode)) {
          throw new WsIOException(
              new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.noContinuation")));
        }
      } else {
        try {
          if (opCode == Constants.OPCODE_BINARY) {
            // New binary message
            textMessage = false;
            int size = wsSession.getMaxBinaryMessageBufferSize();
            if (size != messageBufferBinary.capacity()) {
              messageBufferBinary = ByteBuffer.allocate(size);
            }
            binaryMsgHandler = wsSession.getBinaryMessageHandler();
            textMsgHandler = null;
          } else if (opCode == Constants.OPCODE_TEXT) {
            // New text message
            textMessage = true;
            int size = wsSession.getMaxTextMessageBufferSize();
            if (size != messageBufferText.capacity()) {
              messageBufferText = CharBuffer.allocate(size);
            }
            binaryMsgHandler = null;
            textMsgHandler = wsSession.getTextMessageHandler();
          } else {
            throw new WsIOException(
                new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.invalidOpCode", Integer.valueOf(opCode))));
          }
        } catch (IllegalStateException ise) {
          // Thrown if the session is already closed
          throw new WsIOException(
              new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.sessionClosed")));
        }
      }
      continuationExpected = !fin;
    }
    b = inputBuffer[readPos++];
    // Client data must be masked
    if ((b & 0x80) == 0 && isMasked()) {
      throw new WsIOException(
          new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.notMasked")));
    }
    payloadLength = b & 0x7F;
    state = State.PARTIAL_HEADER;
    return true;
  }