Package org.jwebsocket.token

Examples of org.jwebsocket.token.Token


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

    // instantiate response token
    Token lResponse = lServer.createResponse(aToken);

    // obtain required parameters for file load operation
    String lFilename = aToken.getString("filename");
    String lScope = aToken.getString("scope", JWebSocketCommonConstants.SCOPE_PRIVATE);

    // TODO: Replace optional variables in path like %JWEBSOCKET_HOME% by env var values!

    // scope may be "private" or "public"
    String lBaseDir;
    if (JWebSocketCommonConstants.SCOPE_PRIVATE.equals(lScope)) {
      String lUsername = getUsername(aConnector);
      lBaseDir = getSetting(PRIVATE_DIR_KEY, PRIVATE_DIR_DEF);
      if (lUsername != null) {
        lBaseDir = lBaseDir.replace("{username}", lUsername);
      } else {
        lMsg = "not authenticated to save private file";
        if (log.isDebugEnabled()) {
          log.debug(lMsg);
        }
        lResponse.put("code", -1);
        lResponse.put("msg", lMsg);
        // send error response to requester
        lServer.sendToken(aConnector, lResponse);
        return;
      }
    } else if (JWebSocketCommonConstants.SCOPE_PUBLIC.equals(lScope)) {
      lBaseDir = getSetting(PUBLIC_DIR_KEY, PUBLIC_DIR_DEF);
    } else {
      lMsg = "invalid scope";
      if (log.isDebugEnabled()) {
        log.debug(lMsg);
      }
      lResponse.put("code", -1);
      lResponse.put("msg", lMsg);
      // send error response to requester
      lServer.sendToken(aConnector, lResponse);
      return;
    }

    Boolean lNotify = aToken.getBoolean("notify", false);
    String lBase64 = aToken.getString("data");
    byte[] lBA = null;
    if (lBase64 != null) {
      lBA = Base64.decodeBase64(lBase64);
    }

    // complete the response token
    String lFullPath = lBaseDir + lFilename;
    File lFile = new File(lFullPath);
    try {
      // prevent two threads at a time writing to the same file
      synchronized (this) {
        // force create folder if not yet exists
        File lDir = new File(FilenameUtils.getFullPath(lFullPath));
        FileUtils.forceMkdir(lDir);
        FileUtils.writeByteArrayToFile(lFile, lBA);
      }
    } catch (IOException ex) {
      lResponse.put("code", -1);
      lResponse.put("msg", ex.getMessage());
    }

    // send response to requester
    lServer.sendToken(aConnector, lResponse);

    // send notification event to other affected clients
    // to allow to update their content (if desired)
    if (lNotify) {
      // create token of type "event"
      Token lEvent = new Token(Token.TT_EVENT);
      // include name space of this plug-in
      lEvent.setNS(NS_FILESYSTEM);
      lEvent.put("name", "filesaved");
      lEvent.put("filename", lFilename);
      lEvent.put("sourceId", aConnector.getId());
      lEvent.put("url", getSetting(WEB_ROOT_KEY, WEB_ROOT_DEF) + lFilename);
      // TODO: Limit notification to desired scope
      lServer.broadcastToken(lEvent);
    }
  }
