Search in sources :

Example 1 with HttpStatus

use of org.apache.servicecomb.swagger.invocation.context.HttpStatus in project incubator-servicecomb-java-chassis by apache.

the class SpringmvcProducerResponseMapper method mapResponse.

@SuppressWarnings("unchecked")
@Override
public Response mapResponse(StatusType status, Object response) {
    ResponseEntity<Object> springmvcResponse = (ResponseEntity<Object>) response;
    StatusType responseStatus = new HttpStatus(springmvcResponse.getStatusCode().value(), springmvcResponse.getStatusCode().getReasonPhrase());
    Response cseResponse = null;
    if (HttpStatus.isSuccess(responseStatus)) {
        cseResponse = realMapper.mapResponse(responseStatus, springmvcResponse.getBody());
    } else {
        // not support fail response mapper now
        cseResponse = Response.status(responseStatus).entity(springmvcResponse.getBody());
    }
    HttpHeaders headers = springmvcResponse.getHeaders();
    Headers cseHeaders = cseResponse.getHeaders();
    for (Entry<String, List<String>> entry : headers.entrySet()) {
        if (entry.getValue() == null || entry.getValue().isEmpty()) {
            continue;
        }
        for (String value : entry.getValue()) {
            cseHeaders.addHeader(entry.getKey(), value);
        }
    }
    return cseResponse;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.apache.servicecomb.swagger.invocation.context.HttpStatus) StatusType(javax.ws.rs.core.Response.StatusType) HttpHeaders(org.springframework.http.HttpHeaders) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) List(java.util.List)

Example 2 with HttpStatus

use of org.apache.servicecomb.swagger.invocation.context.HttpStatus in project java-chassis by ServiceComb.

the class TestConfig method testHttpStatus.

@Test
public void testHttpStatus() {
    StatusType oStatus = new HttpStatus(204, "InternalServerError");
    Assert.assertEquals("InternalServerError", oStatus.getReasonPhrase());
}
Also used : HttpStatus(org.apache.servicecomb.swagger.invocation.context.HttpStatus) StatusType(javax.ws.rs.core.Response.StatusType) Test(org.junit.Test)

Example 3 with HttpStatus

use of org.apache.servicecomb.swagger.invocation.context.HttpStatus in project incubator-servicecomb-java-chassis by apache.

the class TestConfig method testHttpStatus.

@Test
public void testHttpStatus() {
    StatusType oStatus = new HttpStatus(204, "InternalServerError");
    Assert.assertEquals("InternalServerError", oStatus.getReasonPhrase());
}
Also used : HttpStatus(org.apache.servicecomb.swagger.invocation.context.HttpStatus) StatusType(javax.ws.rs.core.Response.StatusType) Test(org.junit.Test)

Example 4 with HttpStatus

use of org.apache.servicecomb.swagger.invocation.context.HttpStatus in project java-chassis by ServiceComb.

the class SpringmvcProducerResponseMapper method mapResponse.

@SuppressWarnings("unchecked")
@Override
public Response mapResponse(StatusType status, Object response) {
    ResponseEntity<Object> springmvcResponse = (ResponseEntity<Object>) response;
    StatusType responseStatus = new HttpStatus(springmvcResponse.getStatusCode().value(), springmvcResponse.getStatusCode().getReasonPhrase());
    Response cseResponse = null;
    if (HttpStatus.isSuccess(responseStatus)) {
        cseResponse = realMapper.mapResponse(responseStatus, springmvcResponse.getBody());
    } else {
        // not support fail response mapper now
        cseResponse = Response.status(responseStatus).entity(springmvcResponse.getBody());
    }
    HttpHeaders headers = springmvcResponse.getHeaders();
    for (Entry<String, List<String>> entry : headers.entrySet()) {
        if (entry.getValue() == null) {
            continue;
        }
        for (String value : entry.getValue()) {
            cseResponse.addHeader(entry.getKey(), value);
        }
    }
    return cseResponse;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.apache.servicecomb.swagger.invocation.context.HttpStatus) StatusType(javax.ws.rs.core.Response.StatusType) List(java.util.List)

Example 5 with HttpStatus

use of org.apache.servicecomb.swagger.invocation.context.HttpStatus in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method scheduleInvocation_flowControlReject.

