Search in sources :

Example 16 with ByteArrayHttpMessageConverter

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

the class ServletAnnotationControllerHandlerMethodTests method unsupportedRequestBody.

@Test
public void unsupportedRequestBody() throws ServletException, IOException {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
            adapterDef.getPropertyValues().add("messageConverters", new ByteArrayHttpMessageConverter());
            wac.registerBeanDefinition("handlerAdapter", adapterDef);
        }
    }, RequestResponseBodyController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    String requestBody = "Hello World";
    request.setContent(requestBody.getBytes("UTF-8"));
    request.addHeader("Content-Type", "application/pdf");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(415, response.getStatus());
    assertNotNull("No Accept response header set", response.getHeader("Accept"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 17 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project littlefisher-system by littlefishercoder.

the class WebSpringMvcConfig method configureMessageConverters.

/**
 * Description: 消息转换器配置
 *
 * @param converters converters
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper om = jacksonConverter.getObjectMapper();
    om.setDateFormat(new SimpleDateFormat(DateUtil.DATETIME_FORMAT_1));
    om.setLocale(Locale.CHINA);
    om.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    List<MediaType> mediaTypeList = Lists.newArrayList();
    mediaTypeList.add(new MediaType(MediaType.APPLICATION_JSON, Charsets.UTF_8));
    mediaTypeList.add(new MediaType(MediaType.TEXT_HTML, Charsets.UTF_8));
    jacksonConverter.setSupportedMediaTypes(mediaTypeList);
    StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charsets.UTF_8);
    stringConverter.setWriteAcceptCharset(false);
    // 保持以下顺序
    // "application/json" "application/*+json"
    converters.add(jacksonConverter);
    // "application/xml" "text/xml" "application/*+xml"
    converters.add(new Jaxb2RootElementHttpMessageConverter());
    // "application/xml" "text/xml" "application/*+xml"
    converters.add(new SourceHttpMessageConverter<>());
    // "application/x-www-form-urlencoded" "multipart/form-data"
    converters.add(new AllEncompassingFormHttpMessageConverter());
    // "application/octet-stream" "*/*"
    converters.add(new ByteArrayHttpMessageConverter());
    // "text/plain" "*/*"
    converters.add(stringConverter);
    // "*/*"
    converters.add(new ResourceHttpMessageConverter());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MediaType(org.springframework.http.MediaType) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) SimpleDateFormat(java.text.SimpleDateFormat) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 18 with ByteArrayHttpMessageConverter

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

the class XformMetaDataService method saveAttachedFiles.

public void saveAttachedFiles(String uri, String dir, String fileName) throws IOException {
    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(uri, HttpMethod.GET, entity, byte[].class, "1");
    if (response.getStatusCode().equals(HttpStatus.OK)) {
        FileOutputStream output = new FileOutputStream(new File(dir + File.separator + fileName));
        IOUtils.write(response.getBody(), output);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) FileOutputStream(java.io.FileOutputStream) RestTemplate(org.springframework.web.client.RestTemplate) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) File(java.io.File)

Example 19 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project vorto by eclipse.

the class GenerationDelegateProxyService method generate.

@Override
public GeneratedOutput generate(ModelId modelId, String serviceKey, Map<String, String> requestParams) {
    ModelInfo modelResource = modelRepositoryService.getById(modelId);
    if (modelResource == null) {
        throw new ModelNotFoundException("Model with the given ID does not exist", null);
    }
    if (modelResource.getType() == ModelType.Datatype || modelResource.getType() == ModelType.Mapping) {
        throw new GenerationException("Provided model is neither an information model nor a function block model!");
    }
    restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
    Generator generatorEntity = getGenerator(serviceKey);
    if (generatorEntity == null) {
        throw new GenerationException("Generator with key " + serviceKey + " is not a registered generator");
    }
    generatorEntity.increaseInvocationCount();
    this.registeredGeneratorsRepository.save(generatorEntity);
    ResponseEntity<byte[]> entity = restTemplate.getForEntity(generatorEntity.getGenerationEndpointUrl() + attachRequestParams(requestParams), byte[].class, modelId.getNamespace(), modelId.getName(), modelId.getVersion());
    return new GeneratedOutput(entity.getBody(), extractFileNameFromHeader(entity), entity.getHeaders().getContentLength());
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ModelNotFoundException(org.eclipse.vorto.repository.api.exception.ModelNotFoundException) GeneratedOutput(org.eclipse.vorto.repository.api.generation.GeneratedOutput) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) GenerationException(org.eclipse.vorto.repository.api.exception.GenerationException)

Example 20 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project nakadi by zalando.

the class WebConfig method configureMessageConverters.

@Override
protected void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
    final StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
    stringConverter.setWriteAcceptCharset(false);
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(stringConverter);
    converters.add(new ResourceHttpMessageConverter());
    converters.add(new SourceHttpMessageConverter<>());
    converters.add(mappingJackson2HttpMessageConverter());
    super.configureMessageConverters(converters);
}
Also used : ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Aggregations

ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)33 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)17 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)13 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)12 ArrayList (java.util.ArrayList)11 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)11 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)11 RestTemplate (org.springframework.web.client.RestTemplate)7 Test (org.junit.jupiter.api.Test)6 Test (org.junit.Test)5 Bean (org.springframework.context.annotation.Bean)5 MediaType (org.springframework.http.MediaType)4 Jaxb2RootElementHttpMessageConverter (org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter)4 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)4 Method (java.lang.reflect.Method)3 MethodParameter (org.springframework.core.MethodParameter)3 GsonHttpMessageConverter (org.springframework.http.converter.json.GsonHttpMessageConverter)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2