Here are the examples of the java api org.springframework.http.HttpHeaders.getContentType() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
100 Examples
19
View Source File : MutipartMixedConverterUnitTest.java
License : Apache License 2.0
Project Creator : xm-online
License : Apache License 2.0
Project Creator : xm-online
@Before
public void before() throws IOException {
MockitoAnnotations.initMocks(this);
multipartMixedConverter = new MultipartMixedConverter();
when(httpHeaders.getContentType()).thenReturn(MediaType.ALL);
when(httpOutputMessage.getHeaders()).thenReturn(httpHeaders);
when(httpOutputMessage.getBody()).thenReturn(outputStream);
}
18
View Source File : StringHttpMessageConverter.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException {
HttpHeaders headers = outputMessage.getHeaders();
if (this.writeAcceptCharset && headers.get(HttpHeaders.ACCEPT_CHARSET) == null) {
headers.setAcceptCharset(getAcceptedCharsets());
}
Charset charset = getContentTypeCharset(headers.getContentType());
StreamUtils.copy(str, charset, outputMessage.getBody());
}
18
View Source File : StringHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void writeDefaultCharset() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, null, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertThat(this.outputMessage.getBodyreplacedtring(StandardCharsets.ISO_8859_1)).isEqualTo(body);
replacedertThat(headers.getContentType()).isEqualTo(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
replacedertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.ISO_8859_1).length);
replacedertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}
18
View Source File : ResourceHttpMessageWriterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void writeMultipleRegions() throws Exception {
testWrite(get("/").range(of(0, 5), of(7, 15), of(17, 20), of(22, 38)).build());
HttpHeaders headers = this.response.getHeaders();
String contentType = headers.getContentType().toString();
String boundary = contentType.substring(30);
replacedertThat(contentType).startsWith("multipart/byteranges;boundary=");
StepVerifier.create(this.response.getBodyreplacedtring()).consumeNextWith(content -> {
String[] actualRanges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
String[] expected = new String[] { "--" + boundary, "Content-Type: text/plain", "Content-Range: bytes 0-5/39", "Spring", "--" + boundary, "Content-Type: text/plain", "Content-Range: bytes 7-15/39", "Framework", "--" + boundary, "Content-Type: text/plain", "Content-Range: bytes 17-20/39", "test", "--" + boundary, "Content-Type: text/plain", "Content-Range: bytes 22-38/39", "resource content.", "--" + boundary + "--" };
replacedertThat(actualRanges).isEqualTo(expected);
}).expectComplete().verify();
}
18
View Source File : ProtobufHttpMessageConverter.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
private Charset getCharset(HttpHeaders headers) {
if (headers == null || headers.getContentType() == null || headers.getContentType().getCharSet() == null) {
return DEFAULT_CHARSET;
}
return headers.getContentType().getCharSet();
}
17
View Source File : ResourceWebHandlerTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void getResourceFromSubDirectoryOfAlternatePath() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
setPathWithinHandlerMapping(exchange, "js/baz.js");
this.handler.handle(exchange).block(TIMEOUT);
HttpHeaders headers = exchange.getResponse().getHeaders();
replacedertEquals(MediaType.parseMediaType("application/javascript"), headers.getContentType());
replacedertResponseBody(exchange, "function foo() { console.log(\"hello world\"); }");
}
17
View Source File : StringHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void writeDefaultCharset() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, null, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.ISO_8859_1));
replacedertEquals(new MediaType("text", "plain", StandardCharsets.ISO_8859_1), headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.ISO_8859_1).length, headers.getContentLength());
replacedertTrue(headers.getAcceptCharset().isEmpty());
}
17
View Source File : ResourceHttpMessageWriterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void writeMultipleRegions() throws Exception {
testWrite(get("/").range(of(0, 5), of(7, 15), of(17, 20), of(22, 38)).build());
HttpHeaders headers = this.response.getHeaders();
String contentType = headers.getContentType().toString();
String boundary = contentType.substring(30);
replacedertThat(contentType, startsWith("multipart/byteranges;boundary="));
StepVerifier.create(this.response.getBodyreplacedtring()).consumeNextWith(content -> {
String[] actualRanges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
String[] expected = new String[] { "--" + boundary, "Content-Type: text/plain", "Content-Range: bytes 0-5/39", "Spring", "--" + boundary, "Content-Type: text/plain", "Content-Range: bytes 7-15/39", "Framework", "--" + boundary, "Content-Type: text/plain", "Content-Range: bytes 17-20/39", "test", "--" + boundary, "Content-Type: text/plain", "Content-Range: bytes 22-38/39", "resource content.", "--" + boundary + "--" };
replacedertArrayEquals(expected, actualRanges);
}).expectComplete().verify();
}
17
View Source File : DefaultResponseErrorHandler.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* Determine the charset of the response (for inclusion in a status exception).
* @param response the response to inspect
* @return the replacedociated charset, or {@code null} if none
* @since 4.3.8
*/
@Nullable
protected Charset getCharset(ClientHttpResponse response) {
HttpHeaders headers = response.getHeaders();
MediaType contentType = headers.getContentType();
return (contentType != null ? contentType.getCharset() : null);
}
17
View Source File : AbstractHttpMessageConverter.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* Add default headers to the output message.
* <p>This implementation delegates to {@link #getDefaultContentType(Object)} if a
* content type was not provided, set if necessary the default character set, calls
* {@link #getContentLength}, and sets the corresponding headers.
* @since 4.2
*/
protected void addDefaultHeaders(HttpHeaders headers, T t, @Nullable MediaType contentType) throws IOException {
if (headers.getContentType() == null) {
MediaType contentTypeToUse = contentType;
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
contentTypeToUse = getDefaultContentType(t);
} else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) {
MediaType mediaType = getDefaultContentType(t);
contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse);
}
if (contentTypeToUse != null) {
if (contentTypeToUse.getCharset() == null) {
Charset defaultCharset = getDefaultCharset();
if (defaultCharset != null) {
contentTypeToUse = new MediaType(contentTypeToUse, defaultCharset);
}
}
headers.setContentType(contentTypeToUse);
}
}
if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
Long contentLength = getContentLength(t, headers.getContentType());
if (contentLength != null) {
headers.setContentLength(contentLength);
}
}
}
17
View Source File : StringHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// gh-24283
@Test
public void writeWithWildCardMediaType() throws IOException {
String body = "Hello World";
this.converter.write(body, MediaType.ALL, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertThat(this.outputMessage.getBodyreplacedtring(StandardCharsets.US_ASCII)).isEqualTo(body);
replacedertThat(headers.getContentType()).isEqualTo(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
replacedertThat(headers.getContentLength()).isEqualTo(body.getBytes().length);
replacedertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}
17
View Source File : StringHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void writeUTF8() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, TEXT_PLAIN_UTF_8, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertThat(this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8)).isEqualTo(body);
replacedertThat(headers.getContentType()).isEqualTo(TEXT_PLAIN_UTF_8);
replacedertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
replacedertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}
17
View Source File : AbstractHttpMessageConverter.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
/**
* Add default headers to the output message.
* <p>This implementation delegates to {@link #getDefaultContentType(Object)} if a
* content type was not provided, set if necessary the default character set, calls
* {@link #getContentLength}, and sets the corresponding headers.
* @since 4.2
*/
protected void addDefaultHeaders(HttpHeaders headers, T t, @Nullable MediaType contentType) throws IOException {
if (headers.getContentType() == null) {
MediaType contentTypeToUse = contentType;
if (contentType == null || !contentType.isConcrete()) {
contentTypeToUse = getDefaultContentType(t);
} else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) {
MediaType mediaType = getDefaultContentType(t);
contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse);
}
if (contentTypeToUse != null) {
if (contentTypeToUse.getCharset() == null) {
Charset defaultCharset = getDefaultCharset();
if (defaultCharset != null) {
contentTypeToUse = new MediaType(contentTypeToUse, defaultCharset);
}
}
headers.setContentType(contentTypeToUse);
}
}
if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
Long contentLength = getContentLength(t, headers.getContentType());
if (contentLength != null) {
headers.setContentLength(contentLength);
}
}
}
17
View Source File : ResultResponseErrorHandler.java
License : Apache License 2.0
Project Creator : penggle
License : Apache License 2.0
Project Creator : penggle
protected MediaType getContentType(ClientHttpResponse response) {
HttpHeaders headers = response.getHeaders();
return headers.getContentType();
}
17
View Source File : StringHttpMessageConverterTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
@Test
public void writeDefaultCharset() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, null, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.ISO_8859_1));
replacedertEquals(new MediaType("text", "plain", StandardCharsets.ISO_8859_1), headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.ISO_8859_1).length, headers.getContentLength());
replacedertFalse(headers.getAcceptCharset().isEmpty());
}
17
View Source File : RestResponseErrorHandler.java
License : Apache License 2.0
Project Creator : lihangqi
License : Apache License 2.0
Project Creator : lihangqi
protected Charset getCharset(ClientHttpResponse response) {
HttpHeaders headers = response.getHeaders();
MediaType contentType = headers.getContentType();
return (contentType != null ? contentType.getCharset() : null);
}
17
View Source File : AbstractHttpMessageConverter.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* Add default headers to the output message.
* <p>This implementation delegates to {@link #getDefaultContentType(Object)} if a content
* type was not provided, calls {@link #getContentLength}, and sets the corresponding headers
* @since 4.2
*/
protected void addDefaultHeaders(HttpHeaders headers, T t, MediaType contentType) throws IOException {
if (headers.getContentType() == null) {
MediaType contentTypeToUse = contentType;
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
contentTypeToUse = getDefaultContentType(t);
} else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) {
MediaType mediaType = getDefaultContentType(t);
contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse);
}
if (contentTypeToUse != null) {
headers.setContentType(contentTypeToUse);
}
}
if (headers.getContentLength() == -1) {
Long contentLength = getContentLength(t, headers.getContentType());
if (contentLength != null) {
headers.setContentLength(contentLength);
}
}
}
16
View Source File : StringHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void writeUTF8() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, TEXT_PLAIN_UTF_8, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8));
replacedertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
replacedertTrue(headers.getAcceptCharset().isEmpty());
}
16
View Source File : StringHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
// SPR-8867
@Test
public void writeOverrideRequestedContentType() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
MediaType requestedContentType = new MediaType("text", "html");
HttpHeaders headers = this.outputMessage.getHeaders();
headers.setContentType(TEXT_PLAIN_UTF_8);
this.converter.write(body, requestedContentType, this.outputMessage);
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8));
replacedertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
replacedertTrue(headers.getAcceptCharset().isEmpty());
}
16
View Source File : Jaxb2RootElementHttpMessageConverter.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception {
try {
Clreplaced<?> clazz = ClreplacedUtils.getUserClreplaced(o);
Marshaller marshaller = createMarshaller(clazz);
setCharset(headers.getContentType(), marshaller);
marshaller.marshal(o, result);
} catch (MarshalException ex) {
throw ex;
} catch (JAXBException ex) {
throw new HttpMessageConversionException("Invalid JAXB setup: " + ex.getMessage(), ex);
}
}
16
View Source File : LoggingClientHttpRequestInterceptor.java
License : GNU Lesser General Public License v2.1
Project Creator : Verdoso
License : GNU Lesser General Public License v2.1
Project Creator : Verdoso
private void logBody(String body, HttpHeaders headers) {
MediaType contentType = headers.getContentType();
List<String> contentEncoding = headers.get(CONTENT_ENCODING_HEADER);
if (contentEncoding != null && !contentEncoding.contains("idenreplacedy")) {
log.trace(" Body: encoded, not shown");
} else {
if (contentType != null && contentType.toString().startsWith(APPLICATION_JSON_HEADER)) {
log.trace(" Body: {}", writeJSON(body));
} else {
log.trace(" Body: {}", body);
}
}
}
16
View Source File : LoggingClientHttpRequestInterceptor.java
License : GNU Lesser General Public License v2.1
Project Creator : Verdoso
License : GNU Lesser General Public License v2.1
Project Creator : Verdoso
private boolean hasTextBody(HttpHeaders headers) {
long contentLength = headers.getContentLength();
if (contentLength != 0) {
MediaType contentType = headers.getContentType();
if (contentType != null) {
String subtype = contentType.getSubtype();
return "text".equals(contentType.getType()) || "xml".equals(subtype) || "json".equals(subtype);
}
}
return false;
}
16
View Source File : CustomXMLHttpMessageConverter.java
License : GNU Lesser General Public License v2.1
Project Creator : Verdoso
License : GNU Lesser General Public License v2.1
Project Creator : Verdoso
@Override
@Measured("createXML")
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
Marshaller marshaller = null;
Clreplaced<?> clazz = null;
try {
clazz = ClreplacedUtils.getUserClreplaced(o);
marshaller = marshallerPool.borrowObject(clazz);
setCharset(headers.getContentType(), marshaller);
marshaller.marshal(o, result);
} catch (MarshalException ex) {
throw new HttpMessageNotWritableException("Could not marshal [" + o + "]: " + ex.getMessage(), ex);
} catch (JAXBException ex) {
throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
} catch (Exception ex) {
throw new HttpMessageConversionException("Could not borrow marshaller from the pool: " + ex.getMessage(), ex);
} finally {
if (clazz != null && marshaller != null) {
marshallerPool.returnObject(clazz, marshaller);
}
}
}
16
View Source File : ResourceWebHandlerTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void getResourceFromSubDirectoryOfAlternatePath() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
setPathWithinHandlerMapping(exchange, "js/baz.js");
this.handler.handle(exchange).block(TIMEOUT);
HttpHeaders headers = exchange.getResponse().getHeaders();
replacedertThat(headers.getContentType()).isEqualTo(MediaType.parseMediaType("application/javascript"));
replacedertResponseBody(exchange, "function foo() { console.log(\"hello world\"); }");
}
16
View Source File : RequestPartServletServerHttpRequestTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void getContentType() throws Exception {
MultipartFile part = new MockMultipartFile("part", "", "application/json", "content".getBytes("UTF-8"));
this.mockRequest.addFile(part);
ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
HttpHeaders headers = request.getHeaders();
replacedertThat(headers).isNotNull();
replacedertThat(headers.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
}
16
View Source File : StringHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// gh-24123
@Test
public void writeJson() throws IOException {
String body = "{\"foo\":\"bar\"}";
this.converter.write(body, MediaType.APPLICATION_JSON, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertThat(this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8)).isEqualTo(body);
replacedertThat(headers.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
replacedertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
replacedertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}
16
View Source File : StringHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-8867
@Test
public void writeOverrideRequestedContentType() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
MediaType requestedContentType = new MediaType("text", "html");
HttpHeaders headers = this.outputMessage.getHeaders();
headers.setContentType(TEXT_PLAIN_UTF_8);
this.converter.write(body, requestedContentType, this.outputMessage);
replacedertThat(this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8)).isEqualTo(body);
replacedertThat(headers.getContentType()).isEqualTo(TEXT_PLAIN_UTF_8);
replacedertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
replacedertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}
16
View Source File : BasicHttpHeadersProviderTest.java
License : Apache License 2.0
Project Creator : onap
License : Apache License 2.0
Project Creator : onap
@Test
public void test_getHttpHeaders_ContentTypeIsJson() {
final HttpHeadersProvider objUnderTest = new BasicHttpHeadersProvider();
final HttpHeaders actualHttpHeaders = objUnderTest.getHttpHeaders();
replacedertNull(actualHttpHeaders.get(AUTHORIZATION_HEADER));
replacedertEquals(MediaType.APPLICATION_JSON, actualHttpHeaders.getContentType());
}
16
View Source File : StringHttpMessageConverterTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
@Test
public void writeUTF8() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, TEXT_PLAIN_UTF_8, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8));
replacedertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
replacedertFalse(headers.getAcceptCharset().isEmpty());
}
16
View Source File : StringHttpMessageConverterTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
// SPR-8867
@Test
public void writeOverrideRequestedContentType() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
MediaType requestedContentType = new MediaType("text", "html");
HttpHeaders headers = this.outputMessage.getHeaders();
headers.setContentType(TEXT_PLAIN_UTF_8);
this.converter.write(body, requestedContentType, this.outputMessage);
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8));
replacedertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
replacedertFalse(headers.getAcceptCharset().isEmpty());
}
16
View Source File : SpringHttpRequestSigner.java
License : MIT License
Project Creator : Mastercard
License : MIT License
Project Creator : Mastercard
private static Charset getCharset(HttpHeaders headers) {
Charset defaultCharset = Charset.defaultCharset();
MediaType contentType = headers.getContentType();
if (contentType != null) {
Charset charset = contentType.getCharset();
if (charset != null) {
return charset;
}
}
return defaultCharset;
}
16
View Source File : LoggingRequestResponseInterceptor.java
License : Apache License 2.0
Project Creator : line
License : Apache License 2.0
Project Creator : line
private static Charset extractCharset(HttpHeaders httpHeaders) {
return Optional.ofNullable(httpHeaders.getContentType()).map(MediaType::getCharset).orElse(StandardCharsets.UTF_8);
}
16
View Source File : DefaultResponseErrorHandler.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
private Charset getCharset(ClientHttpResponse response) {
HttpHeaders headers = response.getHeaders();
MediaType contentType = headers.getContentType();
return contentType != null ? contentType.getCharSet() : null;
}
16
View Source File : Jaxb2RootElementHttpMessageConverter.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
try {
Clreplaced<?> clazz = ClreplacedUtils.getUserClreplaced(o);
Marshaller marshaller = createMarshaller(clazz);
setCharset(headers.getContentType(), marshaller);
marshaller.marshal(o, result);
} catch (MarshalException ex) {
throw new HttpMessageNotWritableException("Could not marshal [" + o + "]: " + ex.getMessage(), ex);
} catch (JAXBException ex) {
throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
}
}
16
View Source File : HeimdallResponseErrorHandler.java
License : Apache License 2.0
Project Creator : getheimdall
License : Apache License 2.0
Project Creator : getheimdall
/**
* Determine the charset of the response (for inclusion in a status exception).
* @param response the response to inspect
* @return the replacedociated charset, or {@code null} if none
* @since 4.3.8
*/
protected Charset getCharset(ClientHttpResponse response) {
HttpHeaders headers = response.getHeaders();
MediaType contentType = headers.getContentType();
return (contentType != null ? contentType.getCharset() : null);
}
16
View Source File : HeadersMapperTest.java
License : Apache License 2.0
Project Creator : adorsys
License : Apache License 2.0
Project Creator : adorsys
@Test
void toHttpHeaders() {
HttpHeaders actualHeaders = headersMapper.toHttpHeaders(responseHeaders);
replacedertFalse(actualHeaders.isEmpty());
replacedertTrue(actualHeaders.containsKey(ResponseHeaders.CONTENT_TYPE));
replacedertEquals(MediaType.APPLICATION_JSON, actualHeaders.getContentType());
replacedertTrue(actualHeaders.containsKey(LOCATION));
replacedertEquals(URI.create(LOCATION_VALUE), actualHeaders.getLocation());
}
15
View Source File : MultipartMixedConverter.java
License : Apache License 2.0
Project Creator : xm-online
License : Apache License 2.0
Project Creator : xm-online
@SuppressWarnings("unchecked")
private void writePart(String name, HttpEnreplacedy<?> partEnreplacedy, OutputStream os) throws IOException {
Object partBody = partEnreplacedy.getBody();
Clreplaced<?> partType = partBody.getClreplaced();
HttpHeaders partHeaders = partEnreplacedy.getHeaders();
MediaType partContentType = partHeaders.getContentType();
for (HttpMessageConverter<?> messageConverter : this.partConverters) {
if (messageConverter.canWrite(partType, partContentType)) {
HttpOutputMessage multipartMessage = new MultipartHttpOutputMessage(os);
multipartMessage.getHeaders().setContentDispositionFormData(name, null);
if (!partHeaders.isEmpty()) {
multipartMessage.getHeaders().putAll(partHeaders);
}
((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartMessage);
return;
}
}
throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter found for request type [" + partType.getName() + "]");
}
15
View Source File : PropertiesHttpMessageConverter.java
License : MIT License
Project Creator : wuyouzhuguli
License : MIT License
Project Creator : wuyouzhuguli
@Override
protected void writeInternal(Properties properties, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
// 获取请求头
HttpHeaders headers = outputMessage.getHeaders();
// 获取 content-type
MediaType contentType = headers.getContentType();
// 获取编码
Charset charset = null;
if (contentType != null) {
charset = contentType.getCharset();
}
charset = charset == null ? Charset.forName("UTF-8") : charset;
// 获取请求体
OutputStream body = outputMessage.getBody();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(body, charset);
properties.store(outputStreamWriter, "Serialized by PropertiesHttpMessageConverter#writeInternal");
}
15
View Source File : ResourceWebHandlerTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void getResource() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
setPathWithinHandlerMapping(exchange, "foo.css");
this.handler.handle(exchange).block(TIMEOUT);
HttpHeaders headers = exchange.getResponse().getHeaders();
replacedertEquals(MediaType.parseMediaType("text/css"), headers.getContentType());
replacedertEquals(17, headers.getContentLength());
replacedertEquals("max-age=3600", headers.getCacheControl());
replacedertTrue(headers.containsKey("Last-Modified"));
replacedertEquals(headers.getLastModified() / 1000, resourceLastModifiedDate("test/foo.css") / 1000);
replacedertEquals("bytes", headers.getFirst("Accept-Ranges"));
replacedertEquals(1, headers.get("Accept-Ranges").size());
replacedertResponseBody(exchange, "h1 { color:red; }");
}
15
View Source File : ResourceWebHandlerTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void getResourceFromAlternatePath() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
setPathWithinHandlerMapping(exchange, "baz.css");
this.handler.handle(exchange).block(TIMEOUT);
HttpHeaders headers = exchange.getResponse().getHeaders();
replacedertEquals(MediaType.parseMediaType("text/css"), headers.getContentType());
replacedertEquals(17, headers.getContentLength());
replacedertEquals("max-age=3600", headers.getCacheControl());
replacedertTrue(headers.containsKey("Last-Modified"));
replacedertEquals(headers.getLastModified() / 1000, resourceLastModifiedDate("testalternatepath/baz.css") / 1000);
replacedertEquals("bytes", headers.getFirst("Accept-Ranges"));
replacedertEquals(1, headers.get("Accept-Ranges").size());
replacedertResponseBody(exchange, "h1 { color:red; }");
}
15
View Source File : RequestPartServletServerHttpRequestTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void getContentType() throws Exception {
MultipartFile part = new MockMultipartFile("part", "", "application/json", "content".getBytes("UTF-8"));
this.mockRequest.addFile(part);
ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
HttpHeaders headers = request.getHeaders();
replacedertNotNull(headers);
replacedertEquals(MediaType.APPLICATION_JSON, headers.getContentType());
}
15
View Source File : FastJsonResponseUtil.java
License : GNU Affero General Public License v3.0
Project Creator : stategen
License : GNU Affero General Public License v3.0
Project Creator : stategen
@SuppressWarnings("resource")
public static void writeResponse(Object result) {
HttpServletResponse httpServletResponse = ServletContextUtil.getHttpServletResponse();
ServletServerHttpResponse servletServerHttpResponse = new ServletServerHttpResponse(httpServletResponse);
HttpHeaders headers = servletServerHttpResponse.getHeaders();
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
httpServletResponse.setStatus(HttpStatus.OK.value());
try {
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
FASTJSON_HTTP_MESSAGE_CONVERTOR.write(result, headers.getContentType(), new HttpOutputMessage() {
@Override
public OutputStream getBody() throws IOException {
return outputStream;
}
@Override
public HttpHeaders getHeaders() {
return headers;
}
});
} catch (HttpMessageNotWritableException | IOException e) {
logger.error("internal error", e);
}
}
15
View Source File : MockMultipartHttpServletRequestTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
void mockMultiPartHttpServletRequestWithMixedData() {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.addFile(new MockMultipartFile("file", "myOrigFilename", MediaType.TEXT_PLAIN_VALUE, "myContent2".getBytes()));
MockPart metadataPart = new MockPart("metadata", "{\"foo\": \"bar\"}".getBytes());
metadataPart.getHeaders().setContentType(MediaType.APPLICATION_JSON);
request.addPart(metadataPart);
HttpHeaders fileHttpHeaders = request.getMultipartHeaders("file");
replacedertThat(fileHttpHeaders).isNotNull();
replacedertThat(fileHttpHeaders.getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
HttpHeaders dataHttpHeaders = request.getMultipartHeaders("metadata");
replacedertThat(dataHttpHeaders).isNotNull();
replacedertThat(dataHttpHeaders.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
}
15
View Source File : BasicHttpHeadersProviderTest.java
License : Apache License 2.0
Project Creator : onap
License : Apache License 2.0
Project Creator : onap
@Test
public void test_getHttpHeaders_ContainAuthorizationHeader() {
final HttpHeadersProvider objUnderTest = new BasicHttpHeadersProvider(BASIC_AUTH_VALUE);
final HttpHeaders actualHttpHeaders = objUnderTest.getHttpHeaders();
replacedertEquals(Arrays.asList(BASIC_AUTH_VALUE), actualHttpHeaders.get(AUTHORIZATION_HEADER));
replacedertEquals(MediaType.APPLICATION_JSON, actualHttpHeaders.getContentType());
}
15
View Source File : FormHttpMessageConverter.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@SuppressWarnings("unchecked")
private void writePart(String name, HttpEnreplacedy<?> partEnreplacedy, OutputStream os) throws IOException {
Object partBody = partEnreplacedy.getBody();
Clreplaced<?> partType = partBody.getClreplaced();
HttpHeaders partHeaders = partEnreplacedy.getHeaders();
MediaType partContentType = partHeaders.getContentType();
for (HttpMessageConverter<?> messageConverter : this.partConverters) {
if (messageConverter.canWrite(partType, partContentType)) {
HttpOutputMessage multipartMessage = new MultipartHttpOutputMessage(os);
multipartMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody));
if (!partHeaders.isEmpty()) {
multipartMessage.getHeaders().putAll(partHeaders);
}
((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartMessage);
return;
}
}
throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter " + "found for request type [" + partType.getName() + "]");
}
15
View Source File : MockHttpServletRequestBuilder.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* Add all headers to the request. Values are always added.
* @param httpHeaders the headers and values to add
*/
public MockHttpServletRequestBuilder headers(HttpHeaders httpHeaders) {
MediaType mediaType = httpHeaders.getContentType();
if (mediaType != null) {
this.contentType = mediaType.toString();
}
for (String name : httpHeaders.keySet()) {
Object[] values = ObjectUtils.toObjectArray(httpHeaders.get(name).toArray());
addToMultiValueMap(this.headers, name, values);
}
return this;
}
15
View Source File : HeaderUtilsTest.java
License : MIT License
Project Creator : kanjielu
License : MIT License
Project Creator : kanjielu
@Test
public void replacedignWithNone() throws Exception {
HttpHeaders target = new HttpHeaders();
target.setContentType(MediaType.TEXT_HTML);
HeaderUtils.replacedign(target);
replacedert.replacedertEquals(MediaType.TEXT_HTML, target.getContentType());
}
15
View Source File : HeaderUtilsTest.java
License : MIT License
Project Creator : kanjielu
License : MIT License
Project Creator : kanjielu
@Test
public void mergeOne() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpHeaders result = HeaderUtils.merge(headers);
replacedert.replacedertEquals(headers.getContentType(), result.getContentType());
}
15
View Source File : HeaderUtilsTest.java
License : MIT License
Project Creator : kanjielu
License : MIT License
Project Creator : kanjielu
@Test
public void mergeTwoWithSameNames() throws Exception {
HttpHeaders header1 = new HttpHeaders();
header1.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpHeaders header2 = new HttpHeaders();
header2.setContentType(MediaType.APPLICATION_XML);
HttpHeaders result = HeaderUtils.merge(header1, header2);
replacedert.replacedertEquals(header2.getContentType(), result.getContentType());
}
14
View Source File : FeignUtils.java
License : Eclipse Public License 1.0
Project Creator : zhaoqilong3031
License : Eclipse Public License 1.0
Project Creator : zhaoqilong3031
static Map<String, Collection<String>> getHeaders(HttpHeaders httpHeaders) {
LinkedHashMap<String, Collection<String>> headers = new LinkedHashMap<>();
for (Map.Entry<String, List<String>> entry : httpHeaders.entrySet()) {
headers.put(entry.getKey(), entry.getValue());
}
headers.put(HttpHeaders.CONTENT_TYPE, Lists.newArrayList(httpHeaders.getContentType().getType().concat("/").concat(httpHeaders.getContentType().getSubtype())));
return headers;
}
See More Examples