Search in sources :

Example 1 with ContentTypeHandler

use of org.apache.struts2.rest.handler.ContentTypeHandler in project struts by apache.

the class ContentTypeInterceptor method intercept.

public String intercept(ActionInvocation invocation) throws Exception {
    HttpServletRequest request = ServletActionContext.getRequest();
    ContentTypeHandler handler = selector.getHandlerForRequest(request);
    Object target = invocation.getAction();
    if (target instanceof ModelDriven) {
        target = ((ModelDriven<?>) target).getModel();
    }
    if (request.getContentLength() > 0) {
        InputStream is = request.getInputStream();
        InputStreamReader reader = new InputStreamReader(is);
        handler.toObject(invocation, reader, target);
    }
    return invocation.invoke();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentTypeHandler(org.apache.struts2.rest.handler.ContentTypeHandler) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ModelDriven(com.opensymphony.xwork2.ModelDriven)

Example 2 with ContentTypeHandler

use of org.apache.struts2.rest.handler.ContentTypeHandler in project struts by apache.

the class ContentTypeHandlerManagerTest method testHandlerOverride.

public void testHandlerOverride() {
    Mock mockHandlerXml = new Mock(ContentTypeHandler.class);
    mockHandlerXml.matchAndReturn("getExtension", "xml");
    mockHandlerXml.matchAndReturn("getContentType", "application/xml");
    mockHandlerXml.matchAndReturn("toString", "xml");
    Mock mockHandlerJson = new Mock(ContentTypeHandler.class);
    mockHandlerJson.matchAndReturn("getExtension", "json");
    mockHandlerJson.matchAndReturn("getContentType", "application/javascript");
    mockHandlerJson.matchAndReturn("toString", "json");
    Mock mockHandlerXmlOverride = new Mock(ContentTypeHandler.class);
    mockHandlerXmlOverride.matchAndReturn("getExtension", "xml");
    mockHandlerXmlOverride.matchAndReturn("toString", "xmlOverride");
    mockHandlerXmlOverride.matchAndReturn("getContentType", "application/xml");
    Mock mockContainer = new Mock(Container.class);
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("xmlOverride")), mockHandlerXmlOverride.proxy());
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("xml")), mockHandlerXml.proxy());
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("json")), mockHandlerJson.proxy());
    mockContainer.expectAndReturn("getInstanceNames", C.args(C.eq(ContentTypeHandler.class)), new HashSet<>(Arrays.asList("xml", "xmlOverride", "json")));
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(String.class), C.eq(ContentTypeHandlerManager.STRUTS_REST_HANDLER_OVERRIDE_PREFIX + "xml")), "xmlOverride");
    mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(ContentTypeHandlerManager.STRUTS_REST_HANDLER_OVERRIDE_PREFIX + "json")), null);
    DefaultContentTypeHandlerManager mgr = new DefaultContentTypeHandlerManager();
    mgr.setContainer((Container) mockContainer.proxy());
    Map<String, ContentTypeHandler> handlers = mgr.handlersByExtension;
    assertNotNull(handlers);
    assertEquals(2, handlers.size());
    assertEquals(mockHandlerXmlOverride.proxy(), handlers.get("xml"));
    assertEquals(mockHandlerJson.proxy(), handlers.get("json"));
}
Also used : AbstractContentTypeHandler(org.apache.struts2.rest.handler.AbstractContentTypeHandler) ContentTypeHandler(org.apache.struts2.rest.handler.ContentTypeHandler) Mock(com.mockobjects.dynamic.Mock)

Example 3 with ContentTypeHandler

use of org.apache.struts2.rest.handler.ContentTypeHandler in project struts by apache.

the class ContentTypeHandlerManagerTest method testHandleRequestContentType.

/**
 * Assert that the request content-type and differ from the response content type
 */
