Search in sources :

Example 1 with ResponseHeader

use of org.apache.servicecomb.transport.highway.message.ResponseHeader in project incubator-servicecomb-java-chassis by apache.

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<>());
    header.getContext().put("a", "10");
    Buffer responseBuf = HighwayCodec.encodeResponse(0, header, null, null, new ProtobufFeature());
    TcpData tcp = new TcpData(responseBuf.slice(23, responseBuf.length()), null);
    Response response = HighwayCodec.decodeResponse(invocation, operationProtobuf, tcp, null);
    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(org.apache.servicecomb.swagger.invocation.Response) ResponseHeader(org.apache.servicecomb.transport.highway.message.ResponseHeader) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) NotWrapSchema(org.apache.servicecomb.codec.protobuf.utils.schema.NotWrapSchema) WrapSchema(org.apache.servicecomb.codec.protobuf.utils.WrapSchema) TcpData(org.apache.servicecomb.foundation.vertx.client.tcp.TcpData) ProtobufFeature(io.protostuff.runtime.ProtobufFeature) Test(org.junit.Test)

Example 2 with ResponseHeader

use of org.apache.servicecomb.transport.highway.message.ResponseHeader in project incubator-servicecomb-java-chassis by apache.

the class HighwayCodec method decodeResponse.

public static Response decodeResponse(Invocation invocation, OperationProtobuf operationProtobuf, TcpData tcpData, ProtobufFeature protobufFeature) throws Exception {
    ResponseHeader header = ResponseHeader.readObject(tcpData.getHeaderBuffer(), protobufFeature);
    if (header.getContext() != null) {
        invocation.getContext().putAll(header.getContext());
    }
    WrapSchema bodySchema = operationProtobuf.findResponseSchema(header.getStatusCode());
    Object body = bodySchema.readObject(tcpData.getBodyBuffer(), protobufFeature);
    Response response = Response.create(header.getStatusCode(), header.getReasonPhrase(), body);
    response.setHeaders(header.getHeaders());
    return response;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ResponseHeader(org.apache.servicecomb.transport.highway.message.ResponseHeader) WrapSchema(org.apache.servicecomb.codec.protobuf.utils.WrapSchema)

Example 3 with ResponseHeader

use of org.apache.servicecomb.transport.highway.message.ResponseHeader in project incubator-servicecomb-java-chassis by apache.

the class HighwayServerConnection method onLogin.

protected void onLogin(long msgId, RequestHeader header, Buffer bodyBuffer) {
    LoginRequest request = null;
    try {
        request = LoginRequest.readObject(bodyBuffer);
    } catch (Exception e) {
        String msg = String.format("decode setParameter error, msgId=%d", msgId);
        LOGGER.error(msg, e);
        netSocket.close();
        return;
    }
    if (request != null) {
        this.setProtocol(request.getProtocol());
        this.setZipName(request.getZipName());
        this.protobufFeature.setUseProtobufMapCodec(request.isUseProtobufMapCodec());
    }
    try (HighwayOutputStream os = new HighwayOutputStream(msgId, protobufFeature)) {
        ResponseHeader responseHeader = new ResponseHeader();
        responseHeader.setStatusCode(Status.OK.getStatusCode());
        LoginResponse response = new LoginResponse();
        response.setUseProtobufMapCodec(protobufFeature.isUseProtobufMapCodec());
        os.write(ResponseHeader.getResponseHeaderSchema(), responseHeader, LoginResponse.getLoginResponseSchema(), response);
        netSocket.write(os.getBuffer());
    } catch (Exception e) {
        throw new Error("impossible.", e);
    }
}
Also used : ResponseHeader(org.apache.servicecomb.transport.highway.message.ResponseHeader) LoginResponse(org.apache.servicecomb.transport.highway.message.LoginResponse) LoginRequest(org.apache.servicecomb.transport.highway.message.LoginRequest)

Example 4 with ResponseHeader

use of org.apache.servicecomb.transport.highway.message.ResponseHeader in project incubator-servicecomb-java-chassis by apache.

the class HighwayServerInvoke method sendResponse.

private void sendResponse(Map<String, String> context, Response response) {
    ResponseHeader header = new ResponseHeader();
    header.setStatusCode(response.getStatusCode());
    header.setReasonPhrase(response.getReasonPhrase());
    header.setContext(context);
    header.setHeaders(response.getHeaders());
    WrapSchema bodySchema = operationProtobuf.findResponseSchema(response.getStatusCode());
    Object body = response.getResult();
    if (response.isFailed()) {
        body = ((InvocationException) body).getErrorData();
    }
    try {
        Buffer respBuffer = HighwayCodec.encodeResponse(msgId, header, bodySchema, body, protobufFeature);
        connection.write(respBuffer.getByteBuf());
    } catch (Exception e) {
        // 没招了,直接打日志
        String msg = String.format("encode response failed, %s, msgId=%d", operationProtobuf.getOperationMeta().getMicroserviceQualifiedName(), msgId);
        LOGGER.error(msg, e);
    } finally {
        invocation.onFinish(response);
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ResponseHeader(org.apache.servicecomb.transport.highway.message.ResponseHeader) WrapSchema(org.apache.servicecomb.codec.protobuf.utils.WrapSchema) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException)

Aggregations

ResponseHeader (org.apache.servicecomb.transport.highway.message.ResponseHeader)4 WrapSchema (org.apache.servicecomb.codec.protobuf.utils.WrapSchema)3 Buffer (io.vertx.core.buffer.Buffer)2 Response (org.apache.servicecomb.swagger.invocation.Response)2 ProtobufFeature (io.protostuff.runtime.ProtobufFeature)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 NotWrapSchema (org.apache.servicecomb.codec.protobuf.utils.schema.NotWrapSchema)1 Invocation (org.apache.servicecomb.core.Invocation)1 TcpData (org.apache.servicecomb.foundation.vertx.client.tcp.TcpData)1 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)1 LoginRequest (org.apache.servicecomb.transport.highway.message.LoginRequest)1 LoginResponse (org.apache.servicecomb.transport.highway.message.LoginResponse)1 Test (org.junit.Test)1