Package org.hive2hive.core

Examples of org.hive2hive.core.H2HSession


  }

  @Override
  public void start() {
    try {
      H2HSession session = networkManager.getSession();
      String randomPID = UUID.randomUUID().toString();
      UserProfileManager profileManager = session.getProfileManager();
      UserProfile userProfile = profileManager.getUserProfile(randomPID, true);

      // get and check the file nodes to be rearranged
      FolderIndex oldParent = (FolderIndex) userProfile.getFileById(oldParentKey);
      if (oldParent == null) {
        logger.error("Could not find the old parent.");
        return;
      } else if (!oldParent.canWrite(sender)) {
        logger.error("User was not allowed to change the source directory.");
        return;
      }

      Index child = oldParent.getChildByName(sourceFileName);
      if (child == null) {
        logger.error("File node that should be moved not found.");
        return;
      }

      FolderIndex newParent = (FolderIndex) userProfile.getFileById(newParentKey);
      if (newParent == null) {
        logger.error("Could not find the new parent.");
        return;
      } else if (!newParent.canWrite(sender)) {
        logger.error("User was not allowed to change the destination directory.");
        return;
      }

      // rearrange
      oldParent.removeChild(child);
      newParent.addChild(child);
      child.setParent(newParent);

      // change the child's name
      child.setName(destFileName);

      profileManager.readyToPut(userProfile, randomPID);

      // move the file on disk
      FileUtil.moveFile(session.getRoot(), sourceFileName, destFileName, oldParent, newParent);
    } catch (Hive2HiveException | IOException e) {
      logger.error("Could not process the user profile task.", e);
    }
  }
View Full Code Here


    if (!removePerformed) {
      logger.info("Nothing has been removed. Skip re-adding it to the network.");
      return;
    }

    H2HSession session;
    try {
      session = networkManager.getSession();
    } catch (NoSessionException e1) {
      logger.error("Could not roll back because no session.");
      return;
    }

    UserProfileTask upTask = context.consumeUserProfileTask();
    HybridEncryptedContent encrypted;
    try {
      encrypted = H2HEncryptionUtil.encryptHybrid(upTask, session.getKeyPair().getPublic());
    } catch (DataLengthException | InvalidKeyException | IllegalStateException
        | InvalidCipherTextException | IllegalBlockSizeException | BadPaddingException | IOException e) {
      logger.error("Could not encrypt the user profile task while rollback.");
      return;
    }
