예제 #1
0
  public String insertMessage(Message message) throws SQLException {
    String r = "New  message inserted!";

    if (isFriend(
        message.getSender(),
        message.getReceiver())) { // Check if the sender and receiver are friends
      try {
        // creates a SQL Statement object in order to execute the SQL insert command
        stmt = conn.createStatement();
        stmt.execute(
            "INSERT INTO MESSAGES (Sender, Receiver, Text, DateMsg, TimeMsg, IsDelivered ) "
                + "VALUES ("
                + getProfileID(message.getSender())
                + ", "
                + getProfileID(message.getReceiver())
                + ", '"
                + message.getText()
                + "' , '"
                + message.getDateMsg()
                + "' , '"
                + message.getTimeMsg()
                + "',"
                + message.isDelivered()
                + ")");
        stmt.close();
        // System.out.println("Requête executée");
      } catch (SQLException e) {
        // System.err.println(e.toString());
        r = e.toString();
        throw e;
      }
      return r;
    } else {
      return "WARNING : Can't insert message in database : the sender and receiver are not friends !";
    }
  }
  /** Tests Message class. */
  @Test
  public void messageTest() {
    Message message = new Message();
    message = new Message("xml", this, MessageDirection.OUTBOUND);
    message.setCreationTime(new LocalDateTime());
    message.setSender("sender");
    message.setReceiver("receiver");
    message.setContentHash(null);
    assertNotNull(message.getCreationTime());
    assertEquals("sender", message.getSender());
    assertEquals("receiver", message.getReceiver());
    assertEquals(0, message.getContentHash().length);

    message.setContentHash(new byte[0]);
    assertEquals(0, message.getContentHash().length);
  }
 /**
  * Shows the message with the given ID
  *
  * @param id message id
  * @return String containing the message
  */
 public String read_message(int id) {
   try {
     Query q = em.createQuery("SELECT m FROM Message m where m.msgID = :id");
     q.setParameter("id", id);
     Message m = (Message) q.getSingleResult();
     if (!(dm.lookupRights().compareTo("admin") == 0)) {
       if (!(dm.lookupUserName().compareTo(m.getReceiver()) == 0)
           && !(m.getStatus().compareTo("bcast") == 0)) return "WRONG MESSAGE ID....";
     }
     Query q1 = em.createQuery("UPDATE Message SET status = :state WHERE msgID = :id");
     q1.setParameter("id", id);
     q1.setParameter("state", "read");
     q1.executeUpdate();
     return m.readMessage();
   } catch (Exception e) {
     return "WRONG MESSAGE ID";
   }
 }
  /**
   * Deletes the message with the given ID if the user has the rights
   *
   * @param id message id
   * @return String containing the status of the deletion
   */
  public String delete_message(int id) {
    try {
      Query q = em.createQuery("SELECT m FROM Message m where m.msgID = :id");
      q.setParameter("id", id);
      Message m = (Message) q.getSingleResult();
      if (!(dm.lookupRights().compareTo("admin") == 0)) {
        if (!(dm.lookupUserName().compareTo(m.getSender()) == 0)
            && m.getStatus().compareTo("bcast") == 0) return "You do not have the rights to delete";
        if (!(dm.lookupUserName().compareTo(m.getReceiver()) == 0)
            && !(m.getStatus().compareTo("bcast") == 0))
          return "You do not have the rights to delete";
      }

      Query q1 = em.createQuery("DELETE FROM Message x where x.msgID = :id");
      q1.setParameter("id", id);
      q1.executeUpdate();
      return "Message deleted successfuly";
    } catch (Exception e) {
      return "WRONG MESSAGE ID";
    }
  }
예제 #5
0
 final synchronized void tryMigration(KernelAddress destination, AgentAddress traveler) {
   AbstractAgent ref = Kernel.getReference(traveler);
   Message message = new SynchroMessage(ref);
   for (Iterator i = distantKernels.iterator(); i.hasNext(); ) {
     AgentAddress potentialReceiver = (AgentAddress) i.next();
     if (potentialReceiver.getKernel().equals(destination)) {
       message.setReceiver(potentialReceiver);
       break;
     }
   }
   if (message.getReceiver() != null && ref != null) {
     // kernelAgent.callshook 	...  updateDistantOrgs(Kernel.MIGRATION,traveler,null,null,null);
     if (ref instanceof Agent) {
       ((Agent) ref).getAgentThread().stop();
       ref.setCurrentKernel(null);
       // ref.messageBox=null;
     }
     getCurrentKernel().removeAgentFromOrganizations(traveler);
     getCurrentKernel().removeReferenceOf(traveler);
     message.setSender(getAddress());
     sendDistantMessage(message);
   }
 }
