Java Code Examples for org.springframework.util.ResourceUtils#getFile()

The following examples show how to use org.springframework.util.ResourceUtils#getFile() . 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
Source File: RedisSinkTest.java    From alchemy with Apache License 2.0 6 votes vote down vote up
Response execute(String sql, String command) throws Exception {
    File sqlJobFile = ResourceUtils.getFile("classpath:yaml/sql.yaml");
    SqlSubmitFlinkRequest sqlSubmitFlinkRequest
        = BindPropertiesUtil.bindProperties(sqlJobFile, SqlSubmitFlinkRequest.class);
    SourceDescriptor sourceDescriptor = createSource("csv_table_test", "classpath:yaml/csv-source.yaml", SourceType.CSV, TableType.TABLE);
    SinkDescriptor sinkDescriptor = createSink("redis_sink", "classpath:yaml/redis-sink.yaml", SinkType.REDIS);
    RedisSinkDescriptor redisSinkDescriptor = (RedisSinkDescriptor) sinkDescriptor;
    redisSinkDescriptor.setCommand(command);
    sqlSubmitFlinkRequest.setSources(Lists.newArrayList(sourceDescriptor));
    sqlSubmitFlinkRequest.setSinks(Lists.newArrayList(redisSinkDescriptor));
    sqlSubmitFlinkRequest.setSqls(SqlParseUtil.findQuerySql(Lists.newArrayList(sql)));
    client.submit(sqlSubmitFlinkRequest, (SubmitFlinkResponse response) -> {
        }
    );
    return new Response(true);
}
 
Example 2
Source File: ContractIT.java    From restful-booker-platform with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void checkAuthContract() throws JSONException, FileNotFoundException, URISyntaxException {
    // First we make an HTTP request to get the Booking from Booking API
    Response response = given()
                            .get("http://localhost:3000/booking/1");

    // Next we take the body of the HTTP response and convert it into a JSONObject
    JSONObject parsedResponse = new JSONObject(response.body().prettyPrint());

    // Then we import our expected JSON contract from the contract folder
    // and store in a string
    File file = ResourceUtils.getFile(this.getClass().getResource("/contract.json"));
    String testObject = new Scanner(file).useDelimiter("\\Z").next();

    // Finally we compare the contract string and the JSONObject to compare
    // and pass if they match
    JSONAssert.assertEquals(testObject, parsedResponse, true);
}
 
Example 3
Source File: StripingFilesystemTrackerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void test1() throws FileNotFoundException
{
	final File sourceFolder = ResourceUtils.getFile("classpath:bulkimport");
    final StripingFilesystemTracker tracker = new StripingFilesystemTracker(directoryAnalyser, new NodeRef("workspace", "SpacesStore", "123"), sourceFolder, Integer.MAX_VALUE);
    List<ImportableItem> items = tracker.getImportableItems(Integer.MAX_VALUE);
    assertEquals("", 11, items.size());

    tracker.incrementLevel();
    items = tracker.getImportableItems(Integer.MAX_VALUE);
    assertEquals("", 2, items.size());

    tracker.incrementLevel();
    items = tracker.getImportableItems(Integer.MAX_VALUE);
    assertEquals("", 31, items.size());
}
 
Example 4
Source File: SpringBootTmplFreemarkApplication.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Bean
public freemarker.template.Configuration freemarkConfig() {
    /* ------------------------------------------------------------------------ */
    /* You should do this ONLY ONCE in the whole application life-cycle: */

    /* Create and adjust the configuration singleton */
    freemarker.template.Configuration cfg = new freemarker.template.Configuration(
        freemarker.template.Configuration.VERSION_2_3_22);

    File folder;
    try {
        folder = ResourceUtils.getFile("classpath:templates");
        cfg.setDirectoryForTemplateLoading(folder);
    } catch (IOException e) {
        e.printStackTrace();
    }

    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    return cfg;
}
 
Example 5
Source File: MailUtil.java    From springboot-learn with MIT License 5 votes vote down vote up
/**
 * 获取邮箱模板文件 返回模板内容
 *
 * @return
 * @throws IOException
 */
public static String getHtmlTemplateText(String templatePath, String charset) throws IOException {
    File file = ResourceUtils.getFile("classpath:" + templatePath);
    String template;
    if (StringUtils.isNotBlank(charset)) {
        template = FileUtils.readFileToString(file, charset);
    } else {
        template = FileUtils.readFileToString(file, DAFAULT_CHARSET);
    }
    return template;
}
 
