Example usage for org.springframework.http MediaType MediaType

List of usage examples for org.springframework.http MediaType MediaType

Introduction

In this page you can find the example usage for org.springframework.http MediaType MediaType.

Prototype

public MediaType(String type, String subtype, @Nullable Map<String, String> parameters) 

Source Link

Document

Create a new MediaType for the given type, subtype, and parameters.

Usage

From source file:com.p5solutions.core.json.JsonHttpMessageConverter.java

/**
 * Instantiates a new json http message converter.
 *//*www  .j av  a2 s. c  o m*/
public JsonHttpMessageConverter() {
    super(new MediaType("application", "json", DEFAULT_CHARSET));
}

From source file:net.solarnetwork.web.support.SimpleCsvHttpMessageConverter.java

/**
 * Default constructor./*from   w w  w.j av a  2  s  . c o m*/
 */
public SimpleCsvHttpMessageConverter() {
    super(new MediaType("text", "csv", Charset.forName("UTF-8")));
}

From source file:com.linecorp.platform.channel.sample.ApiHttpClient.java

public ApiHttpClient(final String channelAccessToken) {

    RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeoutInMillis)
            .setConnectTimeout(timeoutInMillis).build();

    CloseableHttpAsyncClient asyncClient = HttpAsyncClientBuilder.create()
            .setDefaultRequestConfig(requestConfig)
            .addInterceptorLast((HttpRequest httpRequest, HttpContext httpContext) -> {
                httpRequest.addHeader("X-Line-ChannelToken", channelAccessToken);
                httpRequest.addHeader("Content-Type", "application/json; charser=UTF-8");
                httpRequest.removeHeaders("Accept");
                httpRequest.addHeader("Accept", "application/json; charset=UTF-8");
            }).setMaxConnTotal(maxConnections).setMaxConnPerRoute(maxConnections).disableCookieManagement()
            .build();//from  w w w . j a v  a  2 s .  c  o  m

    asyncRestTemplate = new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory(asyncClient));
    asyncRestTemplate.setErrorHandler(new ApiResponseErrorHandler());

    httpHeaders = new HttpHeaders();
    httpHeaders.set("X-Line-ChannelToken", channelAccessToken);
    httpHeaders.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
    List<MediaType> list = new ArrayList<>();
    list.add(new MediaType("application", "json", Charset.forName("UTF-8")));
    httpHeaders.setAccept(list);

    objectMapper = new ObjectMapper();
    objectMapper.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
    objectMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()));
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

From source file:com.fiadot.springjsoncrypt.json.CryptMappingJacson2HttpMessageConverter.java

/**
 * Construct a new {@code MappingJackson2HttpMessageConverter}.
 *///  ww  w.j a  v a  2s.  c  o m
public CryptMappingJacson2HttpMessageConverter() {
    super(new MediaType("application", "json", DEFAULT_CHARSET),
            new MediaType("application", "*+json", DEFAULT_CHARSET));
}

From source file:net.eusashead.hateoas.springhalbuilder.controller.CustomerOrdersControllerITCase.java

@Test
public void testGet() throws Exception {
    // Execute controller method
    MvcResult result = this.mockMvc
            .perform(get("http://localhost/customer/1/orders/").contentType(HalHttpMessageConverter.HAL_JSON)
                    .accept(HalHttpMessageConverter.HAL_JSON))
            .andExpect(status().isOk())//from  w  w w .  j av  a  2  s  .  co m
            .andExpect(header().string("ETag", "W/\"c457c4aeaaecafccf898c555663f337e\""))
            .andExpect(
                    content().contentType(new MediaType("application", "hal+json", Charset.forName("UTF-8"))))
            .andReturn();

    // Check result
    ReadableRepresentation returned = new StandardRepresentationFactory()
            .readRepresentation(new StringReader(result.getResponse().getContentAsString()));
    Assert.assertNotNull(returned);
    Link linkByRel = returned.getLinkByRel("self");
    Assert.assertNotNull(linkByRel);
    Assert.assertEquals("/customer/1/orders/", linkByRel.getHref());
    Assert.assertEquals("0", returned.getValue("page").toString());
    Assert.assertEquals("20", returned.getValue("size").toString());
    List<? extends ReadableRepresentation> content = returned.getResourcesByRel("content");
    Assert.assertNotNull(content);
    Assert.assertEquals(Integer.valueOf(3), Integer.valueOf(content.size()));

}

From source file:net.eusashead.hateoas.conditional.interceptor.AsyncTestControllerITCase.java