@SuppressWarnings("deprecation")
@Test
public void scheduleInvocation_flowControlReject() {
    new Expectations(operationMeta) {

        {
            operationMeta.getProviderQpsFlowControlHandler();
            result = (Handler) (invocation, asyncResp) -> asyncResp.producerFail(new InvocationException(new HttpStatus(429, "Too Many Requests"), new CommonExceptionData("rejected by qps flowcontrol")));
        }
    };
    Holder<Integer> status = new Holder<>();
    Holder<String> reasonPhrase = new Holder<>();
    Holder<Integer> endCount = new Holder<>(0);
    Holder<String> responseBody = new Holder<>();
    responseEx = new AbstractHttpServletResponse() {

        @SuppressWarnings("deprecation")
        @Override
        public void setStatus(int sc, String sm) {
            status.value = sc;
            reasonPhrase.value = sm;
        }

        @Override
        public void flushBuffer() {
            endCount.value = endCount.value + 1;
        }

        @Override
        public void setContentType(String type) {
            assertEquals("application/json; charset=utf-8", type);
        }

        @Override
        public void setBodyBuffer(Buffer bodyBuffer) {
            responseBody.value = bodyBuffer.toString();
        }
    };
    initRestInvocation();
    restInvocation.scheduleInvocation();
    assertEquals(Integer.valueOf(429), status.value);
    assertEquals("Too Many Requests", reasonPhrase.value);
    assertEquals("{\"message\":\"rejected by qps flowcontrol\"}", responseBody.value);
    assertEquals(Integer.valueOf(1), endCount.value);
}
Also used : Expectations(mockit.Expectations) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Arrays(java.util.Arrays) SCBEngine(org.apache.servicecomb.core.SCBEngine) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) MultiMap(io.vertx.core.MultiMap) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) RestMetaUtils(org.apache.servicecomb.common.rest.definition.RestMetaUtils) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) AbstractHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse) MediaType(javax.ws.rs.core.MediaType) Map(java.util.Map) HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter) After(org.junit.After) Mock(mockit.Mock) ProduceProcessorManager(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessorManager) SCBBootstrap(org.apache.servicecomb.core.bootstrap.SCBBootstrap) Response(org.apache.servicecomb.swagger.invocation.Response) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) MockUp(mockit.MockUp) Invocation(org.apache.servicecomb.core.Invocation) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Buffer(io.vertx.core.buffer.Buffer) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager) Mocked(mockit.Mocked) CONTENT_LENGTH(com.google.common.net.HttpHeaders.CONTENT_LENGTH) RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) InvocationStartEvent(org.apache.servicecomb.core.event.InvocationStartEvent) Expectations(mockit.Expectations) HttpStatus(org.apache.servicecomb.swagger.invocation.context.HttpStatus) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) TRANSFER_ENCODING(com.google.common.net.HttpHeaders.TRANSFER_ENCODING) HashMap(java.util.HashMap) JsonUtils(org.apache.servicecomb.foundation.common.utils.JsonUtils) EventManager(org.apache.servicecomb.foundation.common.event.EventManager) AsyncContext(javax.servlet.AsyncContext) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Subscribe(com.google.common.eventbus.Subscribe) Status(javax.ws.rs.core.Response.Status) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) OperationLocator(org.apache.servicecomb.common.rest.locator.OperationLocator) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) StandardHttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx) Executor(java.util.concurrent.Executor) Handler(org.apache.servicecomb.core.Handler) HttpServletResponse(javax.servlet.http.HttpServletResponse) Const(org.apache.servicecomb.core.Const) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) ReferenceConfig(org.apache.servicecomb.core.provider.consumer.ReferenceConfig) Rule(org.junit.Rule) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) InvocationFinishEvent(org.apache.servicecomb.core.event.InvocationFinishEvent) Endpoint(org.apache.servicecomb.core.Endpoint) AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) Assert(org.junit.Assert) TestPathSchema(org.apache.servicecomb.common.rest.locator.TestPathSchema) Assert.assertEquals(org.junit.Assert.assertEquals) Buffer(io.vertx.core.buffer.Buffer) HttpStatus(org.apache.servicecomb.swagger.invocation.context.HttpStatus) Holder(org.apache.servicecomb.foundation.common.Holder) Endpoint(org.apache.servicecomb.core.Endpoint) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) AbstractHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Aggregations

HttpStatus (org.apache.servicecomb.swagger.invocation.context.HttpStatus)5 StatusType (javax.ws.rs.core.Response.StatusType)4 List (java.util.List)3 Response (org.apache.servicecomb.swagger.invocation.Response)3 Test (org.junit.Test)3 Subscribe (com.google.common.eventbus.Subscribe)1 CONTENT_LENGTH (com.google.common.net.HttpHeaders.CONTENT_LENGTH)1 TRANSFER_ENCODING (com.google.common.net.HttpHeaders.TRANSFER_ENCODING)1 MultiMap (io.vertx.core.MultiMap)1 Buffer (io.vertx.core.buffer.Buffer)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Executor (java.util.concurrent.Executor)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 AsyncContext (javax.servlet.AsyncContext)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpHeaders (javax.ws.rs.core.HttpHeaders)1 MediaType (javax.ws.rs.core.MediaType)1 Status (javax.ws.rs.core.Response.Status)1