Example usage for org.springframework.core.io UrlResource getInputStream

List of usage examples for org.springframework.core.io UrlResource getInputStream

Introduction

In this page you can find the example usage for org.springframework.core.io UrlResource getInputStream.

Prototype

@Override
public InputStream getInputStream() throws IOException 

Source Link

Document

This implementation opens an InputStream for the given URL.

Usage

From source file:com.collabnet.ccf.core.recovery.HospitalArtifactReplayer.java

@SuppressWarnings("deprecation")
private void loadBeanDefinitionsFromUrl(String url, GenericApplicationContext context) {
    BeanDefinitionReader reader = null;/*from www.j  a  va  2 s.c  o m*/
    if (url.endsWith(".xml")) {
        reader = new XmlBeanDefinitionReader(context);
    } else if (url.endsWith(".properties")) {
        reader = new PropertiesBeanDefinitionReader(context);
    }

    if (reader != null) {
        try {
            UrlResource urlResource = new UrlResource(url);
            InputStream is = urlResource.getInputStream();
            Document document = builder.parse(is);
            Element routerElement = this.getRouterElement(document);
            this.stripOffProcessors(routerElement);
            this.addGAImportComponents(document, routerElement);
            DOMImplementationRegistry registry = null;
            try {
                registry = DOMImplementationRegistry.newInstance();
            } catch (ClassCastException e) {
                log.error("error", e);
                throw new CCFRuntimeException("error", e);
            } catch (ClassNotFoundException e) {
                log.error("error", e);
                throw new CCFRuntimeException("error", e);
            } catch (InstantiationException e) {
                log.error("error", e);
                throw new CCFRuntimeException("error", e);
            } catch (IllegalAccessException e) {
                log.error("error", e);
                throw new CCFRuntimeException("error", e);
            }
            String originalConfigFileAbsolutePath = urlResource.getFile().getAbsolutePath();
            String componentName = entry.getSourceComponent();
            String configComponentIdentifier = "{" + originalConfigFileAbsolutePath + "}" + componentName;

            File outputFile = null;
            if (componentConfigFileMap.containsKey(configComponentIdentifier)) {
                outputFile = componentConfigFileMap.get(configComponentIdentifier);
            } else {
                outputFile = File.createTempFile(componentName, ".xml", replayWorkDir);
                DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
                LSSerializer writer = impl.createLSSerializer();
                LSOutput output = impl.createLSOutput();
                FileOutputStream bas = new FileOutputStream(outputFile.getAbsolutePath());
                output.setByteStream(bas);
                writer.write(document, output);
                bas.flush();
                bas.close();
                componentConfigFileMap.put(configComponentIdentifier, outputFile);
            }

            // FIXME Use of deprecated method
            UrlResource newUrlResource = new UrlResource(outputFile.toURL().toString());
            ((XmlBeanDefinitionReader) reader).registerBeanDefinitions(document, newUrlResource);
        } catch (BeansException e) {
            log.error("error", e);
            throw new RuntimeException("BeansException : " + e.getMessage(), e);
        } catch (MalformedURLException e) {
            log.error("error", e);
            throw new RuntimeException("MalformedUrlException : " + e.getMessage(), e);
        } catch (IOException e) {
            log.error("error", e);
            throw new RuntimeException("IOExceptionException : " + e.getMessage(), e);
        } catch (SAXException e) {
            log.error("error", e);
            throw new RuntimeException("SAXException : " + e.getMessage(), e);
        }
    } else {
        throw new RuntimeException("No BeanDefinitionReader associated with " + url);
    }
}

From source file:org.jahia.modules.tools.taglibs.GroovyConsoleHelper.java

/**
 * Returns a generated HTML with form elements for the script parameters.
 * //from  ww  w .  ja v a2  s  .c om
 * @param scriptURI
 * @param request
 * @return
 */
public static String getScriptCustomFormElements(String scriptURI, HttpServletRequest request) {
    if (StringUtils.isBlank(scriptURI)) {
        return StringUtils.EMPTY;
    }
    final StringBuilder sb = new StringBuilder();
    try {
        final UrlResource resource = new UrlResource(
                StringUtils.substringBeforeLast(scriptURI, ".groovy") + ".properties");
        if (resource.exists()) {
            final Properties confs = new Properties();
            confs.load(resource.getInputStream());
            final String[] paramNames = StringUtils
                    .split(confs.getProperty("script.parameters.names", "").replaceAll("\\s", ""), ",");
            for (String paramName : paramNames) {
                generateFormElement(paramName.trim(), sb, confs, request);
            }

        }
    } catch (IOException e) {
        logger.error("An error occured while reading the configurations for the script " + scriptURI, e);
        return StringUtils.EMPTY;
    }
    final String formElements = sb.toString();
    if (StringUtils.isBlank(formElements)) {
        return StringUtils.EMPTY;
    }
    return sb.delete(0, sb.length()).append("<fieldset><legend>Script configuration</legend>")
            .append(formElements).append("</fieldset>").toString();
}

