Esempio n. 1
0
  public synchronized void addTrustLevels(String name, TrustLevels tl)
      throws GTSInternalFault, InvalidTrustedAuthorityFault, IllegalTrustedAuthorityFault {
    if (tl != null) {
      String[] levels = tl.getTrustLevel();
      if ((levels != null) && (levels.length > 0)) {
        for (int i = 0; i < levels.length; i++) {
          if (!lookup.doesTrustLevelExist(levels[i])) {
            IllegalTrustedAuthorityFault fault = new IllegalTrustedAuthorityFault();
            fault.setFaultString(
                "The trust levels for the Trusted Authority "
                    + name
                    + " could not be updated, the trust level "
                    + levels[i]
                    + " does not exist.");
            throw fault;
          }
        }
      }
      removeTrustedAuthoritysTrustLevels(name);
      if ((levels != null) && (levels.length > 0)) {

        Connection c = null;
        try {
          c = db.getConnection();
          for (int i = 0; i < levels.length; i++) {
            PreparedStatement s =
                c.prepareStatement(
                    "INSERT INTO "
                        + TrustedAuthorityTrustLevelsTable.TABLE_NAME
                        + " SET "
                        + TrustedAuthorityTrustLevelsTable.NAME
                        + "= ?, "
                        + TrustedAuthorityTrustLevelsTable.TRUST_LEVEL
                        + "= ?");
            s.setString(1, name);
            s.setString(2, levels[i]);
            s.execute();
            s.close();
          }
        } catch (Exception e) {
          this.log.error(
              "Unexpected database error incurred in adding the trust levels for the Trusted Authority, "
                  + name
                  + ": "
                  + e.getMessage(),
              e);
          try {
            this.removeTrustedAuthoritysTrustLevels(name);
          } catch (Exception ex) {
            this.log.error(ex.getMessage(), ex);
          }
          GTSInternalFault fault = new GTSInternalFault();
          fault.setFaultString("Unexpected error removing the TrustedAuthority " + name);
          throw fault;
        } finally {
          db.releaseConnection(c);
        }
      }
    }
  }
Esempio n. 2
0
  public synchronized TrustedAuthority[] findTrustAuthorities(TrustedAuthorityFilter filter)
      throws GTSInternalFault {

    this.buildDatabase();
    Connection c = null;
    List authorities = new ArrayList();
    TrustedAuthoritySelectStatement select = new TrustedAuthoritySelectStatement();
    select.addSelectField("*");
    try {
      if (filter != null) {

        if (filter.getName() != null) {
          select.addWhereField(TrustedAuthorityTable.NAME, "=", filter.getName());
        }

        if (filter.getCertificateDN() != null) {
          select.addWhereField(
              TrustedAuthorityTable.CERTIFICATE_DN, "=", filter.getCertificateDN());
        }

        if (filter.getStatus() != null) {
          select.addWhereField(TrustedAuthorityTable.STATUS, "=", filter.getStatus().getValue());
        }

        if (filter.getIsAuthority() != null) {
          select.addWhereField(
              TrustedAuthorityTable.IS_AUTHORITY, "=", String.valueOf(filter.getIsAuthority()));
        }

        if (filter.getAuthorityGTS() != null) {
          select.addWhereField(TrustedAuthorityTable.AUTHORITY_GTS, "=", filter.getAuthorityGTS());
        }

        if (filter.getSourceGTS() != null) {
          select.addWhereField(TrustedAuthorityTable.SOURCE_GTS, "=", filter.getSourceGTS());
        }

        if (filter.getLifetime() != null) {
          if (filter.getLifetime().equals(Lifetime.Valid)) {
            Calendar cal = new GregorianCalendar();
            long time = cal.getTimeInMillis();
            select.addClause(
                "("
                    + TrustedAuthorityTable.EXPIRES
                    + "=0 OR "
                    + TrustedAuthorityTable.EXPIRES
                    + ">"
                    + time
                    + ")");
          } else if (filter.getLifetime().equals(Lifetime.Expired)) {
            Calendar cal = new GregorianCalendar();
            long time = cal.getTimeInMillis();
            select.addClause(
                "("
                    + TrustedAuthorityTable.EXPIRES
                    + "<>0 AND "
                    + TrustedAuthorityTable.EXPIRES
                    + "<"
                    + time
                    + ")");
          }
        }
      }

      c = db.getConnection();
      PreparedStatement s = select.prepareStatement(c);
      ResultSet rs = s.executeQuery();

      while (rs.next()) {
        String name = rs.getString(TrustedAuthorityTable.NAME);
        TrustLevels levels = filter.getTrustLevels();
        boolean okToAdd = true;
        if (levels != null) {
          String[] tl = levels.getTrustLevel();
          if (tl != null) {
            for (int i = 0; i < tl.length; i++) {
              if (!this.hasTrustLevels(name, tl[i])) {
                okToAdd = false;
                break;
              }
            }
          }
        }
        if (okToAdd) {
          TrustedAuthority ta = new TrustedAuthority();
          ta.setName(name);
          ta.setTrustLevels(getTrustLevels(name));
          ta.setStatus(Status.fromValue(rs.getString(TrustedAuthorityTable.STATUS)));
          ta.setIsAuthority(Boolean.valueOf(rs.getBoolean(TrustedAuthorityTable.IS_AUTHORITY)));
          ta.setAuthorityGTS(rs.getString(TrustedAuthorityTable.AUTHORITY_GTS));
          ta.setSourceGTS(rs.getString(TrustedAuthorityTable.SOURCE_GTS));
          ta.setExpires(rs.getLong(TrustedAuthorityTable.EXPIRES));
          ta.setLastUpdated(rs.getLong(TrustedAuthorityTable.LAST_UPDATED));
          ta.setCertificate(
              new gov.nih.nci.cagrid.gts.bean.X509Certificate(
                  rs.getString(TrustedAuthorityTable.CERTIFICATE)));
          String crl = rs.getString(TrustedAuthorityTable.CRL);
          if ((crl != null) && (crl.trim().length() > 0)) {
            ta.setCRL(new gov.nih.nci.cagrid.gts.bean.X509CRL(crl));
          }
          authorities.add(ta);
        }
      }
      rs.close();
      s.close();

      TrustedAuthority[] list = new TrustedAuthority[authorities.size()];
      for (int i = 0; i < authorities.size(); i++) {
        list[i] = (TrustedAuthority) authorities.get(i);
      }
      return list;

    } catch (Exception e) {
      this.log.error(
          "Unexpected database error incurred in finding trusted authorities: " + e.getMessage(),
          e);
      GTSInternalFault fault = new GTSInternalFault();
      fault.setFaultString("Unexpected error occurred in finding Trusted Authorities");
      throw fault;
    } finally {
      db.releaseConnection(c);
    }
  }