Search in sources :

Example 11 with ConfigurationException

use of org.opencastproject.util.ConfigurationException in project opencast by opencast.

the class CatalogUIAdapterConfiguration method loadXmlNSContext.

/**
 * Load the XML namespace bindings from the configuration and build the XML namespace context.
 */
private void loadXmlNSContext() {
    final Enumeration<String> keys = configProperties.keys();
    final Map<String, String> prefixToUri = new HashMap<String, String>();
    while (keys.hasMoreElements()) {
        final String key = keys.nextElement();
        if (key.startsWith(XML_BINDING_KEY_PREFIX)) {
            // First, we need to get the name of the binding
            final String nsBindingName = getXmlBindingNameFromConfigKey(key);
            // Once we have the name, we're able to retrieve the URI as well as the prefix
            final String nsUri = (String) configProperties.get(XML_BINDING_KEY_PREFIX + nsBindingName + XML_BINDING_URI_SUFFIX);
            final String nsPrefix = (String) configProperties.get(XML_BINDING_KEY_PREFIX + nsBindingName + XML_BINDING_PREFIX_SUFFIX);
            // Check if URI and the prefix have valid values
            if (isBlank(nsUri))
                throw new ConfigurationException(format("No URI for namespace binding '%s' found", nsBindingName));
            if (nsPrefix == null)
                throw new ConfigurationException(format("No prefix for namespace binding '%s' found", nsBindingName));
            // Add prefix & URI to the intermediate map
            prefixToUri.put(nsPrefix, nsUri);
        }
    }
    xmlNSContext = new XmlNamespaceContext(prefixToUri);
}
Also used : XmlNamespaceContext(org.opencastproject.util.XmlNamespaceContext) HashMap(java.util.HashMap) ConfigurationException(org.opencastproject.util.ConfigurationException)

Example 12 with ConfigurationException

use of org.opencastproject.util.ConfigurationException in project opencast by opencast.

the class EncodingProfileScanner method loadProfile.

/**
 * Reads the profile from the given properties
 *
 * @param profile
 * @param properties
 * @param artifact
 * @return the loaded profile or null if profile
 * @throws RuntimeException
 */
private EncodingProfile loadProfile(String profile, Properties properties, File artifact) throws ConfigurationException {
    List<String> defaultProperties = new ArrayList<>(10);
    String name = getDefaultProperty(profile, PROP_NAME, properties, defaultProperties);
    if (name == null || "".equals(name))
        throw new ConfigurationException("Distribution profile '" + profile + "' is missing a name (" + PROP_NAME + "). (Check web.xml profiles.)");
    EncodingProfileImpl df = new EncodingProfileImpl(profile, name, artifact);
    // Output Type
    String type = getDefaultProperty(profile, PROP_OUTPUT, properties, defaultProperties);
    if (StringUtils.isBlank(type))
        throw new ConfigurationException("Output type (" + PROP_OUTPUT + ") of profile '" + profile + "' is missing");
    try {
        df.setOutputType(MediaType.parseString(StringUtils.trimToEmpty(type)));
    } catch (IllegalArgumentException e) {
        throw new ConfigurationException("Output type (" + PROP_OUTPUT + ") '" + type + "' of profile '" + profile + "' is unknwon");
    }
    // Suffixes with tags?
    List<String> tags = getTags(profile, properties, defaultProperties);
    if (tags.size() > 0) {
        for (String tag : tags) {
            String prop = PROP_SUFFIX + "." + tag;
            String suffixObj = getDefaultProperty(profile, prop, properties, defaultProperties);
            df.setSuffix(tag, StringUtils.trim(suffixObj));
        }
    } else {
        // Suffix old stile, without tags
        String suffixObj = getDefaultProperty(profile, PROP_SUFFIX, properties, defaultProperties);
        if (StringUtils.isBlank(suffixObj))
            throw new ConfigurationException("Suffix (" + PROP_SUFFIX + ") of profile '" + profile + "' is missing");
        df.setSuffix(StringUtils.trim(suffixObj));
    }
    // Mimetype
    String mimeTypeObj = getDefaultProperty(profile, PROP_MIMETYPE, properties, defaultProperties);
    if (StringUtils.isNotBlank(mimeTypeObj)) {
        MimeType mimeType;
        try {
            mimeType = MimeTypes.parseMimeType(mimeTypeObj);
        } catch (Exception e) {
            throw new ConfigurationException("Mime type (" + PROP_MIMETYPE + ") " + mimeTypeObj + " could not be parsed as a mime type! Expressions are not allowed!");
        }
        df.setMimeType(mimeType.toString());
    }
    // Applicable to the following track categories
    String applicableObj = getDefaultProperty(profile, PROP_APPLICABLE, properties, defaultProperties);
    if (StringUtils.isBlank(applicableObj))
        throw new ConfigurationException("Input type (" + PROP_APPLICABLE + ") of profile '" + profile + "' is missing");
    df.setApplicableType(MediaType.parseString(StringUtils.trimToEmpty(applicableObj)));
    String jobLoad = getDefaultProperty(profile, PROP_JOBLOAD, properties, defaultProperties);
    if (!StringUtils.isBlank(jobLoad)) {
        df.setJobLoad(Float.valueOf(jobLoad));
        logger.debug("Setting job load for profile {} to {}", profile, jobLoad);
    }
    // Look for extensions
    String extensionKey = PROP_PREFIX + profile + ".";
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
        String key = entry.getKey().toString();
        if (key.startsWith(extensionKey) && !defaultProperties.contains(key)) {
            String k = key.substring(extensionKey.length());
            String v = StringUtils.trimToEmpty(entry.getValue().toString());
            df.addExtension(k, v);
        }
    }
    return df;
}
Also used : ConfigurationException(org.opencastproject.util.ConfigurationException) ArrayList(java.util.ArrayList) EncodingProfileImpl(org.opencastproject.composer.api.EncodingProfileImpl) HashMap(java.util.HashMap) Map(java.util.Map) MimeType(org.opencastproject.util.MimeType) ConfigurationException(org.opencastproject.util.ConfigurationException) IOException(java.io.IOException)

