Java Code Examples for org.springframework.core.io.Resource#getFile()

The following examples show how to use org.springframework.core.io.Resource#getFile() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public Set<String> getResourcePaths(String path) {
	String actualPath = (path.endsWith("/") ? path : path + "/");
	Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath));
	try {
		File file = resource.getFile();
		String[] fileList = file.list();
		if (ObjectUtils.isEmpty(fileList)) {
			return null;
		}
		Set<String> resourcePaths = new LinkedHashSet<String>(fileList.length);
		for (String fileEntry : fileList) {
			String resultPath = actualPath + fileEntry;
			if (resource.createRelative(fileEntry).getFile().isDirectory()) {
				resultPath += "/";
			}
			resourcePaths.add(resultPath);
		}
		return resourcePaths;
	}
	catch (IOException ex) {
		logger.warn("Couldn't get resource paths for " + resource, ex);
		return null;
	}
}
 
Example 2
@Test
public void marshalAttachments() throws Exception {
	marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(BinaryObject.class);
	marshaller.setMtomEnabled(true);
	marshaller.afterPropertiesSet();
	MimeContainer mimeContainer = mock(MimeContainer.class);

	Resource logo = new ClassPathResource("spring-ws.png", getClass());
	DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));

	given(mimeContainer.convertToXopPackage()).willReturn(true);
	byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream());
	BinaryObject object = new BinaryObject(bytes, dataHandler);
	StringWriter writer = new StringWriter();
	marshaller.marshal(object, new StreamResult(writer), mimeContainer);
	assertTrue("No XML written", writer.toString().length() > 0);
	verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class));
}
 
Example 3
Source Project: java-technology-stack   File: DataBufferUtils.java    License: MIT License 6 votes vote down vote up
/**
 * Read the given {@code Resource} into a {@code Flux} of {@code DataBuffer}s
 * starting at the given position.
 * <p>If the resource is a file, it is read into an
 * {@code AsynchronousFileChannel} and turned to {@code Flux} via
 * {@link #readAsynchronousFileChannel(Callable, DataBufferFactory, int)} or else
 * fall back on {@link #readByteChannel(Callable, DataBufferFactory, int)}.
 * Closes the channel when the flux is terminated.
 * @param resource the resource to read from
 * @param position the position to start reading from
 * @param dataBufferFactory the factory to create data buffers with
 * @param bufferSize the maximum size of the data buffers
 * @return a flux of data buffers read from the given channel
 */
public static Flux<DataBuffer> read(
		Resource resource, long position, DataBufferFactory dataBufferFactory, int bufferSize) {

	try {
		if (resource.isFile()) {
			File file = resource.getFile();
			return readAsynchronousFileChannel(
					() -> AsynchronousFileChannel.open(file.toPath(), StandardOpenOption.READ),
					position, dataBufferFactory, bufferSize);
		}
	}
	catch (IOException ignore) {
		// fallback to resource.readableChannel(), below
	}

	Flux<DataBuffer> result = readByteChannel(resource::readableChannel, dataBufferFactory, bufferSize);
	return position == 0 ? result : skipUntilByteCount(result, position);
}
 
Example 4
@Test
public void marshalAttachments() throws Exception {
	marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(BinaryObject.class);
	marshaller.setMtomEnabled(true);
	marshaller.afterPropertiesSet();
	MimeContainer mimeContainer = mock(MimeContainer.class);

	Resource logo = new ClassPathResource("spring-ws.png", getClass());
	DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));

	given(mimeContainer.convertToXopPackage()).willReturn(true);
	byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream());
	BinaryObject object = new BinaryObject(bytes, dataHandler);
	StringWriter writer = new StringWriter();
	marshaller.marshal(object, new StreamResult(writer), mimeContainer);
	assertTrue("No XML written", writer.toString().length() > 0);
	verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class));
}
 
