Example usage for org.springframework.core.io FileSystemResource FileSystemResource

List of usage examples for org.springframework.core.io FileSystemResource FileSystemResource

Introduction

In this page you can find the example usage for org.springframework.core.io FileSystemResource FileSystemResource.

Prototype

public FileSystemResource(Path filePath) 

Source Link

Document

Create a new FileSystemResource from a Path handle, performing all file system interactions via NIO.2 instead of File .

Usage

From source file:org.xsocket.connection.SpringTest.java

@Test
public void testSimple() throws Exception {

    File file = QAUtil.createTempfile();
    FileWriter fw = new FileWriter(file);
    fw.write(SPRING_XML);//from   www .j  av  a 2 s . co  m
    fw.close();

    BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource(file));
    IServer server = (IServer) beanFactory.getBean("server");
    Assert.assertTrue(server.isOpen());
    assert (server.isOpen() == true);

    IBlockingConnection connection = new BlockingConnection("localhost", server.getLocalPort());
    connection.setAutoflush(true);
    String request = "3453543535";
    connection.write(request + EchoHandler.DELIMITER);

    String response = connection.readStringByDelimiter(EchoHandler.DELIMITER, Integer.MAX_VALUE);
    connection.close();

    Assert.assertTrue(request.equals(response));

    file.delete();
    server.close();
}

From source file:org.sakaiproject.entitybroker.util.spring.ResourceFinder.java

private static Resource makeResource(String path) {
    if (path.startsWith("/")) {
        path = path.substring(1);/*from w  ww  .j av a 2  s.  c o m*/
    }
    Resource r = null;
    // try the environment path first
    String envPath = getEnvironmentPath() + path;
    r = new FileSystemResource(envPath);
    if (!r.exists()) {
        // try the relative path next
        String relPath = getRelativePath() + path;
        r = new FileSystemResource(relPath);
        if (!r.exists()) {
            // now try the classloader
            ClassLoader cl = ResourceFinder.class.getClassLoader();
            r = new ClassPathResource(path, cl);
            if (!r.exists()) {
                // finally try the system classloader
                cl = ClassLoader.getSystemClassLoader();
                r = new ClassPathResource(path, cl);
            }
        }
    }
    if (!r.exists()) {
        throw new IllegalArgumentException(
                "Could not find this resource (" + path + ") in any of the checked locations");
    }
    return r;
}

From source file:com.wooki.services.ImportServiceImpl.java

public Book importApt(Resource apt) {
    try {/*ww  w  . ja v  a  2 s.c  om*/
        String from = "apt";
        File out = File.createTempFile("fromAptToXHTML", ".html");
        String to = "html";

        Converter converter = new DefaultConverter();

        InputFileWrapper input = InputFileWrapper.valueOf(apt.getFilename(), from, "ISO-8859-1",
                converter.getInputFormats());
        OutputFileWrapper output = OutputFileWrapper.valueOf(out.getAbsolutePath(), to, "UTF-8",
                converter.getOutputFormats());

        converter.convert(input, output);
        return importDocbook(fromAptToDocbook.performTransformation(new FileSystemResource(out)));
    } catch (UnsupportedFormatException e) {
        e.printStackTrace();
    } catch (ConverterException e) {
        e.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return null;
}

From source file:architecture.ee.web.struts2.action.admin.ajax.SqlFinderAction.java

public Resource getTargetRootResource() {
    return new FileSystemResource(getSqlFileLocation());
}

From source file:no.dusken.common.control.MailController.java

/**
 *
 * @param mail the mail part of the mail
 * @param model other things that should be in the mail.
 * @param template - the path to the velocitytemplate to use (mail/template.vm)
 *//*from   w  w  w  . ja v a2s.co m*/
public void sendEmail(final Mail mail, final Map model, final String template) {
    final Map<String, Object> map = new HashMap<String, Object>();
    if (model != null) {
        map.putAll(model);
    }
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo(mail.getToAddress());
            if (mail.getFromAddress() == null || mail.getFromAddress().equals("")) {
                mail.setFromAddress(defaultSenderAddress);
            }
            message.setFrom(mail.getFromAddress());
            message.setSubject(mail.getSubject());

            map.put("mail", mail);
            String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, map);
            message.setText(text, false);
            if (mail.getAttachment() != null) {
                FileSystemResource res = new FileSystemResource(mail.getAttachment());
                message.addAttachment(mail.getAttachment().getName(), res);
            }
        }
    };
    this.mailSender.send(preparator);
}

From source file:com.cfitzarl.cfjwed.core.config.WebApplicationMvcConfigurer.java