Example 13 with ConfigurationException

use of org.opencastproject.util.ConfigurationException in project opencast by opencast.

the class EncodingProfileScanner method loadFromProperties.

/**
 * Reads the profiles from the given set of properties.
 *
 * @param artifact
 *          the properties file
 * @return the profiles found in the properties
 */
Map<String, EncodingProfile> loadFromProperties(File artifact) throws IOException {
    // Format name
    FileInputStream in = null;
    Properties properties = new Properties();
    try {
        in = new FileInputStream(artifact);
        properties.load(in);
    } finally {
        IOUtils.closeQuietly(in);
    }
    // Find list of formats in properties
    List<String> profileNames = new ArrayList<>();
    for (Object fullKey : properties.keySet()) {
        String key = fullKey.toString();
        if (key.startsWith(PROP_PREFIX) && key.endsWith(PROP_NAME)) {
            int separatorLocation = fullKey.toString().lastIndexOf('.');
            key = key.substring(PROP_PREFIX.length(), separatorLocation);
            if (!profileNames.contains(key)) {
                profileNames.add(key);
            } else {
                throw new ConfigurationException("Found duplicate definition for encoding profile '" + key + "'");
            }
        }
    }
    // Load the formats
    Map<String, EncodingProfile> profiles = new HashMap<String, EncodingProfile>();
    for (String profileId : profileNames) {
        logger.debug("Enabling media format " + profileId);
        EncodingProfile profile = loadProfile(profileId, properties, artifact);
        profiles.put(profileId, profile);
    }
    return profiles;
}
Also used : ConfigurationException(org.opencastproject.util.ConfigurationException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EncodingProfile(org.opencastproject.composer.api.EncodingProfile) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Aggregations

ConfigurationException (org.opencastproject.util.ConfigurationException)13 Test (org.junit.Test)5 MalformedURLException (java.net.MalformedURLException)4 XPath (javax.xml.xpath.XPath)4 XPathExpressionException (javax.xml.xpath.XPathExpressionException)4 Document (org.w3c.dom.Document)4 HashMap (java.util.HashMap)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URISyntaxException (java.net.URISyntaxException)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Properties (java.util.Properties)1 Scanner (java.util.Scanner)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1