static boolean getConnection(String username, String password, String host, int port) {
    try {
      JSch jsch = new JSch();

      session = jsch.getSession(username, host, port);

      UserInfo ui = new MyUserInfo();
      session.setUserInfo(ui);
      MyUserInfo temp = (MyUserInfo) ui;

      temp.setPassword(password);

      session.connect();

      Channel channel = session.openChannel("sftp");
      channel.connect();
      SFTPFileLoader.sftpChannel = (ChannelSftp) channel;

      return true;

    } catch (Exception e) {
      System.out.println(e);
      errorMessage += e.toString();
    }

    return false;
  }
 private void updateUserList() {
   List<String> usernames = new ArrayList<>();
   for (Session s : session.getOpenSessions()) {
     String uname = (String) s.getUserProperties().get(USERNAME_KEY);
     usernames.add(uname);
   }
   this.endpointConfig.getUserProperties().put(USERNAMES_KEY, usernames);
 }
 private void broadcastUserListUpdate() {
   UserListUpdateMessage ulum = new UserListUpdateMessage(this.getUserList());
   for (Session nextSession : session.getOpenSessions()) {
     try {
       nextSession.getBasicRemote().sendObject(ulum);
     } catch (IOException | EncodeException ex) {
       System.out.println("Error updating a client : " + ex.getMessage());
     }
   }
 }
  void processNewUser(NewUserMessage message) {
    String newUsername = this.validateUsername(message.getUsername());
    NewUserMessage uMessage = new NewUserMessage(newUsername);
    try {
      session.getBasicRemote().sendObject(uMessage);
    } catch (IOException | EncodeException ioe) {
      System.out.println(
          "Error signing " + message.getUsername() + " into chat : " + ioe.getMessage());
    }
    this.registerUser(newUsername);

    try {
      ResultSet rs = null, ds = null;
      String z, x;
      String url1 = "jdbc:mysql://localhost:3306/chat";
      Connection conn1 = DriverManager.getConnection(url1, "root", "root");
      Statement st1 = conn1.createStatement();
      Statement st2 = conn1.createStatement();
      String myQuery = "SELECT Message,Recepient FROM Log WHERE User = '******'";
      System.out.println(myQuery);
      rs =
          st1.executeQuery("SELECT Message,Recepient FROM Log WHERE User = '******';");
      while (rs.next()) {
        z = rs.getString("Message");
        x = rs.getString("Recepient");
        ChatUpdateMessage cdm1 = new ChatUpdateMessage(newUsername, z, x);
        try {
          session.getBasicRemote().sendObject(cdm1);
        } catch (IOException | EncodeException ex) {
          System.out.println("Error updating a client : " + ex.getMessage());
        }
      }

      ds =
          st2.executeQuery("SELECT Message,User FROM Log WHERE Recepient = '" + newUsername + "';");
      while (ds.next()) {
        z = ds.getString("Message");
        x = ds.getString("User");
        ChatUpdateMessage cdm2 = new ChatUpdateMessage(x, z, newUsername);
        try {
          session.getBasicRemote().sendObject(cdm2);
        } catch (IOException | EncodeException ex) {
          System.out.println("Error updating a client : " + ex.getMessage());
        }
      }

    } catch (Exception e) {
      System.err.println("Got an exception! ");
      System.err.println(e.getMessage());
    }

    this.broadcastUserListUpdate();
  }
