use of org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream in project java-chassis by ServiceComb.
the class ServerRestArgsFilter method beforeSendResponseAsync.
@Override
public CompletableFuture<Void> beforeSendResponseAsync(Invocation invocation, HttpServletResponseEx responseEx) {
Response response = (Response) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_RESPONSE);
ProduceProcessor produceProcessor = (ProduceProcessor) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_PROCESSOR);
Object body = response.getResult();
if (response.isFailed()) {
body = ((InvocationException) body).getErrorData();
}
if (null != invocation && isDownloadFileResponseType(invocation, response)) {
return responseEx.sendPart(PartUtils.getSinglePart(null, body));
}
responseEx.setContentType(produceProcessor.getName() + "; charset=utf-8");
CompletableFuture<Void> future = new CompletableFuture<>();
try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) {
produceProcessor.encodeResponse(output, body);
responseEx.setBodyBuffer(output.getBuffer());
future.complete(null);
} catch (Throwable e) {
future.completeExceptionally(ExceptionFactory.convertProducerException(e));
}
return future;
}
use of org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream in project java-chassis by ServiceComb.
the class RestClientRequestImpl method genBodyBuffer.
private void genBodyBuffer() throws Exception {
if (bodyBuffer != null) {
return;
}
if (formMap == null) {
return;
}
request.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED);
try (BufferOutputStream output = new BufferOutputStream()) {
for (Entry<String, Object> entry : formMap.entrySet()) {
output.write(entry.getKey().getBytes(StandardCharsets.UTF_8));
output.write('=');
if (entry.getValue() != null) {
String value = RestObjectMapperFactory.getRestObjectMapper().convertToString(entry.getValue());
value = URLEncoder.encode(value, StandardCharsets.UTF_8.name());
output.write(value.getBytes(StandardCharsets.UTF_8));
}
output.write('&');
}
bodyBuffer = output.getBuffer();
}
}
Aggregations