Search in sources :

Example 1 with MessageFormat

use of java.text.MessageFormat in project tomcat by apache.

the class Localizer method getMessage.

/*
     * Returns the localized error message corresponding to the given error
     * code.
     *
     * If the given error code is not defined in the resource bundle for
     * localized error messages, it is used as the error message.
     *
     * @param errCode Error code to localize
     * @param args Arguments for parametric replacement
     *
     * @return Localized error message
     */
public static String getMessage(String errCode, Object[] args) {
    String errMsg = errCode;
    try {
        errMsg = bundle.getString(errCode);
        if (args != null && args.length > 0) {
            MessageFormat formatter = new MessageFormat(errMsg);
            errMsg = formatter.format(args);
        }
    } catch (MissingResourceException e) {
    }
    return errMsg;
}
Also used : MessageFormat(java.text.MessageFormat) MissingResourceException(java.util.MissingResourceException)

Example 2 with MessageFormat

use of java.text.MessageFormat in project checkstyle by checkstyle.

the class CheckUtil method getCheckMessage.

/**
     * Gets the check message 'as is' from appropriate 'messages.properties'
     * file.
     *
     * @param locale the locale to get the message for.
     * @param messageKey the key of message in 'messages*.properties' file.
     * @param arguments the arguments of message in 'messages*.properties' file.
     * @return the check's formatted message.
     */
public static String getCheckMessage(Class<?> module, Locale locale, String messageKey, Object... arguments) {
    final Properties pr = new Properties();
    try {
        if (locale == Locale.ENGLISH) {
            pr.load(module.getResourceAsStream("messages.properties"));
        } else {
            pr.load(module.getResourceAsStream("messages_" + locale.getLanguage() + ".properties"));
        }
    } catch (IOException ex) {
        return null;
    }
    final MessageFormat formatter = new MessageFormat(pr.getProperty(messageKey), locale);
    return formatter.format(arguments);
}
Also used : MessageFormat(java.text.MessageFormat) IOException(java.io.IOException) Properties(java.util.Properties)

Example 3 with MessageFormat

use of java.text.MessageFormat in project checkstyle by checkstyle.

the class BaseCheckTestSupport method getCheckMessage.

/**
     * Gets the check message 'as is' from appropriate 'messages.properties'
     * file.
     *
     * @param messageKey the key of message in 'messages.properties' file.
     * @param arguments  the arguments of message in 'messages.properties' file.
     */
protected String getCheckMessage(Class<? extends AbstractViolationReporter> aClass, String messageKey, Object... arguments) {
    final Properties pr = new Properties();
    try {
        pr.load(aClass.getResourceAsStream("messages.properties"));
    } catch (IOException ex) {
        return null;
    }
    final MessageFormat formatter = new MessageFormat(pr.getProperty(messageKey), Locale.ROOT);
    return formatter.format(arguments);
}
Also used : MessageFormat(java.text.MessageFormat) IOException(java.io.IOException) Properties(java.util.Properties)

Example 4 with MessageFormat

use of java.text.MessageFormat in project midpoint by Evolveum.

the class ResourceValidatorImpl method getString.

@NotNull
private String getString(ResourceBundle bundle, String key, Object... parameters) {
    final String resolvedKey;
    if (key != null) {
        if (bundle.containsKey(key)) {
            resolvedKey = bundle.getString(key);
        } else {
            resolvedKey = key;
        }
    } else {
        resolvedKey = "";
    }
    final MessageFormat format = new MessageFormat(resolvedKey, bundle.getLocale());
    return format.format(parameters);
}
Also used : MessageFormat(java.text.MessageFormat) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with MessageFormat

use of java.text.MessageFormat in project otertool by wuntee.

the class JarSigner method signJar.

