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

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

Introduction

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

Prototype

public UrlResource(String path) throws MalformedURLException 

Source Link

Document

Create a new UrlResource based on a URL path.

Usage

From source file:org.atricore.idbus.kernel.planning.jbpm.ProcessFragmentRegistryApplicationContext.java

@Override
protected Resource[] getConfigResources() {

    List<Resource> resources = new ArrayList<Resource>();

    try {/*from www  .j  a  v  a  2  s.c  o  m*/
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
                Thread.currentThread().getContextClassLoader());

        Collections.addAll(resources, resolver.getResources(DEFAULT_JBPM_FRAGMENT_CFG_FILE));

        Resource[] exts = resolver.getResources(DEFAULT_JBPM_EXT_FRAGMENT_CFG_FILE);
        for (Resource r : exts) {
            InputStream is = r.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = rd.readLine();
            while (line != null) {
                if (!"".equals(line)) {
                    resources.add(resolver.getResource(line));
                }
                line = rd.readLine();
            }
            is.close();
        }

    } catch (IOException ex) {
        // ignore
    }

    if (null == cfgFiles) {
        cfgFiles = new String[] { DEFAULT_PROCESS_DESCRIPTOR_FILE };
    }

    for (String cfgFile : cfgFiles) {
        boolean found = false;
        Resource cpr = new ClassPathResource(cfgFile);
        if (!cpr.exists()) {
            try {
                //see if it's a URL
                URL url = new URL(cfgFile);
                cpr = new UrlResource(url);
                if (cpr.exists()) {
                    resources.add(cpr);
                    found = true;
                }
            } catch (MalformedURLException e) {
                //ignore
            }
            if (!found) {
                //try loading it our way
                URL url = getResource(cfgFile, this.getClass());
                if (url != null) {
                    cpr = new UrlResource(url);
                    if (cpr.exists()) {
                        resources.add(cpr);
                        found = true;
                    }
                }
            }
        } else {
            resources.add(cpr);
            found = true;
        }
        if (!found) {
            logger.warn("No Process Descriptor found: " + cfgFile);
        }
    }

    if (null != cfgFileURLs) {
        for (URL cfgFileURL : cfgFileURLs) {
            UrlResource ur = new UrlResource(cfgFileURL);
            if (ur.exists()) {
                resources.add(ur);
            } else {
                logger.warn("No Process Descriptor found: " + cfgFileURL);
            }
        }
    }

    logger.info("Creating application context with resources: " + resources);

    if (0 == resources.size()) {
        return null;
    }

    Resource[] res = new Resource[resources.size()];
    res = resources.toArray(res);
    return res;
}

From source file:org.codehaus.groovy.grails.commons.GrailsResourceUtils.java

public static Resource getViewsDir(Resource resource) {
    if (resource == null)
        return null;

    try {//  w  w  w.  j  a va 2 s.co  m
        Resource appDir = getAppDir(resource);
        return new UrlResource(appDir.getURL().toString() + "/views");
    } catch (IOException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error reading URL whilst resolving views dir from [" + resource + "]: " + e.getMessage(),
                    e);
        }
        return null;
    }
}

From source file:org.codehaus.groovy.grails.commons.GrailsResourceUtils.java

public static Resource getAppDir(Resource resource) {
    if (resource == null)
        return null;

    try {/*w  ww.  j a v  a  2s  .c o m*/
        String url = resource.getURL().toString();

        int i = url.lastIndexOf(GRAILS_APP_DIR);
        if (i > -1) {
            url = url.substring(0, i + 10);
            return new UrlResource(url);
        }

        return null;
    } catch (MalformedURLException e) {
        return null;
    } catch (IOException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error reading URL whilst resolving app dir from [" + resource + "]: " + e.getMessage(),
                    e);
        }
        return null;
    }
}

From source file:org.codehaus.groovy.grails.compiler.GrailsClassLoader.java

protected Resource loadGroovySource(String name) throws MalformedURLException {
    URL resourceURL = grailsResourceLoader.loadGroovySource(name);
    if (resourceURL == null) {
        return null;
    }//w ww .j  a  va  2s.  c  o m

    return new UrlResource(resourceURL);
}

From source file:org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine.java

/**
 * Creates a Template for the given URL/*from   ww  w  .java  2  s  . c  o  m*/
 *
 * @param url The URL to use to construct the template with
 * @return A Groovy Template instance
 *
 * @throws CompilationFailedException When an error occured compiling the Template
 * @throws ClassNotFoundException When a Class cannot be found within the given Template
 * @throws IOException When a I/O Exception occurs reading the Template
 */
@Override
public Template createTemplate(URL url) throws CompilationFailedException, ClassNotFoundException, IOException {
    return createTemplate(new UrlResource(url));
}

From source file:org.dataconservancy.ui.dcpmap.DataSetMapper.java

/**
 * Maps a File (from a DataItem) to a DcsFile; includes the generation of fixity.  TODO: format.
 *
 * @param dataFile the data file to map//from ww w  . ja  v a 2s .  c  o  m
 * @return the DcsFile containing the DataFile
 * @throws DcpMappingException if there is an error performing the mapping
 */