public void testHandleRequestContentType() {
    Mock mockHandlerForm = new Mock(ContentTypeHandler.class);
    mockHandlerForm.matchAndReturn("getExtension", null);
    mockHandlerForm.matchAndReturn("getContentType", "application/x-www-form-urlencoded");
    mockHandlerForm.matchAndReturn("toString", "x-www-form-urlencoded");
    Mock mockHandlerJson = new Mock(ContentTypeHandler.class);
    mockHandlerJson.matchAndReturn("getExtension", "json");
    mockHandlerJson.matchAndReturn("getContentType", "application/javascript");
    mockHandlerJson.matchAndReturn("toString", "json");
    Mock mockContainer = new Mock(Container.class);
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("x-www-form-urlencoded")), mockHandlerForm.proxy());
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("json")), mockHandlerJson.proxy());
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(String.class), C.eq("struts.rest.handlerOverride.json")), null);
    mockContainer.expectAndReturn("getInstanceNames", C.args(C.eq(ContentTypeHandler.class)), new HashSet<>(Arrays.asList("x-www-form-urlencoded", "json")));
    mockRequest.setContentType(FormUrlEncodedHandler.CONTENT_TYPE);
    mockRequest.setContent("a=1&b=2".getBytes(StandardCharsets.UTF_8));
    mgr.setContainer((Container) mockContainer.proxy());
    ContentTypeHandler handler = mgr.getHandlerForRequest(mockRequest);
    assertEquals("application/x-www-form-urlencoded", handler.getContentType());
}
Also used : AbstractContentTypeHandler(org.apache.struts2.rest.handler.AbstractContentTypeHandler) ContentTypeHandler(org.apache.struts2.rest.handler.ContentTypeHandler) Mock(com.mockobjects.dynamic.Mock)

Example 4 with ContentTypeHandler

use of org.apache.struts2.rest.handler.ContentTypeHandler in project struts by apache.

the class DummyContainer method testObtainingHandlerForRequestWithoutEncoding.

public void testObtainingHandlerForRequestWithoutEncoding() throws Exception {
    // given
    DefaultContentTypeHandlerManager handlerManager = new DefaultContentTypeHandlerManager();
    handlerManager.setContainer(new DummyContainer("application/json", null));
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContentType("application/json;charset=UTF-8");
    // when
    ContentTypeHandler handler = handlerManager.getHandlerForRequest(request);
    // then
    assertNotNull(handler);
    assertEquals("application/json", handler.getContentType());
}
Also used : AbstractContentTypeHandler(org.apache.struts2.rest.handler.AbstractContentTypeHandler) ContentTypeHandler(org.apache.struts2.rest.handler.ContentTypeHandler) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest)

Example 5 with ContentTypeHandler

use of org.apache.struts2.rest.handler.ContentTypeHandler in project struts by apache.

the class DummyContainer method testObtainingHandlerForResponseByAcceptHeader.

public void testObtainingHandlerForResponseByAcceptHeader() throws Exception {
    // given
    final DefaultContentTypeHandlerManager handlerManager = new DefaultContentTypeHandlerManager();
    handlerManager.setContainer(new DummyContainer("application/json", "json"));
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContentType("application/json;charset=UTF-8");
    request.addHeader("accept", "application/json;charset=UTF-8");
    request.setRequestURI("/index");
    final MockHttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("application/json;charset=UTF-8");
    // when
    ContentTypeHandler handler = handlerManager.getHandlerForResponse(request, response);
    // then
    assertNotNull(handler);
    assertEquals("application/json", handler.getContentType());
    assertEquals("json", handler.getExtension());
}
Also used : AbstractContentTypeHandler(org.apache.struts2.rest.handler.AbstractContentTypeHandler) ContentTypeHandler(org.apache.struts2.rest.handler.ContentTypeHandler) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Aggregations

ContentTypeHandler (org.apache.struts2.rest.handler.ContentTypeHandler)14 AbstractContentTypeHandler (org.apache.struts2.rest.handler.AbstractContentTypeHandler)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 Mock (com.mockobjects.dynamic.Mock)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)1 ModelDriven (com.opensymphony.xwork2.ModelDriven)1 Inject (com.opensymphony.xwork2.inject.Inject)1 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)1 MockActionProxy (com.opensymphony.xwork2.mock.MockActionProxy)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HtmlHandler (org.apache.struts2.rest.handler.HtmlHandler)1 HttpHeaderResult (org.apache.struts2.result.HttpHeaderResult)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1