void signJar(String jarName, String alias, String[] args) throws Exception {
    boolean aliasUsed = false;
    X509Certificate tsaCert = null;
    if (sigfile == null) {
        sigfile = alias;
        aliasUsed = true;
    }
    if (sigfile.length() > 8) {
        sigfile = sigfile.substring(0, 8).toUpperCase();
    } else {
        sigfile = sigfile.toUpperCase();
    }
    StringBuilder tmpSigFile = new StringBuilder(sigfile.length());
    for (int j = 0; j < sigfile.length(); j++) {
        char c = sigfile.charAt(j);
        if (!((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '-') || (c == '_'))) {
            if (aliasUsed) {
                // convert illegal characters from the alias to be _'s
                c = '_';
            } else {
                throw new RuntimeException(rb.getString("signature filename must consist of the following characters: A-Z, 0-9, _ or -"));
            }
        }
        tmpSigFile.append(c);
    }
    sigfile = tmpSigFile.toString();
    String tmpJarName;
    if (signedjar == null)
        tmpJarName = jarName + ".sig";
    else
        tmpJarName = signedjar;
    File jarFile = new File(jarName);
    File signedJarFile = new File(tmpJarName);
    // Open the jar (zip) file
    try {
        zipFile = new ZipFile(jarName);
    } catch (IOException ioe) {
        error(rb.getString("unable to open jar file: ") + jarName, ioe);
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(signedJarFile);
    } catch (IOException ioe) {
        error(rb.getString("unable to create: ") + tmpJarName, ioe);
    }
    PrintStream ps = new PrintStream(fos);
    ZipOutputStream zos = new ZipOutputStream(ps);
    /* First guess at what they might be - we don't xclude RSA ones. */
    String sfFilename = (META_INF + sigfile + ".SF").toUpperCase();
    String bkFilename = (META_INF + sigfile + ".DSA").toUpperCase();
    Manifest manifest = new Manifest();
    Map<String, Attributes> mfEntries = manifest.getEntries();
    // The Attributes of manifest before updating
    Attributes oldAttr = null;
    boolean mfModified = false;
    boolean mfCreated = false;
    byte[] mfRawBytes = null;
    try {
        MessageDigest[] digests = { MessageDigest.getInstance(digestalg) };
        // Check if manifest exists
        ZipEntry mfFile;
        if ((mfFile = getManifestFile(zipFile)) != null) {
            // Manifest exists. Read its raw bytes.
            mfRawBytes = getBytes(zipFile, mfFile);
            manifest.read(new ByteArrayInputStream(mfRawBytes));
            oldAttr = (Attributes) (manifest.getMainAttributes().clone());
        } else {
            // Create new manifest
            Attributes mattr = manifest.getMainAttributes();
            mattr.putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0");
            String javaVendor = System.getProperty("java.vendor");
            String jdkVersion = System.getProperty("java.version");
            mattr.putValue("Created-By", jdkVersion + " (" + javaVendor + ")");
            mfFile = new ZipEntry(JarFile.MANIFEST_NAME);
            mfCreated = true;
        }
        /*
             * For each entry in jar
             * (except for signature-related META-INF entries),
             * do the following:
             *
             * - if entry is not contained in manifest, add it to manifest;
             * - if entry is contained in manifest, calculate its hash and
             *   compare it with the one in the manifest; if they are
             *   different, replace the hash in the manifest with the newly
             *   generated one. (This may invalidate existing signatures!)
             */
        BASE64Encoder encoder = new JarBASE64Encoder();
        Vector<ZipEntry> mfFiles = new Vector<ZipEntry>();
        for (Enumeration<? extends ZipEntry> enum_ = zipFile.entries(); enum_.hasMoreElements(); ) {
            ZipEntry ze = enum_.nextElement();
            if (ze.getName().startsWith(META_INF)) {
                // Store META-INF files in vector, so they can be written
                // out first
                mfFiles.addElement(ze);
                if (signatureRelated(ze.getName())) {
                    // ignore signature-related and manifest files
                    continue;
                }
            }
            if (manifest.getAttributes(ze.getName()) != null) {
                // possibly update its digest attributes
                if (updateDigests(ze, zipFile, digests, encoder, manifest) == true) {
                    mfModified = true;
                }
            } else if (!ze.isDirectory()) {
                // Add entry to manifest
                Attributes attrs = getDigestAttributes(ze, zipFile, digests, encoder);
                mfEntries.put(ze.getName(), attrs);
                mfModified = true;
            }
        }
        // Recalculate the manifest raw bytes if necessary
        if (mfModified) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            manifest.write(baos);
            byte[] newBytes = baos.toByteArray();
            if (mfRawBytes != null && oldAttr.equals(manifest.getMainAttributes())) {
                /*
                     * Note:
                     *
                     * The Attributes object is based on HashMap and can handle
                     * continuation columns. Therefore, even if the contents are 
                     * not changed (in a Map view), the bytes that it write() 
                     * may be different from the original bytes that it read()
                     * from. Since the signature on the main attributes is based 
                     * on raw bytes, we must retain the exact bytes.
                     */
                int newPos = findHeaderEnd(newBytes);
                int oldPos = findHeaderEnd(mfRawBytes);
                if (newPos == oldPos) {
                    System.arraycopy(mfRawBytes, 0, newBytes, 0, oldPos);
                } else {
                    // cat oldHead newTail > newBytes
                    byte[] lastBytes = new byte[oldPos + newBytes.length - newPos];
                    System.arraycopy(mfRawBytes, 0, lastBytes, 0, oldPos);
                    System.arraycopy(newBytes, newPos, lastBytes, oldPos, newBytes.length - newPos);
                    newBytes = lastBytes;
                }
            }
            mfRawBytes = newBytes;
        }
        // Write out the manifest
        if (mfModified) {
            // manifest file has new length
            mfFile = new ZipEntry(JarFile.MANIFEST_NAME);
        }
        zos.putNextEntry(mfFile);
        zos.write(mfRawBytes);
        // Calculate SignatureFile (".SF") and SignatureBlockFile
        ManifestDigester manDig = new ManifestDigester(mfRawBytes);
        SignatureFile sf = new SignatureFile(digests, manifest, manDig, sigfile, signManifest);
        if (tsaAlias != null) {
            tsaCert = getTsaCert(tsaAlias);
        }
        SignatureFile.Block block = null;
        try {
            block = sf.generateBlock(privateKey, sigalg, certChain, externalSF, tsaUrl, tsaCert, signingMechanism, args, zipFile);
        } catch (SocketTimeoutException e) {
            // Provide a helpful message when TSA is beyond a firewall
            error(rb.getString("unable to sign jar: ") + rb.getString("no response from the Timestamping Authority. ") + rb.getString("When connecting from behind a firewall then an HTTP proxy may need to be specified. ") + rb.getString("Supply the following options to jarsigner: ") + "\n  -J-Dhttp.proxyHost=<hostname> " + "\n  -J-Dhttp.proxyPort=<portnumber> ", e);
        }
        sfFilename = sf.getMetaName();
        bkFilename = block.getMetaName();
        ZipEntry sfFile = new ZipEntry(sfFilename);
        ZipEntry bkFile = new ZipEntry(bkFilename);
        long time = System.currentTimeMillis();
        sfFile.setTime(time);
        bkFile.setTime(time);
        // signature file
        zos.putNextEntry(sfFile);
        sf.write(zos);
        // signature block file
        zos.putNextEntry(bkFile);
        block.write(zos);
        // vector
        for (int i = 0; i < mfFiles.size(); i++) {
            ZipEntry ze = mfFiles.elementAt(i);
            if (!ze.getName().equalsIgnoreCase(JarFile.MANIFEST_NAME) && !ze.getName().equalsIgnoreCase(sfFilename) && !ze.getName().equalsIgnoreCase(bkFilename)) {
                writeEntry(zipFile, zos, ze);
            }
        }
        // Write out all other files
        for (Enumeration<? extends ZipEntry> enum_ = zipFile.entries(); enum_.hasMoreElements(); ) {
            ZipEntry ze = enum_.nextElement();
            if (!ze.getName().startsWith(META_INF)) {
                writeEntry(zipFile, zos, ze);
            }
        }
    } catch (IOException ioe) {
        error(rb.getString("unable to sign jar: ") + ioe, ioe);
    } finally {
        // close the resouces
        if (zipFile != null) {
            zipFile.close();
            zipFile = null;
        }
        if (zos != null) {
            zos.close();
        }
    }
    // try {
    if (signedjar == null) {
        // one, then delete the original.
        if (!signedJarFile.renameTo(jarFile)) {
            File origJar = new File(jarName + ".orig");
            if (jarFile.renameTo(origJar)) {
                if (signedJarFile.renameTo(jarFile)) {
                    origJar.delete();
                } else {
                    MessageFormat form = new MessageFormat(rb.getString("attempt to rename signedJarFile to jarFile failed"));
                    Object[] source = { signedJarFile, jarFile };
                    error(form.format(source));
                }
            } else {
                MessageFormat form = new MessageFormat(rb.getString("attempt to rename jarFile to origJar failed"));
                Object[] source = { jarFile, origJar };
                error(form.format(source));
            }
        }
    }
    if (hasExpiredCert || hasExpiringCert || notYetValidCert || badKeyUsage || badExtendedKeyUsage || badNetscapeCertType) {
        logger.warn(rb.getString("Warning: "));
        if (badKeyUsage) {
            logger.warn(rb.getString("The signer certificate's KeyUsage extension doesn't allow code signing."));
        }
        if (badExtendedKeyUsage) {
            logger.warn(rb.getString("The signer certificate's ExtendedKeyUsage extension doesn't allow code signing."));
        }
        if (badNetscapeCertType) {
            logger.warn(rb.getString("The signer certificate's NetscapeCertType extension doesn't allow code signing."));
        }
        if (hasExpiredCert) {
            logger.warn(rb.getString("The signer certificate has expired."));
        } else if (hasExpiringCert) {
            logger.warn(rb.getString("The signer certificate will expire within six months."));
        } else if (notYetValidCert) {
            logger.warn(rb.getString("The signer certificate is not yet valid."));
        }
    }
