@Override
 public String then() {
   System.out.println(message.getMessage());
   message.setMessage("oodbyte cruel world");
   message.setStatus(Message.GOODBYE);
   return null;
 }
Esempio n. 2
0
  public void createMessage(int id, String content_message) {
    Session session = sessionFactory.getCurrentSession();
    Message message = new Message();

    Date date = new java.util.Date();
    message.setId_user(id);
    message.setDate(new Timestamp(date.getTime()));
    message.setMessage(content_message);
    session.persist(message);
  }
Esempio n. 3
0
  /** 处理接收到得用户信息 */
  private void receiveServerMsg() {
    try {

      ObjectInputStream serverInput = new ObjectInputStream(socket.getInputStream());
      Object object = serverInput.readObject();
      // 判断对方是否在线
      if (object == null) { // type为3表示用户不在线,转离线消息
        ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
        Message m = new Message();
        m.setType(3);
        m.setSendIP(myIP);
        m.setMessage(sendMsgTextField.getText().trim());
        m.setReceiveIP(destinationIP);
        output.writeObject(m);
        chartTextArea.append("对方不在线,信息已经转入离线信息。\n");
      } else {
        // 用户收到的是正常的聊天信息
        if (object instanceof Message) {
          Message m = (Message) object;
          /*String destinationIP = m.getSendIP();
          new Client(destinationIP, serverIP, PORT, table);*/
          chartTextArea.append(m.getSendIP() + ":\n" + m.getMessage() + "\n");
        }
        // 用户收到的是更新在线用户的信息
        else {
          @SuppressWarnings("unchecked")
          List<String> onlineIP = (List<String>) object;

          System.out.println("friend list:" + onlineIP.toString());

          // 将在线用户加入到界面中显示
          String[] tableHeads = new String[] {"在线用户"};
          DefaultTableModel dtm = (DefaultTableModel) friendTable.getModel();
          dtm.setColumnIdentifiers(tableHeads);

          for (int i = 0; i < onlineIP.size(); i++) {
            Vector<String> onlineIPVector = new Vector<String>();
            onlineIPVector.add(onlineIP.get(i));
            dtm.addRow(onlineIPVector);
          }
        }
      }

    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }
  public static void getmessage() {
    List<Message> messagelist = dbmsg.getindividualuserid(senderid);

    if (messagelist.size() > 0) {
      amList.clear();
      Message messages;
      for (Message msg : messagelist) {
        messages = new Message();
        String fname = msg.getFname();
        String lname = msg.getLname();
        String tstamp = msg.getTimestamp();
        String message = msg.getMessage();
        String me = msg.getIsmine();
        String msgtype = "";

        if (msg.getMsgtype() != null) {

          msgtype = msg.getMsgtype();

        } else {
          msgtype = "";
        }
        messages.setMessage(message);
        messages.setFname(fname);
        messages.setLname(lname);
        messages.setTimestamp(tstamp);
        messages.setIsmine(me);
        messages.setMsgtype(msgtype);

        amList.add(messages);
      }

      txtalert.setVisibility(View.INVISIBLE);

      AwesomeAdapter adapter = new AwesomeAdapter(context, amList);
      adapter.notifyDataSetChanged();
      list.setAdapter(adapter);
      list.setSelection(amList.size() - 1);
    }
    int seencount = dbmsg.getunseeenindividual(senderid, "no");
    if (seencount > 0) {
      dbmsg.updateindividualseen(senderid);
    }
    cancelnotification();
  }
        @Override
        public void onClick(View view) {

          Utils.Log("Send clicked");

          // If the message field is not empty
          if (!messageText.getText().toString().isEmpty()) {

            final Message message = new Message();
            message.setSender(username);
            message.setRecipient(conversation.getUsername());

            // If DHKE was completed and a key exists
            if (!dbManager.getAESKey(conversation.getUsername()).isEmpty()) {
              String signature = "";
              String key = dbManager.getAESKey(conversation.getUsername());
              byte[] iv = Crypto.GenerateRandomIV();
              final String plainText = messageText.getText().toString();
              final String cipherText = Crypto.AESencrypt(key, plainText, iv);
              final String base64IV = Base64.encodeToString(iv, Base64.NO_WRAP);

              try {
                PrivateKey RSAKeySign =
                    Crypto.RSAStringToPrivateKey(dbManager.getRSAKeySignaturePrivate());

                signature =
                    Base64.encodeToString(
                        Crypto.RSASign(Base64.decode(cipherText, Base64.NO_WRAP), RSAKeySign),
                        Base64.NO_WRAP);

              } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
              } catch (InvalidKeySpecException e) {
                e.printStackTrace();
              } catch (NoSuchProviderException e) {
                e.printStackTrace();
              } catch (InvalidKeyException e) {
                e.printStackTrace();
              } catch (SignatureException e) {
                e.printStackTrace();
              }

              message.setMessage(cipherText);
              message.setIv(base64IV);
              message.setSignature(signature);

              new HttpHandler() {
                @Override
                public HttpUriRequest getHttpRequestMethod() {
                  HttpPost httpPost = new HttpPost(Utils.SERVER_SEND_MESSAGE);

                  List<NameValuePair> nameValuePairs = new ArrayList<>();
                  nameValuePairs.add(new BasicNameValuePair("sender", message.getSender()));
                  nameValuePairs.add(new BasicNameValuePair("recipient", message.getRecipient()));
                  nameValuePairs.add(new BasicNameValuePair("message", message.getMessage()));
                  nameValuePairs.add(new BasicNameValuePair("iv", message.getIv()));
                  nameValuePairs.add(new BasicNameValuePair("signature", message.getSignature()));

                  try {
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                  } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                  }

                  return httpPost;
                }

                @Override
                public void onResponse(String result) {
                  Utils.Log("HttpResult: " + result);

                  if (conversation.isEncrypted().equals("true")) {
                    message.setMessage(
                        Crypto.AESencrypt(
                            conversationPass,
                            plainText,
                            Base64.decode(message.getIv(), Base64.NO_WRAP)));
                    message.setEncrypted(true);

                    dbManager.addMessage(message);

                    message.setEncrypted(false);
                    message.setMessage(plainText);

                    messages.add(message);
                    adapter.notifyDataSetChanged();

                  } else {
                    message.setMessage(plainText);
                    message.setEncrypted(false);

                    dbManager.addMessage(message);

                    messages.add(message);
                    adapter.notifyDataSetChanged();
                  }

                  messageList.setSelection(messages.size() + 1);
                }
              }.execute();

              messageText.setText("");

            }

            // DHKE was not complete yet, store messages unencrypted temporarily
            // and then encrypt and send them once the exchange is complete
            else {
              message.setEncrypted(false);
              message.setMessage(messageText.getText().toString());
              message.setIv(Base64.encodeToString(Crypto.GenerateRandomIV(), Base64.NO_WRAP));

              dbManager.addMessage(message);

              messages.add(message);
              adapter.notifyDataSetChanged();

              messageList.setSelection(adapter.getCount() - 1);
              messageText.setText("");
            }
          }
        }
