use of io.servicecomb.transport.highway.message.ResponseHeader in project java-chassis by ServiceComb.
the class HighwayCodec method decodeResponse.
public static Response decodeResponse(Invocation invocation, OperationProtobuf operationProtobuf, TcpData tcpData) throws Exception {
ResponseHeader header = ResponseHeader.readObject(tcpData.getHeaderBuffer());
invocation.getContext().putAll(header.getContext());
WrapSchema bodySchema = operationProtobuf.findResponseSchema(header.getStatusCode());
Object body = bodySchema.readObject(tcpData.getBodyBuffer());
Response response = Response.create(header.getStatusCode(), header.getReasonPhrase(), body);
response.setHeaders(header.getHeaders());
return response;
}
use of io.servicecomb.transport.highway.message.ResponseHeader in project java-chassis by ServiceComb.
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());
}
try (HighwayOutputStream os = new HighwayOutputStream(msgId)) {
ResponseHeader responseHeader = new ResponseHeader();
responseHeader.setStatusCode(Status.OK.getStatusCode());
LoginResponse response = new LoginResponse();
os.write(ResponseHeader.getResponseHeaderSchema(), responseHeader, LoginResponse.getLoginResponseSchema(), response);
netSocket.write(os.getBuffer());
} catch (Exception e) {
throw new Error("impossible.", e);
}
}
use of io.servicecomb.transport.highway.message.ResponseHeader in project java-chassis by ServiceComb.
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);
netSocket.write(respBuffer);
} catch (Exception e) {
// 没招了,直接打日志
String msg = String.format("encode response failed, %s, msgId=%d", operationProtobuf.getOperationMeta().getMicroserviceQualifiedName(), msgId);
LOGGER.error(msg, e);
}
}
use of io.servicecomb.transport.highway.message.ResponseHeader 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());
}
Aggregations