Beispiel #5
0
  /** Sends Emails to Customers who have not submitted their Bears */
  public static void BearEmailSendMessage(String msgsubject, String msgText, String msgTo) {
    try {
      BearFrom = props.getProperty("BEARFROM");
      // To = props.getProperty("TO");
      SMTPHost = props.getProperty("SMTPHOST");
      Properties mailprops = new Properties();
      mailprops.put("mail.smtp.host", SMTPHost);

      // create some properties and get the default Session
      Session session = Session.getDefaultInstance(mailprops, null);

      // create a message
      Message msg = new MimeMessage(session);

      // set the from
      InternetAddress from = new InternetAddress(BearFrom);
      msg.setFrom(from);
      InternetAddress[] address = InternetAddress.parse(msgTo);
      msg.setRecipients(Message.RecipientType.TO, address);
      msg.setSubject(msgsubject);
      msg.setContent(msgText, "text/plain");
      Transport.send(msg);
    } // end try
    catch (MessagingException mex) {
      USFEnv.getLog().writeCrit("Message not sent", null, null);
    } catch (Exception ex) {
      USFEnv.getLog().writeCrit("Message not sent", null, null);
    }
  } // end BearEmailSendMessage
 static void disconnectFromSFTP() {
   try {
     session.disconnect();
   } catch (Exception e) {
     errorMessage +=
         "Exception in loadFilesFromSFTP(exception during disconnecting): " + e.toString() + "\n";
   }
 }
  private void broadcastTranscriptUpdate() {
    String v = this.transcript.getRec();
    String g;
    if (c.equals(v)) {
      for (Session nextSession : session.getOpenSessions()) {

        ChatUpdateMessage cdm =
            new ChatUpdateMessage(
                this.transcript.getLastUsername(),
                this.transcript.getLastMessage(),
                this.transcript.getRec());

        try {
          nextSession.getBasicRemote().sendObject(cdm);
        } catch (IOException | EncodeException ex) {
          System.out.println("Error updating a client : " + ex.getMessage());
        }
      }
    } else {
      for (Session nextSession : session.getOpenSessions()) {
        String check = this.transcript.getRec();
        ChatUpdateMessage cdm =
            new ChatUpdateMessage(
                this.transcript.getLastUsername(),
                this.transcript.getLastMessage(),
                this.transcript.getRec());
        g = (String) nextSession.getUserProperties().get(USERNAME_KEY);
        if (v.equals(g)) {
          try {
            nextSession.getBasicRemote().sendObject(cdm);
          } catch (IOException | EncodeException ex) {
            System.out.println("Error updating a client : " + ex.getMessage());
          }
        }
      }
    }
  }
  private List getNextSequenceImpl(TypeDescriptor typeDescriptor, Session session) {
    Field[] pkFields = typeDescriptor.getPkFields();

    Assert.condition(
        1 == pkFields.length,
        "Automatic PK values are only supported for types with a single PK field.");

    String createPKStmt =
        dbDescriptor.getCreatePKStatement(
            sqlUtils.getSchemaName(), typeDescriptor.getPkSequence(), this.sequenceBatchSize);

    Field field = pkFields[0];
    if (session.isUsingPreparedStatements(typeDescriptor.getType())) {
      PreparedStatement pkStatement = null;
      try {
        Connection connection = session.getConnection();
        PreparedStatement result;
        try {
          result = connection.prepareStatement(createPKStmt);
        } catch (SQLException x) {
          throw new InternalException(x);
        }
        pkStatement = result;
        ResultSet pkQuery = pkStatement.executeQuery();
        List newIds = new LinkedList();
        while (pkQuery.next()) {
          newIds.add(
              DmlManager.getJavaValue(
                  field.getType(),
                  typeDescriptor.getPersistentField(field).getLength(),
                  pkQuery,
                  1,
                  true,
                  false));
        }
        return newIds;
      } catch (SQLException e) {
        throw new InternalException(e);
      } finally {
        QueryUtils.closeStatement(pkStatement);
      }
    } else {
      Statement pkStmt = null;
      try {
        Connection connection = session.getConnection();
        pkStmt = connection.createStatement();
        ResultSet pkQuery = pkStmt.executeQuery(createPKStmt);
        List newIds = new LinkedList();
        while (pkQuery.next()) {
          newIds.add(
              DmlManager.getJavaValue(
                  field.getType(),
                  typeDescriptor.getPersistentField(field).getLength(),
                  pkQuery,
                  1,
                  true,
                  false));
        }
        return newIds;
      } catch (SQLException e) {
        throw new InternalException(e);
      } finally {
        QueryUtils.closeStatement(pkStmt);
      }
    }
  }
 private void registerUser(String username) {
   System.out.println("Inside registration");
   session.getUserProperties().put(USERNAME_KEY, username);
   this.updateUserList();
 }
 private String getCurrentUsername() {
   return (String) session.getUserProperties().get(USERNAME_KEY);
 }