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

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

Introduction

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

Prototype

public ByteArrayResource(byte[] byteArray) 

Source Link

Document

Create a new ByteArrayResource .

Usage

From source file:com.hybris.backoffice.cockpitng.search.DefaultAdvancedSearchOperatorServiceTest.java

@Override
public void prepareApplicationContextAndSession() {
    final ApplicationContext parentApplicationContext = getApplicationContext();
    final GenericApplicationContext applicationContext = new GenericApplicationContext(
            parentApplicationContext);// ww  w  . j a v a  2s  . c o m
    final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
    xmlReader.loadBeanDefinitions(new ByteArrayResource(BEANS_DEFINITION.getBytes()));
    applicationContext.refresh();

    autowireProperties(applicationContext);
}

From source file:com.wooki.test.unit.ConversionsTest.java

@Test(enabled = true)
public void docbookConversion() {
    String result = "<book>     <bookinfo>       <title>An Example Book</title>              <author>         <firstname>Your first name</firstname>         <surname>Your surname</surname>         <affiliation>           <address><email>foo@example.com</email></address>         </affiliation>       </author>          <copyright>         <year>2000</year>         <holder>Copyright string here</holder>       </copyright>          <abstract>         <para>If your book has an abstract then it should go here.</para>       </abstract>     </bookinfo>        <preface>       <title>Preface</title>          <para>Your book may have a preface, in which case it should be placed         here.</para>     </preface>              <chapter>       <title>My First Chapter</title>          <para>This is the first chapter in my book.</para>          <sect1>         <title>My First Section</title>            <para>This is the first section in my book.</para>       </sect1>     </chapter>   </book>";
    Resource resource = new ByteArrayResource(result.getBytes());
    InputStream xhtml = fromDocbookConvertor.performTransformation(resource);
    File htmlFile = null;/*from   www.ja  v a2  s . c  om*/
    try {
        htmlFile = File.createTempFile("wooki", ".html");
        FileOutputStream fos = new FileOutputStream(htmlFile);
        logger.debug("HTML File is " + htmlFile.getAbsolutePath());
        byte[] content = null;
        int available = 0;
        while ((available = xhtml.available()) > 0) {
            content = new byte[available];
            xhtml.read(content);
            fos.write(content);
        }
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getLocalizedMessage());
        return;
    }
    logger.debug("Docbook to xhtml ok");
    SAXParserFactory factory = SAXParserFactory.newInstance();

    // cration d'un parseur SAX
    SAXParser parser;
    try {
        parser = factory.newSAXParser();
        parser.parse(new InputSource(new FileInputStream(htmlFile)), htmlParser);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        logger.error(e.getLocalizedMessage());
    } catch (SAXException e) {
        e.printStackTrace();
        logger.error(e.getLocalizedMessage());
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getLocalizedMessage());
    }

    Book book = htmlParser.getBook();
    logger.debug("The book title is " + book.getTitle());

}

From source file:it.polimi.diceH2020.launcher.Experiment.java

