Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-4588

Improve the Struts2 Rest plugin to honor Accept header

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.3.24
    • Fix Version/s: 2.3.28, 2.5
    • Component/s: Plugin - REST
    • Labels:
    • Environment:

      tomcat

      Description

      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:

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

      the output show the failure to acquire a handler:

      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)
      

      FWIW: Attached is a modified version of the 2.3.24.1 file that implements this

        Activity

        Hide
        hudson Hudson added a comment -

        SUCCESS: Integrated in Struts-JDK6-support-2.3 #963 (See https://builds.apache.org/job/Struts-JDK6-support-2.3/963/)
        WW-4588: Improve the Struts2 Rest plugin to honor Accept header (lukaszlenart: rev 2ec3890dc7d704d3098e1bd738bff9ae5ca4f989)

        • plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java
        • plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
        Show
        hudson Hudson added a comment - SUCCESS: Integrated in Struts-JDK6-support-2.3 #963 (See https://builds.apache.org/job/Struts-JDK6-support-2.3/963/ ) WW-4588 : Improve the Struts2 Rest plugin to honor Accept header (lukaszlenart: rev 2ec3890dc7d704d3098e1bd738bff9ae5ca4f989) plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
        Hide
        hudson Hudson added a comment -

        SUCCESS: Integrated in Struts-JDK7-master #413 (See https://builds.apache.org/job/Struts-JDK7-master/413/)
        WW-4588: Improve the Struts2 Rest plugin to honor Accept header (rich.peters: rev 7899f10d800f0c48cb73d02c93500687717c745e)

        • plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
        • plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java
        Show
        hudson Hudson added a comment - SUCCESS: Integrated in Struts-JDK7-master #413 (See https://builds.apache.org/job/Struts-JDK7-master/413/ ) WW-4588 : Improve the Struts2 Rest plugin to honor Accept header (rich.peters: rev 7899f10d800f0c48cb73d02c93500687717c745e) plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        PR merged and cherry-picked to 2.3.x branch, thanks!

        Show
        lukaszlenart Lukasz Lenart added a comment - PR merged and cherry-picked to 2.3.x branch, thanks!
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 2ec3890dc7d704d3098e1bd738bff9ae5ca4f989 in struts's branch refs/heads/support-2-3 from petersr
        [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=2ec3890 ]

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

        Conflicts:
        plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java

        Show
        jira-bot ASF subversion and git services added a comment - Commit 2ec3890dc7d704d3098e1bd738bff9ae5ca4f989 in struts's branch refs/heads/support-2-3 from petersr [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=2ec3890 ] WW-4588 : Improve the Struts2 Rest plugin to honor Accept header Conflicts: plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
        Hide
        githubbot ASF GitHub Bot added a comment -

        Github user asfgit closed the pull request at:

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

        Show
        githubbot ASF GitHub Bot added a comment - Github user asfgit closed the pull request at: https://github.com/apache/struts/pull/82
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit cae57093b555d553873dd4b3d15ab2d437f387e6 in struts's branch refs/heads/master from Lukasz Lenart
        [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=cae5709 ]

        Merge branch 'WW-4588'

        Show
        jira-bot ASF subversion and git services added a comment - Commit cae57093b555d553873dd4b3d15ab2d437f387e6 in struts's branch refs/heads/master from Lukasz Lenart [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=cae5709 ] Merge branch ' WW-4588 '
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 7899f10d800f0c48cb73d02c93500687717c745e in struts's branch refs/heads/master from petersr
        [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=7899f10 ]

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

        Show
        jira-bot ASF subversion and git services added a comment - Commit 7899f10d800f0c48cb73d02c93500687717c745e in struts's branch refs/heads/master from petersr [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=7899f10 ] WW-4588 : Improve the Struts2 Rest plugin to honor Accept header
        Hide
        githubbot ASF GitHub Bot added a comment -

        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 <rich.peters@braxtontech.com>
        Date: 2016-01-20T14:34:25Z

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


        Show
        githubbot ASF GitHub Bot added a comment - 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 <rich.peters@braxtontech.com> Date: 2016-01-20T14:34:25Z WW-4588 : Improve the Struts2 Rest plugin to honor Accept header
        Hide
        rpii Rich Peters added a comment -

        FWIW a modified version of DefaultContentTypeHandlerManager which implements this feature

        Show
        rpii Rich Peters added a comment - FWIW a modified version of DefaultContentTypeHandlerManager which implements this feature

          People

          • Assignee:
            Unassigned
            Reporter:
            rpii Rich Peters
          • Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development