private DcsFile mapDataFile(DataFile dataFile) throws DcpMappingException {
    DcsFile dcsFile = new DcsFile();

    dcsFile.setId(dataFile.getId());
    // Map File name and source to DcsFile name and source
    dcsFile.setSource(dataFile.getSource());
    dcsFile.setName(dataFile.getName());

    // add ui generated format to use as mime type: identify this format by setting version to "dcs-ui"
    // we need this version specified to correctly do the inverse mapping
    if (dataFile.getFormat() != null && !dataFile.getFormat().isEmpty()) {//have to check this, some DcsFormat fields
        //may not be null or empty
        DcsFormat format = new DcsFormat();
        format.setName(dataFile.getFormat());
        format.setFormat(dataFile.getFormat());
        format.setSchemeUri("http://www.iana.org/assignments/media-types/");
        format.setVersion("dcs-ui");
        dcsFile.addFormat(format);
    }

    // Mapped DcsFiles automatically have extant set to 'true'
    dcsFile.setExtant(true);

    // Map the business id to an alternate id.
    dcsFile.addAlternateId(
            new DcsResourceIdentifier(Id.getAuthority(), dataFile.getId(), Types.DATA_FILE.name()));

    try {
        Resource r = new UrlResource(dcsFile.getSource());

        if (dataFile.getSize() > 0) {
            dcsFile.setSizeBytes(dataFile.getSize());
        } else {
            dcsFile.setSizeBytes(r.contentLength());
        }

        dcsFile.addFixity(calculateFixity(r.getInputStream(), MessageDigest.getInstance("MD5")));
        dcsFile.addFixity(calculateFixity(r.getInputStream(), MessageDigest.getInstance("SHA1")));
    } catch (Exception e) {
        throw new DcpMappingException("Error calculating file length or fixity: " + e.getMessage(), e);
    }

    return dcsFile;
}

From source file:org.dataconservancy.ui.it.ArchiveServiceIT.java

private DataFile addFile(DataItem ds, String name) throws Exception {
    DataFile file = new DataFile();
    file.setParentId(ds.getId());/*from  w w w.ja va  2s .c  o m*/

    CreateIdApiRequest fileIdRequest = reqFactory.createIdApiRequest(Types.DATA_FILE);
    file.setId(fileIdRequest.execute(hc));
    file.setName(name);

    java.io.File tmp = java.io.File.createTempFile(this.getClass().getName() + "-", ".txt");
    FileUtils.writeStringToFile(tmp, "ArchiveServiceIT temp file.");
    tmp.deleteOnExit();
    file.setSource(tmp.toURI().toString());
    file.setPath(System.getProperty("java.io.tmpdir"));

    Resource r = new UrlResource(file.getSource());
    file.setSize(r.contentLength());

    ds.addFile(file);

    return file;
}

From source file:org.dataconservancy.ui.it.ArchiveServiceIT.java

private MetadataFile addMetadataFile(String id, String name) throws IOException {
    final MetadataFile file = new MetadataFile();
    file.setId(id);/*from   w  w w  .j av a 2  s  .c  om*/
    file.setName(name);
    file.setMetadataFormatId("format:id");
    java.io.File tmp = java.io.File.createTempFile(this.getClass().getName() + "-", ".txt");
    tmp.deleteOnExit();
    file.setSource(tmp.toURI().toString());
    file.setPath(System.getProperty("java.io.tmpdir"));

    Resource r = new UrlResource(file.getSource());
    file.setSize(r.contentLength());

    return file;
}

From source file:org.flockdata.integration.FileProcessor.java

public Collection<String> resolveFiles(String source) throws IOException, NotFoundException {
    ArrayList<String> results = new ArrayList<>();
    boolean absoluteFile = true;
    if (source.contains("*") || source.contains("?") || source.endsWith(File.separator))
        absoluteFile = false;/*  ww w .  j  av a  2s  .  c  om*/

    if (absoluteFile) {
        Reader reader;
        reader = getReader(source);
        if (reader != null) {
            reader.close();
            results.add(source);
            return results;
        }
    }

    String filter;
    String path;

    Path toResolve = Paths.get(source);

    if (source.endsWith(File.separator))
        filter = "*";
    else
        filter = toResolve.getFileName().toString();
    if (filter == null)
        filter = "*";
    path = source.substring(0, source.lastIndexOf(File.separator) + 1);
    FileFilter fileFilter = new WildcardFileFilter(filter);

    // Split the source in to path and filter.
    //path = source.substring(0, source.indexOf())

    File folder = new UrlResource("file:" + path).getFile();
    File[] listOfFiles = folder.listFiles(fileFilter);
    if (listOfFiles == null) {
        folder = new ClassPathResource(path).getFile();
        listOfFiles = folder.listFiles(fileFilter);
    }

    for (File file : listOfFiles) {
        results.add(file.toString());
    }

    return results;
}

From source file:org.geoserver.geofence.AccessManagerConfigTest.java

@Override
protected void setUpInternal() throws Exception {
    super.setUpInternal();

    // get the beans we use for testing
    //        manager = (GeofenceAccessManager) applicationContext.getBean("geofenceRuleAccessManager");
    //        geofenceService = (RuleReaderService) applicationContext.getBean("ruleReaderService");
    manager = (GeoFenceConfigurationManager) applicationContext.getBean("geofenceConfigurationManager");

    configurer = (GeoFencePropertyPlaceholderConfigurer) applicationContext.getBean("geofence-configurer");
    configurer.setLocation(new UrlResource(this.getClass().getResource("/test-config.properties")));
}