use of org.springframework.http.HttpOutputMessage in project spring-security-oauth by spring-projects.
the class DefaultOAuth2ExceptionRenderer method handleHttpEntityResponse.
public void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest) throws Exception {
if (responseEntity == null) {
return;
}
HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
HttpOutputMessage outputMessage = createHttpOutputMessage(webRequest);
if (responseEntity instanceof ResponseEntity && outputMessage instanceof ServerHttpResponse) {
((ServerHttpResponse) outputMessage).setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
}
HttpHeaders entityHeaders = responseEntity.getHeaders();
if (!entityHeaders.isEmpty()) {
outputMessage.getHeaders().putAll(entityHeaders);
}
Object body = responseEntity.getBody();
if (body != null) {
writeWithMessageConverters(body, inputMessage, outputMessage);
} else {
// flush headers
outputMessage.getBody();
}
}
use of org.springframework.http.HttpOutputMessage in project incubator-servicecomb-java-chassis by apache.
the class TestCseHttpMessageConverter method testAll.
@Test
public void testAll() {
MockUtil.getInstance().mockReflectionUtils();
MockUtil.getInstance().mockCseClientHttpRequest();
CseHttpMessageConverter lCseHttpMessageConverter = new CseHttpMessageConverter();
lCseHttpMessageConverter.canWrite(null, null);
lCseHttpMessageConverter.getSupportedMediaTypes();
try {
lCseHttpMessageConverter.read(this.getClass(), null);
} catch (HttpMessageNotReadableException | IOException ignored) {
}
try {
HttpOutputMessage httpOutputMessage = Mockito.mock(CseClientHttpRequest.class);
lCseHttpMessageConverter.write(null, null, httpOutputMessage);
} catch (HttpMessageNotWritableException | IOException ignored) {
}
Assert.assertEquals(true, lCseHttpMessageConverter.canRead(null, null));
}
use of org.springframework.http.HttpOutputMessage in project spring-data-document-examples by spring-projects.
the class FormHttpMessageConverter method writePart.
@SuppressWarnings("unchecked")
private void writePart(String name, HttpEntity partEntity, OutputStream os) throws IOException {
Object partBody = partEntity.getBody();
Class<?> partType = partBody.getClass();
HttpHeaders partHeaders = partEntity.getHeaders();
MediaType partContentType = partHeaders.getContentType();
for (HttpMessageConverter messageConverter : partConverters) {
if (messageConverter.canWrite(partType, partContentType)) {
HttpOutputMessage multipartOutputMessage = new MultipartHttpOutputMessage(os);
multipartOutputMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody));
if (!partHeaders.isEmpty()) {
multipartOutputMessage.getHeaders().putAll(partHeaders);
}
messageConverter.write(partBody, partContentType, multipartOutputMessage);
return;
}
}
throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter found for request type [" + partType.getName() + "]");
}
use of org.springframework.http.HttpOutputMessage in project fastjson by alibaba.
the class DateFormatPriorityTest method test_priority_01.
// Annotation + FastJsonConfig (Annotation优先)
public void test_priority_01() throws Exception {
// FastJsonConfig
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM.dd");
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setFastJsonConfig(config);
converter.canRead(VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(VO.class, MediaType.APPLICATION_JSON_UTF8);
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
HttpOutputMessage out = new HttpOutputMessage() {
public HttpHeaders getHeaders() {
return new HttpHeaders() {
private static final long serialVersionUID = 1L;
@Override
public MediaType getContentType() {
return MediaType.APPLICATION_JSON;
}
};
}
public OutputStream getBody() throws IOException {
return byteOut;
}
};
VO2 vo = new VO2();
vo.setDate(calendar.getTime());
converter.write(vo, VO.class, MediaType.APPLICATION_JSON_UTF8, out);
byte[] bytes = byteOut.toByteArray();
String jsonString = new String(bytes, "UTF-8");
assertEquals("{\"date\":\"1995.10-26\"}", jsonString);
}
use of org.springframework.http.HttpOutputMessage in project fastjson by alibaba.
the class FastJsonHttpMessageConverter4Test method test_1.
public void test_1() throws Exception {
FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
Assert.assertNotNull(converter.getFastJsonConfig());
converter.setFastJsonConfig(new FastJsonConfig());
converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
Method method1 = FastJsonHttpMessageConverter4.class.getDeclaredMethod("supports", Class.class);
method1.setAccessible(true);
method1.invoke(converter, int.class);
HttpInputMessage input = new HttpInputMessage() {
public HttpHeaders getHeaders() {
// TODO Auto-generated method stub
return null;
}
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("{\"id\":123}".getBytes(Charset.forName("UTF-8")));
}
};
VO vo = (VO) converter.read(VO.class, VO.class, input);
Assert.assertEquals(123, vo.getId());
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
HttpOutputMessage out = new HttpOutputMessage() {
public HttpHeaders getHeaders() {
return new HttpHeaders();
}
public OutputStream getBody() throws IOException {
return byteOut;
}
};
converter.write(vo, VO.class, MediaType.TEXT_PLAIN, out);
byte[] bytes = byteOut.toByteArray();
Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));
Method method2 = FastJsonHttpMessageConverter4.class.getDeclaredMethod("readInternal", Class.class, HttpInputMessage.class);
method2.setAccessible(true);
method2.invoke(converter, VO.class, input);
}
Aggregations