View Full Code Here


    String lFilename = aToken.getString("filename");
    String lScope = aToken.getString("scope", JWebSocketCommonConstants.SCOPE_PRIVATE);
    String lData = "";

    // instantiate response token
    Token lResponse = lServer.createResponse(aToken);

    String lBaseDir;
    if (JWebSocketCommonConstants.SCOPE_PRIVATE.equals(lScope)) {
      String lUsername = getUsername(aConnector);
      lBaseDir = getSetting(PRIVATE_DIR_KEY, PRIVATE_DIR_DEF);
      if (lUsername != null) {
        lBaseDir = lBaseDir.replace("{username}", lUsername);
      } else {
        lMsg = "not authenticated to load private file";
        if (log.isDebugEnabled()) {
          log.debug(lMsg);
        }
        lResponse.put("code", -1);
        lResponse.put("msg", lMsg);
        // send error response to requester
        lServer.sendToken(aConnector, lResponse);
        return;
      }
    } else if (JWebSocketCommonConstants.SCOPE_PUBLIC.equals(lScope)) {
      lBaseDir = getSetting(PUBLIC_DIR_KEY, PUBLIC_DIR_DEF);
    } else {
      lMsg = "invalid scope";
      if (log.isDebugEnabled()) {
        log.debug(lMsg);
      }
      lResponse.put("code", -1);
      lResponse.put("msg", lMsg);
      // send error response to requester
      lServer.sendToken(aConnector, lResponse);
      return;
    }

    // complete the response token
    File lFile = new File(lBaseDir + lFilename);
    byte[] lBA = null;
    try {
      lBA = FileUtils.readFileToByteArray(lFile);
      if (lBA != null && lBA.length > 0) {
        lData = new String(Base64.encodeBase64(lBA), "UTF-8");
      }
      lResponse.put("data", lData);
    } catch (IOException ex) {
      lResponse.put("code", -1);
      lResponse.put("msg", ex.getMessage());
    }

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

    if (!SecurityFactory.checkRight(getUsername(aConnector), NS_RPC_DEFAULT + ".rpc")) {
      sendToken(aConnector, aConnector, createAccessDenied(aToken));
      return;
    }

    Token lResponseToken = createResponse(aToken);

    String lClassName = aToken.getString("classname");
    String lMethod = aToken.getString("method");
    Object lArgs = aToken.get("args");
    // TODO: Tokens should always be a map of maps
    if (lArgs instanceof JSONObject) {
      lArgs = new Token((JSONObject) lArgs);
    }

    String lMsg = null;

    if (mLog.isDebugEnabled()) {
      mLog.debug("Processing RPC to class '" + lClassName + "', method '" + lMethod + "', args: '" + lArgs + "'...");
    }

    String lKey = lClassName + "." + lMethod;
    if (mGrantedProcs.containsKey(lKey)) {
      // class is ignored until security restrictions are finished.
      try {
        // TODO: use RpcCallable here!
        Object lInstance = mClasses.get(lClassName);
        if (lInstance != null) {
          Object lObj = call(lInstance, lMethod, lArgs);
          lResponseToken.put("result", lObj);
        } else {
          lMsg = "Class '" + lClassName + "' not found or not properly loaded.";
        }
      } catch (NoSuchMethodException ex) {
        lMsg = "NoSuchMethodException calling '" + lMethod + "' for class " + lClassName + ": " + ex.getMessage();
      } catch (IllegalAccessException ex) {
        lMsg = "IllegalAccessException calling '" + lMethod + "' for class " + lClassName + ": " + ex.getMessage();
      } catch (InvocationTargetException ex) {
        lMsg = "InvocationTargetException calling '" + lMethod + "' for class " + lClassName + ": " + ex.getMessage();
      }
    } else {
      lMsg = "Call to " + lKey + " is not granted!";
    }
    if (lMsg != null) {
      lResponseToken.put("code", -1);
      lResponseToken.put("msg", lMsg);
    }

    /* just for testing purposes of multi-threaded rpc's
    try {
    Thread.sleep(3000);
View Full Code Here

    if (mLog.isDebugEnabled()) {
      mLog.debug("Processing 'rrpc'...");
    }
    if (lTargetConnector != null) {
      Token lRRPC = new Token(lNS, "rrpc");
      lRRPC.put("classname", lClassname);
      lRRPC.put("method", lMethod);
      lRRPC.put("args", lArgs);
      lRRPC.put("sourceId", aConnector.getRemotePort());

      sendToken(aConnector, lTargetConnector, lRRPC);
    } else {
      Token lResponse = createResponse(aToken);
      lResponse.put("code", -1);
      lResponse.put("error", "Target " + lTargetId + " not found.");
      sendToken(aConnector, aConnector, lResponse);
    }
  }
View Full Code Here

    String lType = aToken.getType();

    // check if token has a type and a matching namespace
    if (lType != null && "my.namespace".equals(lNS)) {
      // if type is "getInfo" return some server information
      Token lResponse = aEvent.createResponse(aToken);
      if ("getInfo".equals(lType)) {
        lResponse.put("vendor", JWebSocketCommonConstants.VENDOR);
        lResponse.put("version", JWebSocketServerConstants.VERSION_STR);
        lResponse.put("copyright", JWebSocketCommonConstants.COPYRIGHT);
        lResponse.put("license", JWebSocketCommonConstants.LICENSE);
      } else {
        // if unknown type in this namespace, return corresponding error message
        lResponse.put("code", -1);
        lResponse.put("msg", "Token type '" + lType + "' not supported in namespace '" + lNS + "'.");
      }
      aEvent.sendToken(lResponse);
    }
  }
View Full Code Here

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

          Token lToken = new Token("event");
          lToken.put("name", "stream");
          lToken.put("streamID", getStreamID());

          // collect some data to monitor
          Runtime lRT = Runtime.getRuntime();
          lToken.put("totalMem", lRT.totalMemory());
          lToken.put("freeMem", lRT.freeMemory());

          TokenServer lServer = getServer();
          lToken.put("clientCount", lServer.getAllConnectors().size());

          File lFile = new File(".");
          lToken.put("freeDisk", lFile.getFreeSpace());
          lToken.put("totalDisk", lFile.getTotalSpace());
          lToken.put("usableDisk", lFile.getUsableSpace());

          // : further tags to be continued....

          put(lToken);
        } catch (InterruptedException ex) {
View Full Code Here

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

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

          // log.debug("Time streamer queues '" + lData + "'...");
          put(lToken);
        } catch (InterruptedException ex) {
          log.error("(run) " + ex.getClass().getSimpleName() + ": " + ex.getMessage());
View Full Code Here

    TokenServer lServer = getServer();
    String lUsername = lServer.getUsername(aConnector);

    // TODO: very first security test, replace by user's locked state!
    if ("locked".equals(lUsername)) {
      Token lToken = lServer.createAccessDenied(aToken);
      lServer.sendToken(aConnector, lToken);
      aResponse.rejectMessage();
      return;
    }
  }
View Full Code Here

    TokenServer lServer = getServer();
    String lUsername = lServer.getUsername(aConnector);

    // TODO: very first security test, replace by user's locked state!
    if( "locked".equals(lUsername) ) {
      Token lToken = lServer.createAccessDenied(aToken);
      lServer.sendToken(aConnector, lToken);
      aResponse.rejectMessage();
      return;
    }
  }
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.