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:org.yes.cart.service.mail.impl.MailComposerImpl.java

/**
 * Add inline resource to mail message.//from   ww w. j a v a 2 s  .co m
 * Resource id will be interpreted as file name in following fashion: filename_ext.
 *
 * @param helper          MimeMessageHelper, that has mail message
 * @param htmlTemplate    html message template
 * @param mailTemplateChain physical path to resources
 * @param shopCode        shop code
 * @param locale          locale
 * @param templateName    template name
 *
 * @throws javax.mail.MessagingException in case if resource can not be inlined
 */
void inlineResources(final MimeMessageHelper helper, final String htmlTemplate,
        final List<String> mailTemplateChain, final String shopCode, final String locale,
        final String templateName) throws MessagingException, IOException {

    if (StringUtils.isNotBlank(htmlTemplate)) {
        final List<String> resourcesIds = getResourcesId(htmlTemplate);
        if (!resourcesIds.isEmpty()) {
            for (String resourceId : resourcesIds) {
                final String resourceFilename = transformResourceIdToFileName(resourceId);
                final byte[] content = mailTemplateResourcesProvider.getResource(mailTemplateChain, shopCode,
                        locale, templateName, resourceFilename);
                helper.addInline(resourceId, new ByteArrayResource(content) {
                    @Override
                    public String getFilename() {
                        return resourceFilename;
                    }
                });
            }
        }
    }

}

From source file:org.yes.cart.service.mail.impl.MailComposerImpl.java

/**
 * Add inline resource to mail message./*from  w  w w  .j  a  v a 2 s .c om*/
 * Resource id will be interpreted as file name in following fashion: filename_ext.
 *
 * @param helper          MimeMessageHelper, that has mail message
 * @param mail            html message template
 *
 * @throws javax.mail.MessagingException in case if resource can not be inlined
 */
void inlineResources(final MimeMessageHelper helper, final Mail mail) throws MessagingException {

    if (CollectionUtils.isNotEmpty(mail.getParts())) {
        for (final MailPart part : mail.getParts()) {
            final String fileName = part.getFilename();
            final String resourceId = part.getResourceId();
            helper.addInline(resourceId, new ByteArrayResource(part.getData()) {
                @Override
                public String getFilename() {
                    return fileName;
                }
            });
        }
    }

}

From source file:piecework.ui.StaticResourceAggregator.java

public ContentResource getStaticResource() {
    String content = this.buffer.toString();
    return new DatedByteArrayContentResource(new ByteArrayResource(content.getBytes(Charset.forName("UTF-8"))));
}

From source file:tds.itemrenderer.repository.impl.RemoteContentRepositoryTest.java

@Test
public void shouldFindResource() throws IOException {
    final String resourcePath = "/path/to/resource";
    final String data = "myData";
    final Resource resource = new ByteArrayResource(data.getBytes());

    ResponseEntity<Resource> responseEntity = new ResponseEntity<>(resource, HttpStatus.OK);
    when(mockRestTemplate.exchange(isA(URI.class), isA(HttpMethod.class), isA(HttpEntity.class),
            isA(ParameterizedTypeReference.class))).thenReturn(responseEntity);
    final InputStream retData = remoteContentRepository.findResource(resourcePath);
    assertEquals(IOUtils.toString(retData), data);
    verify(mockRestTemplate).exchange(isA(URI.class), isA(HttpMethod.class), isA(HttpEntity.class),
            isA(ParameterizedTypeReference.class));
}