@Test
public void testConditionalGet() throws Exception {

    // Expected result
    HttpHeaders headers = new HttpHeaders();
    headers.setETag("\"123456\"");
    ResponseEntity<String> expectedResult = new ResponseEntity<String>("hello", headers, HttpStatus.OK);

    // Execute asynchronously
    MvcResult mvcResult = this.mockMvc
            .perform(get("http://localhost/async/123").contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON))
            .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn();

    // Perform asynchronous dispatch
    this.mockMvc.perform(asyncDispatch(mvcResult)).andExpect(status().isOk())
            .andExpect(header().string("ETag", "\"123456\""))
            .andExpect(content().contentType(new MediaType("application", "json", Charset.forName("UTF-8"))));

    // The 304 should be returned synchronously (it bypasses the controller)
    this.mockMvc//w  ww.  jav  a  2 s.  c o  m
            .perform(get("http://localhost/async/123").contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON).header("If-None-Match", "\"123456\""))
            .andExpect(status().isNotModified()).andReturn();

}

From source file:com.jy51.app.base.converter.StringHttpMessageConverter.java

@Override
protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException {
    if (this.writeAcceptCharset) {
        outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
    }//w ww.  ja v  a 2s. c om
    //
    Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
    MediaType mediaType = new MediaType(MediaType.TEXT_HTML.getType(), MediaType.TEXT_HTML.getSubtype(),
            charset);
    outputMessage.getHeaders().setContentType(mediaType);
    StreamUtils.copy(s, charset, outputMessage.getBody());
}

From source file:it.reply.orchestrator.controller.TemplateControllerTest.java

@Test
public void getTemplate() throws Exception {

    Deployment deployment = ControllerTestUtils.createDeployment();

    String template = new NoNullOrEmptyFile(new Utf8File(Paths.get(templatePath))).read();
    deployment.setTemplate(template);/* w w  w.ja  v a2  s  .co  m*/

    Mockito.when(templateService.getTemplate(deployment.getId())).thenReturn(deployment.getTemplate());

    MvcResult result = mockMvc
            .perform(get("/deployments/" + deployment.getId() + "/template")
                    .header(HttpHeaders.AUTHORIZATION, OAuth2AccessToken.BEARER_TYPE + " <access token>"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(new MediaType(MediaType.TEXT_PLAIN.getType(),
                    MediaType.TEXT_PLAIN.getSubtype(), Charset.forName("ISO-8859-1"))))
            .andDo(document("get-template")).andReturn();

    String content = result.getResponse().getContentAsString();
    assertEquals(content, template);

}

From source file:net.eusashead.hateoas.springhalbuilder.controller.CustomerListControllerITCase.java

@Test
public void testGet() throws Exception {

    // Execute controller method
    MvcResult result = this.mockMvc
            .perform(get("http://localhost/customer/?page=0&size=3")
                    .contentType(HalHttpMessageConverter.HAL_JSON).accept(HalHttpMessageConverter.HAL_JSON))
            .andExpect(status().isOk())/*w  w  w . ja v  a2 s  .  co m*/
            .andExpect(header().string("ETag", "W/\"f4da35bd8dcf93431dfef867460dcdd8\""))
            .andExpect(
                    content().contentType(new MediaType("application", "hal+json", Charset.forName("UTF-8"))))
            .andReturn();

    // Check result
    ReadableRepresentation returned = new StandardRepresentationFactory()
            .readRepresentation(new StringReader(result.getResponse().getContentAsString()));
    Assert.assertNotNull(returned);
    Link linkByRel = returned.getLinkByRel("self");
    Assert.assertNotNull(linkByRel);
    Assert.assertEquals("/customer/?page=0&size=3", linkByRel.getHref());
    List<? extends ReadableRepresentation> customers = returned.getResourcesByRel("content");
    Assert.assertNotNull(customers);
    Assert.assertEquals(Integer.valueOf(3), Integer.valueOf(customers.size()));
}

From source file:org.oncoblocks.centromere.web.config.WebServicesConfig.java

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

    FilteringJackson2HttpMessageConverter jsonConverter = new FilteringJackson2HttpMessageConverter();
    jsonConverter.setSupportedMediaTypes(ApiMediaTypes.getJsonMediaTypes());
    converters.add(jsonConverter);/*from w  w w  .j a v a 2  s  .  co m*/

    MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
    xmlConverter.setSupportedMediaTypes(ApiMediaTypes.getXmlMediaTypes());
    XStreamMarshaller xStreamMarshaller = new XStreamMarshaller();
    xmlConverter.setMarshaller(xStreamMarshaller);
    xmlConverter.setUnmarshaller(xStreamMarshaller);
    converters.add(xmlConverter);

    FilteringTextMessageConverter filteringTextMessageConverter = new FilteringTextMessageConverter(
            new MediaType("text", "plain", Charset.forName("utf-8")));
    filteringTextMessageConverter.setDelimiter("\t");
    converters.add(filteringTextMessageConverter);

}