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

Improve the Struts2 Rest plugin to honor Accept header

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.24
    • 2.3.28, 2.5
    • Plugin - REST
    • 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

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: