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.moserp.product.rest.ProductController.java

@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/{productId}/ean")
public ResponseEntity<?> getProductEAN(@PathVariable String productId) throws WriterException, IOException {
    Product product = repository.findOne(productId);
    if (product == null) {
        return ResponseEntity.notFound().build();
    }//from  ww  w. j ava  2  s . c om
    EAN13Writer ean13Writer = new EAN13Writer();
    BitMatrix matrix = ean13Writer.encode(product.getEan(), BarcodeFormat.EAN_13, 300, 200);
    BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(matrix);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "png", baos);
    byte[] imageData = baos.toByteArray();
    ByteArrayResource byteArrayResource = new ByteArrayResource(imageData);
    return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(byteArrayResource);
}

From source file:net.javacrumbs.springws.test.template.XsltTemplateProcessor.java

protected Resource transform(Resource resource, WebServiceMessage message) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Source envelopeSource = message != null ? getXmlUtil().getEnvelopeSource(message) : new DOMSource();
    getXmlUtil().transform(new ResourceSource(resource), envelopeSource, new StreamResult(baos));
    if (logger.isDebugEnabled()) {
        logger.debug("Transformation result:\n" + new String(baos.toByteArray(), "UTF-8"));
    }//from w  w w .ja va2 s  . c  o m
    return new ByteArrayResource(baos.toByteArray());
}

From source file:org.cloudfoundry.identity.uaa.config.YamlMapFactoryBeanTests.java

@Test
public void testFirstFound() throws Exception {
    factory.setResolutionMethod(ResolutionMethod.FIRST_FOUND);
    factory.setResources(new Resource[] { new AbstractResource() {
        @Override/*from  w w w  . j  a va 2 s. com*/
        public String getDescription() {
            return "non-existent";
        }

        @Override
        public InputStream getInputStream() throws IOException {
            throw new IOException("planned");
        }
    }, new ByteArrayResource("foo:\n  spam: bar".getBytes()) });
    assertEquals(1, factory.getObject().size());
}

From source file:org.cloudfoundry.identity.uaa.config.YamlPropertiesFactoryBeanTests.java

@Test
@Ignore // We can't fail on duplicate keys because the Map is created by the YAML library
public void testLoadResourcesWithNestedInternalOverride() throws Exception {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(new Resource[] {
            new ByteArrayResource("foo:\n  bar: spam\n  foo: baz\nbreak: it\nfoo: bucket".getBytes()) });
    Properties properties = factory.getObject();
    assertEquals("spam", properties.get("foo.bar"));
}

From source file:io.spring.initializr.service.InitializrServiceSmokeTests.java

@Test
public void configurationCanBeSerialized() throws URISyntaxException {
    RequestEntity<Void> request = RequestEntity.get(new URI("/metadata/config"))
            .accept(MediaType.APPLICATION_JSON).build();
    ResponseEntity<String> response = this.restTemplate.exchange(request, String.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    InitializrMetadata actual = InitializrMetadataBuilder.create()
            .withInitializrMetadata(new ByteArrayResource(response.getBody().getBytes())).build();
    assertThat(actual).isNotNull();/* w  w  w.  j a  v a 2 s.co  m*/
    InitializrMetadata expected = this.metadataProvider.get();
    assertThat(actual.getDependencies().getAll().size()).isEqualTo(expected.getDependencies().getAll().size());
    assertThat(actual.getConfiguration().getEnv().getBoms().size())
            .isEqualTo(expected.getConfiguration().getEnv().getBoms().size());
}

From source file:nl.surfnet.coin.teams.service.impl.GroupServiceBasicAuthentication10aTest.java

@Test
public void testGetGroupFaultyJsonResponse() {
    super.setResponseResource(new ByteArrayResource("not-even-valid-json".getBytes()));
    Group20 group20 = groupService.getGroup20(provider, "personId", "whatever");
    assertNull(group20);//from w  w  w .j  av a 2s.  c om
}

From source file:nl.surfnet.coin.mock.MockSoapServerTest.java

@Test
public void testStatus() throws Exception {
    super.setResponseResource(new ByteArrayResource("bye".getBytes()));
    super.setStatus(403);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(new HttpGet("http://localhost:8088/testUrl"));
    assertEquals(403, response.getStatusLine().getStatusCode());
    client = new DefaultHttpClient();
    response = client.execute(new HttpGet("http://localhost:8088/testUrl"));
    assertEquals(200, response.getStatusLine().getStatusCode());
}

From source file:org.cloudfoundry.identity.uaa.config.YamlServletProfileInitializerTests.java

@Test
public void testLoadDefaultResource() throws Exception {

    Mockito.when(context.getResource(Matchers.contains("${APPLICATION_CONFIG_URL}")))
            .thenReturn(new ByteArrayResource("foo: bar\nspam:\n  foo: baz".getBytes()));

    initializer.initialize(context);/*  w  w  w  .j  av  a  2s.  c o m*/

    assertEquals("bar", environment.getProperty("foo"));
    assertEquals("baz", environment.getProperty("spam.foo"));

}

From source file:nl.surfnet.coin.teams.service.impl.GroupServiceBasicAuthentication10aTest.java

@Test
public void testGetGroupFaultyHttpResponse() {
    super.setResponseResource(new ByteArrayResource("not-even-valid-json".getBytes()));
    super.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    Group20 group20 = groupService.getGroup20(provider, "personId", "whatever");
    assertNull(group20);// ww  w.ja  va 2  s. c  o  m
}

From source file:org.cloudfoundry.identity.uaa.config.YamlPropertiesFactoryBeanTests.java

@Test
public void testLoadResourceWithMultipleDocuments() throws Exception {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(//from  w  w w  .  java 2  s.  c  o  m
            new Resource[] { new ByteArrayResource("foo: bar\nspam: baz\n---\nfoo: bag".getBytes()) });
    Properties properties = factory.getObject();
    assertEquals("bag", properties.get("foo"));
    assertEquals("baz", properties.get("spam"));
}