Java Code Examples for org.springframework.http.HttpOutputMessage#getBody()
The following examples show how to use
org.springframework.http.HttpOutputMessage#getBody() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationMethodHandlerAdapter.java From lams with GNU General Public License v2.0 | 6 votes |
private 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(); } }
Example 2
Source File: GsonHttpMessageConverter.java From lams with GNU General Public License v2.0 | 6 votes |
@Override protected void writeInternal(Object o, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { Charset charset = getCharset(outputMessage.getHeaders()); OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset); try { if (this.jsonPrefix != null) { writer.append(this.jsonPrefix); } if (type != null) { this.gson.toJson(o, type, writer); } else { this.gson.toJson(o, writer); } writer.close(); } catch (JsonIOException ex) { throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex); } }
Example 3
Source File: PropertiesHttpMessageConverter.java From SpringAll with MIT License | 6 votes |
@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"); }
Example 4
Source File: ResultConverter.java From BlogManagePlatform with Apache License 2.0 | 6 votes |
@Override protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { try { OutputStream outputStream = outputMessage.getBody(); //对通用Result采用特殊的优化过的方式 byte[] cacheBytes = ((Result) object).cacheBytes(); if (cacheBytes != null) { outputStream.write(cacheBytes); } else { Result.writer().writeValue(outputStream, object); } outputStream.flush(); } catch (JsonProcessingException ex) { throw new HttpMessageNotWritableException(StrUtil.concat("Could not write JSON: ", ex.getOriginalMessage()), ex); } }
Example 5
Source File: Log4jFastJsonHttpMessageConverter.java From ueboot with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { OutputStream out = outputMessage.getBody(); Class<? extends Object> clazz = obj.getClass(); NotLog notLog = clazz.getAnnotation(NotLog.class); String text = JSON.toJSONString(obj, features); if(notLog==null){ String logText = text; if(!isEmpty(text)&&text.length()>1000){ logText = text.substring(0,1000)+"..."; } log.info("Response:{}", logText); } byte[] bytes = text.getBytes(charset); out.write(bytes); }
Example 6
Source File: ProtobufHttpMessageConverter.java From lams with GNU General Public License v2.0 | 5 votes |
@Override protected void writeInternal(Message message, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { MediaType contentType = outputMessage.getHeaders().getContentType(); if (contentType == null) { contentType = getDefaultContentType(message); } Charset charset = contentType.getCharset(); if (charset == null) { charset = DEFAULT_CHARSET; } if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset); TextFormat.print(message, outputStreamWriter); outputStreamWriter.flush(); } else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) { JSON_FORMAT.print(message, outputMessage.getBody(), charset); } else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) { XML_FORMAT.print(message, outputMessage.getBody(), charset); } else if (MediaType.TEXT_HTML.isCompatibleWith(contentType)) { HTML_FORMAT.print(message, outputMessage.getBody(), charset); } else if (PROTOBUF.isCompatibleWith(contentType)) { setProtoHeader(outputMessage, message); FileCopyUtils.copy(message.toByteArray(), outputMessage.getBody()); } }
Example 7
Source File: HttpConverterText.java From mercury with Apache License 2.0 | 5 votes |
@Override public void write(Object o, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws HttpMessageNotWritableException, IOException { outputMessage.getHeaders().setContentType(TEXT_CONTENT); // this may be too late to validate because Spring RestController has already got the object SimpleObjectMapper mapper = SimpleMapper.getInstance().getWhiteListMapper(o.getClass().getTypeName()); OutputStream out = outputMessage.getBody(); if (o instanceof String) { out.write(util.getUTF((String) o)); } else if (o instanceof byte[]) { out.write((byte[]) o); } else { out.write(mapper.writeValueAsBytes(o)); } }
Example 8
Source File: HttpConverterHtml.java From mercury with Apache License 2.0 | 5 votes |
@Override public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws HttpMessageNotWritableException, IOException { outputMessage.getHeaders().setContentType(HTML_CONTENT); // this may be too late to validate because Spring RestController has already got the object SimpleObjectMapper mapper = SimpleMapper.getInstance().getWhiteListMapper(o.getClass().getTypeName()); OutputStream out = outputMessage.getBody(); if (o instanceof String) { out.write(util.getUTF((String) o)); } else if (o instanceof byte[]) { out.write((byte[]) o); } else { out.write(util.getUTF("<html><body><pre>\n")); out.write(mapper.writeValueAsBytes(o)); out.write(util.getUTF("\n</pre></body></html>")); } }
Example 9
Source File: HttpConverterJson.java From mercury with Apache License 2.0 | 5 votes |
@Override public void write(Object o, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws HttpMessageNotWritableException, IOException { outputMessage.getHeaders().setContentType(JSON); // this may be too late to validate because Spring RestController has already got the object SimpleObjectMapper mapper = SimpleMapper.getInstance().getWhiteListMapper(o.getClass().getTypeName()); OutputStream out = outputMessage.getBody(); if (o instanceof String) { out.write(util.getUTF((String) o)); } else if (o instanceof byte[]) { out.write((byte[]) o); } else { out.write(mapper.writeValueAsBytes(o)); } }
Example 10
Source File: FastJsonHttpMessageConverter.java From mcg-helper with Apache License 2.0 | 5 votes |
protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { HttpHeaders headers = outputMessage.getHeaders(); String text = null; this.writeBefore(obj); if (!(obj instanceof JsonpResult)) { text = JSON.toJSONString(obj, // SerializeConfig.globalInstance, // filters, // dateFormat, // JSON.DEFAULT_GENERATE_FEATURE, // features); } else { JsonpResult jsonp = (JsonpResult) obj; text = new StringBuilder(jsonp.getJsonpFunction()) .append('(') .append(JSON.toJSONString( jsonp.getValue(), // SerializeConfig.globalInstance, filters, dateFormat, JSON.DEFAULT_GENERATE_FEATURE, features)) .append(");").toString(); } this.writeAfter(text); byte[] bytes = text.getBytes(charset); headers.setContentLength(bytes.length); OutputStream out = outputMessage.getBody(); out.write(bytes); out.flush(); }
Example 11
Source File: SourceHttpMessageConverter.java From java-technology-stack with MIT License | 5 votes |
@Override protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { try { Result result = new StreamResult(outputMessage.getBody()); transform(t, result); } catch (TransformerException ex) { throw new HttpMessageNotWritableException("Could not transform [" + t + "] to output message", ex); } }
Example 12
Source File: ProtobufHttpMessageConverter.java From java-technology-stack with MIT License | 5 votes |
@Override protected void writeInternal(Message message, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { MediaType contentType = outputMessage.getHeaders().getContentType(); if (contentType == null) { contentType = getDefaultContentType(message); Assert.state(contentType != null, "No content type"); } Charset charset = contentType.getCharset(); if (charset == null) { charset = DEFAULT_CHARSET; } if (PROTOBUF.isCompatibleWith(contentType)) { setProtoHeader(outputMessage, message); CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody()); message.writeTo(codedOutputStream); codedOutputStream.flush(); } else if (TEXT_PLAIN.isCompatibleWith(contentType)) { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset); TextFormat.print(message, outputStreamWriter); outputStreamWriter.flush(); outputMessage.getBody().flush(); } else if (this.protobufFormatSupport != null) { this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset); outputMessage.getBody().flush(); } }
Example 13
Source File: SourceHttpMessageConverter.java From spring-analysis-note with MIT License | 5 votes |
@Override protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { try { Result result = new StreamResult(outputMessage.getBody()); transform(t, result); } catch (TransformerException ex) { throw new HttpMessageNotWritableException("Could not transform [" + t + "] to output message", ex); } }
Example 14
Source File: ProtobufHttpMessageConverter.java From spring-analysis-note with MIT License | 5 votes |
@Override protected void writeInternal(Message message, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { MediaType contentType = outputMessage.getHeaders().getContentType(); if (contentType == null) { contentType = getDefaultContentType(message); Assert.state(contentType != null, "No content type"); } Charset charset = contentType.getCharset(); if (charset == null) { charset = DEFAULT_CHARSET; } if (PROTOBUF.isCompatibleWith(contentType)) { setProtoHeader(outputMessage, message); CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody()); message.writeTo(codedOutputStream); codedOutputStream.flush(); } else if (TEXT_PLAIN.isCompatibleWith(contentType)) { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset); TextFormat.print(message, outputStreamWriter); outputStreamWriter.flush(); outputMessage.getBody().flush(); } else if (this.protobufFormatSupport != null) { this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset); outputMessage.getBody().flush(); } }
Example 15
Source File: ResourceRegionHttpMessageConverter.java From java-technology-stack with MIT License | 4 votes |
private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions, HttpOutputMessage outputMessage) throws IOException { Assert.notNull(resourceRegions, "Collection of ResourceRegion should not be null"); HttpHeaders responseHeaders = outputMessage.getHeaders(); MediaType contentType = responseHeaders.getContentType(); String boundaryString = MimeTypeUtils.generateMultipartBoundaryString(); responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString); OutputStream out = outputMessage.getBody(); for (ResourceRegion region : resourceRegions) { long start = region.getPosition(); long end = start + region.getCount() - 1; InputStream in = region.getResource().getInputStream(); try { // Writing MIME header. println(out); print(out, "--" + boundaryString); println(out); if (contentType != null) { print(out, "Content-Type: " + contentType.toString()); println(out); } Long resourceLength = region.getResource().contentLength(); end = Math.min(end, resourceLength - 1); print(out, "Content-Range: bytes " + start + '-' + end + '/' + resourceLength); println(out); println(out); // Printing content StreamUtils.copyRange(in, out, start, end); } finally { try { in.close(); } catch (IOException ex) { // ignore } } } println(out); print(out, "--" + boundaryString + "--"); }
Example 16
Source File: AbstractJsonHttpMessageConverter.java From java-technology-stack with MIT License | 4 votes |
private static Writer getWriter(HttpOutputMessage outputMessage) throws IOException { return new OutputStreamWriter(outputMessage.getBody(), getCharset(outputMessage.getHeaders())); }
Example 17
Source File: ResourceRegionHttpMessageConverter.java From lams with GNU General Public License v2.0 | 4 votes |
private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions, HttpOutputMessage outputMessage) throws IOException { Assert.notNull(resourceRegions, "Collection of ResourceRegion should not be null"); HttpHeaders responseHeaders = outputMessage.getHeaders(); MediaType contentType = responseHeaders.getContentType(); String boundaryString = MimeTypeUtils.generateMultipartBoundaryString(); responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString); OutputStream out = outputMessage.getBody(); for (ResourceRegion region : resourceRegions) { long start = region.getPosition(); long end = start + region.getCount() - 1; InputStream in = region.getResource().getInputStream(); try { // Writing MIME header. println(out); print(out, "--" + boundaryString); println(out); if (contentType != null) { print(out, "Content-Type: " + contentType.toString()); println(out); } Long resourceLength = region.getResource().contentLength(); end = Math.min(end, resourceLength - 1); print(out, "Content-Range: bytes " + start + '-' + end + '/' + resourceLength); println(out); println(out); // Printing content StreamUtils.copyRange(in, out, start, end); } finally { try { in.close(); } catch (IOException ex) { // ignore } } } println(out); print(out, "--" + boundaryString + "--"); }
Example 18
Source File: ServletAnnotationControllerHandlerMethodTests.java From java-technology-stack with MIT License | 4 votes |
@Override public void write(Object o, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { outputMessage.getHeaders().setContentType(contentType); outputMessage.getBody(); // force a header write }
Example 19
Source File: ResourceRegionHttpMessageConverter.java From spring-analysis-note with MIT License | 4 votes |
private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions, HttpOutputMessage outputMessage) throws IOException { Assert.notNull(resourceRegions, "Collection of ResourceRegion should not be null"); HttpHeaders responseHeaders = outputMessage.getHeaders(); MediaType contentType = responseHeaders.getContentType(); String boundaryString = MimeTypeUtils.generateMultipartBoundaryString(); responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString); OutputStream out = outputMessage.getBody(); for (ResourceRegion region : resourceRegions) { long start = region.getPosition(); long end = start + region.getCount() - 1; InputStream in = region.getResource().getInputStream(); try { // Writing MIME header. println(out); print(out, "--" + boundaryString); println(out); if (contentType != null) { print(out, "Content-Type: " + contentType.toString()); println(out); } Long resourceLength = region.getResource().contentLength(); end = Math.min(end, resourceLength - 1); print(out, "Content-Range: bytes " + start + '-' + end + '/' + resourceLength); println(out); println(out); // Printing content StreamUtils.copyRange(in, out, start, end); } finally { try { in.close(); } catch (IOException ex) { // ignore } } } println(out); print(out, "--" + boundaryString + "--"); }
Example 20
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring-analysis-note with MIT License | 4 votes |
@Override public void write(Object o, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { outputMessage.getHeaders().setContentType(contentType); outputMessage.getBody(); // force a header write }