Java Code Examples for org.apache.camel.util.FileUtil

The following examples show how to use org.apache.camel.util.FileUtil. 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
Source Project: camel-quarkus   Source File: QuarkusPlatformHttpConsumer.java    License: Apache License 2.0 5 votes vote down vote up
void populateAttachments(Set<FileUpload> uploads, Message message) {
    for (FileUpload upload : uploads) {
        final String name = upload.name();
        final String fileName = upload.fileName();
        LOG.tracef("HTTP attachment %s = %s", name, fileName);
        // is the file name accepted
        boolean accepted = true;

        if (fileNameExtWhitelist != null) {
            String ext = FileUtil.onlyExt(fileName);
            if (ext != null) {
                ext = ext.toLowerCase(Locale.US);
                if (!fileNameExtWhitelist.equals("*") && !fileNameExtWhitelist.contains(ext)) {
                    accepted = false;
                }
            }
        }
        if (accepted) {
            final File localFile = new File(upload.uploadedFileName());
            uploadAttacher.attachUpload(localFile, fileName, message);
        } else {
            LOG.debugf(
                    "Cannot add file as attachment: %s because the file is not accepted according to fileNameExtWhitelist: %s",
                    fileName, fileNameExtWhitelist);
        }
    }
}
 
Example 2
Source Project: camelinaction2   Source File: FileRollback.java    License: Apache License 2.0 5 votes vote down vote up
public void onFailure(Exchange exchange) {
    // this method is executed when the Exchange failed.
    // the cause Exception is stored on the Exchange which you can obtain using getException

    // delete the file
    String name = exchange.getIn().getHeader(Exchange.FILE_NAME_PRODUCED, String.class);
    LOG.warn("Failure occurred so deleting backup file: " + name);

    FileUtil.deleteFile(new File(name));
}
 
Example 3
Source Project: camelinaction   Source File: FileRollback.java    License: Apache License 2.0 5 votes vote down vote up
public void onFailure(Exchange exchange) {
    // this method is executed when the Exchange failed.
    // the cause Exception is stored on the Exchange which you can obtain using getException

    // delete the file
    String name = exchange.getIn().getHeader(Exchange.FILE_NAME_PRODUCED, String.class);
    LOG.warn("Failure occurred so deleting backup file: " + name);

    FileUtil.deleteFile(new File(name));
}
 
Example 4
Source Project: tutorials   Source File: AppIntegrationTest.java    License: MIT License 5 votes vote down vote up
private void deleteFile(String path) {
    try {
        FileUtil.removeDir(new File(path));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 5
@Test
public void testValidMessage() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        public void configure() throws Exception {
            from("file:target/validator?noop=true")
            .to("json-validator:jsonvalidator/schema.json")
            .to("mock:valid");
        }
    });

    MockEndpoint mockValid = camelctx.getEndpoint("mock:valid", MockEndpoint.class);
    mockValid.expectedMessageCount(1);

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("file:target/validator",
                "{ \"name\": \"Joe Doe\", \"id\": 1, \"price\": 12.5 }",
                Exchange.FILE_NAME, "valid.json");

        mockValid.assertIsSatisfied();

        Assert.assertTrue("Can delete the file", FileUtil.deleteFile(new File("target/validator/valid.json")));

    } finally {
      camelctx.close();
    }
}
 
Example 6
@Test
public void testInvalidMessage() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        public void configure() throws Exception {
            from("file:target/validator?noop=true")
                .doTry()
                    .to("json-validator:jsonvalidator/schema.json")
                .doCatch(ValidationException.class)
                    .to("mock:invalid")
                .end();
        }
    });

    MockEndpoint mockInvalid = camelctx.getEndpoint("mock:invalid", MockEndpoint.class);
    mockInvalid.expectedMessageCount(1);

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("file:target/validator",
                "{ \"name\": \"Joe Doe\", \"id\": \"AA1\", \"price\": 12.5 }",
                Exchange.FILE_NAME, "invalid.json");

        mockInvalid.assertIsSatisfied();


        Assert.assertTrue("Can delete the file", FileUtil.deleteFile(new File("target/validator/invalid.json")));

    } finally {
      camelctx.close();
    }
}
 
Example 7
Source Project: camel-quarkus   Source File: BuildTimeUriResolver.java    License: Apache License 2.0 4 votes vote down vote up
private String toTransletName(final String compacted) {
    final String fileName = FileUtil.stripPath(compacted);
    final String name = FileUtil.stripExt(fileName, true);
    return StringHelper.capitalize(name, true);
}
 
Example 8
Source Project: camel-quarkus   Source File: BuildTimeUriResolver.java    License: Apache License 2.0 4 votes vote down vote up
private static String compact(String href, String scheme) {
    final String afterScheme = scheme != null ? StringHelper.after(href, scheme) : href;
    final String compacted = FileUtil.compactPath(afterScheme, '/');
    return compacted;
}
 
Example 9
Source Project: wildfly-camel   Source File: WildFlyClassResolver.java    License: Apache License 2.0 4 votes vote down vote up
public InputStream loadResourceAsStream(String uri) {
    IllegalArgumentAssertion.assertNotNull(uri, "uri");
    String resolvedName = FileUtil.compactPath(uri, '/');
    return classLoader.getResourceAsStream(resolvedName);
}
 
Example 10
Source Project: wildfly-camel   Source File: WildFlyClassResolver.java    License: Apache License 2.0 4 votes vote down vote up
public URL loadResourceAsURL(String uri) {
    IllegalArgumentAssertion.assertNotNull(uri, "uri");
    String resolvedName = FileUtil.compactPath(uri, '/');
    return classLoader.getResource(resolvedName);
}