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();
}
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"));
}
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());
}
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());
}
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());
}
Aggregations