Search in sources :

Example 1 with HttpServletResponseFilter

use of org.apache.cxf.jaxrs.impl.HttpServletResponseFilter in project cxf by apache.

the class JAXRSUtilsTest method testSingletonContextFields.

@SuppressWarnings("unchecked")
@Test
public void testSingletonContextFields() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    Customer c = new Customer();
    cri.setResourceProvider(new SingletonResourceProvider(c));
    Message m = createMessage();
    m.put(Message.PROTOCOL_HEADERS, new HashMap<String, List<String>>());
    ServletContext servletContextMock = EasyMock.createNiceMock(ServletContext.class);
    m.put(AbstractHTTPDestination.HTTP_CONTEXT, servletContextMock);
    HttpServletRequest httpRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    m.put(AbstractHTTPDestination.HTTP_REQUEST, httpRequest);
    HttpServletResponse httpResponse = EasyMock.createMock(HttpServletResponse.class);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, httpResponse);
    InjectionUtils.injectContextProxies(cri, cri.getResourceProvider().getInstance(null));
    InjectionUtils.injectContextFields(c, cri, m);
    InjectionUtils.injectContextMethods(c, cri, m);
    assertSame(ThreadLocalUriInfo.class, c.getUriInfo2().getClass());
    assertSame(UriInfoImpl.class, ((ThreadLocalProxy<UriInfo>) c.getUriInfo2()).get().getClass());
    assertSame(HttpHeadersImpl.class, ((ThreadLocalProxy<HttpHeaders>) c.getHeaders()).get().getClass());
    assertSame(RequestImpl.class, ((ThreadLocalProxy<Request>) c.getRequest()).get().getClass());
    assertSame(ResourceInfoImpl.class, ((ThreadLocalProxy<ResourceInfo>) c.getResourceInfo()).get().getClass());
    assertSame(SecurityContextImpl.class, ((ThreadLocalProxy<SecurityContext>) c.getSecurityContext()).get().getClass());
    assertSame(ProvidersImpl.class, ((ThreadLocalProxy<Providers>) c.getBodyWorkers()).get().getClass());
    assertSame(servletContextMock, ((ThreadLocalProxy<ServletContext>) c.getThreadLocalServletContext()).get());
    assertSame(servletContextMock, ((ThreadLocalProxy<ServletContext>) c.getServletContext()).get());
    assertSame(servletContextMock, ((ThreadLocalProxy<ServletContext>) c.getSuperServletContext()).get());
    HttpServletRequest currentReq = ((ThreadLocalProxy<HttpServletRequest>) c.getServletRequest()).get();
    assertSame(httpRequest, ((HttpServletRequestFilter) currentReq).getRequest());
    HttpServletResponseFilter filter = (HttpServletResponseFilter) ((ThreadLocalProxy<HttpServletResponse>) c.getServletResponse()).get();
    assertSame(httpResponse, filter.getResponse());
}
Also used : ThreadLocalHttpServletRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) List(java.util.List) ArrayList(java.util.ArrayList) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) Test(org.junit.Test)

Example 2 with HttpServletResponseFilter

use of org.apache.cxf.jaxrs.impl.HttpServletResponseFilter in project cxf by apache.

the class MessageContextImplTest method testHttpResponse.

@Test
public void testHttpResponse() {
    Message m = createMessage();
    MessageContext mc = new MessageContextImpl(m);
    HttpServletResponse request = EasyMock.createMock(HttpServletResponse.class);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, request);
    HttpServletResponseFilter filter = (HttpServletResponseFilter) mc.getHttpServletResponse();
    assertSame(request.getClass(), filter.getResponse().getClass());
    filter = (HttpServletResponseFilter) mc.getContext(HttpServletResponse.class);
    assertSame(request.getClass(), filter.getResponse().getClass());
}
Also used : Message(org.apache.cxf.message.Message) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) Test(org.junit.Test)

Example 3 with HttpServletResponseFilter

use of org.apache.cxf.jaxrs.impl.HttpServletResponseFilter in project cxf by apache.

the class JAXRSUtilsTest method testSingletonHttpResourceFields.

@SuppressWarnings("unchecked")
@Test
public void testSingletonHttpResourceFields() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    Customer c = new Customer();
    cri.setResourceProvider(new SingletonResourceProvider(c));
    Message m = createMessage();
    ServletContext servletContextMock = EasyMock.createNiceMock(ServletContext.class);
    m.put(AbstractHTTPDestination.HTTP_CONTEXT, servletContextMock);
    HttpServletRequest httpRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    m.put(AbstractHTTPDestination.HTTP_REQUEST, httpRequest);
    HttpServletResponse httpResponse = EasyMock.createMock(HttpServletResponse.class);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, httpResponse);
    InjectionUtils.injectContextProxies(cri, cri.getResourceProvider().getInstance(null));
    InjectionUtils.injectContextFields(c, cri, m);
    assertSame(servletContextMock, ((ThreadLocalProxy<ServletContext>) c.getServletContextResource()).get());
    HttpServletRequest currentReq = ((ThreadLocalProxy<HttpServletRequest>) c.getServletRequestResource()).get();
    assertSame(httpRequest, ((HttpServletRequestFilter) currentReq).getRequest());
    HttpServletResponseFilter filter = (HttpServletResponseFilter) ((ThreadLocalProxy<HttpServletResponse>) c.getServletResponseResource()).get();
    assertSame(httpResponse, filter.getResponse());
}
Also used : ThreadLocalHttpServletRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) Test(org.junit.Test)

