Java Code Examples for org.apache.servicecomb.swagger.invocation.Response#create()

The following examples show how to use org.apache.servicecomb.swagger.invocation.Response#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HighwayCodec.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public static Response decodeResponse(Invocation invocation, OperationProtobuf operationProtobuf, TcpData tcpData)
    throws Exception {
  ResponseHeader header = ResponseHeader.readObject(tcpData.getHeaderBuffer());
  if (header.getContext() != null) {
    invocation.getContext().putAll(header.getContext());
  }

  ResponseRootDeserializer<Object> bodySchema = operationProtobuf
      .findResponseRootDeserializer(header.getStatusCode());
  Object body = bodySchema
      .deserialize(tcpData.getBodyBuffer().getBytes(), invocation.findResponseType(header.getStatusCode()));

  Response response = Response.create(header.getStatusCode(), header.getReasonPhrase(), body);
  response.setHeaders(header.getHeaders());

  return response;
}
 
Example 2
Source File: ClientSignature.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public Response afterReceiveResponse(Invocation invocation, HttpServletResponseEx responseEx) {
  String signature = SignatureUtils.genSignature(responseEx);
  String serverSignature = responseEx.getHeader("signature");

  if (serverSignature != null) {
    LOGGER.debug("check response signature, client: {}, server: {}.", signature, serverSignature);
    if (!signature.equals(serverSignature)) {
      LOGGER.error("check response signature failed");
      return Response.create(Status.UNAUTHORIZED, "check response signature failed");
    }
  }
  return null;
}
 
Example 3
Source File: TestResponse.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
  Response r = Response.create(200, "200", 2);
  Assert.assertEquals(200, r.getStatusCode());
  Assert.assertEquals(2, (int) r.getResult());
  Response r1 = r.build();
  Assert.assertEquals(r, r1);

  r = Response.create(300, "300", 3);
  Assert.assertEquals(300, r.getStatusCode());
  Assert.assertEquals("300", r.getReasonPhrase());
  Assert.assertEquals(3, ((InvocationException) r.getResult()).getErrorData());

  r = Response.createSuccess(Status.OK, 2);
  Assert.assertEquals(200, r.getStatusCode());
  Assert.assertEquals(2, (int) r.getResult());

  r = Response.success(2, Status.OK);
  Assert.assertEquals(200, r.getStatusCode());
  Assert.assertEquals(2, (int) r.getResult());

  r = Response.createFail(InvocationType.CONSUMER, "abc");
  Assert.assertEquals("CommonExceptionData [message=abc]",
      ((InvocationException) r.getResult()).getErrorData().toString());
  Assert.assertEquals(490, r.getStatusCode());

  r = Response.createFail(InvocationType.PRODUCER, "def");
  Assert.assertEquals("CommonExceptionData [message=def]",
      ((InvocationException) r.getResult()).getErrorData().toString());
  Assert.assertEquals(590, r.getStatusCode());
}
 
Example 4
Source File: TestConfig.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void testResponse() {
  Response response = Response.create(400, "test", null);
  InvocationException exception = response.getResult();
  Assert.assertEquals(null, exception.getErrorData());

  response = Response.create(400, "test", "errorData");
  exception = response.getResult();
  Assert.assertEquals("errorData", exception.getErrorData());
}
 
Example 5
Source File: DefaultProducerResponseMapper.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public Response mapResponse(StatusType status, Object response) {
  return Response.create(status, response);
}
 
Example 6
Source File: ErrorToProducerResponseConverter.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public Response convert(SwaggerInvocation swaggerInvocation, Error e) {
  return Response.create(Status.OK, "response from error: " + e.getMessage());
}
 
Example 7
Source File: ErrorToProducerResponseConverter.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public Response convert(SwaggerInvocation swaggerInvocation, Error e) {
  return Response.create(Status.OK, "response from error: " + e.getMessage());
}