@Bean
public static PropertySourcesPlaceholderConfigurer configurePropertiesLoader() {
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setIgnoreResourceNotFound(true);
    configurer.setLocations(new ClassPathResource("application.properties"),
            new FileSystemResource("/usr/local/cfjwed/application.properties"));
    return configurer;
}

From source file:org.mousephenotype.dcc.utils.net.email.EMAILUtils.java

public void sendEmail(String[] to, String[] cc, String[] bcc, String from, String subject, String text,
        String[] attachmentFilenames) throws MailException, MessagingException {
    MimeMessage message = this.mailSender.createMimeMessage();
    this.helper = new MimeMessageHelper(message, true);
    this.helper.setFrom(from);

    this.helper.setTo(to);
    if (cc != null) {
        this.helper.setCc(cc);
    }//  ww  w .j  a  v a2 s. c  o m
    if (bcc != null) {
        this.helper.setBcc(bcc);
    }
    this.helper.setSubject(subject);
    this.helper.setText(text);
    if (attachmentFilenames != null) {
        for (String attachmentFilename : attachmentFilenames) {
            FileSystemResource file = new FileSystemResource(new File(attachmentFilename));
            this.helper.addAttachment(attachmentFilename, file);
        }
    }

    this.mailSender.send(message);
}

From source file:com.siberhus.tdfl.DefaultResourceCreator.java

@Override
public Resource create(Resource example) throws IOException {

    String parent = example.getFile().getParent();
    String targetDirPath = parent + File.separator + subdirectory;
    File targetDir = new File(targetDirPath);
    if (!targetDir.exists()) {
        if (createDirectory) {
            targetDir.mkdir();// w  ww.  jav  a  2  s  .co m
        }
    }
    String filename = example.getFilename();
    if (extension == null)
        extension = FilenameUtils.getExtension(filename);
    filename = FilenameUtils.getBaseName(filename);
    if (prefix != null) {
        filename = prefix + filename;
    }
    if (suffix != null) {
        filename = filename + suffix;
    }
    filename = filename + "." + extension;
    return new FileSystemResource(targetDirPath + File.separator + filename);
}

From source file:de.tudarmstadt.ukp.clarin.webanno.webapp.security.preauth.WebAnnoApplicationContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext aApplicationContext) {
    WebAnnoLoggingFilter.setLoggingUsername("SYSTEM");

    log.info("  _      __    __   ___                ");
    log.info(" | | /| / /__ / /  / _ | ___  ___  ___ ");
    log.info(" | |/ |/ / -_) _ \\/ __ |/ _ \\/ _ \\/ _ \\");
    log.info(" |__/|__/\\__/_.__/_/ |_/_//_/_//_/\\___/");
    log.info(SettingsUtil.getVersionString());

    ConfigurableEnvironment aEnvironment = aApplicationContext.getEnvironment();

    File settings = SettingsUtil.getSettingsFile();

    // If settings were found, add them to the environment
    if (settings != null) {
        log.info("Settings: " + settings);
        try {//from  ww w . j a v a  2s . c o  m
            aEnvironment.getPropertySources()
                    .addFirst(new ResourcePropertySource(new FileSystemResource(settings)));
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    // Activate bean profile depending on authentication mode
    if (AUTH_MODE_PREAUTH.equals(aEnvironment.getProperty(PROP_AUTH_MODE))) {
        aEnvironment.setActiveProfiles(PROFILE_PREAUTH);
        log.info("Authentication: pre-auth");
    } else {
        aEnvironment.setActiveProfiles(PROFILE_DATABASE);
        log.info("Authentication: database");
    }
}

From source file:org.cbioportal.annotation.pipeline.MutationRecordWriter.java

@Override
public void open(ExecutionContext ec) throws ItemStreamException {
    stagingFile = Paths.get(outputFilename);

    PassThroughLineAggregator aggr = new PassThroughLineAggregator();
    flatFileItemWriter.setLineAggregator(aggr);
    flatFileItemWriter.setResource(new FileSystemResource(stagingFile.toString()));
    flatFileItemWriter.setHeaderCallback(new FlatFileHeaderCallback() {
        @Override//w  w w.  ja v  a 2 s  .  c  om
        public void writeHeader(Writer writer) throws IOException {
            AnnotatedRecord record = new AnnotatedRecord();

            // first write out the comment lines, then write the actual header
            for (String comment : commentLines) {
                writer.write(comment + "\n");
            }
            writer.write(StringUtils.join(header, "\t"));
        }
    });
    flatFileItemWriter.open(ec);
}