Example 4 with HttpServletResponseFilter

use of org.apache.cxf.jaxrs.impl.HttpServletResponseFilter in project cxf by apache.

the class JAXRSUtilsTest method testServletContextParameters.

@Test
public void testServletContextParameters() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("testServletParams", new Class[] { HttpServletRequest.class, HttpServletResponse.class, ServletContext.class, ServletConfig.class }), cri);
    ori.setHttpMethod("GET");
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpServletResponse response = new HttpServletResponseFilter(EasyMock.createMock(HttpServletResponse.class), null);
    ServletContext context = EasyMock.createMock(ServletContext.class);
    ServletConfig config = EasyMock.createMock(ServletConfig.class);
    EasyMock.replay(request);
    EasyMock.replay(context);
    EasyMock.replay(config);
    Message m = createMessage();
    m.put(AbstractHTTPDestination.HTTP_REQUEST, request);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, response);
    m.put(AbstractHTTPDestination.HTTP_CONTEXT, context);
    m.put(AbstractHTTPDestination.HTTP_CONFIG, config);
    List<Object> params = JAXRSUtils.processParameters(ori, new MetadataMap<String, String>(), m);
    assertEquals("4 parameters expected", 4, params.size());
    assertSame(request.getClass(), ((HttpServletRequestFilter) params.get(0)).getRequest().getClass());
    assertSame(response.getClass(), params.get(1).getClass());
    assertSame(context.getClass(), params.get(2).getClass());
    assertSame(config.getClass(), params.get(3).getClass());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ServletConfig(javax.servlet.ServletConfig) ThreadLocalServletConfig(org.apache.cxf.jaxrs.impl.tl.ThreadLocalServletConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) ThreadLocalHttpServletRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) HttpServletRequestFilter(org.apache.cxf.jaxrs.impl.HttpServletRequestFilter) Test(org.junit.Test)

Example 5 with HttpServletResponseFilter

use of org.apache.cxf.jaxrs.impl.HttpServletResponseFilter in project cxf by apache.

the class JAXRSUtilsTest method testServletResourceFields.

@Test
public void testServletResourceFields() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    cri.setResourceProvider(new PerRequestResourceProvider(Customer.class));
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("postConstruct", new Class[] {}), cri);
    Customer c = new Customer();
    // Creating mocks for the servlet request, response and context
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
    ServletContext context = EasyMock.createMock(ServletContext.class);
    EasyMock.replay(request);
    EasyMock.replay(response);
    EasyMock.replay(context);
    Message m = createMessage();
    m.put(AbstractHTTPDestination.HTTP_REQUEST, request);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, response);
    m.put(AbstractHTTPDestination.HTTP_CONTEXT, context);
    InjectionUtils.injectContextFields(c, ori.getClassResourceInfo(), m);
    assertSame(request.getClass(), ((HttpServletRequestFilter) c.getServletRequestResource()).getRequest().getClass());
    HttpServletResponseFilter filter = (HttpServletResponseFilter) c.getServletResponseResource();
    assertSame(response.getClass(), filter.getResponse().getClass());
    assertSame(context.getClass(), c.getServletContextResource().getClass());
    assertNotNull(c.getServletRequest());
    assertNotNull(c.getServletResponse());
    assertNotNull(c.getServletContext());
    assertNotNull(c.getServletRequestResource());
    assertNotNull(c.getServletResponseResource());
    assertNotNull(c.getServletContextResource());
    assertSame(request.getClass(), ((HttpServletRequestFilter) c.getServletRequestResource()).getRequest().getClass());
    filter = (HttpServletResponseFilter) c.getServletResponse();
    assertSame(response.getClass(), filter.getResponse().getClass());
    assertSame(context.getClass(), c.getServletContext().getClass());
}
Also used : ThreadLocalHttpServletRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) HttpServletRequestFilter(org.apache.cxf.jaxrs.impl.HttpServletRequestFilter) Test(org.junit.Test)

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)6 HttpServletResponseFilter (org.apache.cxf.jaxrs.impl.HttpServletResponseFilter)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Message (org.apache.cxf.message.Message)5 Test (org.junit.Test)5 ServletContext (javax.servlet.ServletContext)4 Customer (org.apache.cxf.jaxrs.Customer)4 ThreadLocalHttpServletRequest (org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest)4 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)4 HttpServletRequestFilter (org.apache.cxf.jaxrs.impl.HttpServletRequestFilter)3 ServletConfig (javax.servlet.ServletConfig)2 ThreadLocalProxy (org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy)2 SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)2 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ThreadLocalServletConfig (org.apache.cxf.jaxrs.impl.tl.ThreadLocalServletConfig)1 PerRequestResourceProvider (org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider)1