public boolean send(String filename, String content) {
    MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
    map.add("name", filename);
    map.add("filename", filename);

    try {/*  w w w  .  j a va  2  s . c  o m*/
        ByteArrayResource contentsAsResource = new ByteArrayResource(content.getBytes("UTF-8")) {
            @Override
            public String getFilename() {
                return filename;
            }
        };
        map.add("file", contentsAsResource);

        try {
            restWrapper.postForObject(UPLOAD_ENDPOINT, map, String.class);
        } catch (Exception e) {
            notifyWsUnreachability();
            return false;
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:eu.trentorise.smartcampus.citizenportal.service.EmailService.java

public String sendMailVLClassification(final String period, final String mailDate, final String protocolCode,
        final String recipientName, final String recipientAddress, final String recipientCity,
        final String recipientPhone, final String recipientEmail, final String practice_id,
        final String position, final String score, final String determinationCode,
        final String determinationDate, final String alboDate, final String expirationDate, final String phase,
        final String ef_period, final String ef_category, final String ef_tool, final String classificationUrl,
        final String respName, final String subject, final Locale locale, final MailImage logoImage,
        final MailImage footerImage, final int type) throws MessagingException {

    // Prepare the evaluation context
    final Context ctx = new Context(locale);
    ctx.setVariable("imagelogoMyweb", logoImage.getImageName());
    ctx.setVariable("mailDate", mailDate);
    ctx.setVariable("period", period);
    ctx.setVariable("protCode", protocolCode);
    ctx.setVariable("name", recipientName);
    ctx.setVariable("address", recipientAddress);
    ctx.setVariable("city", recipientCity);
    ctx.setVariable("phone", recipientPhone);
    ctx.setVariable("detCode", determinationCode);
    ctx.setVariable("detDate", determinationDate);
    ctx.setVariable("alboDate", alboDate);
    ctx.setVariable("expDate", expirationDate);
    ctx.setVariable("practice_id", practice_id);
    ctx.setVariable("position", position);
    ctx.setVariable("score", score);
    ctx.setVariable("phase", phase);
    ctx.setVariable("ef_period", ef_period);
    ctx.setVariable("ef_category", ef_category);
    ctx.setVariable("ef_tool", ef_tool);
    ctx.setVariable("classification_url", classificationUrl);
    ctx.setVariable("respName", respName);
    ctx.setVariable("subscriptionDate", new Date());
    //ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
    ctx.setVariable("text", subject);
    ctx.setVariable("imagefooterVallag", footerImage.getImageName());

    // Prepare message using a Spring helper
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8");
    message.setSubject("Graduatoria Edilizia Abitativa");
    //message.setFrom("thymeleaf@example.com");
    message.setFrom("myweb.edilizia@comunitadellavallagarina.tn.it");
    //message.setFrom("myweb-graduatoria@smartcommunitylab.it");
    message.setTo(recipientEmail);/*from  w w w.  ja v a 2  s.co m*/

    // Create the HTML body using Thymeleaf
    if (type == 1) {
        final String htmlContent = this.templateEngine.process("email-vallagarina.html", ctx);
        message.setText(htmlContent, true /* isHtml */);
    } else {
        final String htmlContent = this.templateEngine.process("email-vallagarina-final.html", ctx);
        message.setText(htmlContent, true /* isHtml */);
    }

    // Add the inline titles image, referenced from the HTML code as "cid:${imageResourceName}"
    final InputStreamSource imageLogo = new ByteArrayResource(logoImage.getImageByte());
    message.addInline(logoImage.getImageName(), imageLogo, logoImage.getImageType());

    // Add the inline footer image, referenced from the HTML code as "cid:${imageResourceName}"
    final InputStreamSource imageFooter = new ByteArrayResource(footerImage.getImageByte());
    message.addInline(footerImage.getImageName(), imageFooter, footerImage.getImageType());

    // Add the attachment
    //final InputStreamSource attachmentSource = new ByteArrayResource(attachmentBytes);
    //message.addAttachment(
    //        attachmentFileName, attachmentSource, attachmentContentType);

    // Send mail
    this.mailSender.send(mimeMessage);

    return recipientName + "OK";
}

From source file:com.acc.xstream.XmlXStreamMarshallerFactoryTest.java

@Test
public void testInstantiateBeanFactory() {

    final GenericApplicationContext applicationContext = new GenericApplicationContext();
    final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
    xmlReader.loadBeanDefinitions(new ByteArrayResource(SERVICE_BEAN_DEF.getBytes()));
    applicationContext.refresh();//www .ja  v  a2  s  .  c o m

    final XStreamMarshaller instanceOneXStream = (XStreamMarshaller) applicationContext
            .getBean("onlyOneInstance");
    final XStreamMarshaller instanceTwoXStream = (XStreamMarshaller) applicationContext
            .getBean("onlyOneInstance");

    Assert.assertSame("Factory should produce same instance ", instanceOneXStream, instanceTwoXStream);

}

From source file:gemlite.core.util.Util.java

public static Resource toResource(ClassLoader loader, String file) {
    InputStream in = loader.getResourceAsStream(file);
    byte[] bt;/*from  w w  w. java 2 s.  c  o m*/
    try {
        bt = new byte[in.available()];
        in.read(bt);
        in.close();
        ByteArrayResource res = new ByteArrayResource(bt);
        return res;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:aiai.ai.launchpad.server.ServerController.java

private HttpEntity<AbstractResource> returnEmptyAsGone(HttpServletResponse response) throws IOException {
    response.sendError(HttpServletResponse.SC_GONE);
    return new HttpEntity<>(new ByteArrayResource(new byte[0]), getHeader(0));
}

From source file:aiai.ai.launchpad.server.ServerController.java

private HttpEntity<AbstractResource> returnEmptyAsConflict(HttpServletResponse response) throws IOException {
    response.sendError(HttpServletResponse.SC_CONFLICT);
    return new HttpEntity<>(new ByteArrayResource(new byte[0]), getHeader(0));
}

From source file:gr.abiss.calipso.service.EmailService.java

@Async
public void sendMailWithInline(final String recipientName, final String recipientEmail,
        final String imageResourceName, final byte[] imageBytes, final String imageContentType,
        final Locale locale) throws MessagingException {

    // Prepare the evaluation context
    final Context ctx = new Context(locale);
    ctx.setVariable("name", recipientName);
    ctx.setVariable("subscriptionDate", new Date());
    ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
    ctx.setVariable("imageResourceName", imageResourceName); // so that we can reference it from HTML

    // Prepare message using a Spring helper
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8");
    message.setSubject("Example HTML email with inline image");
    message.setFrom("thymeleaf@example.com");
    message.setTo(recipientEmail);/*ww  w  .  j  a  v  a 2 s  .com*/

    // Create the HTML body using Thymeleaf
    final String htmlContent = this.templateEngine.process("email-inlineimage.html", ctx);
    LOGGER.info("Sending email body: " + htmlContent);
    message.setText(htmlContent, true /* isHtml */);

    // Add the inline image, referenced from the HTML code as "cid:${imageResourceName}"
    final InputStreamSource imageSource = new ByteArrayResource(imageBytes);
    message.addInline(imageResourceName, imageSource, imageContentType);

    // Send mail
    this.mailSender.send(mimeMessage);

}

From source file:ch.algotrader.cache.CacheTest.java

@AfterClass
public static void afterClass() {

    // cleanup the database
    ResourceDatabasePopulator dbPopulator = new ResourceDatabasePopulator();
    dbPopulator.addScript(new ByteArrayResource("DROP ALL OBJECTS".getBytes(Charsets.US_ASCII)));

    DatabasePopulatorUtils.execute(dbPopulator, database);

    context.close();// ww  w .  ja  va2  s .com

    net.sf.ehcache.CacheManager ehCacheManager = net.sf.ehcache.CacheManager.getInstance();
    ehCacheManager.shutdown();
}