Search in sources :

Example 81 with StringJoiner

use of java.util.StringJoiner in project jdk8u_jdk by JetBrains.

the class PKIXMasterCertPathValidator method validate.

/**
     * Validates a certification path consisting exclusively of
     * <code>X509Certificate</code>s using the specified
     * <code>PKIXCertPathChecker</code>s. It is assumed that the
     * <code>PKIXCertPathChecker</code>s
     * have been initialized with any input parameters they may need.
     *
     * @param cpOriginal the original X509 CertPath passed in by the user
     * @param reversedCertList the reversed X509 CertPath (as a List)
     * @param certPathCheckers the PKIXCertPathCheckers
     * @throws CertPathValidatorException if cert path does not validate
     */
static void validate(CertPath cpOriginal, List<X509Certificate> reversedCertList, List<PKIXCertPathChecker> certPathCheckers) throws CertPathValidatorException {
    // we actually process reversedCertList, but we keep cpOriginal because
    // we need to return the original certPath when we throw an exception.
    // we will also need to modify the index appropriately when we
    // throw an exception.
    int cpSize = reversedCertList.size();
    if (debug != null) {
        debug.println("--------------------------------------------------" + "------------");
        debug.println("Executing PKIX certification path validation " + "algorithm.");
    }
    for (int i = 0; i < cpSize; i++) {
        /* The basic loop algorithm is that we get the
             * current certificate, we verify the current certificate using
             * information from the previous certificate and from the state,
             * and we modify the state for the next loop by setting the
             * current certificate of this loop to be the previous certificate
             * of the next loop. The state is initialized during first loop.
             */
        X509Certificate currCert = reversedCertList.get(i);
        if (debug != null) {
            debug.println("Checking cert" + (i + 1) + " - Subject: " + currCert.getSubjectX500Principal());
        }
        Set<String> unresCritExts = currCert.getCriticalExtensionOIDs();
        if (unresCritExts == null) {
            unresCritExts = Collections.<String>emptySet();
        }
        if (debug != null && !unresCritExts.isEmpty()) {
            StringJoiner joiner = new StringJoiner(", ", "{", "}");
            for (String oid : unresCritExts) {
                joiner.add(oid);
            }
            debug.println("Set of critical extensions: " + joiner.toString());
        }
        for (int j = 0; j < certPathCheckers.size(); j++) {
            PKIXCertPathChecker currChecker = certPathCheckers.get(j);
            if (debug != null) {
                debug.println("-Using checker" + (j + 1) + " ... [" + currChecker.getClass().getName() + "]");
            }
            if (i == 0)
                currChecker.init(false);
            try {
                currChecker.check(currCert, unresCritExts);
                if (debug != null) {
                    debug.println("-checker" + (j + 1) + " validation succeeded");
                }
            } catch (CertPathValidatorException cpve) {
                throw new CertPathValidatorException(cpve.getMessage(), (cpve.getCause() != null) ? cpve.getCause() : cpve, cpOriginal, cpSize - (i + 1), cpve.getReason());
            }
        }
        if (!unresCritExts.isEmpty()) {
            throw new CertPathValidatorException("unrecognized " + "critical extension(s)", null, cpOriginal, cpSize - (i + 1), PKIXReason.UNRECOGNIZED_CRIT_EXT);
        }
        if (debug != null)
            debug.println("\ncert" + (i + 1) + " validation succeeded.\n");
    }
    if (debug != null) {
        debug.println("Cert path validation succeeded. (PKIX validation " + "algorithm)");
        debug.println("-------------------------------------------------" + "-------------");
    }
}
Also used : CertPathValidatorException(java.security.cert.CertPathValidatorException) PKIXCertPathChecker(java.security.cert.PKIXCertPathChecker) X509Certificate(java.security.cert.X509Certificate) StringJoiner(java.util.StringJoiner)

Example 82 with StringJoiner

use of java.util.StringJoiner in project gerrit by GerritCodeReview.

the class OutgoingEmail method send.

/**
   * Format and enqueue the message for delivery.
   *
   * @throws EmailException
   */
