Search in sources :

Example 21 with Response

use of io.servicecomb.core.Response in project java-chassis by ServiceComb.

the class TestProducerSchemaFactory method testGetOrCreateProducer.

@Test
public void testGetOrCreateProducer() throws Exception {
    OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
    Assert.assertEquals("add", operationMeta.getOperationId());
    ProducerOperation producerOperation = operationMeta.getExtData(Const.PRODUCER_OPERATION);
    Object addBody = Class.forName("cse.gen.app.ms.schema.addBody").newInstance();
    ReflectUtils.setField(addBody, "x", 1);
    ReflectUtils.setField(addBody, "y", 2);
    Invocation invocation = new Invocation((Endpoint) null, operationMeta, new Object[] { addBody });
    Holder<Response> holder = new Holder<>();
    producerOperation.invoke(invocation, resp -> {
        holder.value = resp;
    });
    Assert.assertEquals(3, (int) holder.value.getResult());
    invocation = new Invocation((Endpoint) null, operationMeta, new Object[] { 1, 2 });
    producerOperation.invoke(invocation, resp -> {
        holder.value = resp;
    });
    Assert.assertEquals(true, holder.value.isFailed());
    InvocationException exception = (InvocationException) holder.value.getResult();
    CommonExceptionData data = (CommonExceptionData) exception.getErrorData();
    Assert.assertEquals("Cse Internal Server Error", data.getMessage());
}
Also used : Response(io.servicecomb.core.Response) Invocation(io.servicecomb.core.Invocation) Endpoint(io.servicecomb.core.Endpoint) ProducerOperation(io.servicecomb.core.provider.producer.ProducerOperation) InvocationException(io.servicecomb.core.exception.InvocationException) Holder(javax.xml.ws.Holder) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) OperationMeta(io.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 22 with Response

use of io.servicecomb.core.Response in project java-chassis by ServiceComb.

the class TestHighwayCodec method testDecodeResponse.

@Test
public void testDecodeResponse() throws Exception {
    Invocation invocation = Mockito.mock(Invocation.class);
    Mockito.when(operationProtobuf.findResponseSchema(200)).thenReturn(Mockito.mock(WrapSchema.class));
    Map<String, String> context = new HashMap<>();
    Mockito.when(invocation.getContext()).thenReturn(context);
    TcpData tcpData = Mockito.mock(TcpData.class);
    Mockito.when(tcpData.getHeaderBuffer()).thenReturn(bodyBuffer);
    commonMock();
    ResponseHeader header = new ResponseHeader();
    header.setStatusCode(200);
    header.setContext(new HashMap<String, String>());
    header.getContext().put("a", "10");
    Buffer responseBuf = HighwayCodec.encodeResponse(0, header, null, null);
    TcpData tcp = new TcpData(responseBuf.slice(23, responseBuf.length()), null);
    Response response = HighwayCodec.decodeResponse(invocation, operationProtobuf, tcp);
    Assert.assertEquals("10", invocation.getContext().get("a"));
    Assert.assertEquals(200, response.getStatusCode());
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(io.vertx.core.buffer.Buffer) Response(io.servicecomb.core.Response) ResponseHeader(io.servicecomb.transport.highway.message.ResponseHeader) Invocation(io.servicecomb.core.Invocation) HashMap(java.util.HashMap) WrapSchema(io.servicecomb.codec.protobuf.utils.WrapSchema) NotWrapSchema(io.servicecomb.codec.protobuf.utils.schema.NotWrapSchema) TcpData(io.servicecomb.foundation.vertx.client.tcp.TcpData) Test(org.junit.Test)

Example 23 with Response

use of io.servicecomb.core.Response 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)

Example 24 with Response

use of io.servicecomb.core.Response in project java-chassis by ServiceComb.

the class TestServletRestServer method testDoSendResponse.

@Test
public void testDoSendResponse() {
    boolean status = true;
    HttpServletResponse httpServerResponse = Mockito.mock(HttpServletResponse.class);
    ProduceProcessor produceProcessor = Mockito.mock(ProduceProcessor.class);
    Object result = Mockito.mock(Object.class);
    Mockito.when(produceProcessor.getName()).thenReturn("testCall");
    assertEquals("testCall", produceProcessor.getName());
    try {
        Response response = Response.create(12, "gjhghjgk", result);
        servletRestServer.doSendResponse(httpServerResponse, produceProcessor, response);
    } catch (Exception exce) {
        Assert.assertNotNull(exce);
        status = false;
    }
    Assert.assertTrue(status);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(io.servicecomb.core.Response) ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 25 with Response

use of io.servicecomb.core.Response in project java-chassis by ServiceComb.

the class BizkeeperHandlerDelegate method forceFallbackCommand.

protected HystrixObservable<Response> forceFallbackCommand(Invocation invocation) {
    return new HystrixObservable<Response>() {

        @Override
        public Observable<Response> observe() {
            ReplaySubject<Response> subject = ReplaySubject.create();
            final Subscription sourceSubscription = toObservable().subscribe(subject);
            return subject.doOnUnsubscribe(new Action0() {

                @Override
                public void call() {
                    sourceSubscription.unsubscribe();
                }
            });
        }

        @Override
        public Observable<Response> toObservable() {
            return Observable.create(f -> {
                try {
                    if (Configuration.FALLBACKPOLICY_POLICY_RETURN.equals(Configuration.INSTANCE.getFallbackPolicyPolicy(handler.groupname, invocation.getMicroserviceName(), invocation.getOperationMeta().getMicroserviceQualifiedName()))) {
                        f.onNext(Response.succResp(null));
                    } else {
                        f.onNext(Response.failResp(invocation.getInvocationType(), BizkeeperExceptionUtils.createBizkeeperException(BizkeeperExceptionUtils.CSE_HANDLER_BK_FALLBACK, null, invocation.getOperationMeta().getMicroserviceQualifiedName())));
                    }
                } catch (Exception e) {
                    f.onError(e);
                }
            });
        }

        ;
    };
}
Also used : Response(io.servicecomb.core.Response) Action0(rx.functions.Action0) Subscription(rx.Subscription) HystrixObservable(com.netflix.hystrix.HystrixObservable)

Aggregations

Response (io.servicecomb.core.Response)37 Test (org.junit.Test)21 Invocation (io.servicecomb.core.Invocation)20 OperationMeta (io.servicecomb.core.definition.OperationMeta)11 AsyncResponse (io.servicecomb.core.AsyncResponse)8 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)7 WrapSchema (io.servicecomb.codec.protobuf.utils.WrapSchema)7 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)6 HttpServerResponse (io.vertx.core.http.HttpServerResponse)5 InvocationException (io.servicecomb.core.exception.InvocationException)4 MockUp (mockit.MockUp)4 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)3 SyncResponseExecutor (io.servicecomb.core.provider.consumer.SyncResponseExecutor)3 User (io.servicecomb.demo.server.User)3 HttpClientResponse (io.vertx.core.http.HttpClientResponse)3 List (java.util.List)3 ExecutionListener (com.netflix.loadbalancer.reactive.ExecutionListener)2 CommonExceptionData (io.servicecomb.core.exception.CommonExceptionData)2 ResponseHeaders (io.servicecomb.swagger.extend.annotations.ResponseHeaders)2 ApiResponse (io.swagger.annotations.ApiResponse)2