// no IOException thrown in the above try clause, so disable
// the catch clause.
// } catch(IOException ioe) {
//     error(rb.getString("unable to sign jar: ")+ioe, ioe);
// }
}
Also used : ZipEntry(java.util.zip.ZipEntry) Attributes(java.util.jar.Attributes) MessageDigest(java.security.MessageDigest) Vector(java.util.Vector) PrintStream(java.io.PrintStream) MessageFormat(java.text.MessageFormat) BASE64Encoder(sun.misc.BASE64Encoder) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) X509Certificate(java.security.cert.X509Certificate) SocketTimeoutException(java.net.SocketTimeoutException) ZipFile(java.util.zip.ZipFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ManifestDigester(sun.security.util.ManifestDigester) FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

MessageFormat (java.text.MessageFormat)693 LogMessage (org.apache.qpid.server.logging.LogMessage)105 CertificateException (java.security.cert.CertificateException)52 KeyStoreException (java.security.KeyStoreException)48 IOException (java.io.IOException)47 UnrecoverableKeyException (java.security.UnrecoverableKeyException)46 UnrecoverableEntryException (java.security.UnrecoverableEntryException)43 CertStoreException (java.security.cert.CertStoreException)43 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)33 X509Certificate (java.security.cert.X509Certificate)30 ArrayList (java.util.ArrayList)28 ResourceBundle (java.util.ResourceBundle)26 Date (java.util.Date)23 Locale (java.util.Locale)22 File (java.io.File)21 Certificate (java.security.cert.Certificate)19 MissingResourceException (java.util.MissingResourceException)17 PrivateKey (java.security.PrivateKey)16 Format (java.text.Format)16 HashMap (java.util.HashMap)15