public void send() throws EmailException {
    if (NotifyHandling.NONE.equals(notify) && accountsToNotify.isEmpty()) {
        return;
    }
    if (!args.emailSender.isEnabled()) {
        //
        return;
    }
    init();
    if (useHtml()) {
        appendHtml(soyHtmlTemplate("HeaderHtml"));
    }
    format();
    appendText(textTemplate("Footer"));
    if (useHtml()) {
        appendHtml(soyHtmlTemplate("FooterHtml"));
    }
    Set<Address> smtpRcptToPlaintextOnly = new HashSet<>();
    if (shouldSendMessage()) {
        if (fromId != null) {
            final Account fromUser = args.accountCache.get(fromId).getAccount();
            GeneralPreferencesInfo senderPrefs = fromUser.getGeneralPreferencesInfo();
            if (senderPrefs != null && senderPrefs.getEmailStrategy() == CC_ON_OWN_COMMENTS) {
                // If we are impersonating a user, make sure they receive a CC of
                // this message so they can always review and audit what we sent
                // on their behalf to others.
                //
                add(RecipientType.CC, fromId);
            } else if (!accountsToNotify.containsValue(fromId) && rcptTo.remove(fromId)) {
                // If they don't want a copy, but we queued one up anyway,
                // drop them from the recipient lists.
                //
                removeUser(fromUser);
            }
        }
        // In addition, check if users only want to receive plaintext email.
        for (Account.Id id : rcptTo) {
            Account thisUser = args.accountCache.get(id).getAccount();
            GeneralPreferencesInfo prefs = thisUser.getGeneralPreferencesInfo();
            if (prefs == null || prefs.getEmailStrategy() == DISABLED) {
                removeUser(thisUser);
            } else if (useHtml() && prefs.getEmailFormat() == EmailFormat.PLAINTEXT) {
                removeUser(thisUser);
                smtpRcptToPlaintextOnly.add(new Address(thisUser.getFullName(), thisUser.getPreferredEmail()));
            }
            if (smtpRcptTo.isEmpty() && smtpRcptToPlaintextOnly.isEmpty()) {
                return;
            }
        }
        // inbound email replies.
        if (!headers.containsKey("Reply-To")) {
            StringJoiner j = new StringJoiner(", ");
            if (fromId != null) {
                Address address = toAddress(fromId);
                if (address != null) {
                    j.add(address.getEmail());
                }
            }
            smtpRcptTo.stream().forEach(a -> j.add(a.getEmail()));
            smtpRcptToPlaintextOnly.stream().forEach(a -> j.add(a.getEmail()));
            setHeader("Reply-To", j.toString());
        }
        String textPart = textBody.toString();
        OutgoingEmailValidationListener.Args va = new OutgoingEmailValidationListener.Args();
        va.messageClass = messageClass;
        va.smtpFromAddress = smtpFromAddress;
        va.smtpRcptTo = smtpRcptTo;
        va.headers = headers;
        va.body = textPart;
        if (useHtml()) {
            va.htmlBody = htmlBody.toString();
        } else {
            va.htmlBody = null;
        }
        for (OutgoingEmailValidationListener validator : args.outgoingEmailValidationListeners) {
            try {
                validator.validateOutgoingEmail(va);
            } catch (ValidationException e) {
                return;
            }
        }
        if (!smtpRcptTo.isEmpty()) {
            // Send multipart message
            args.emailSender.send(va.smtpFromAddress, va.smtpRcptTo, va.headers, va.body, va.htmlBody);
        }
        if (!smtpRcptToPlaintextOnly.isEmpty()) {
            // Send plaintext message
            Map<String, EmailHeader> shallowCopy = new HashMap<>();
            shallowCopy.putAll(headers);
            // Remove To and Cc
            shallowCopy.remove(HDR_TO);
            shallowCopy.remove(HDR_CC);
            for (Address a : smtpRcptToPlaintextOnly) {
                // Add new To
                EmailHeader.AddressList to = new EmailHeader.AddressList();
                to.add(a);
                shallowCopy.put(HDR_TO, to);
            }
            args.emailSender.send(va.smtpFromAddress, smtpRcptToPlaintextOnly, shallowCopy, va.body);
        }
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ValidationException(com.google.gerrit.server.validators.ValidationException) Address(com.google.gerrit.server.mail.Address) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OutgoingEmailValidationListener(com.google.gerrit.server.validators.OutgoingEmailValidationListener) AddressList(com.google.gerrit.server.mail.send.EmailHeader.AddressList) GeneralPreferencesInfo(com.google.gerrit.extensions.client.GeneralPreferencesInfo) AddressList(com.google.gerrit.server.mail.send.EmailHeader.AddressList) StringJoiner(java.util.StringJoiner) HashSet(java.util.HashSet)

Example 83 with StringJoiner

use of java.util.StringJoiner in project jdk8u_jdk by JetBrains.

the class StringJoinerTest method stringFromtoString.

public void stringFromtoString() {
    StringJoiner sj = new StringJoiner(", ");
    assertEquals(sj.toString(), "");
    sj = new StringJoiner(",", "{", "}");
    assertEquals(sj.toString(), "{}");
    sj = new StringJoiner(",");
    sj.add(ONE);
    assertEquals(sj.toString(), ONE);
    sj.add(TWO);
    assertEquals(sj.toString(), ONE + "," + TWO);
    sj = new StringJoiner(",", "{--", "--}");
    sj.add(ONE);
    sj.add(TWO);
    assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");
}
Also used : StringJoiner(java.util.StringJoiner)

Example 84 with StringJoiner

use of java.util.StringJoiner in project jdk8u_jdk by JetBrains.

the class StringJoinerTest method testCombos.

private void testCombos(String infix, String prefix, String suffix) {
    StringJoiner sj = new StringJoiner(infix, prefix, suffix);
    assertEquals(sj.toString(), prefix + suffix);
    assertEquals(sj.toString().length(), sj.length());
    // EmptyValue
    sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");
    assertEquals(sj.toString(), "<NONE>");
    assertEquals(sj.toString().length(), sj.length());
    // empty in front
    sj.add("");
    assertEquals(sj.toString(), prefix + suffix);
    // empty in middle
    sj.add("");
    assertEquals(sj.toString(), prefix + infix + suffix);
    sj.add("1");
    assertEquals(sj.toString(), prefix + infix + infix + "1" + suffix);
    // empty at end
    sj.add("");
    assertEquals(sj.toString(), prefix + infix + infix + "1" + infix + suffix);
    sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");
    sj.add("1");
    assertEquals(sj.toString(), prefix + "1" + suffix);
    sj.add("2");
    assertEquals(sj.toString(), prefix + "1" + infix + "2" + suffix);
    sj.add("");
    assertEquals(sj.toString(), prefix + "1" + infix + "2" + infix + suffix);
    sj.add("3");
    assertEquals(sj.toString(), prefix + "1" + infix + "2" + infix + infix + "3" + suffix);
}
Also used : StringJoiner(java.util.StringJoiner)

Example 85 with StringJoiner

use of java.util.StringJoiner in project jdk8u_jdk by JetBrains.

the class StringJoinerTest method addAddAll.

public void addAddAll() {
    StringJoiner sj = new StringJoiner(DASH, "{", "}");
    sj.add(ONE);
    ArrayList<String> nextOne = new ArrayList<>();
    nextOne.add(TWO);
    nextOne.add(THREE);
    nextOne.stream().forEachOrdered(sj::add);
    String expected = "{" + ONE + DASH + TWO + DASH + THREE + "}";
    assertEquals(sj.toString(), expected);
}
Also used : ArrayList(java.util.ArrayList) StringJoiner(java.util.StringJoiner)

Aggregations

StringJoiner (java.util.StringJoiner)98 ArrayList (java.util.ArrayList)22 List (java.util.List)11 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 Map (java.util.Map)6 HashMap (java.util.HashMap)4 Collectors (java.util.stream.Collectors)4 ClassName (com.squareup.javapoet.ClassName)3 FieldSpec (com.squareup.javapoet.FieldSpec)3 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)3 TypeName (com.squareup.javapoet.TypeName)3 TypeSpec (com.squareup.javapoet.TypeSpec)3 Expression (com.sri.ai.expresso.api.Expression)3 Attribute (io.requery.meta.Attribute)3 Field (java.lang.reflect.Field)3 Scanner (java.util.Scanner)3 RaptorColumnHandle (com.facebook.presto.raptor.RaptorColumnHandle)2 Range (com.facebook.presto.spi.predicate.Range)2 MaterializedResult (com.facebook.presto.testing.MaterializedResult)2