Пример #1
0
  private static List<String[]> readRecords(
      final Hashtable<String, String> global, final CsvReader reader) throws IOException {
    final List<String[]> transactions = new ArrayList<String[]>();
    reader.setDelimiter(';');
    reader.setUseComments(false);
    reader.setTextQualifier('\'');

    int state = 0;

    while (reader.readRecord()) {
      final String[] record = reader.getValues();
      final String first = record[0];
      switch (state) {
        case 0:
          if (first.startsWith("#Klient")) {
            if (reader.readRecord()) {
              global.put("name", reader.getValues()[0]);
              state = 1;
            }
          }
          break;
        case 1:
          if (first.startsWith("#Za okres:")) {
            if (reader.readRecord()) {
              global.put("start_date", reader.getValues()[0]);
              global.put("end_date", reader.getValues()[1]);
              state = 2;
            }
          }
          break;
        case 2:
          if (first.startsWith("#Waluta")) {
            if (reader.readRecord()) {
              global.put("currency", reader.getValues()[0]);
              state = 3;
            }
          }
          break;
        case 3:
          if (first.startsWith("#Numer rachunku")) {
            if (reader.readRecord()) {
              global.put("account_number", reader.getValues()[0]);
              state = 4;
            }
          }
          break;
        case 4:
          if (first.startsWith("#Saldo pocz")) {
            global.put("start_balance", reader.getValues()[1]);
            state = 5;
          }
          break;
        case 5:
          if (first.startsWith("#Data operacji")) {
            state = 6;
          }
          break;
        case 6:
          if (record.length > 6 && record[6].startsWith("#Saldo ko")) {
            global.put("end_balance", record[4]);
            state = 7;
            continue;
          }
          if (record.length != 9) continue;
          transactions.add(record);
          break;
      }
    }
    return transactions;
  }
Пример #2
0
  /** Checks if some update is needed to be done and executes it if neccessary */
  public static void update() throws Exception {
    String version = Config.getInstance().getVersion();
    if (StringUtils.isEmpty(version)) {
      // program is started for the first time, no update neccessary
      return;
    }
    if (ObjectUtils.equals(version, Config.getLatestVersion())) { // already updated
      return;
    }
    logger.log(
        Level.INFO,
        "Updating from legacy version {0} to current version {1}",
        new Object[] {version, Config.getLatestVersion()});

    // changes to 1.4
    if (Config.compareProgramVersions(version, "1.4") < 0) {
      // add message ID to queue
      logger.fine("Updating queue to add message IDs...");
      try {
        Field queueFileField = PersistenceManager.class.getDeclaredField("queueFile");
        queueFileField.setAccessible(true);
        File queueFile = (File) queueFileField.get(null);

        List<String> lines = FileUtils.readLines(queueFile, "UTF-8");
        ArrayList<String> newLines = new ArrayList<String>();
        for (String line : lines) {
          newLines.add(line + ",");
        }
        FileUtils.writeLines(queueFile, "UTF-8", newLines);
      } catch (Exception ex) {
        logger.log(Level.SEVERE, "Updating queue file failed", ex);
      }
    }

    // changes to 0.8.0
    if (Config.compareProgramVersions(version, "0.8.0") < 0) {
      // set country prefix from locale
      if (StringUtils.isEmpty(Config.getInstance().getCountryPrefix())) {
        Config.getInstance()
            .setCountryPrefix(CountryPrefix.getCountryPrefix(Locale.getDefault().getCountry()));
      }
    }

    // changes to 0.17.0
    if (Config.compareProgramVersions(version, "0.16.0") <= 0) {
      // keyring encryption changed from AES to XOR
      logger.fine("Updating keyring file to newer encryption...");
      try {
        byte[] passphrase =
            new byte[] {
              -53, -103, 123, -53, -119, -12, -27, -82,
              3, -115, 119, -101, 86, 92, 92, 28
            };
        SecretKeySpec keySpec = new SecretKeySpec(passphrase, "AES");
        String CIPHER_TRANSFORMATION = "AES/ECB/PKCS5Padding";
        Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        Field keyringFileField = PersistenceManager.class.getDeclaredField("keyringFile");
        keyringFileField.setAccessible(true);
        File keyringFile = (File) keyringFileField.get(null);

        ContinuousSaveManager.disableKeyring();
        Keyring keyring = Keyring.getInstance();
        keyring.clearKeys();

        CsvReader reader = new CsvReader(keyringFile.getPath(), ',', Charset.forName("UTF-8"));
        reader.setUseComments(true);
        while (reader.readRecord()) {
          String gatewayName = reader.get(0);
          String login = reader.get(1);
          String password = reader.get(2);

          byte[] ciphertext = Base64.decodeBase64(password.getBytes("UTF-8"));
          byte[] cleartext = cipher.doFinal(ciphertext);
          password = new String(cleartext, "UTF-8");

          Tuple<String, String> key = new Tuple<String, String>(login, password);
          keyring.putKey(gatewayName, key);
        }
        reader.close();

        ContinuousSaveManager.enableKeyring();
      } catch (Exception ex) {
        logger.log(Level.SEVERE, "Updating keyring file failed", ex);
      }
    }

    // changes to 0.22.0
    if (Config.compareProgramVersions(version, "0.21") <= 0) {
      // transfer senderName and senderNumber settings

      Field configFileField = PersistenceManager.class.getDeclaredField("configFile");
      configFileField.setAccessible(true);
      File configFile = (File) configFileField.get(null);

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      XPathFactory xpf = XPathFactory.newInstance();
      XPath xpath = xpf.newXPath();

      Document doc = db.parse(configFile);
      String senderNumber = xpath.evaluate("//void[@property='senderNumber']/string", doc);
      String senderName = xpath.evaluate("//void[@property='senderName']/string", doc);

      Signature defaultSig = Signatures.getInstance().get(Signature.DEFAULT.getProfileName());
      if (StringUtils.isNotEmpty(senderName)) {
        defaultSig.setUserName(senderName);
      }
      if (StringUtils.isNotEmpty(senderNumber)) {
        defaultSig.setUserNumber(senderNumber);
      }
    }
  }