List of usage examples for org.springframework.core.io Resource getFilename
@Nullable String getFilename();
From source file:org.springframework.web.servlet.resource.GzipResourceResolver.java
@Override public Resource resolveResource(HttpServletRequest request, String requestPath, List<Resource> locations, ResourceResolverChain chain) {// w w w . j a v a 2s. c o m Resource resource = chain.resolveResource(request, requestPath, locations); if ((resource == null) || !isGzipAccepted(request)) { return resource; } try { Resource gzipped = new GzippedResource(resource); if (gzipped.exists()) { return gzipped; } } catch (IOException e) { logger.trace("No gzipped resource for [" + resource.getFilename() + "]", e); } return resource; }
From source file:org.springframework.xd.dirt.rest.ModulesController.java
/** * Retrieve the configuration file for the provided module information. * /* w w w.ja v a 2s . c o m*/ * @param name the name of an existing resource (required) * @param type the type of the module (required) */ @RequestMapping(value = "/{type}/{name}/definition", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE) @ResponseStatus(HttpStatus.OK) @ResponseBody public Resource downloadDefinition(@PathVariable("type") ModuleType type, @PathVariable("name") String name) { final ModuleDefinition definition = this.compositeModuleDefinitionService.getModuleDefinitionRepository() .findByNameAndType(name, type); if (definition == null) { throw new NoSuchModuleException(name, type); } final Resource resource = definition.getResource(); try { if (logger.isWarnEnabled() && resource.getFile().length() == 0) { logger.warn(String.format("The length of the file '%s' for module '%s' (%s) is zero.", resource.getFilename(), definition.getName(), definition.getType().name())); } } catch (IOException e) { throw new IllegalStateException( "Unable to return the file for the provided resource: " + resource.getFilename(), e); } return resource; }
From source file:org.springframework.yarn.fs.DefaultResourceLocalizer.java
/** * Gets the destination path./*from ww w . j a va 2 s. c o m*/ * * @param entry the entry * @param res the res * @return the destination path * @throws IOException */ private Path getDestinationPath(CopyEntry entry, Resource res) throws IOException { Path dest = null; Path resolvedStagingDirectory = resolveStagingDirectory(); if (entry.staging) { if (StringUtils.hasText(entry.dest)) { dest = new Path(resolvedStagingDirectory, entry.dest); } else { dest = new Path(resolvedStagingDirectory, res.getFilename()); } } else { dest = new Path(entry.dest, res.getFilename()); } if (log.isDebugEnabled()) { log.debug("Copy for resource=[" + res + "] dest=[" + dest + "]" + " resolvedStagingDirectory=" + resolvedStagingDirectory); } return dest; }
From source file:org.squashtest.tm.api.report.spring.view.docxtemplater.DocxTemplaterDocxView.java
@Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { for (String aTemplatePath : templatePath) { Resource resource = getApplicationContext().getResource(aTemplatePath); try {//from ww w . ja v a 2s .c o m InputStream inputStream = resource.getInputStream(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + resource.getFilename()); IOUtils.copy(inputStream, response.getOutputStream()); response.flushBuffer(); inputStream.close(); break; } catch (Exception e) { LOGGER.debug("file don't exist" + resource.getFilename()); } } }
From source file:org.squashtest.tm.web.internal.context.ReloadableSquashTmMessageSource.java
private void addLookedUpBasenames(Set<String> consolidatedBasenames) { try {// w w w .j a va 2s . c o m Resource[] resources = resourcePatternResolver.getResources(PLUGIN_MESSAGES_SCAN_PATTERN); for (Resource resource : resources) { if (isFirstLevelDirectory(resource)) { String basename = MESSAGES_BASE_PATH + resource.getFilename() + "/messages"; consolidatedBasenames.add(basename); LOGGER.info("Registering *discovered* path {} as a basename for application MessageSource", basename); } } } catch (IOException e) { LOGGER.info( "Error during bean initialization, no fragment messages will be registered. Maybe there are no fragments.", e); } }
From source file:org.squashtest.tm.web.internal.context.ReloadableSquashTmMessageSource.java
private boolean isFirstLevelDirectory(Resource resource) throws IOException { if (!resource.exists()) { return false; }//from www . j a v a 2 s.c om // in runtime env we do not work with file (exception) so we have to use URLs URL url = resource.getURL(); return url.getPath().endsWith(MESSAGES_BASE_PATH + resource.getFilename() + '/'); }
From source file:org.teiid.spring.autoconfigure.TeiidAutoConfiguration.java
@Bean @ConditionalOnMissingBean/*from w ww .j ava2s .c o m*/ public VDBMetaData teiidVDB() { List<Resource> resources = TeiidInitializer.getScripts("teiid.vdb-file", this.properties.getVdbFile(), "teiid.ddl", this.context); VDBMetaData vdb = null; if (!resources.isEmpty()) { for (Resource resource : resources) { if (resource.getFilename().endsWith(".ddl")) { try { DeploymentBasedDatabaseStore store = new DeploymentBasedDatabaseStore(new VDBRepository()); String db = "CREATE DATABASE " + VDBNAME + " VERSION '" + VDBVERSION + "';\n"; db = db + "USE DATABASE " + VDBNAME + " VERSION '" + VDBVERSION + "';\n"; db = db + ObjectConverterUtil.convertToString(resources.get(0).getInputStream()); vdb = store.getVDBMetadata(db); logger.info("Predefined VDB found :" + resources.get(0).getFilename()); } catch (FileNotFoundException e) { // no-op } catch (IOException e) { throw new IllegalStateException("Failed to parse the VDB defined"); } break; } else if (resource.getFilename().endsWith("-vdb.xml")) { try { vdb = VDBMetadataParser.unmarshell(resources.get(0).getInputStream()); } catch (XMLStreamException | IOException e) { throw new IllegalStateException("Failed to load the VDB defined", e); } } else if (resource.getFilename().endsWith(".vdb")) { try { vdb = loadVDB(new File(resource.getFilename()).toURI().toURL()); } catch (VirtualDatabaseException | ConnectorManagerException | TranslatorException | IOException | URISyntaxException e) { throw new IllegalStateException("Failed to load the VDB defined", e); } } } } if (vdb == null) { vdb = new VDBMetaData(); vdb.addProperty("implicit", "true"); vdb.setName(VDBNAME); vdb.setVersion(VDBVERSION); } return vdb; }
From source file:org.thingsboard.demo.loader.data.DemoData.java
private List<String> getResourceFiles(String path) throws IOException { List<String> filenames = new ArrayList<>(); ClassLoader cl = this.getClass().getClassLoader(); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl); Resource[] resources = resolver.getResources("classpath*:" + path + "/*.json"); for (Resource resource : resources) { filenames.add(path + "/" + resource.getFilename()); }//from www . j av a 2s . co m return filenames; }
From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalUserDataServiceImpl.java
@Override public void afterPropertiesSet() throws Exception { log.debug("Load Resource from Location {}", properties.getLocation()); Resource resource = resourceLoader.getResource(properties.getLocation()); Validate.notNull(resource);// w w w . j a v a 2s . com Validate.isTrue(resource.exists()); Validate.isTrue(resource.isReadable()); log.debug("Read the Resource {} to the Class {}", resource.getFilename(), OwncloudLocalUserData.class.getName()); OwncloudLocalUserData resourceData = xmlMapper.readValue(resource.getInputStream(), OwncloudLocalUserData.class); checkGroupReferences(resourceData); log.trace("Clear the Users Map"); users.clear(); log.debug("Read the Users as a Map"); if (CollectionUtils.isNotEmpty(resourceData.getUsers())) { for (OwncloudLocalUserData.User user : resourceData.getUsers()) { users.put(user.getUsername(), user); } } log.trace("Clear the Groups Map"); groups.clear(); log.debug("Read the Groups as a Map"); if (CollectionUtils.isNotEmpty(resourceData.getGroups())) { groups.addAll(resourceData.getGroups()); } log.info("User Information from Resource Location {} successfully loaded", properties.getLocation()); }
From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalUserDataServiceImpl.java
@Override public void destroy() throws Exception { log.debug("Load Resource from Location {}", properties.getLocation()); Resource resource = resourceLoader.getResource(properties.getLocation()); if (!(resource instanceof UrlResource)) { log.debug("Resource {} is not of Type {}. Can't synchronize changed Data", resource.getFilename(), UrlResource.class.getName()); return;//from ww w. j a v a 2 s.co m } OwncloudLocalUserData resourceData = new OwncloudLocalUserData(); log.debug("Add Users to the Synchronization Structure {}", OwncloudLocalUserData.class.getName()); resourceData.setUsers(users.values()); log.debug("Add Groups to the Synchronization Structure {}", OwncloudLocalUserData.class.getName()); resourceData.setGroups(groups); File file = resource.getFile(); log.info("Save changed Data to Resource {}", resource.getFilename()); try (OutputStream output = new BufferedOutputStream(new FileOutputStream(file))) { xmlMapper.writeValue(output, resourceData); } }