Example 6
Source File: TemplateExtractorTest.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testExtractVars() throws Exception {
    File template = ResourceUtils.getFile("classpath:report-template-v2.xlsx");
    Set<String> vars = new TemplateExtractor(template, true).extractVars();
    System.out.println(vars);
    Assert.assertTrue(vars.size() >= 7);
}
 
Example 7
Source File: IngestionTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
public static File getFile(String fileResourcePath) throws FileNotFoundException {
    if (!fileResourcePath.startsWith("classpath:")) {
        fileResourcePath = "classpath:" + fileResourcePath;
    }
    File file = ResourceUtils.getFile(fileResourcePath);
    return file;
}
 
Example 8
Source File: CipherResourcePropertiesEncryptorTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void whenDecryptResource_thenAllEncryptedValuesDecrypted() throws Exception {
	// given
	Environment environment = new Environment("name", "profile", "label");
	File file = ResourceUtils.getFile("classpath:resource-encryptor/test.properties");
	String text = new String(Files.readAllBytes(file.toPath()));

	// when
	String decyptedResource = encryptor.decrypt(text, environment);

	// then
	assertThat(decyptedResource.contains("{cipher}")).isFalse();
}
 
Example 9
Source File: BulkImportTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Simplifies calling {@ResourceUtils.getFile} so that a {@link RuntimeException}
 * is thrown rather than a checked {@link FileNotFoundException} exception.
 *
 * @param resourceName e.g. "classpath:folder/file"
 * @return File object
 */
private File resourceAsFile(String resourceName)
{
    try
    {
        return ResourceUtils.getFile(resourceName);
    }
    catch (FileNotFoundException e)
    {
        throw new RuntimeException("Resource "+resourceName+" not found", e);
    }
}
 
Example 10
Source File: IngestionTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
public static InputStream getFileInputStream(String fileResourcePath) throws FileNotFoundException {
    if (!fileResourcePath.startsWith("classpath:")) {
        fileResourcePath = "classpath:" + fileResourcePath;
    }
    File file = ResourceUtils.getFile(fileResourcePath);
    return new BufferedInputStream(new FileInputStream(file));
}
 
Example 11
Source File: JarRequestTest.java    From alchemy with Apache License 2.0 5 votes vote down vote up
@Test
public void bind() throws Exception {
    File file = ResourceUtils.getFile("classpath:yaml/jar.yaml");
    JarSubmitFlinkRequest flinkRequest = BindPropertiesUtil.bindProperties(file, JarSubmitFlinkRequest.class);
    assertThat(flinkRequest.isCache()).isTrue();
    assertThat(flinkRequest.getDependency()).isNotNull();
    assertThat(flinkRequest.getEntryClass()).isNotNull();
    assertThat(flinkRequest.getAllowNonRestoredState()).isNotNull();
    assertThat(flinkRequest.getParallelism()).isEqualTo(2);
    assertThat(flinkRequest.getProgramArgs()).isNotNull();
    assertThat(flinkRequest.getSavepointPath()).isNotNull();
}
 
Example 12
Source File: SqlRequestTest.java    From alchemy with Apache License 2.0 5 votes vote down vote up
@Test
public void bind() throws Exception {
    File file = ResourceUtils.getFile("classpath:yaml/sql.yaml");
    SqlSubmitFlinkRequest flinkRequest = BindPropertiesUtil.bindProperties(file, SqlSubmitFlinkRequest.class);
    assertThat(flinkRequest.getParallelism()).isEqualTo(1);
    assertThat(flinkRequest.getRestartStrategies()).isNotNull();
    assertThat(flinkRequest.getTimeCharacteristic()).isNotNull();
    assertThat(flinkRequest.getRestartParams().get("restartAttempts")).isEqualTo(10);
    assertThat(flinkRequest.getRestartParams().get("delayBetweenAttempts")).isEqualTo(10000);
    assertThat(flinkRequest.getDependencies().size()).isEqualTo(1);
}
 
Example 13
Source File: AbstractFileResolvingResource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * This implementation returns a File reference for the underlying class path
 * resource, provided that it refers to a file in the file system.
 * @see org.springframework.util.ResourceUtils#getFile(java.net.URL, String)
 */