Example 5
@Test
public void marshalAttachments() throws Exception {
	unmarshaller = new Jaxb2Marshaller();
	unmarshaller.setClassesToBeBound(BinaryObject.class);
	unmarshaller.setMtomEnabled(true);
	unmarshaller.afterPropertiesSet();
	MimeContainer mimeContainer = mock(MimeContainer.class);

	Resource logo = new ClassPathResource("spring-ws.png", getClass());
	DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));

	given(mimeContainer.isXopPackage()).willReturn(true);
	given(mimeContainer.getAttachment("<[email protected]://springframework.org/spring-ws>")).willReturn(dataHandler);
	given(mimeContainer.getAttachment("<[email protected]://springframework.org/spring-ws>")).willReturn(dataHandler);
	given(mimeContainer.getAttachment("[email protected]")).willReturn(dataHandler);
	String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" +
			"<xop:Include href='cid:[email protected]://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
			"</bytes>" + "<dataHandler>" +
			"<xop:Include href='cid:[email protected]://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
			"</dataHandler>" +
			"<swaDataHandler>[email protected]</swaDataHandler>" +
			"</binaryObject>";

	StringReader reader = new StringReader(content);
	Object result = unmarshaller.unmarshal(new StreamSource(reader), mimeContainer);
	assertTrue("Result is not a BinaryObject", result instanceof BinaryObject);
	BinaryObject object = (BinaryObject) result;
	assertNotNull("bytes property not set", object.getBytes());
	assertTrue("bytes property not set", object.getBytes().length > 0);
	assertNotNull("datahandler property not set", object.getSwaDataHandler());
}
 
Example 6
public static File[] getFiles(List<String> paths) {
   List<Resource> rs = makeResources(paths);
   File[] files = new File[rs.size()];
   for (int i = 0; i < rs.size(); i++) {
      Resource r = rs.get(i);
      try {
         files[i] = r.getFile();
      } catch (IOException e) {
         throw new RuntimeException("Failed to get file for: " + r.getFilename(), e);
      }
   }
   return files;
}
 
Example 7
@Test
public void marshalAttachments() throws Exception {
	unmarshaller = new Jaxb2Marshaller();
	unmarshaller.setClassesToBeBound(BinaryObject.class);
	unmarshaller.setMtomEnabled(true);
	unmarshaller.afterPropertiesSet();
	MimeContainer mimeContainer = mock(MimeContainer.class);

	Resource logo = new ClassPathResource("spring-ws.png", getClass());
	DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));

	given(mimeContainer.isXopPackage()).willReturn(true);
	given(mimeContainer.getAttachment("<[email protected]://springframework.org/spring-ws>")).willReturn(dataHandler);
	given(mimeContainer.getAttachment("<[email protected]://springframework.org/spring-ws>")).willReturn(dataHandler);
	given(mimeContainer.getAttachment("[email protected]")).willReturn(dataHandler);
	String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" +
			"<xop:Include href='cid:[email protected]://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
			"</bytes>" + "<dataHandler>" +
			"<xop:Include href='cid:[email protected]://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
			"</dataHandler>" +
			"<swaDataHandler>[email protected]</swaDataHandler>" +
			"</binaryObject>";

	StringReader reader = new StringReader(content);
	Object result = unmarshaller.unmarshal(new StreamSource(reader), mimeContainer);
	assertTrue("Result is not a BinaryObject", result instanceof BinaryObject);
	BinaryObject object = (BinaryObject) result;
	assertNotNull("bytes property not set", object.getBytes());
	assertTrue("bytes property not set", object.getBytes().length > 0);
	assertNotNull("datahandler property not set", object.getSwaDataHandler());
}
 
Example 8
Source Project: genie   File: ApisAutoConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get the jobs dir as a Spring Resource. Will create if it doesn't exist.
 *
 * @param resourceLoader The resource loader to use
 * @param jobsProperties The jobs properties to use
 * @return The job dir as a resource
 * @throws IOException on error reading or creating the directory
 */
@Bean
@ConditionalOnMissingBean(name = "jobsDir", value = Resource.class)
public Resource jobsDir(
    final ResourceLoader resourceLoader,
    final JobsProperties jobsProperties
) throws IOException {
    final String jobsDirLocation = jobsProperties.getLocations().getJobs().toString();
    final Resource tmpJobsDirResource = resourceLoader.getResource(jobsDirLocation);
    if (tmpJobsDirResource.exists() && !tmpJobsDirResource.getFile().isDirectory()) {
        throw new IllegalStateException(jobsDirLocation + " exists but isn't a directory. Unable to continue");
    }

    // We want the resource to end in a slash for use later in the generation of URL's
    final String slash = "/";
    String localJobsDir = jobsDirLocation;
    if (!jobsDirLocation.endsWith(slash)) {
        localJobsDir = localJobsDir + slash;
    }
    final Resource jobsDirResource = resourceLoader.getResource(localJobsDir);

    if (!jobsDirResource.exists()) {
        final File file = jobsDirResource.getFile();
        if (!file.mkdirs()) {
            throw new IllegalStateException(
                "Unable to create jobs directory " + jobsDirLocation + " and it doesn't exist."
            );
        }
    }

    return jobsDirResource;
}
 
