Author: lukaszlenart
Date: Fri Mar 22 18:33:58 2013
New Revision: 1459920
URL: http://svn.apache.org/r1459920
Log:
WW-3975 Adds logic to look for handler without specified charset
Added:
struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java
Modified:
struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
Modified:
struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java?rev=1459920&r1=1459919&r2=1459920&view=diff
==============================================================================
---
struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
(original)
+++
struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
Fri Mar 22 18:33:58 2013
@@ -78,24 +78,28 @@ public class DefaultContentTypeHandlerMa
}
}
}
-
+
/**
* Gets the handler for the request by looking at the request content type
and extension
- * @param req The request
+ * @param request The request
* @return The appropriate handler
*/
- public ContentTypeHandler getHandlerForRequest(HttpServletRequest req) {
+ public ContentTypeHandler getHandlerForRequest(HttpServletRequest request)
{
ContentTypeHandler handler = null;
- String contentType = req.getContentType();
+ String contentType = request.getContentType();
if (contentType != null) {
- int index = contentType.indexOf(';');
- if( index != -1)
- contentType = contentType.substring(0,index).trim();
handler = handlersByContentType.get(contentType);
+ if (handler == null) {
+ // strip off encoding and search again (e.g.,
application/json;charset=ISO-8859-1)
+ int index = contentType.indexOf(';');
+ if (index != -1) {
+ contentType = contentType.substring(0, index).trim();
+ }
+ handler = handlersByContentType.get(contentType);
+ }
}
-
if (handler == null) {
- String extension = findExtension(req.getRequestURI());
+ String extension = findExtension(request.getRequestURI());
handler = handlersByExtension.get(extension);
}
return handler;
Added:
struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java?rev=1459920&view=auto
==============================================================================
---
struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java
(added)
+++
struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java
Fri Mar 22 18:33:58 2013
@@ -0,0 +1,126 @@
+package org.apache.struts2.rest;
+
+import com.opensymphony.xwork2.XWorkTestCase;
+import com.opensymphony.xwork2.inject.Container;
+import com.opensymphony.xwork2.inject.Scope;
+import org.apache.struts2.rest.handler.ContentTypeHandler;
+import org.springframework.mock.web.MockHttpServletRequest;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.HashSet;
+import java.util.Set;
+
+public class DefaultContentTypeHandlerManagerTest extends XWorkTestCase {
+
+ public void testObtainingHandlerForRequestWithEncoding() throws Exception {
+ // given
+ DefaultContentTypeHandlerManager handlerManager = new
DefaultContentTypeHandlerManager();
+ handlerManager.setContainer(new
DummyContainer("application/json;charset=UTF-8", null));
+
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setContentType("application/json;charset=UTF-8");
+
+ // when
+ ContentTypeHandler handler =
handlerManager.getHandlerForRequest(request);
+
+ // then
+ assertNotNull(handler);
+ assertEquals("application/json;charset=UTF-8",
handler.getContentType());
+ }
+
+ public void testObtainingHandlerForRequestWithoutEncoding() throws
Exception {
+ // given
+ DefaultContentTypeHandlerManager handlerManager = new
DefaultContentTypeHandlerManager();
+ handlerManager.setContainer(new DummyContainer("application/json",
null));
+
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setContentType("application/json;charset=UTF-8");
+
+ // when
+ ContentTypeHandler handler =
handlerManager.getHandlerForRequest(request);
+
+ // then
+ assertNotNull(handler);
+ assertEquals("application/json", handler.getContentType());
+ }
+
+ public void testObtainingHandlerForRequestByExtension() throws Exception {
+ // given
+ DefaultContentTypeHandlerManager handlerManager = new
DefaultContentTypeHandlerManager();
+ handlerManager.setContainer(new DummyContainer("text/html", "json"));
+
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setContentType("application/json;charset=UTF-8");
+ request.setRequestURI("/index.json");
+
+ // when
+ ContentTypeHandler handler =
handlerManager.getHandlerForRequest(request);
+
+ // then
+ assertNotNull(handler);
+ assertEquals("text/html", handler.getContentType());
+ assertEquals("json", handler.getExtension());
+ }
+
+}
+
+class DummyContainer implements Container {
+
+ private ContentTypeHandler handler;
+
+ DummyContainer(final String contentType, final String extension) {
+ handler = new ContentTypeHandler() {
+
+ public void toObject(Reader in, Object target) throws IOException {
+
+ }
+
+ public String fromObject(Object obj, String resultCode, Writer
stream) throws IOException {
+ return null;
+ }
+
+ public String getContentType() {
+ return contentType;
+ }
+
+ public String getExtension() {
+ return extension;
+ }
+ };
+ }
+
+ public void inject(Object o) {
+
+ }
+
+ public <T> T inject(Class<T> implementation) {
+ return null;
+ }
+
+ public <T> T getInstance(Class<T> type, String name) {
+ if
(name.startsWith(DefaultContentTypeHandlerManager.STRUTS_REST_HANDLER_OVERRIDE_PREFIX))
{
+ return null;
+ }
+ return (T) handler;
+ }
+
+ public <T> T getInstance(Class<T> type) {
+ return null;
+ }
+
+ public Set<String> getInstanceNames(Class<?> type) {
+ Set<String> handlers = new HashSet<String>();
+ handlers.add("handler");
+ return handlers;
+ }
+
+ public void setScopeStrategy(Scope.Strategy scopeStrategy) {
+
+ }
+
+ public void removeScopeStrategy() {
+
+ }
+}