Search in sources :

Example 1 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project OpenClinica by OpenClinica.

the class XformMetaDataService method getMediaFileItemFromFormManager.

public FileItem getMediaFileItemFromFormManager(String fileLink, String crfOid, String formLayoutOid) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
    HttpEntity<String> entity = new HttpEntity<String>(headers);
    ResponseEntity<byte[]> response = restTemplate.exchange(fileLink, HttpMethod.GET, entity, byte[].class, "1");
    FileItem fileItem = null;
    if (response.getStatusCode() == HttpStatus.OK) {
        String fileName = "";
        FileOutputStream output = null;
        try {
            String disposition = response.getHeaders().get("Content-Disposition").get(0);
            fileName = disposition.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*$", "$1");
            String dir = Utils.getCrfMediaFilePath(crfOid, formLayoutOid);
            if (!new File(dir).exists()) {
                new File(dir).mkdirs();
                logger.debug("Made the directory " + dir);
            }
            File file = new File(dir + fileName);
            output = new FileOutputStream(file);
            IOUtils.write(response.getBody(), output);
            fileItem = new DiskFileItem("media_file", response.getHeaders().get("Content-Type").get(0), false, fileName, 100000000, file);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return fileItem;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) FileItem(org.apache.commons.fileupload.FileItem) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) HttpEntity(org.springframework.http.HttpEntity) FileOutputStream(java.io.FileOutputStream) RestTemplate(org.springframework.web.client.RestTemplate) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) IOException(java.io.IOException) File(java.io.File) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem)

Example 2 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project geode by apache.

the class RestTestUtils method getRestTemplate.

public static RestTemplate getRestTemplate() {
    if (restTemplate == null) {
        restTemplate = new RestTemplate();
        final List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        messageConverters.add(new ByteArrayHttpMessageConverter());
        messageConverters.add(new ResourceHttpMessageConverter());
        messageConverters.add(new StringHttpMessageConverter());
        messageConverters.add(createMappingJackson2HttpMessageConverter());
        restTemplate.setMessageConverters(messageConverters);
    }
    return restTemplate;
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 3 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project apollo by ctripcorp.

the class HttpMessageConverterConfiguration method messageConverters.

@Bean
public HttpMessageConverters messageConverters() {
    GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
    gsonHttpMessageConverter.setGson(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create());
    final List<HttpMessageConverter<?>> converters = Lists.newArrayList(new ByteArrayHttpMessageConverter(), new StringHttpMessageConverter(), new AllEncompassingFormHttpMessageConverter(), gsonHttpMessageConverter);
    return new HttpMessageConverters() {

        @Override
        public List<HttpMessageConverter<?>> getConverters() {
            return converters;
        }
    };
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverters(org.springframework.boot.autoconfigure.web.HttpMessageConverters) Bean(org.springframework.context.annotation.Bean)

Example 4 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project ArachneCentralAPI by OHDSI.

the class IntegrationConfig method centralRestTemplate.

@Bean(name = "restTemplate")
public RestTemplate centralRestTemplate() {
    RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(getHttpClient()));
    restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
    return restTemplate;
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) Bean(org.springframework.context.annotation.Bean)

Example 5 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project crnk-framework by crnk-project.

the class SpringParameterProvider method getHttpMessageConverters.

private List<HttpMessageConverter<?>> getHttpMessageConverters() {
    StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
    // see SPR-7316
    stringHttpMessageConverter.setWriteAcceptCharset(false);
    List<HttpMessageConverter<?>> converters = new ArrayList<>(4);
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(stringHttpMessageConverter);
    converters.add(new SourceHttpMessageConverter<>());
    converters.add(new AllEncompassingFormHttpMessageConverter());
    return converters;
}
Also used : AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) SourceHttpMessageConverter(org.springframework.http.converter.xml.SourceHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Aggregations

ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)31 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)16 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)13 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)11 ArrayList (java.util.ArrayList)10 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)10 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)10 Test (org.junit.jupiter.api.Test)6 RestTemplate (org.springframework.web.client.RestTemplate)6 Test (org.junit.Test)5 Bean (org.springframework.context.annotation.Bean)4 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)4 Method (java.lang.reflect.Method)3 MethodParameter (org.springframework.core.MethodParameter)3 MediaType (org.springframework.http.MediaType)3 GsonHttpMessageConverter (org.springframework.http.converter.json.GsonHttpMessageConverter)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 JsonMessageConverter (org.hisp.dhis.webapi.mvc.messageconverter.JsonMessageConverter)2 XmlMessageConverter (org.hisp.dhis.webapi.mvc.messageconverter.XmlMessageConverter)2