[ 
https://issues.apache.org/jira/browse/WW-4588?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Rich P Peters, II updated WW-4588:
----------------------------------
    Attachment: DefaultContentTypeHandlerManager.java

FWIW a modified version of DefaultContentTypeHandlerManager which implements 
this feature

> Improve the Struts2 Rest plugin to honor Accept and Content-Type headers
> ------------------------------------------------------------------------
>
>                 Key: WW-4588
>                 URL: https://issues.apache.org/jira/browse/WW-4588
>             Project: Struts 2
>          Issue Type: Improvement
>          Components: Plugin - REST
>    Affects Versions: 2.3.24
>         Environment: tomcat
>            Reporter: Rich P Peters, II
>              Labels: rest, struts2
>         Attachments: DefaultContentTypeHandlerManager.java
>
>
> Currently the rest plugin primarily looks at the url extension to connect the 
> proper content type handlers.  This should be extended to use the Accept and 
> Content-Type headers to drive the content-type handlers when no extension is 
> present.  Note that the plugin currently partially handles the case for input 
> request using the content-type header, but can be overridden by the extension.
> In a use case,  if a request specifies in the header that the Content-Type is 
> application/json, the input request data is converted to the json data 
> format. If a request specifies the Accept:application/json header ,  the 
> output data will be converted to json.  This is cleaner than specifying the 
> extension in a lot of cases where the application rest urls have to be 
> generated in javascript.
> a short unit test shows how the accept header is ignored:
> {code:java}
>     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());
>     }
> {code}
> the output show the failure to acquire a handler:
> {code:java}
> junit.framework.AssertionFailedError: null
>       at junit.framework.Assert.fail(Assert.java:47)
>       at junit.framework.Assert.assertTrue(Assert.java:20)
>       at junit.framework.Assert.assertNotNull(Assert.java:214)
>       at junit.framework.Assert.assertNotNull(Assert.java:207)
>       at 
> org.apache.struts2.rest.DefaultContentTypeHandlerManagerTest.testObtainingHandlerForResponseByAcceptHeader(DefaultContentTypeHandlerManagerTest.java:104)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>       at java.lang.reflect.Method.invoke(Method.java:497)
>       at junit.framework.TestCase.runTest(TestCase.java:168)
>       at junit.framework.TestCase.runBare(TestCase.java:134)
>       at junit.framework.TestResult$1.protect(TestResult.java:110)
>       at junit.framework.TestResult.runProtected(TestResult.java:128)
>       at junit.framework.TestResult.run(TestResult.java:113)
>       at junit.framework.TestCase.run(TestCase.java:124)
>       at junit.framework.TestSuite.runTest(TestSuite.java:243)
>       at junit.framework.TestSuite.run(TestSuite.java:238)
>       at 
> org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
>       at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
>       at 
> com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
>       at 
> com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
>       at 
> com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
> {code}
> FWIW: Attached is a modified version of the 2.3.24.1 file that implements this



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to