Example 9
@Override
@Nullable
public Set<String> getResourcePaths(String path) {
	String actualPath = (path.endsWith("/") ? path : path + "/");
	Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath));
	try {
		File file = resource.getFile();
		String[] fileList = file.list();
		if (ObjectUtils.isEmpty(fileList)) {
			return null;
		}
		Set<String> resourcePaths = new LinkedHashSet<>(fileList.length);
		for (String fileEntry : fileList) {
			String resultPath = actualPath + fileEntry;
			if (resource.createRelative(fileEntry).getFile().isDirectory()) {
				resultPath += "/";
			}
			resourcePaths.add(resultPath);
		}
		return resourcePaths;
	}
	catch (IOException ex) {
		if (logger.isWarnEnabled()) {
			logger.warn("Could not get resource paths for " + resource, ex);
		}
		return null;
	}
}
 
Example 10
Source Project: QuickProject   File: GlobalConfig.java    License: Apache License 2.0 5 votes vote down vote up
private File getFileByConfig(String configParamName, String configPath) throws GlobalConfigException {
    Resource resource = resourceLoader.getResource(configPath);
    if (!resource.exists()) {
        throw new GlobalConfigException(configParamName, "路径不存在");
    }
    try {
        return resource.getFile();
    } catch (IOException e) {
        throw new GlobalConfigException(configParamName, e);
    }
}
 
Example 11
@Override
protected Object parse(Resource resource, Class<?> pluginConfigClass)
        throws Exception{
    InputStream input = new FileInputStream(resource.getFile());
    YAMLParser yamlParser = yamlFactory.createParser(input);
    final JsonNode node = objectMapper.readTree(yamlParser);
    if(node == null){
        return pluginConfigClass.newInstance();
    }
    TreeTraversingParser treeTraversingParser = new TreeTraversingParser(node);
    return objectMapper.readValue(treeTraversingParser, pluginConfigClass);
}
 
Example 12
Source Project: genie   File: JobCompletionService.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param dataServices      The {@link DataServices} instance to use
 * @param jobArchiveService An implementation of {@link JobArchiveService}
 * @param genieWorkingDir   The working directory where all job directories are created.
 * @param mailServiceImpl   An implementation of the mail service.
 * @param registry          The metrics registry to use
 * @param jobsProperties    The properties relating to running jobs
 * @param retryTemplate     Retry template for retrying remote calls
 * @throws GenieException if there is a problem
 */
public JobCompletionService(
    final DataServices dataServices,
    final JobArchiveService jobArchiveService,
    final Resource genieWorkingDir,
    final MailService mailServiceImpl,
    final MeterRegistry registry,
    final JobsProperties jobsProperties,
    @NotNull final RetryTemplate retryTemplate
) throws GenieException {
    this.persistenceService = dataServices.getPersistenceService();
    this.jobArchiveService = jobArchiveService;
    this.mailServiceImpl = mailServiceImpl;
    this.deleteDependencies = jobsProperties.getCleanup().isDeleteDependencies();
    this.runAsUserEnabled = jobsProperties.getUsers().isRunAsUserEnabled();

    this.executor = new DefaultExecutor();
    this.executor.setStreamHandler(new PumpStreamHandler(null, null));

    try {
        this.baseWorkingDir = genieWorkingDir.getFile();
    } catch (IOException gse) {
        throw new GenieServerException("Could not load the base path from resource", gse);
    }

    // Set up the metrics
    this.registry = registry;
    // Retry template
    this.retryTemplate = retryTemplate;
    this.jobCompletionHandlingLocksMap = new ConcurrentHashMap<>();
}
 
Example 13
/**
 * Determine a FreeMarker TemplateLoader for the given path.
 * <p>Default implementation creates either a FileTemplateLoader or
 * a SpringTemplateLoader.
 * @param templateLoaderPath the path to load templates from
 * @return an appropriate TemplateLoader
 * @see freemarker.cache.FileTemplateLoader
 * @see SpringTemplateLoader
 */
protected TemplateLoader getTemplateLoaderForPath(String templateLoaderPath) {
	if (isPreferFileSystemAccess()) {
		// Try to load via the file system, fall back to SpringTemplateLoader
		// (for hot detection of template changes, if possible).
		try {
			Resource path = getResourceLoader().getResource(templateLoaderPath);
			File file = path.getFile();  // will fail if not resolvable in the file system
			if (logger.isDebugEnabled()) {
				logger.debug(
						"Template loader path [" + path + "] resolved to file path [" + file.getAbsolutePath() + "]");
			}
			return new FileTemplateLoader(file);
		}
		catch (IOException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Cannot resolve template loader path [" + templateLoaderPath +
						"] to [java.io.File]: using SpringTemplateLoader as fallback", ex);
			}
			return new SpringTemplateLoader(getResourceLoader(), templateLoaderPath);
		}
	}
	else {
		// Always load via SpringTemplateLoader (without hot detection of template changes).
		logger.debug("File system access not preferred: using SpringTemplateLoader");
		return new SpringTemplateLoader(getResourceLoader(), templateLoaderPath);
	}
}
 
