Example #1
0
  @Override
  public void push(Session session, String callback, Map<String, Object> m) {
    Set<Binding> bindings = pushBindingsMap.get(callback);
    if (bindings == null) return;
    for (Binding binding : bindings) {
      if (binding.sessionId.equals(session.getGuid())) {
        String orgDomain = session.getOrgDomain();

        Message msg = createMessage(orgDomain, binding, callback, m);
        if (logger.isTraceEnabled()) tracePush(orgDomain, callback, m, binding.sessionId, binding);
        msgbus.send(msg);
      }
    }
  }
  public static Message decode(Session session, String text) {
    Logger logger = LoggerFactory.getLogger(KrakenMessageDecoder.class.getName());
    Charset utf8 = Charset.forName("utf-8");

    // remove potential control characters
    text = text.trim();
    if (text.length() == 0) return null;

    if (logger.isDebugEnabled())
      logger.debug(
          "kraken webconsole: debug websocket frame length {}, json [{}]", text.length(), text);

    if (text.equals("ping")) return null;

    // decrypt if msg is encrypted
    if (session.has("enc_key")) {
      try {
        JSONTokener tokenizer = new JSONTokener(new StringReader(text));
        JSONArray container = (JSONArray) tokenizer.nextValue();
        JSONObject header = container.getJSONObject(0);
        JSONObject body = container.getJSONObject(1);

        if (header.has("iv") && body.has("data")) {
          String data = body.getString("data");

          byte[] iv =
              ByteUtil.asArray(
                  Base64.decode(ChannelBuffers.wrappedBuffer(header.getString("iv").getBytes())));
          byte[] buf =
              ByteUtil.asArray(Base64.decode(ChannelBuffers.wrappedBuffer(data.getBytes())));
          byte[] key = ByteUtil.asByteArray(UUID.fromString(session.getString("enc_key")));
          SecretKeySpec secret = new SecretKeySpec(key, "AES");

          Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
          cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
          byte[] plain = new byte[cipher.getOutputSize(buf.length)];
          int plainLength = cipher.update(buf, 0, buf.length, plain, 0);
          plainLength += cipher.doFinal(plain, plainLength);

          text = new String(plain, 0, plainLength, utf8);
          logger.trace("kraken webconsole: decrypted msg [{}]", text);
        }
      } catch (Exception e) {
        logger.error("kraken webconsole: cannot decode encrypted msg [" + text + "]", e);
      }
    }

    try {
      JSONTokener tokenizer = new JSONTokener(new StringReader(text));
      JSONArray container = (JSONArray) tokenizer.nextValue();
      JSONObject header = container.getJSONObject(0);
      JSONObject body = container.getJSONObject(1);

      Message msg = new Message();

      msg.setGuid(header.getString("guid").trim());
      msg.setType(Message.Type.valueOf(header.getString("type").trim()));
      msg.setSource(header.getString("source"));
      msg.setTarget(header.getString("target"));
      msg.setMethod(header.getString("method").trim());
      msg.setParameters(parse(body));

      return msg;
    } catch (JSONException e) {
      logger.error("kraken webconsole: invalid json => " + text, e);
    }
    return null;
  }