Search in sources :

Example 1 with HttpServletRequestFilter

use of org.apache.cxf.jaxrs.impl.HttpServletRequestFilter 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 2 with HttpServletRequestFilter

use of org.apache.cxf.jaxrs.impl.HttpServletRequestFilter 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)

Example 3 with HttpServletRequestFilter

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

the class HttpUtils method createServletResourceValue.

public static <T> T createServletResourceValue(Message m, Class<T> clazz) {
    Object value = null;
    if (clazz == HttpServletRequest.class) {
        HttpServletRequest request = (HttpServletRequest) m.get(AbstractHTTPDestination.HTTP_REQUEST);
        value = request != null ? new HttpServletRequestFilter(request, m) : null;
    } else if (clazz == HttpServletResponse.class) {
        HttpServletResponse response = (HttpServletResponse) m.get(AbstractHTTPDestination.HTTP_RESPONSE);
        value = response != null ? new HttpServletResponseFilter(response, m) : null;
    } else if (clazz == ServletContext.class) {
        value = m.get(AbstractHTTPDestination.HTTP_CONTEXT);
    } else if (clazz == ServletConfig.class) {
        value = m.get(AbstractHTTPDestination.HTTP_CONFIG);
    }
    return clazz.cast(value);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletConfig(javax.servlet.ServletConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) HttpServletRequestFilter(org.apache.cxf.jaxrs.impl.HttpServletRequestFilter)

Example 4 with HttpServletRequestFilter

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

the class MessageContextImplTest method testHttpRequest.

@Test
public void testHttpRequest() {
    Message m = createMessage();
    MessageContext mc = new MessageContextImpl(m);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    m.put(AbstractHTTPDestination.HTTP_REQUEST, request);
    assertSame(request.getClass(), ((HttpServletRequestFilter) mc.getHttpServletRequest()).getRequest().getClass());
    assertSame(request.getClass(), ((HttpServletRequestFilter) mc.getContext(HttpServletRequest.class)).getRequest().getClass());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Message(org.apache.cxf.message.Message) HttpServletRequestFilter(org.apache.cxf.jaxrs.impl.HttpServletRequestFilter) Test(org.junit.Test)

Aggregations

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