View Full Code Here

      }
    }
  }

  private HybridEncryptedContent signAndEncryptMessage(BaseMessage message, PublicKey targetPublicKey) {
    H2HSession session;
    try {
      session = networkManager.getSession();
    } catch (NoSessionException e2) {
      logger.error("No logged in user / no session. The message will not be sent.");
      return null;
    }

    try {
      // asymmetrically encrypt message
      byte[] messageBytes = EncryptionUtil.serializeObject(message);
      HybridEncryptedContent encryptedMessage = EncryptionUtil.encryptHybrid(messageBytes,
          targetPublicKey, H2HConstants.KEYLENGTH_HYBRID_AES);

      // create signature
      try {
        byte[] signature = EncryptionUtil.sign(messageBytes, session.getKeyPair().getPrivate());
        encryptedMessage.setSignature(session.getUserId(), signature);
      } catch (InvalidKeyException | SignatureException e1) {
        logger.error("An exception occured while signing the message. The message will not be sent.",
            e1);
        return null;
      }
View Full Code Here

    if (!(request instanceof HybridEncryptedContent)) {
      logger.error("Received unknown object.");
      return null;
    }

    H2HSession session;
    try {
      if (networkManager.getSession() == null) {
        throw new NoSessionException();
      } else {
        session = networkManager.getSession();
      }
    } catch (NoSessionException e) {
      logger.warn("Currently no user is logged in! Keys for decryption needed. Node ID = '{}'.",
          networkManager.getNodeId());
      return AcceptanceReply.FAILURE;
    }

    HybridEncryptedContent encryptedMessage = (HybridEncryptedContent) request;

    // get signature
    String senderId = encryptedMessage.getUserId();
    byte[] signature = encryptedMessage.getSignature();
    if (senderId == null || signature == null) {
      logger.warn("No signature for message.");
      return AcceptanceReply.FAILURE_SIGNATURE;
    }

    // asymmetrically decrypt message
    byte[] decryptedMessage = null;
    try {
      KeyPair keys = session.getKeyPair();
      decryptedMessage = EncryptionUtil.decryptHybrid(encryptedMessage, keys.getPrivate());
    } catch (Exception e) {
      logger.warn("Decryption of message failed.", e);
      return AcceptanceReply.FAILURE_DECRYPTION;
    }

    // deserialize decrypted message
    Object message = null;
    try {
      message = EncryptionUtil.deserializeObject(decryptedMessage);
    } catch (IOException | ClassNotFoundException e) {
      logger.error("Message could not be deserialized. Reason = '{}'.", e.getMessage());
    }

    if (message != null && message instanceof BaseMessage) {
      BaseMessage receivedMessage = (BaseMessage) message;

      // verify the signature
      if (session.getKeyManager().containsPublicKey(senderId)) {
        if (!verifySignature(senderId, decryptedMessage, signature))
          return AcceptanceReply.FAILURE_SIGNATURE;

        // give a network manager reference to work (verify, handle)
        try {
View Full Code Here

   * @throws NoSessionException
   */
  public ProcessComponent createLogoutProcess(NetworkManager networkManager) throws NoPeerConnectionException,
      NoSessionException {
    DataManager dataManager = networkManager.getDataManager();
    H2HSession session = networkManager.getSession();
    LogoutProcessContext context = new LogoutProcessContext(session);

    // process composition
    SequentialProcess process = new SequentialProcess();

    process.add(new GetUserLocationsStep(session.getCredentials().getUserId(), context, dataManager));
    process.add(new RemoveOwnLocationsStep(context, networkManager));
    process.add(new StopDownloadsStep(session.getDownloadManager()));
    process.add(new WritePersistentStep(session.getRoot(), session.getKeyManager(), session.getDownloadManager()));
    process.add(new DeleteSessionStep(networkManager));

    // TODO to be implemented:
    // // stop all running processes
    // ProcessManager.getInstance().stopAll("Logout stopped all processes.");
View Full Code Here

   * Process to create a new file. Note that this is only applicable for a single file, not a whole file
   * tree.
   */
  public ProcessComponent createNewFileProcess(File file, NetworkManager networkManager) throws NoSessionException,
      NoPeerConnectionException {
    H2HSession session = networkManager.getSession();
    DataManager dataManager = networkManager.getDataManager();
    AddFileProcessContext context = new AddFileProcessContext(file);

    SequentialProcess process = new SequentialProcess();
    process.add(new ValidateFileSizeStep(context, session.getFileConfiguration(), true));
    process.add(new CheckWriteAccessStep(context, session.getProfileManager(), session.getRoot()));
    if (file.isFile()) {
      // file needs to upload the chunks and a meta file
      process.add(new InitializeChunksStep(context, dataManager, session.getFileConfiguration()));
      process.add(new CreateMetaFileStep(context));
      process.add(new PutMetaFileStep(context, dataManager));
    }
    process.add(new AddIndexToUserProfileStep(context, session.getProfileManager(), session.getRoot()));
    process.add(new PrepareNotificationStep(context));
    process.add(createNotificationProcess(context, networkManager));

    return process;
  }
View Full Code Here

  public ProcessComponent createUpdateFileProcess(File file, NetworkManager networkManager) throws NoSessionException,
      IllegalArgumentException, NoPeerConnectionException {
    DataManager dataManager = networkManager.getDataManager();
    UpdateFileProcessContext context = new UpdateFileProcessContext(file);

    H2HSession session = networkManager.getSession();

    SequentialProcess process = new SequentialProcess();
    process.add(new ValidateFileSizeStep(context, session.getFileConfiguration(), false));
    process.add(new CheckWriteAccessStep(context, session.getProfileManager(), session.getRoot()));
    process.add(new File2MetaFileComponent(file, context, context, networkManager));
    process.add(new InitializeChunksStep(context, dataManager, session.getFileConfiguration()));
    process.add(new CreateNewVersionStep(context, session.getFileConfiguration()));
    process.add(new PutMetaFileStep(context, dataManager));
    process.add(new UpdateMD5inUserProfileStep(context, session.getProfileManager()));

    // TODO: cleanup can be made async because user operation does not depend on it
    process.add(new CleanupChunksStep(context, dataManager));
    process.add(new PrepareNotificationStep(context));
    process.add(createNotificationProcess(context, networkManager));
View Full Code Here

   * @return A file list process.
   * @throws NoSessionException
   */
  public IResultProcessComponent<List<FileTaste>> createFileListProcess(NetworkManager networkManager)
      throws NoSessionException {
    H2HSession session = networkManager.getSession();
    GetFileListStep listStep = new GetFileListStep(session.getProfileManager(), session.getRootFile());
    return new AsyncResultComponent<List<FileTaste>>(listStep);
  }
View Full Code Here

      PublicKeyManager keyManager = new PublicKeyManager(userCredentials.getUserId(), keyPair, node.getDataManager());
      IFileConfiguration config = FileConfiguration.createDefault();
      DownloadManager downloadManager = new DownloadManager(node.getDataManager(), node.getMessageManager(),
          keyManager, config);
      File root = new File(System.getProperty("java.io.tmpdir"), NetworkTestUtil.randomString());
      H2HSession session;
      session = new H2HSession(profileManager, keyManager, downloadManager, config, root.toPath());
      node.setSession(session);
    }
  }
View Full Code Here

      PublicKeyManager keyManager = new PublicKeyManager(userCredentials.getUserId(), keyPair, node.getDataManager());
      IFileConfiguration config = FileConfiguration.createDefault();
      DownloadManager downloadManager = new DownloadManager(node.getDataManager(), node.getMessageManager(),
          keyManager, config);
      File root = new File(System.getProperty("java.io.tmpdir"), NetworkTestUtil.randomString());
      H2HSession session;
      session = new H2HSession(profileManager, keyManager, downloadManager, config, root.toPath());
      node.setSession(session);
    }
  }
View Full Code Here

TOP

Related Classes of org.hive2hive.core.H2HSession

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.