Package org.jwebsocket.token

Examples of org.jwebsocket.token.Token


      isRunning = true;
      while (isRunning) {
        try {
          Thread.sleep(50);

          Token lToken = new Token("event");
          lToken.put("name", "stream");
          lToken.put("msg", new Date().getTime());

          put(lToken);
        } catch (InterruptedException ex) {
          log.error("(run) " + ex.getClass().getSimpleName() + ": " + ex.getMessage());
        }
View Full Code Here


    String lDataType = aToken.getString("datatype");
    String lValue = aToken.getString("value");

  if (lType != null && (lNS == null || lNS.equals(NS_SHARED_OBJECTS))) {

      Token lResponse = getServer().createResponse(aToken);

      // create
      if (lType.equals("create")) {
        if (log.isDebugEnabled()) {
          log.debug("Processing 'create'...");
        }
        if (!isDataTypeValid(lDataType, aConnector, lResponse)) {
          return;
        }
        if (alreadyExists(lID, aConnector, lResponse)) {
          return;
        }
        sharedObjects.put(lID, string2Object(lDataType, lValue));

        Token lBCT = new Token(lNS, "event");
        lBCT.put("name", "created");
        lBCT.put("id", lID);
        lBCT.put("datatype", lDataType);
        lBCT.put("value", lValue);
        getServer().broadcastToken(aConnector, lBCT);

        // destroy
      } else if (lType.equals("destroy")) {
        if (log.isDebugEnabled()) {
          log.debug("Processing 'destroy'...");
        }
        if (!doesContain(lID, aConnector, lResponse)) {
          return;
        }
        sharedObjects.remove(lID);

        Token lBCT = new Token(lNS, "event");
        lBCT.put("name", "destroyed");
        lBCT.put("id", lID);
        getServer().broadcastToken(aConnector, lBCT);

        // get
      } else if (lType.equals("get")) {
        if (log.isDebugEnabled()) {
          log.debug("Processing 'get'...");
        }
        if (!doesContain(lID, aConnector, lResponse)) {
          return;
        }
        Object lObj = sharedObjects.get(lID);
        lResponse.put("id", lID);
        lResponse.put("result", lObj.toString());

        // put
      } else if (lType.equals("update")) {
        if (log.isDebugEnabled()) {
          log.debug("Processing 'update'...");
        }
        if (!isDataTypeValid(lDataType, aConnector, lResponse)) {
          return;
        }
        sharedObjects.put(lID, string2Object(lDataType, lValue));
        Token lBCT = new Token(lNS, "event");
        lBCT.put("name", "updated");
        lBCT.put("id", lID);
        lBCT.put("datatype", lDataType);
        lBCT.put("value", lValue);
        getServer().broadcastToken(aConnector, lBCT);

        // init
      } else if (lType.equals("init")) {
        if (log.isDebugEnabled()) {
          log.debug("Processing 'init'...");
        }
        Token lBCT = new Token(lNS, "event");
        lBCT.put("name", "init");

        String lData = null;
        try {
          JSONStringer jsonStringer = new JSONStringer();
          // start main object
          jsonStringer.object();
          // iterate through all items (fields) of the token
          Iterator<String> lIterator = sharedObjects.getKeys().iterator();
          while (lIterator.hasNext()) {
            String lKey = lIterator.next();
            Object lVal = sharedObjects.get(lKey);
            if (lVal instanceof Collection) {
              jsonStringer.key(lKey).array();
              for (Object item : (Collection) lVal) {
                jsonStringer.value(item);
              }
              jsonStringer.endArray();
            } else {
              jsonStringer.key(lKey).value(lVal);
            }
          }
          // end main object
          jsonStringer.endObject();
          lData = jsonStringer.toString();
        } catch (JSONException ex) {
          log.error(ex.getClass().getSimpleName() + ": " + ex.getMessage());
        }
        lBCT.put("value", lData);
        getServer().sendToken(aConnector, lBCT);

      } else {
        log.warn("Invalid command " + lType + " received...");
        lResponse.put("code", -1);
View Full Code Here

      lServer.sendToken(aConnector, lServer.createAccessDenied(aToken));
      return;
    }

    // notify clients about shutdown
    Token lResponseToken = lServer.createResponse(aToken);
    lResponseToken.put("msg", "Shutdown in progress...");
    lServer.broadcastToken(lResponseToken);

    JWebSocketInstance.setStatus(JWebSocketInstance.SHUTTING_DOWN);

  }
View Full Code Here

    if (!SecurityFactory.checkRight(lServer.getUsername(aConnector), NS_ADMIN + ".getConnections")) {
      lServer.sendToken(aConnector, lServer.createAccessDenied(aToken));
      return;
    }

    Token lResponse = lServer.createResponse(aToken);
    try {
      List<Map> lResultList = new FastList<Map>();
      Map lConnectorMap = lServer.getAllConnectors();
      Collection<WebSocketConnector> lConnectors = lConnectorMap.values();
      for (WebSocketConnector lConnector : lConnectors) {
        Map lResultItem = new FastMap<String,Object>();
        lResultItem.put("port", lConnector.getRemotePort());
        lResultItem.put("usid", lConnector.getSession().getSessionId());
        lResultItem.put("username", lConnector.getUsername());
        lResultItem.put("isToken", lConnector.getBoolean(TokenServer.VAR_IS_TOKENSERVER));
        lResultList.add(lResultItem);
      }
      lResponse.put("connections", lResultList);
    } catch (Exception ex) {
      log.error(ex.getClass().getSimpleName()
          + " on getConnections: " + ex.getMessage());
    }
View Full Code Here

    if (lOrder != null && lOrder.length() > 0) {
      lSQL += " order by " + lOrder;
    }

    // instantiate response token
    Token lResponse = lServer.createResponse(aToken);
    // TODO: should work with usual arrays as well!
    // Object[] lColumns = null;
    int lRowCount = 0;
    int lColCount = 0;
    List<Map> lColumns = new FastList<Map>();
    List lData = new FastList<Map>();
    try {
      DBQueryResult lRes = DBConnectSingleton.querySQL(DBConnectSingleton.USR_SYSTEM, lSQL);

      // TODO: metadata should be optional to save bandwidth!
      // generate the meta data for the response
      lColCount = lRes.metaData.getColumnCount();
      lResponse.put("colcount", lColCount);

      for (int i = 1; i <= lColCount; i++) {
        // get name of colmuns
        String lSimpleClass = JDBCTools.extractSimpleClass(
            lRes.metaData.getColumnClassName(i));
        // convert to json type
        String lRIAType = JDBCTools.getJSONType(lSimpleClass, lRes.metaData);

        Map lColHeader = new FastMap<String, Object>();
        lColHeader.put("name", lRes.metaData.getColumnName(i));
        lColHeader.put("jsontype", lRIAType);
        lColHeader.put("jdbctype", lRes.metaData.getColumnTypeName(i));

        lColumns.add(lColHeader);
      }

      // generate the result data
      while (lRes.resultSet.next()) {
        lData.add(getResultColumns(lRes.resultSet, lColCount));
        lRowCount++;
      }
    } catch (Exception ex) {
      mLog.error(ex.getClass().getSimpleName() + " on query: " + ex.getMessage());
    }

    // complete the response token
    lResponse.put("rowcount", lRowCount);
    lResponse.put("columns", lColumns);
    lResponse.put("data", lData);

    // send response to requester
    lServer.sendToken(aConnector, lResponse);
  }
