Search in sources :

Example 6 with ProduceProcessor

use of org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor in project incubator-servicecomb-java-chassis by apache.

the class RestOperationMeta method createProduceProcessors.

// 为operation创建支持的多种produce processor
protected void createProduceProcessors() {
    if (null == produces || produces.isEmpty()) {
        for (ProduceProcessor processor : ProduceProcessorManager.INSTANCE.values()) {
            this.produceProcessorMap.put(processor.getName(), processor);
        }
    } else {
        for (String produce : produces) {
            ProduceProcessor processor = ProduceProcessorManager.INSTANCE.findValue(produce);
            if (processor == null) {
                LOGGER.error("produce {} is not supported", produce);
                continue;
            }
            this.produceProcessorMap.put(produce, processor);
        }
        if (produceProcessorMap.isEmpty()) {
            produceProcessorMap.put(ProduceProcessorManager.DEFAULT_TYPE, ProduceProcessorManager.DEFAULT_PROCESSOR);
        }
    }
    defaultProcessor = produceProcessorMap.values().stream().findFirst().get();
    produceProcessorMap.putIfAbsent(MediaType.WILDCARD, defaultProcessor);
}
Also used : ProduceProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor)

Example 7 with ProduceProcessor

use of org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor in project incubator-servicecomb-java-chassis by apache.

the class ServerRestArgsFilter method beforeSendResponse.

@Override
public void beforeSendResponse(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();
    }
    try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) {
        produceProcessor.encodeResponse(output, body);
        responseEx.setBodyBuffer(output.getBuffer());
    } catch (Throwable e) {
        throw ExceptionFactory.convertProducerException(e);
    }
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ProduceProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor) BufferOutputStream(org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream)

Aggregations

ProduceProcessor (org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor)7 Test (org.junit.Test)4 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)3 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)3 HttpClientResponse (io.vertx.core.http.HttpClientResponse)2 URLPathBuilder (org.apache.servicecomb.common.rest.definition.path.URLPathBuilder)2 Endpoint (org.apache.servicecomb.core.Endpoint)2 Invocation (org.apache.servicecomb.core.Invocation)2 Response (org.apache.servicecomb.swagger.invocation.Response)2 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 BufferOutputStream (org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream)1 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)1 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)1 ResponseMeta (org.apache.servicecomb.swagger.invocation.response.ResponseMeta)1