Search in sources :

Example 1 with Headers

use of org.apache.servicecomb.swagger.invocation.response.Headers in project incubator-servicecomb-java-chassis by apache.

the class CodeFirstJaxrs method response.

@ApiResponse(code = 200, response = User.class, message = "")
@ResponseHeaders({ @ResponseHeader(name = "h1", response = String.class), @ResponseHeader(name = "h2", response = String.class) })
@Path("/response")
@GET
public Response response(InvocationContext c1) {
    Response response = Response.createSuccess(Status.ACCEPTED, new User());
    Headers headers = response.getHeaders();
    headers.addHeader("h1", "h1v " + c1.getContext().toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    headers.addHeader("h2", "h2v " + c2.getContext().toString());
    return response;
}
Also used : ApiResponse(io.swagger.annotations.ApiResponse) Response(org.apache.servicecomb.swagger.invocation.Response) User(org.apache.servicecomb.demo.server.User) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) ApiResponse(io.swagger.annotations.ApiResponse)

Example 2 with Headers

use of org.apache.servicecomb.swagger.invocation.response.Headers in project incubator-servicecomb-java-chassis by apache.

the class TestAbstractRestInvocation method testDoSendResponseHeaderNormal.

@Test
public void testDoSendResponseHeaderNormal(@Mocked Response response) throws Exception {
    Headers headers = new Headers();
    headers.addHeader("h1", "h1v1");
    headers.addHeader("h1", "h1v2");
    headers.addHeader("h2", "h2v");
    new Expectations() {

        {
            response.getResult();
            result = new Error("stop");
            response.getHeaders();
            result = headers;
        }
    };
    Headers resultHeaders = new Headers();
    responseEx = new MockUp<HttpServletResponseEx>() {

        private Map<String, Object> attributes = new HashMap<>();

        @Mock
        public void setAttribute(String key, Object value) {
            this.attributes.put(key, value);
        }

        @Mock
        public Object getAttribute(String key) {
            return this.attributes.get(key);
        }

        @Mock
        void addHeader(String name, String value) {
            resultHeaders.addHeader(name, value);
        }
    }.getMockInstance();
    initRestInvocation();
    try {
        restInvocation.sendResponse(response);
        Assert.fail("must throw exception");
    } catch (Error e) {
        Assert.assertEquals(headers.getHeaderMap(), resultHeaders.getHeaderMap());
    }
}
Also used : Expectations(mockit.Expectations) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) MockUp(mockit.MockUp) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 3 with Headers

use of org.apache.servicecomb.swagger.invocation.response.Headers 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 4 with Headers

use of org.apache.servicecomb.swagger.invocation.response.Headers in project incubator-servicecomb-java-chassis by apache.

the class CodeFirstSpringmvc method cseResponse.

@ApiResponse(code = 200, response = User.class, message = "")
@ResponseHeaders({ @ResponseHeader(name = "h1", response = String.class), @ResponseHeader(name = "h2", response = String.class) })
@RequestMapping(path = "/cseResponse", method = RequestMethod.GET)
public Response cseResponse(InvocationContext c1) {
    Response response = Response.createSuccess(Status.ACCEPTED, new User());
    Headers headers = response.getHeaders();
    headers.addHeader("h1", "h1v " + c1.getContext().get(Const.SRC_MICROSERVICE).toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    headers.addHeader("h2", "h2v " + c2.getContext().get(Const.SRC_MICROSERVICE).toString());
    return response;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ApiResponse(io.swagger.annotations.ApiResponse) User(org.apache.servicecomb.demo.server.User) HttpHeaders(org.springframework.http.HttpHeaders) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) ApiResponse(io.swagger.annotations.ApiResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with Headers

use of org.apache.servicecomb.swagger.invocation.response.Headers in project incubator-servicecomb-java-chassis by apache.

the class CseClientHttpResponse method getHeaders.

@Override
public HttpHeaders getHeaders() {
    if (httpHeaders == null) {
        HttpHeaders tmpHeaders = new HttpHeaders();
        Headers headers = response.getHeaders();
        if (headers.getHeaderMap() != null) {
            for (Entry<String, List<Object>> entry : headers.getHeaderMap().entrySet()) {
                for (Object value : entry.getValue()) {
                    tmpHeaders.add(entry.getKey(), String.valueOf(value));
                }
            }
        }
        httpHeaders = tmpHeaders;
    }
    return httpHeaders;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpHeaders(org.springframework.http.HttpHeaders) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) List(java.util.List)

Aggregations

Headers (org.apache.servicecomb.swagger.invocation.response.Headers)9 Response (org.apache.servicecomb.swagger.invocation.Response)5 User (org.apache.servicecomb.demo.server.User)4 InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ApiResponse (io.swagger.annotations.ApiResponse)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Expectations (mockit.Expectations)3 MockUp (mockit.MockUp)3 ResponseHeaders (org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders)3 Test (org.junit.Test)3 List (java.util.List)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Buffer (io.vertx.core.buffer.Buffer)1 StatusType (javax.ws.rs.core.Response.StatusType)1 Mock (mockit.Mock)1 HttpServerFilter (org.apache.servicecomb.common.rest.filter.HttpServerFilter)1 Invocation (org.apache.servicecomb.core.Invocation)1