Esempio n. 6
0
 public SystemMessage(MessageType type, T payload) {
   super.setMessage(type.getMessage());
   this.setType(type);
   this.payload = payload;
 }
Esempio n. 7
0
  public static void main(String[] args) {
    try {
      // connect to mongoDB, ip and port number
      Mongo mongo = new Mongo("127.0.0.1", 27017);

      // get database from MongoDB,
      // if database doesn't exists, mongoDB will create it automatically
      DB db = mongo.getDB("yourdb");

      // Getting A List Of Collections
      Set<String> collections = db.getCollectionNames();

      for (String s : collections) {
        System.out.println(s);
      }

      // Get collection from MongoDB, database named "yourDB"
      // if collection doesn't exists, mongoDB will create it automatically
      DBCollection collection = db.getCollection("yourCollection");

      // create a document to store key and value
      DBObject document = new BasicDBObject();
      document.put("id", 1001);
      document.put("msg", "hello world mongoDB in Java");

      // save it into collection named "yourCollection"
      collection.insert(document);

      // search query
      DBObject searchQuery = new BasicDBObject();
      searchQuery.put("id", 1001);

      // query it
      DBCursor cursor = collection.find(searchQuery);

      // loop over the cursor and display the retrieved result
      while (cursor.hasNext()) {
        System.out.println("Our collection after putting document here: " + cursor.next());
      }

      // Counting Documents in Collection
      System.out.println("Elements in collection " + collection.getCount());

      // update document (just replase exist - it is normal practise)
      DBObject updatedDocument = new BasicDBObject();
      updatedDocument.put("id", 1001);
      updatedDocument.put("msg", "hello world mongoDB in Java updated");
      collection.update(new BasicDBObject().append("id", 1001), updatedDocument);

      // query it
      DBCursor cursorAfterUpdate = collection.find(searchQuery);

      // loop over the cursor and display the retrieved result
      while (cursorAfterUpdate.hasNext()) {
        System.out.println("Our collection after update: " + cursorAfterUpdate.next());
      }

      // Counting Documents in Collection
      System.out.println("Elements in collection " + collection.getCount());

      // Map to object
      Message message = new Message();
      message.setId((Integer) document.get("id"));
      message.setMessage((String) document.get("msg"));

      System.out.println("Id putted in object: " + message.getId());
      System.out.println("Message putted in object: " + message.getMessage());

      // Remove document from collection
      DBObject doc = collection.findOne(); // get first document
      collection.remove(doc);

      // query it
      DBCursor cursorAfterDelete = collection.find(searchQuery);

      // loop over the cursor and display the retrieved result
      while (cursorAfterDelete.hasNext()) {
        System.out.println("Our collection after delete: " + cursorAfterDelete.next());
      }

      // Counting Documents in Collection
      System.out.println("Elements in collection " + collection.getCount());

      // Close connection to db
      mongo.close();

      System.out.println("Done");

    } catch (UnknownHostException e) {
      e.printStackTrace();
    } catch (MongoException e) {
      e.printStackTrace();
    }
  }