Example 14
public static File getFile(String path) {
   Resource r = getResource(path);
   File f = null;
   try {
      f = r.getFile();
   } catch (IOException e) {
      throw new RuntimeException("Failed to get file for: " + r.getFilename(), e);
   }
   return f;
}
 
Example 15
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
	try {
		ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;
		Resource logo = new ClassPathResource("spring.png", ZeroCopyIntegrationTests.class);
		File logoFile = logo.getFile();
		zeroCopyResponse.getHeaders().setContentType(MediaType.IMAGE_PNG);
		zeroCopyResponse.getHeaders().setContentLength(logoFile.length());
		return zeroCopyResponse.writeWith(logoFile, 0, logoFile.length());
	}
	catch (Throwable ex) {
		return Mono.error(ex);
	}
}
 
Example 16
@Test
public void marshalAttachments() throws Exception {
	unmarshaller = new Jaxb2Marshaller();
	unmarshaller.setClassesToBeBound(BinaryObject.class);
	unmarshaller.setMtomEnabled(true);
	unmarshaller.afterPropertiesSet();
	MimeContainer mimeContainer = mock(MimeContainer.class);

	Resource logo = new ClassPathResource("spring-ws.png", getClass());
	DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));

	given(mimeContainer.isXopPackage()).willReturn(true);
	given(mimeContainer.getAttachment("<[email protected]://springframework.org/spring-ws>")).willReturn(dataHandler);
	given(mimeContainer.getAttachment("<[email protected]://springframework.org/spring-ws>")).willReturn(dataHandler);
	given(mimeContainer.getAttachment("[email protected]")).willReturn(dataHandler);
	String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" +
			"<xop:Include href='cid:[email protected]://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
			"</bytes>" + "<dataHandler>" +
			"<xop:Include href='cid:[email protected]://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
			"</dataHandler>" +
			"<swaDataHandler>[email protected]</swaDataHandler>" +
			"</binaryObject>";

	StringReader reader = new StringReader(content);
	Object result = unmarshaller.unmarshal(new StreamSource(reader), mimeContainer);
	assertTrue("Result is not a BinaryObject", result instanceof BinaryObject);
	BinaryObject object = (BinaryObject) result;
	assertNotNull("bytes property not set", object.getBytes());
	assertTrue("bytes property not set", object.getBytes().length > 0);
	assertNotNull("datahandler property not set", object.getSwaDataHandler());
}
 
Example 17
Source Project: Cleanstone   File: ConsoleEndpoint.java    License: MIT License 5 votes vote down vote up
/**
 * Returns a reversed Output from the Logfile.
 *
 * @return
 * @throws IOException
 */
@ReadOperation
public List<String> getLog() throws IOException {
    Resource logFileResource = getLogFileResource();
    if (logFileResource == null || !logFileResource.isReadable()) {
        return null;
    }

    ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(logFileResource.getFile(), StandardCharsets.UTF_8);

    List<String> log = new ArrayList<>();

    for (int i = 0; i < 20; i++) {
        final String line = reversedLinesFileReader.readLine();

        if (line.startsWith("\t") || line.isEmpty()) { //Remove Stacktrace and Empty lines
            i--;
            continue;
        }

        log.add(line);
    }

    reversedLinesFileReader.close();

    return log;
}
 
Example 18
private boolean resourceParentIsDirectory(final Resource resource) throws IOException {
    return resource.getFile() != null && resource.getFile().getParentFile() != null && resource.getFile().getParentFile().isDirectory();
}
 
Example 19
private boolean resourceParentIsDirectory(final Resource resource) throws IOException {
    return resource.getFile() != null && resource.getFile().getParentFile() != null && resource.getFile().getParentFile().isDirectory();
}
 
