Search in sources :

Example 1 with ProduceProcessor

use of io.servicecomb.common.rest.codec.produce.ProduceProcessor in project java-chassis by ServiceComb.

the class TestVertxRestServer method testDoSend.

@Test
public void testDoSend() {
    boolean status = false;
    try {
        HttpServerResponse httpServerResponse = Mockito.mock(HttpServerResponse.class);
        ProduceProcessor produceProcessor = Mockito.mock(ProduceProcessor.class);
        Response response = Response.create(0, "reasonPhrase", new Object());
        instance.doSendResponse(httpServerResponse, produceProcessor, response);
    } catch (Exception e) {
        status = true;
    }
    Assert.assertFalse(status);
}
Also used : HttpServerResponse(io.vertx.core.http.HttpServerResponse) Response(io.servicecomb.core.Response) ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Test(org.junit.Test)

Example 2 with ProduceProcessor

use of io.servicecomb.common.rest.codec.produce.ProduceProcessor in project java-chassis by ServiceComb.

the class AbstractRestServer method runOnExecutor.

protected void runOnExecutor(RestServerRequestInternal restRequest, RestOperationMeta restOperation, HTTP_RESPONSE httpResponse) throws Exception {
    String acceptType = restRequest.getHeaderParam("Accept");
    ProduceProcessor produceProcessor = locateProduceProcessor(restRequest, httpResponse, restOperation, acceptType);
    if (produceProcessor == null) {
        // locateProduceProcessor内部已经应答了
        return;
    }
    Object[] args = RestCodec.restToArgs(restRequest, restOperation);
    Invocation invocation = InvocationFactory.forProvider(transport.getEndpoint(), restOperation.getOperationMeta(), args);
    this.setContext(invocation, restRequest);
    this.setHttpRequestContext(invocation, restRequest);
    invocation.next(resp -> {
        sendResponse(restRequest, httpResponse, produceProcessor, resp);
    });
}
Also used : ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor) Invocation(io.servicecomb.core.Invocation)

Example 3 with ProduceProcessor

use of io.servicecomb.common.rest.codec.produce.ProduceProcessor in project java-chassis by ServiceComb.

the class AbstractRestServer method locateProduceProcessor.

protected ProduceProcessor locateProduceProcessor(RestServerRequestInternal restRequest, HTTP_RESPONSE httpResponse, RestOperationMeta restOperation, String acceptType) {
    ProduceProcessor produceProcessor = restOperation.ensureFindProduceProcessor(acceptType);
    if (produceProcessor != null) {
        return produceProcessor;
    }
    String msg = String.format("Accept %s is not supported", acceptType);
    InvocationException exception = new InvocationException(Status.NOT_ACCEPTABLE, msg);
    sendFailResponse(restRequest, httpResponse, exception);
    return null;
}
Also used : ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor) InvocationException(io.servicecomb.core.exception.InvocationException)

Example 4 with ProduceProcessor

use of io.servicecomb.common.rest.codec.produce.ProduceProcessor in project java-chassis by ServiceComb.

the class RestOperationMeta method createProduceProcessors.

protected void createProduceProcessors() {
    if (null == produces || produces.isEmpty()) {
        for (ProduceProcessor processor : ProduceProcessorManager.INSTANCE.values()) {
            this.produceProcessorMap.put(processor.getName(), processor);
        }
        return;
    }
    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);
    }
    defaultProcessor = getDefaultOrFirstProcessor();
}
Also used : ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor)

Example 5 with ProduceProcessor

use of io.servicecomb.common.rest.codec.produce.ProduceProcessor in project java-chassis by ServiceComb.

the class VertxHttpMethod method handleResponse.

protected void handleResponse(Invocation invocation, HttpClientResponse httpResponse, RestOperationMeta restOperation, AsyncResponse asyncResp) {
    ProduceProcessor produceProcessor = null;
    String contentType = httpResponse.getHeader("Content-Type");
    if (contentType != null) {
        String contentTypeForFind = contentType;
        int idx = contentType.indexOf(';');
        if (idx != -1) {
            contentTypeForFind = contentType.substring(0, idx);
        }
        produceProcessor = restOperation.findProduceProcessor(contentTypeForFind);
    }
    if (produceProcessor == null) {
        String msg = String.format("path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", restOperation.getAbsolutePath(), httpResponse.statusCode(), httpResponse.statusMessage(), contentType);
        Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
        asyncResp.fail(invocation.getInvocationType(), exception);
        return;
    }
    ProduceProcessor finalProduceProcessor = produceProcessor;
    httpResponse.bodyHandler(responseBuf -> {
        invocation.getResponseExecutor().execute(() -> {
            try {
                ResponseMeta responseMeta = restOperation.getOperationMeta().findResponseMeta(httpResponse.statusCode());
                Object result = finalProduceProcessor.decodeResponse(responseBuf, responseMeta.getJavaType());
                Response response = Response.create(httpResponse.statusCode(), httpResponse.statusMessage(), result);
                for (String headerName : responseMeta.getHeaders().keySet()) {
                    List<String> headerValues = httpResponse.headers().getAll(headerName);
                    for (String headerValue : headerValues) {
                        response.getHeaders().addHeader(headerName, headerValue);
                    }
                }
                asyncResp.complete(response);
            } catch (Throwable e) {
                asyncResp.fail(invocation.getInvocationType(), e);
            }
        });
    });
}
Also used : AsyncResponse(io.servicecomb.core.AsyncResponse) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Response(io.servicecomb.core.Response) ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor) ResponseMeta(io.servicecomb.swagger.invocation.response.ResponseMeta) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData)

Aggregations

ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)8 Test (org.junit.Test)4 Invocation (io.servicecomb.core.Invocation)3 Response (io.servicecomb.core.Response)3 HttpClientResponse (io.vertx.core.http.HttpClientResponse)3 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)2 URLPathBuilder (io.servicecomb.common.rest.definition.path.URLPathBuilder)2 AsyncResponse (io.servicecomb.core.AsyncResponse)2 Endpoint (io.servicecomb.core.Endpoint)2 OperationMeta (io.servicecomb.core.definition.OperationMeta)2 CommonExceptionData (io.servicecomb.core.exception.CommonExceptionData)1 InvocationException (io.servicecomb.core.exception.InvocationException)1 ResponseMeta (io.servicecomb.swagger.invocation.response.ResponseMeta)1 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1