Author: chris.laprun(a)jboss.com
Date: 2010-11-26 06:12:13 -0500 (Fri, 26 Nov 2010)
New Revision: 5290
Modified:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/RequestProcessor.java
components/wsrp/trunk/producer/src/test/java/org/gatein/wsrp/producer/handlers/processors/MimeResponseProcessorTestCase.java
Log:
- GTNWSRP-175: Properly deal with wildcard in MIME type requests.
- GNTWSRP-176: Stop looking for next possible matches if we've found one already.
- Updated and added more tests.
Modified:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/RequestProcessor.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/RequestProcessor.java 2010-11-26
10:18:30 UTC (rev 5289)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/RequestProcessor.java 2010-11-26
11:12:13 UTC (rev 5290)
@@ -211,16 +211,44 @@
MarkupType markupType = null;
// Get the MIME type to use
+ // todo: MIME type resolution should really be done in common... maybe as part of
GTNCOMMON-14?
for (String desiredMIMEType : desiredMIMETypes)
{
- for (MarkupType type : markupTypes)
+ desiredMIMEType = desiredMIMEType.trim();
+
+ // first deal with full wildcards
+ if ("*".equals(desiredMIMEType) ||
"*/*".equals(desiredMIMEType))
{
- if (desiredMIMEType.equals(type.getMimeType()))
+ markupType = markupTypes.get(0);
+ break;
+ }
+ else
+ {
+ MediaType mt = MediaType.create(desiredMIMEType);
+ String superType = mt.getType().getName();
+ String subType = mt.getSubtype().getName();
+ boolean isWildcard = "*".equals(subType);
+
+ for (MarkupType type : markupTypes)
{
- markupType = type;
- break;
+ if (isWildcard && type.getMimeType().startsWith(superType))
+ {
+ markupType = type;
+ break;
+ }
+ else if (desiredMIMEType.equals(type.getMimeType()))
+ {
+ markupType = type;
+ break;
+ }
}
}
+
+ // if we've found a match, do not examine the other possible matches
+ if (markupType != null)
+ {
+ break;
+ }
}
// no MIME type was found: error!
Modified:
components/wsrp/trunk/producer/src/test/java/org/gatein/wsrp/producer/handlers/processors/MimeResponseProcessorTestCase.java
===================================================================
---
components/wsrp/trunk/producer/src/test/java/org/gatein/wsrp/producer/handlers/processors/MimeResponseProcessorTestCase.java 2010-11-26
10:18:30 UTC (rev 5289)
+++
components/wsrp/trunk/producer/src/test/java/org/gatein/wsrp/producer/handlers/processors/MimeResponseProcessorTestCase.java 2010-11-26
11:12:13 UTC (rev 5290)
@@ -72,8 +72,87 @@
assertEquals("namespace",
processor.invocation.getWindowContext().getNamespace());
}
+ public void testShouldProperlyHandleWildCardsInRequestedMimeTypes() throws
OperationFailed, UnsupportedMode, InvalidHandle, MissingParameters, UnsupportedMimeType,
ModifyRegistrationRequired, UnsupportedWindowState, InvalidRegistration
+ {
+ List<String> mimeTypes = new ArrayList<String>(1);
+ mimeTypes.add("*/*");
+
ServletAccess.setRequestAndResponse(MockHttpServletRequest.createMockRequest(MockHttpSession.createMockSession()),
MockHttpServletResponse.createMockResponse());
+
+ MimeResponseProcessor processor = new RenderRequestProcessor(new
TestProducerHelper(), WSRPTypeFactory.createGetMarkup(null,
+ WSRPTypeFactory.createPortletContext(PORTLET_HANDLE),
+ WSRPTypeFactory.createRuntimeContext(WSRPConstants.NONE_USER_AUTHENTICATION,
"foo", "ns"), null,
+ WSRPTypeFactory.createMarkupParams(false, WSRPConstants.getDefaultLocales(),
mimeTypes, WSRPConstants.VIEW_MODE, WSRPConstants.NORMAL_WINDOW_STATE)));
+
+ assertEquals(TestProducerHelper.PORTLET_MIME_TYPE,
processor.markupRequest.getMediaType());
+
+ mimeTypes = new ArrayList<String>(1);
+ mimeTypes.add("*");
+
+ processor = new RenderRequestProcessor(new TestProducerHelper(),
WSRPTypeFactory.createGetMarkup(null,
+ WSRPTypeFactory.createPortletContext(PORTLET_HANDLE),
+ WSRPTypeFactory.createRuntimeContext(WSRPConstants.NONE_USER_AUTHENTICATION,
"foo", "ns"), null,
+ WSRPTypeFactory.createMarkupParams(false, WSRPConstants.getDefaultLocales(),
mimeTypes, WSRPConstants.VIEW_MODE, WSRPConstants.NORMAL_WINDOW_STATE)));
+
+ assertEquals(TestProducerHelper.PORTLET_MIME_TYPE,
processor.markupRequest.getMediaType());
+
+ mimeTypes = new ArrayList<String>(1);
+ mimeTypes.add("text/*");
+
+ processor = new RenderRequestProcessor(new TestProducerHelper(),
WSRPTypeFactory.createGetMarkup(null,
+ WSRPTypeFactory.createPortletContext(PORTLET_HANDLE),
+ WSRPTypeFactory.createRuntimeContext(WSRPConstants.NONE_USER_AUTHENTICATION,
"foo", "ns"), null,
+ WSRPTypeFactory.createMarkupParams(false, WSRPConstants.getDefaultLocales(),
mimeTypes, WSRPConstants.VIEW_MODE, WSRPConstants.NORMAL_WINDOW_STATE)));
+
+ assertEquals(TestProducerHelper.PORTLET_MIME_TYPE,
processor.markupRequest.getMediaType());
+
+ mimeTypes = new ArrayList<String>(1);
+ mimeTypes.add("image/*");
+
+ try
+ {
+ new RenderRequestProcessor(new TestProducerHelper(),
WSRPTypeFactory.createGetMarkup(null,
+ WSRPTypeFactory.createPortletContext(PORTLET_HANDLE),
+ WSRPTypeFactory.createRuntimeContext(WSRPConstants.NONE_USER_AUTHENTICATION,
"foo", "ns"), null,
+ WSRPTypeFactory.createMarkupParams(false, WSRPConstants.getDefaultLocales(),
mimeTypes, WSRPConstants.VIEW_MODE, WSRPConstants.NORMAL_WINDOW_STATE)));
+ fail("Should have failed on unsupported MIME type");
+ }
+ catch (UnsupportedMimeType unsupportedMimeType)
+ {
+ // expected
+ }
+ }
+
+ public void testShouldReturnFirstMimeTypeMatching() throws OperationFailed,
UnsupportedMode, InvalidHandle, MissingParameters, UnsupportedMimeType,
ModifyRegistrationRequired, UnsupportedWindowState, InvalidRegistration
+ {
+ List<String> mimeTypes = new ArrayList<String>(2);
+ mimeTypes.add("text/xml");
+ mimeTypes.add("text/*");
+
+
ServletAccess.setRequestAndResponse(MockHttpServletRequest.createMockRequest(MockHttpSession.createMockSession()),
MockHttpServletResponse.createMockResponse());
+
+ MimeResponseProcessor processor = new RenderRequestProcessor(new
TestProducerHelper(), WSRPTypeFactory.createGetMarkup(null,
+ WSRPTypeFactory.createPortletContext(PORTLET_HANDLE),
+ WSRPTypeFactory.createRuntimeContext(WSRPConstants.NONE_USER_AUTHENTICATION,
"foo", "ns"), null,
+ WSRPTypeFactory.createMarkupParams(false, WSRPConstants.getDefaultLocales(),
mimeTypes, WSRPConstants.VIEW_MODE, WSRPConstants.NORMAL_WINDOW_STATE)));
+
+ assertEquals("text/xml", processor.markupRequest.getMediaType());
+
+ mimeTypes = new ArrayList<String>(2);
+ mimeTypes.add("image/*");
+ mimeTypes.add("text/*");
+
+ processor = new RenderRequestProcessor(new TestProducerHelper(),
WSRPTypeFactory.createGetMarkup(null,
+ WSRPTypeFactory.createPortletContext(PORTLET_HANDLE),
+ WSRPTypeFactory.createRuntimeContext(WSRPConstants.NONE_USER_AUTHENTICATION,
"foo", "ns"), null,
+ WSRPTypeFactory.createMarkupParams(false, WSRPConstants.getDefaultLocales(),
mimeTypes, WSRPConstants.VIEW_MODE, WSRPConstants.NORMAL_WINDOW_STATE)));
+
+ assertEquals(TestProducerHelper.PORTLET_MIME_TYPE,
processor.markupRequest.getMediaType());
+ }
+
private static class TestProducerHelper implements ProducerHelper
{
+ static final String PORTLET_MIME_TYPE = MediaType.TEXT_HTML.getValue();
+
public Portlet getPortletWith(PortletContext portletContext, Registration
registration) throws InvalidHandle, PortletInvokerException
{
return new Portlet()
@@ -109,7 +188,8 @@
windowStateNames.add(WSRPConstants.NORMAL_WINDOW_STATE);
List<MarkupType> markupTypes = new ArrayList<MarkupType>(1);
- markupTypes.add(WSRPTypeFactory.createMarkupType(MediaType.TEXT_HTML.getValue(),
modeNames, windowStateNames, locales));
+ markupTypes.add(WSRPTypeFactory.createMarkupType(PORTLET_MIME_TYPE, modeNames,
windowStateNames, locales));
+ markupTypes.add(WSRPTypeFactory.createMarkupType("text/xml",
modeNames, windowStateNames, locales));
return WSRPTypeFactory.createPortletDescription(PORTLET_HANDLE, markupTypes);
}