Example 20
@Test
public void testStreamTimeLogAsHdfsResource() throws Exception {
	assertThat(context.containsBean("appDeployer"), is(true));
	assertThat(context.getBean("appDeployer"), instanceOf(YarnAppDeployer.class));
	AppDeployer deployer = context.getBean("appDeployer", AppDeployer.class);
	YarnCloudAppService yarnCloudAppService = context.getBean(YarnCloudAppService.class);

	MavenProperties m2Properties = new MavenProperties();
	Map<String, RemoteRepository> remoteRepositories = new HashMap<>();
	remoteRepositories.put("default", new RemoteRepository("https://repo.spring.io/libs-snapshot-local"));
	m2Properties.setRemoteRepositories(remoteRepositories);

	MavenResource timeResourceBase = new MavenResource.Builder(m2Properties)
			.artifactId("time-source")
			.groupId(GROUP_ID)
			.version(artifactVersion)
			.extension("jar")
			.classifier("exec")
			.build();

	MavenResource logResourceBase = new MavenResource.Builder(m2Properties)
			.artifactId("log-sink")
			.groupId(GROUP_ID)
			.version(artifactVersion)
			.extension("jar")
			.classifier("exec")
			.build();

	copyFile(timeResourceBase, "/dataflow/artifacts/repo/");
	copyFile(logResourceBase, "/dataflow/artifacts/repo/");

	@SuppressWarnings("resource")
	HdfsResourceLoader resourceLoader = new HdfsResourceLoader(getConfiguration());
	resourceLoader.setHandleNoprefix(true);
	Resource timeResource = resourceLoader.getResource("hdfs:/dataflow/artifacts/repo/time-source-1.0.0.BUILD-SNAPSHOT-exec.jar");
	Resource logResource = resourceLoader.getResource("hdfs:/dataflow/artifacts/repo/log-sink-1.0.0.BUILD-SNAPSHOT-exec.jar");

	Map<String, String> timeProperties = new HashMap<>();
	timeProperties.put("spring.cloud.stream.bindings.output.destination", "ticktock.0");
	AppDefinition timeDefinition = new AppDefinition("time", timeProperties);
	Map<String, String> timeEnvironmentProperties = new HashMap<>();
	timeEnvironmentProperties.put(AppDeployer.COUNT_PROPERTY_KEY, "1");
	timeEnvironmentProperties.put(AppDeployer.GROUP_PROPERTY_KEY, "ticktock");
	AppDeploymentRequest timeRequest = new AppDeploymentRequest(timeDefinition, timeResource, timeEnvironmentProperties);

	Map<String, String> logProperties = new HashMap<>();
	logProperties.put("spring.cloud.stream.bindings.input.destination", "ticktock.0");
	logProperties.put("expression", "new String(payload + ' hello')");
	AppDefinition logDefinition = new AppDefinition("log", logProperties);
	Map<String, String> logEnvironmentProperties = new HashMap<>();
	logEnvironmentProperties.put(AppDeployer.COUNT_PROPERTY_KEY, "1");
	logEnvironmentProperties.put(AppDeployer.GROUP_PROPERTY_KEY, "ticktock");
	AppDeploymentRequest logRequest = new AppDeploymentRequest(logDefinition, logResource, logEnvironmentProperties);


	String timeId = deployer.deploy(timeRequest);
	assertThat(timeId, notNullValue());

	ApplicationId applicationId = assertWaitApp(2, TimeUnit.MINUTES, yarnCloudAppService);
	assertWaitFileContent(2, TimeUnit.MINUTES, applicationId, "Started TimeSourceApplication");
	assertThat(deployer.status(timeId).getState(), is(DeploymentState.deployed));

	String logId = deployer.deploy(logRequest);
	assertThat(logId, notNullValue());

	assertWaitFileContent(1, TimeUnit.MINUTES, applicationId, "Started LogSinkApplication");
	assertThat(deployer.status(logId).getState(), is(DeploymentState.deployed));

	assertWaitFileContent(1, TimeUnit.MINUTES, applicationId, "hello");

	deployer.undeploy(timeId);
	assertWaitFileContent(2, TimeUnit.MINUTES, applicationId, "stopped outbound.ticktock.0");
	deployer.undeploy(logId);
	assertWaitFileContent(2, TimeUnit.MINUTES, applicationId, "stopped inbound.ticktock.0");

	assertThat(deployer.status(timeId).getState(), is(DeploymentState.unknown));
	assertThat(deployer.status(logId).getState(), is(DeploymentState.unknown));

	List<Resource> resources = ContainerLogUtils.queryContainerLogs(
			getYarnCluster(), applicationId);

	assertThat(resources, notNullValue());
	assertThat(resources.size(), is(6));

	for (Resource res : resources) {
		File file = res.getFile();
		String content = ContainerLogUtils.getFileContent(file);
		if (file.getName().endsWith("stdout")) {
			assertThat(file.length(), greaterThan(0l));
		} else if (file.getName().endsWith("Container.stderr")) {
			assertThat("stderr with content: " + content, file.length(), is(0l));
		}
	}
}