View Full Code Here

   * @param aDataPacket
   * @return
   */
  public Token packetToToken(WebSocketConnector aConnector, WebSocketPacket aDataPacket) {
    String lSubProt = aConnector.getHeader().getSubProtocol(JWebSocketCommonConstants.SUB_PROT_DEFAULT);
    Token lToken = null;
    if (lSubProt.equals(JWebSocketCommonConstants.SUB_PROT_JSON)) {
      lToken = JSONProcessor.packetToToken(aDataPacket);
    } else if (lSubProt.equals(JWebSocketCommonConstants.SUB_PROT_CSV)) {
      lToken = CSVProcessor.packetToToken(aDataPacket);
    } else if (lSubProt.equals(JWebSocketCommonConstants.SUB_PROT_XML)) {
View Full Code Here

  @Override
  public void processPacket(WebSocketEngine aEngine, final WebSocketConnector aConnector, WebSocketPacket aDataPacket) {
    // is the data packet supposed to be interpreted as token?
    if (aConnector.getBool(VAR_IS_TOKENSERVER)) {
      final Token lToken = packetToToken(aConnector, aDataPacket);
      if (lToken != null) {
        boolean lRunReqInOwnThread = "true".equals(lToken.getString("spawnThread"));
        // TODO: create list of running threads and close all properly on shutdown
        if (lRunReqInOwnThread) {
          if (mLog.isDebugEnabled()) {
            mLog.debug("Processing threaded token '" + lToken.toString() + "' from '" + aConnector + "'...");
          }
          new Thread(new Runnable() {

            @Override
            public void run() {
              processToken(aConnector, lToken);
            }
          }).start();
        } else {
          if (mLog.isDebugEnabled()) {
            mLog.debug("Processing token '" + lToken.toString() + "' from '" + aConnector + "'...");
          }
          processToken(aConnector, lToken);
        }
        /*
        // before forwarding the token to the plug-ins push it through filter chain
View Full Code Here

   */
  public Token createResponse(Token aInToken) {
    Integer lTokenId = aInToken.getInteger("utid", -1);
    String lType = aInToken.getString("type");
    String lNS = aInToken.getString("ns");
    Token lResToken = new Token("response");
    lResToken.put("code", 0);
    lResToken.put("msg", "ok");
    if (lTokenId != null) {
      lResToken.put("utid", lTokenId);
    }
    if (lNS != null) {
      lResToken.put("ns", lNS);
    }
    if (lType != null) {
      lResToken.put("reqType", lType);
    }
    return lResToken;
  }
View Full Code Here

   * creates a response with the standard "not authenticated" message
   * @param aInToken
   * @return
   */
  public Token createNotAuthToken(Token aInToken) {
    Token lResToken = createResponse(aInToken);
    lResToken.put("code", -1);
    lResToken.put("msg", "not authenticated");
    return lResToken;
  }
View Full Code Here

   * creates a response with the standard "not granted" message
   * @param aInToken
   * @return
   */
  public Token createAccessDenied(Token aInToken) {
    Token lResToken = createResponse(aInToken);
    lResToken.put("code", -1);
    lResToken.put("msg", "access denied");
    return lResToken;
  }
View Full Code Here

TOP

Related Classes of org.jwebsocket.token.Token

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.