Search in sources :

Example 11 with BufferOutputStream

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;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ProduceProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor) CompletableFuture(java.util.concurrent.CompletableFuture) BufferOutputStream(org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream)

Example 12 with BufferOutputStream

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();
    }
}
Also used : BufferOutputStream(org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream)

Aggregations

BufferOutputStream (org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream)12 ProduceProcessor (org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor)2 Response (org.apache.servicecomb.swagger.invocation.Response)2 Before (org.junit.Before)2 Test (org.junit.Test)2 LinkedBuffer (io.protostuff.LinkedBuffer)1 ProtobufOutput (io.protostuff.ProtobufOutput)1 Buffer (io.vertx.core.buffer.Buffer)1 IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1