下面列出了org.springframework.core.io.ClassPathResource#getFile ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SneakyThrows
private File locationOfJar() {
ClassPathResource properties = new ClassPathResource("/application.properties");
Assert.assertTrue("the manifest.yml doesn't exist.", properties.exists());
File path = properties.getFile();
File parentFile = path;
while (true) {
parentFile = parentFile.getParentFile();
if (parentFile.getName().equalsIgnoreCase("integration-tests")) {
break;
}
}
Assert.assertNotNull(parentFile);
File jarTargetDir = new File(parentFile, "customer-service/target/");
File[] files = jarTargetDir.listFiles(file -> file.isFile() && file.getName().toLowerCase().endsWith(".jar"));
Assert.assertTrue(files.length > 0);
return files[0];
}
/**
* 加载文件后替换头和尾并解密
*
* @return 文件字节
*/
private byte[] replaceAndBase64Decode(
final String filePath, final String headReplace, final String tailReplace) throws Exception {
// 从 classpath:resources/ 中加载资源
final ClassPathResource resource = new ClassPathResource(filePath);
if (!resource.exists()) {
throw new Exception("公私钥文件找不到");
}
final byte[] keyBytes = new byte[(int) resource.getFile().length()];
final FileInputStream in = new FileInputStream(resource.getFile());
in.read(keyBytes);
in.close();
final String keyPEM =
new String(keyBytes).replace(headReplace, "").trim().replace(tailReplace, "").trim();
return Base64.decodeBase64(keyPEM);
}
/**
* 获取文件
* @param fileName 文件名称
* @return
*/
public static File getFile(String fileName) throws IOException {
// 获取当前执行启动命令的路径
String userDir = System.getProperty(USER_DIR);
System.out.println("user.dir:"+userDir);
File targetFile;
// 打包后启动时,路径以\\bin结束:E:\\github\\spring-boot-assembly\\target\\spring-boot-assembly\\bin
// 执行startup.bat或者startup.sh命令的路径,默认目录是bin,如果不是,请修改
if (userDir.endsWith(BIN)){
File file = new File(userDir);
// 获取E:\\github\\spring-boot-assembly
File parentFile = file.getParentFile();
// 获取配置文件目录,默认配置文件目录是config,如果不是,请修改
File configDir = new File(parentFile,CONFIG);
// 目标文件
targetFile = new File(configDir,fileName);
}else{
// 工具中启动
// E:\github\spring-boot-assembly\target\classes\test.jks
ClassPathResource classPathResource = new ClassPathResource(fileName);
targetFile = classPathResource.getFile();
}
System.out.println("targetFile = " + targetFile);
return targetFile;
}
@Test
public void verifyTrustStoreLoadingSuccessfullyWithCertAvailable() throws Exception {
final ClassPathResource resource = new ClassPathResource("truststore.jks");
final FileTrustStoreSslSocketFactory factory = new FileTrustStoreSslSocketFactory(resource.getFile(), "changeit");
final SimpleHttpClientFactoryBean clientFactory = new SimpleHttpClientFactoryBean();
clientFactory.setSslSocketFactory(factory);
final HttpClient client = clientFactory.getObject();
assertTrue(client.isValidEndPoint("https://www.cacert.org"));
}
@Test
public void verifyTrustStoreLoadingSuccessfullyWithCertAvailable2() throws Exception {
final ClassPathResource resource = new ClassPathResource("truststore.jks");
final FileTrustStoreSslSocketFactory factory = new FileTrustStoreSslSocketFactory(resource.getFile(), "changeit");
final SimpleHttpClientFactoryBean clientFactory = new SimpleHttpClientFactoryBean();
clientFactory.setSslSocketFactory(factory);
final HttpClient client = clientFactory.getObject();
assertTrue(client.isValidEndPoint("https://test.scaldingspoon.org/idp/shibboleth"));
}
@Test
public void verifyTrustStoreLoadingSuccessfullyForValidEndpointWithNoCert() throws Exception {
final ClassPathResource resource = new ClassPathResource("truststore.jks");
final FileTrustStoreSslSocketFactory factory = new FileTrustStoreSslSocketFactory(resource.getFile(), "changeit");
final SimpleHttpClientFactoryBean clientFactory = new SimpleHttpClientFactoryBean();
clientFactory.setSslSocketFactory(factory);
final HttpClient client = clientFactory.getObject();
assertTrue(client.isValidEndPoint("https://www.google.com"));
}
@Test
public void verifyTrustStoreLoadingSuccessfullyWihInsecureEndpoint() throws Exception {
final ClassPathResource resource = new ClassPathResource("truststore.jks");
final FileTrustStoreSslSocketFactory factory = new FileTrustStoreSslSocketFactory(resource.getFile(), "changeit");
final SimpleHttpClientFactoryBean clientFactory = new SimpleHttpClientFactoryBean();
clientFactory.setSslSocketFactory(factory);
final HttpClient client = clientFactory.getObject();
assertTrue(client.isValidEndPoint("http://wikipedia.org"));
}
private void importInternal(String acpName, NodeRef space) throws IOException
{
// Importing IMAP test acp
ClassPathResource acpResource = new ClassPathResource(acpName);
ACPImportPackageHandler acpHandler = new ACPImportPackageHandler(acpResource.getFile(), null);
Location importLocation = new Location(space);
importerService.importView(acpHandler, importLocation, null, null);
}
private void importInternal(String acpName, NodeRef space)
throws IOException
{
ClassPathResource acpResource = new ClassPathResource(acpName);
ACPImportPackageHandler acpHandler = new ACPImportPackageHandler(acpResource.getFile(), null);
Location importLocation = new Location(space);
importerService.importView(acpHandler, importLocation, null, null);
}
/**
* Test attachment extraction with a TNEF message
* @throws Exception
*/
public void testAttachmentExtraction() throws Exception
{
AuthenticationUtil.setRunAsUserSystem();
/**
* Load a TNEF message
*/
ClassPathResource fileResource = new ClassPathResource("imap/test-tnef-message.eml");
assertNotNull("unable to find test resource test-tnef-message.eml", fileResource);
InputStream is = new FileInputStream(fileResource.getFile());
MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), is);
NodeRef companyHomeNodeRef = findCompanyHomeNodeRef();
FileInfo f1 = fileFolderService.create(companyHomeNodeRef, "ImapServiceImplTest", ContentModel.TYPE_FOLDER);
FileInfo f2 = fileFolderService.create(f1.getNodeRef(), "test-tnef-message.eml", ContentModel.TYPE_CONTENT);
ContentWriter writer = fileFolderService.getWriter(f2.getNodeRef());
writer.putContent(new FileInputStream(fileResource.getFile()));
imapService.extractAttachments(f2.getNodeRef(), message);
List<AssociationRef> targetAssocs = nodeService.getTargetAssocs(f2.getNodeRef(), ImapModel.ASSOC_IMAP_ATTACHMENTS_FOLDER);
assertTrue("attachment folder is found", targetAssocs.size() == 1);
NodeRef attachmentFolderRef = targetAssocs.get(0).getTargetRef();
assertNotNull(attachmentFolderRef);
List<FileInfo> files = fileFolderService.listFiles(attachmentFolderRef);
assertTrue("three files not found", files.size() == 3);
}
private void importTestData(String acpName, NodeRef space) throws IOException
{
ClassPathResource acpResource = new ClassPathResource(acpName);
ACPImportPackageHandler acpHandler = new ACPImportPackageHandler(acpResource.getFile(), null);
Location importLocation = new Location(space);
importerService.importView(acpHandler, importLocation, null, null);
}
private File getKeyStoreFile() throws IOException {
ClassPathResource resource = new ClassPathResource(sslKeystoreFile);
try {
return resource.getFile();
} catch (Exception ex) {
File temp = File.createTempFile("keystore", ".tmp");
FileCopyUtils.copy(resource.getInputStream(), new FileOutputStream(temp));
return temp;
}
}
@Test(expected = RuntimeException.class)
public void verifyTrustStoreBadPassword() throws Exception {
final ClassPathResource resource = new ClassPathResource("truststore.jks");
new FileTrustStoreSslSocketFactory(resource.getFile(), "invalid");
}
protected String readCertificateContents() throws IOException {
ClassPathResource classPathResource = new ClassPathResource(CERTIFICATE_FILE_PATH);
File jsonFile = classPathResource.getFile();
return FileUtils.readFileToString(jsonFile, Charset.defaultCharset());
}
private String getNotificationContentFromFile(String notificationJsonFileName) throws Exception {
ClassPathResource classPathResource = new ClassPathResource(notificationJsonFileName);
File jsonFile = classPathResource.getFile();
return FileUtils.readFileToString(jsonFile, Charset.defaultCharset());
}
private String getNotificationContentFromFile(String notificationJsonFileName) throws Exception {
ClassPathResource classPathResource = new ClassPathResource(notificationJsonFileName);
File jsonFile = classPathResource.getFile();
return FileUtils.readFileToString(jsonFile, Charset.defaultCharset());
}
private VulnerabilityNotificationView getNotificationView(String path) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(path);
File jsonFile = classPathResource.getFile();
String notificationString = Files.toString(jsonFile, Charset.defaultCharset());
return gson.fromJson(notificationString, VulnerabilityNotificationView.class);
}
private static void resolveAndConfigureGeodeHome() throws IOException {
ClassPathResource resource = new ClassPathResource("/geode-home");
File resourceFile = resource.getFile();
System.setProperty(GEODE_HOME_PROPERTY, resourceFile.getAbsolutePath());
}