@Override
public File getFile() throws IOException {
	URL url = getURL();
	if (url.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
		return VfsResourceDelegate.getResource(url).getFile();
	}
	return ResourceUtils.getFile(url, getDescription());
}
 
Example 14
Source File: AbstractFileResolvingResource.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation returns a File reference for the underlying class path
 * resource, provided that it refers to a file in the file system.
 * @see org.springframework.util.ResourceUtils#getFile(java.net.URL, String)
 */
@Override
public File getFile() throws IOException {
	URL url = getURL();
	if (url.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
		return VfsResourceDelegate.getResource(url).getFile();
	}
	return ResourceUtils.getFile(url, getDescription());
}
 
Example 15
Source File: Es6Test.java    From alchemy with Apache License 2.0 5 votes vote down vote up
Response execute(String sql) throws Exception {
    File sqlJobFile = ResourceUtils.getFile("classpath:yaml/sql.yaml");
    SqlSubmitFlinkRequest sqlSubmitFlinkRequest
        = BindPropertiesUtil.bindProperties(sqlJobFile, SqlSubmitFlinkRequest.class);
    SourceDescriptor sourceDescriptor = createSource("csv_source", "classpath:yaml/csv-source.yaml", SourceType.CSV, TableType.TABLE);
    SinkDescriptor sinkDescriptor = createSink("es_sink", "classpath:yaml/es6-sink.yaml", SinkType.ELASTICSEARCH6);
    sqlSubmitFlinkRequest.setSources(Lists.newArrayList(sourceDescriptor));
    sqlSubmitFlinkRequest.setSinks(Lists.newArrayList(sinkDescriptor));
    sqlSubmitFlinkRequest.setSqls(SqlParseUtil.findQuerySql(Lists.newArrayList(sql)));
    client.submit(sqlSubmitFlinkRequest, (SubmitFlinkResponse response) -> {
        }
    );
    return new Response(true);
}
 
Example 16
Source File: CipherResourceYamlEncryptorTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void whenDecryptResource_thenAllEncryptedValuesDecrypted() throws Exception {
	// given
	Environment environment = new Environment("name", "profile", "label");
	File file = ResourceUtils.getFile("classpath:resource-encryptor/test.yml");
	String text = new String(Files.readAllBytes(file.toPath()));

	// when
	String decyptedResource = encryptor.decrypt(text, environment);

	// then
	assertThat(decyptedResource.contains("{cipher}")).isFalse();
}
 
Example 17
Source File: AbstractFileResolvingResource.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation determines the underlying File
 * (or jar file, in case of a resource in a jar/zip).
 */
@Override
protected File getFileForLastModifiedCheck() throws IOException {
	URL url = getURL();
	if (ResourceUtils.isJarURL(url)) {
		URL actualUrl = ResourceUtils.extractArchiveURL(url);
		if (actualUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
			return VfsResourceDelegate.getResource(actualUrl).getFile();
		}
		return ResourceUtils.getFile(actualUrl, "Jar URL");
	}
	else {
		return getFile();
	}
}
 
Example 18
Source File: DataReportGeneratorTest.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testGeneratorV2Simple() throws Exception {
    File template = ResourceUtils.getFile("classpath:report-template-v2.xlsx");
    ID record = addRecordOfTestAllFields();

    File file = new EasyExcelGenerator(template, record).setUser(UserService.ADMIN_USER).generate();
    System.out.println("Report : " + file);
}
 
Example 19
Source File: AbstractFileResolvingResource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * This implementation determines the underlying File
 * (or jar file, in case of a resource in a jar/zip).
 */
@Override
protected File getFileForLastModifiedCheck() throws IOException {
	URL url = getURL();
	if (ResourceUtils.isJarURL(url)) {
		URL actualUrl = ResourceUtils.extractArchiveURL(url);
		if (actualUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
			return VfsResourceDelegate.getResource(actualUrl).getFile();
		}
		return ResourceUtils.getFile(actualUrl, "Jar URL");
	}
	else {
		return getFile();
	}
}
 
Example 20
Source File: DefaultScrapeManager.java    From sofa-lookout with Apache License 2.0 4 votes vote down vote up
@Override
public List<ScrapeConfig> loadConfigFile(String configFile) throws FileNotFoundException {
    File file = ResourceUtils.getFile(configFile);
    return loadConfigFile(file);
}