예제 #6
0
  /**
   * Reads messages from the network. A message is either a command for the server or for an
   * internal Service.
   *
   * @param sock the Socket
   */
  private void getMessage(Socket sock) {
    int i;
    boolean alreadyConnected;
    boolean isAuthentication;

    Message message;
    byte[] signature;

    String cmd;
    String cmdData;
    StringTokenizer stk;

    ObjectConnection oc = null;

    try {
      /* streams initialization */
      oc = new ObjectConnection(sock);
      message = (Message) oc.read();
      signature = (byte[]) oc.read();
    } catch (Exception ex) {
      ex.printStackTrace();
      Logging.getLogger().warning("#Err > Unable to read message.");
      return;
    }

    // check wether a user is known or not
    alreadyConnected = isAlreadyKnown(message.getSender());

    // check if command is authentication
    isAuthentication =
        message.getApplication().equals("Server")
            && ((String) message.getData()).startsWith("AUTH");

    // signature check
    if (alreadyConnected && !isAuthentication) {
      boolean sigok = false;
      try {
        ConnectInfo ci = message.getSender();
        if (ci.verifier == null) ci = this.getCompleteConnectInfo(ci);
        sigok = ci.verifier.verify(message, signature);
      } catch (Exception e) {
        e.printStackTrace();
      }

      if (!sigok) {
        try {
          oc.write("FAILED bad signature");
        } catch (Exception e) {
        }
        Logging.getLogger().warning("#Err > bad signature: " + message.getSender());
        return;
      }
    }

    if (message.getApplication().equals("Server")) {
      cmd = null;

      try {
        stk = new StringTokenizer((String) message.getData());
        cmd = stk.nextToken();
        cmdData = stk.nextToken("\0").substring(1);
      } catch (Exception ex) {
        if (cmd == null) cmd = "";

        cmdData = "";
      }

      /* if the user asks for authentication, we try to do it and exits this method */
      if (cmd.equals("AUTH")) {
        try {
          oc.write("OK");
        } catch (Exception e) {
          e.printStackTrace();
        }

        authentification(oc, message, cmdData);
      } else if (!alreadyConnected) {
        Logging.getLogger().info("Access denied to " + message.getSender());
        try {
          oc.write("FAILED No Connection");
        } catch (Exception e) {
        }
      } else {
        internalCommand(oc, message, cmd, cmdData);
      }
    } else if (!alreadyConnected) {
      Logging.getLogger().info("Access denied to " + message.getSender());
      try {
        oc.write("FAILED No Connection");
      } catch (Exception e) {
      }
    } else {
      Service s;

      /* seek the destination service */
      boolean serviceFound = false;
      for (i = 0; i < this.services.size(); i++) {
        s = (Service) services.get(i);

        if (s.getName().equalsIgnoreCase(message.getApplication())) {
          serviceFound = true;
          UserConcept user = null;
          ServiceConcept service = null;
          try {
            user = store.getUserStore().getUser(message.getSender().getName());
            service = store.getServiceStore().getService(message.getReceiver().getName());
          } catch (Exception e) {
          }

          /* tests serviceManager for permissions */
          boolean isAutorizedService = false;
          try {
            isAutorizedService = store.getServiceStore().isAuthorizedService(user, service);
          } catch (Exception e) {
          }

          if (!isAutorizedService) {
            Logging.getLogger()
                .info(
                    "#Err > "
                        + message.getSender()
                        + " : Service denied to "
                        + message.getReceiver().getName());
            try {
              oc.write("FAILED You don't have acces to this service");
            } catch (Exception e) {
            }
          } else {
            try {
              oc.write("OK");
            } catch (Exception e) {
            }
            serviceFound = true;
            s.process(oc, message);
          }
          break;
        }
      }

      if (!serviceFound) {
        try {
          oc.write("FAILED unknown");
        } catch (Exception e) {
        }
        Logging.getLogger()
            .warning("#Err > Service " + message.getReceiver().getName() + " unknown");
      }
    }

    oc.close();
  }