From source file:org.jahia.modules.tools.taglibs.GroovyConsoleHelper.java

/**
 * Returns an array of parameter names for the specified script or <code>null</code> if the script has no parameters.
 * /*w w  w  .jav a2 s.c  om*/
 * @param scriptURI the script URI to get parameter names for
 * @return an array of parameter names for the specified script or <code>null</code> if the script has no parameters
 */
public static String[] getScriptParamNames(String scriptURI) {
    try {
        final UrlResource resource = new UrlResource(
                StringUtils.substringBeforeLast(scriptURI, ".groovy") + ".properties");
        if (resource.exists()) {
            final Properties confs = new Properties();
            confs.load(resource.getInputStream());
            return StringUtils.split(confs.getProperty("script.parameters.names", "").replaceAll("\\s", ""),
                    ",");
        }
    } catch (IOException e) {
        logger.error("An error occured while reading the configurations for the script " + scriptURI, e);
    }
    return null;
}

From source file:org.jahia.modules.ugp.showcase.persistence.DataInitializer.java

private List<String> readInitialData() throws IOException {
    UrlResource dataResource = new UrlResource(bundle.getEntry("/META-INF/users.csv"));
    try (InputStream is = dataResource.getInputStream()) {
        return IOUtils.readLines(is);
    }//  ww w.ja v a  2s.c  o  m
}

From source file:org.kuali.kfs.module.tem.batch.PerDiemXmlInputFileType.java

/**
 * @see org.kuali.kfs.sys.batch.XmlBatchInputFileTypeBase#validateContentsAgainstSchema(java.lang.String, java.io.InputStream)
 */// w  w  w . ja va 2s .c  o  m
@Override
protected void validateContentsAgainstSchema(String schemaLocation, InputStream fileContents)
        throws ParseException {

    try {
        // get schemaFile
        UrlResource schemaResource = new UrlResource(schemaLocation);

        // load a WXS schema, represented by a Schema instance
        Source schemaSource = new StreamSource(schemaResource.getInputStream());

        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(schemaSource);

        // create a validator instance, which can be used to validate an instance document
        Validator validator = schema.newValidator();
        validator.setErrorHandler(new XmlErrorHandler());

        Source source = this.transform(fileContents);
        validator.validate(source);
    } catch (MalformedURLException e2) {
        LOG.error("error getting schema url: " + e2.getMessage());
        throw new RuntimeException("error getting schema url:  " + e2.getMessage(), e2);
    } catch (SAXException e) {
        LOG.error("error encountered while parsing xml " + e.getMessage());
        throw new ParseException("Schema validation error occured while processing file: " + e.getMessage(), e);
    } catch (IOException e1) {
        LOG.error("error occured while validating file contents: " + e1.getMessage());
        throw new RuntimeException("error occurred while validating file contents: " + e1.getMessage(), e1);
    }
}

From source file:org.kuali.ole.sys.batch.XmlBatchInputFileTypeBase.java

/**
 * Validates the xml contents against the batch input type schema using the java 1.5 validation package.
 * //from  ww w  .jav  a  2s . c  om
 * @param schemaLocation - location of the schema file
 * @param fileContents - xml contents to validate against the schema
 */
protected void validateContentsAgainstSchema(String schemaLocation, InputStream fileContents)
        throws ParseException {
    // create a SchemaFactory capable of understanding WXS schemas
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    // get schemaFile
    UrlResource schemaResource = null;
    try {
        schemaResource = new UrlResource(schemaLocation);
    } catch (MalformedURLException e2) {
        LOG.error("error getting schema url: " + e2.getMessage());
        throw new RuntimeException("error getting schema url:  " + e2.getMessage(), e2);
    }

    // load a WXS schema, represented by a Schema instance
    Source schemaSource = null;
    try {
        schemaSource = new StreamSource(schemaResource.getInputStream());
    } catch (IOException e2) {
        LOG.error("error getting schema stream from url: " + e2.getMessage());
        throw new RuntimeException("error getting schema stream from url:   " + e2.getMessage(), e2);
    }

    Schema schema = null;
    try {
        schema = factory.newSchema(schemaSource);
    } catch (SAXException e) {
        LOG.error("error occured while setting schema file: " + e.getMessage());
        throw new RuntimeException("error occured while setting schema file: " + e.getMessage(), e);
    }

    // create a Validator instance, which can be used to validate an instance document
    Validator validator = schema.newValidator();
    validator.setErrorHandler(new XmlErrorHandler());

    // validate
    try {
        validator.validate(new StreamSource(fileContents));
    } catch (SAXException e) {
        LOG.error("error encountered while parsing xml " + e.getMessage());
        throw new ParseException("Schema validation error occured while processing file: " + e.getMessage(), e);
    } catch (IOException e1) {
        LOG.error("error occured while validating file contents: " + e1.getMessage());
        throw new RuntimeException("error occured while validating file contents: " + e1.getMessage(), e1);
    }
}