You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2016/01/20 15:44:39 UTC

[jira] [Commented] (WW-4588) Improve the Struts2 Rest plugin to honor Accept and Content-Type headers

    [ https://issues.apache.org/jira/browse/WW-4588?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15108634#comment-15108634 ] 

ASF GitHub Bot commented on WW-4588:
------------------------------------

GitHub user rpii opened a pull request:

    https://github.com/apache/struts/pull/82

    WW-4588:  Improve the Struts2 Rest plugin to honor Accept header

    Lukasz
    
    here is a proposed enhancement to implement WW-4588.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/rpii/struts WW-4588

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/struts/pull/82.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #82
    
----
commit 7899f10d800f0c48cb73d02c93500687717c745e
Author: petersr <ri...@braxtontech.com>
Date:   2016-01-20T14:34:25Z

    WW-4588:  Improve the Struts2 Rest plugin to honor Accept header

----


> 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 Peters
>              Labels: rest, struts2
>             Fix For: 2.3.25, 2.5
>
>         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)