Author: julien(a)jboss.com
Date: 2008-04-18 09:23:00 -0400 (Fri, 18 Apr 2008)
New Revision: 10646
Added:
modules/web/trunk/web/src/main/java/org/jboss/portal/web/util/
modules/web/trunk/web/src/main/java/org/jboss/portal/web/util/RequestDecoder.java
Modified:
modules/web/trunk/web/src/main/java/org/jboss/portal/web/impl/AbstractWebRequest.java
Log:
factor our usefull request decoder into a decoder class
Modified:
modules/web/trunk/web/src/main/java/org/jboss/portal/web/impl/AbstractWebRequest.java
===================================================================
---
modules/web/trunk/web/src/main/java/org/jboss/portal/web/impl/AbstractWebRequest.java 2008-04-18
12:32:38 UTC (rev 10645)
+++
modules/web/trunk/web/src/main/java/org/jboss/portal/web/impl/AbstractWebRequest.java 2008-04-18
13:23:00 UTC (rev 10646)
@@ -22,18 +22,15 @@
******************************************************************************/
package org.jboss.portal.web.impl;
-import org.jboss.portal.common.http.QueryStringParser;
-import org.jboss.portal.common.net.media.ContentType;
import org.jboss.portal.common.net.media.MediaType;
import org.jboss.portal.web.Body;
import org.jboss.portal.web.IllegalRequestException;
import org.jboss.portal.web.WebRequest;
+import org.jboss.portal.web.util.RequestDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Map;
-import java.util.Collections;
-import java.util.HashMap;
import java.nio.charset.Charset;
import java.io.UnsupportedEncodingException;
@@ -86,93 +83,15 @@
throw new IllegalRequestException("HTTP Method " + req.getMethod() +
" not accepted");
}
- // Compute the media type in the content type
- MediaType mediaType = null;
- if (req.getContentType() != null)
- {
- ContentType contentType = ContentType.create(req.getContentType());
- mediaType = contentType.getMediaType();
- }
+ //
+ RequestDecoder decoder = new RequestDecoder(req);
- // Parse the query string to have the get parameters
- // The resulting map has its parameters decoded from the x-www-form-url encoding
- Map<String, String[]> queryParameterMap;
- String queryString = req.getQueryString();
- if (queryString != null)
- {
- queryParameterMap =
QueryStringParser.getInstance().parseQueryString(queryString);
- }
- else
- {
- queryParameterMap = Collections.emptyMap();
- }
- // Only affect the charset encoding if the servlet container will decode the
request
- Body body = null;
- if (verb == Verb.POST)
- {
- if (APPLICATION_X_WWW_FORM_URLENCODED_MEDIA_TYPE.equals(mediaType))
- {
- // Now we must ensure that we have either an equals or a trailing space after
the media-type
- String characterEncoding = req.getCharacterEncoding();
- if (characterEncoding == null)
- {
- // Set out charset for the request
- req.setCharacterEncoding(UTF_8_CHARSET.name());
- }
- else
- {
- Charset charset = Charset.forName(characterEncoding);
- if (!UTF_8_CHARSET.equals(charset))
- {
- throw new IllegalRequestException("Charset " +
characterEncoding + " not accepted, it should be UTF8");
- }
- }
-
- //
- Map<String, String[]> bodyParameterMap = new HashMap<String,
String[]>();
- for (Map.Entry<String, String[]> entry : ((Map<String,
String[]>)req.getParameterMap()).entrySet())
- {
- // Get param name
- String paramName = entry.getKey();
-
- // Values that are aggregated from the query string and the body
- String[] paramValues = entry.getValue();
-
- // Values decoded from the query string
- String[] queryValues = queryParameterMap.get(paramName);
-
- //
- if (queryValues != null)
- {
- int bodyValuesLength = paramValues.length - queryValues.length;
- if (bodyValuesLength > 0)
- {
- String[] bodyValues = new String[bodyValuesLength];
- System.arraycopy(paramValues, queryValues.length, bodyValues, 0,
bodyValuesLength);
- bodyParameterMap.put(paramName, bodyValues);
- }
- }
- else
- {
- bodyParameterMap.put(paramName, paramValues);
- }
- }
-
- //
- body = new Body.Form(req.getCharacterEncoding(), bodyParameterMap);
- }
- else
- {
- body = new Body.Raw(req.getCharacterEncoding(), req);
- }
- }
-
//
this.verb = verb;
- this.queryParameterMap = queryParameterMap;
- this.body = body;
- this.mediaType = mediaType;
+ this.queryParameterMap = decoder.getQueryParameters();
+ this.body = decoder.getBody();
+ this.mediaType = decoder.getMediaType();
}
public Verb getVerb()
Added: modules/web/trunk/web/src/main/java/org/jboss/portal/web/util/RequestDecoder.java
===================================================================
--- modules/web/trunk/web/src/main/java/org/jboss/portal/web/util/RequestDecoder.java
(rev 0)
+++
modules/web/trunk/web/src/main/java/org/jboss/portal/web/util/RequestDecoder.java 2008-04-18
13:23:00 UTC (rev 10646)
@@ -0,0 +1,167 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web.util;
+
+import org.jboss.portal.web.Body;
+import org.jboss.portal.web.IllegalRequestException;
+import org.jboss.portal.common.http.QueryStringParser;
+import org.jboss.portal.common.net.media.MediaType;
+import org.jboss.portal.common.net.media.ContentType;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.Map;
+import java.util.Collections;
+import java.util.HashMap;
+import java.nio.charset.Charset;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * Useful request decoder.
+ *
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class RequestDecoder
+{
+
+ /** . */
+ private static final Charset UTF_8_CHARSET = Charset.forName("UTF-8");
+
+ /** . */
+ private final MediaType mediaType;
+
+ /** . */
+ private final Map<String, String[]> queryParameters;
+
+ /** . */
+ private final Body body;
+
+ public RequestDecoder(HttpServletRequest request) throws UnsupportedEncodingException
+ {
+ if (request == null)
+ {
+ throw new IllegalArgumentException("No null http request accepted");
+ }
+
+ // Parse the query string to have the get parameters
+ // The resulting map has its parameters decoded from the x-www-form-url encoding
+ String queryString = request.getQueryString();
+ if (queryString != null)
+ {
+ queryParameters =
QueryStringParser.getInstance().parseQueryString(queryString);
+ }
+ else
+ {
+ queryParameters = Collections.emptyMap();
+ }
+
+ if (request.getContentType() != null)
+ {
+ ContentType contentType = ContentType.create(request.getContentType());
+ mediaType = contentType.getMediaType();
+ }
+ else
+ {
+ mediaType = null;
+ }
+
+ //
+ if ("POST".equals(request.getMethod()))
+ {
+ if (MediaType.APPLICATION_X_WWW_FORM_URLENCODED.equals(mediaType))
+ {
+ // Now we must ensure that we have either an equals or a trailing space after
the media-type
+ String characterEncoding = request.getCharacterEncoding();
+ if (characterEncoding == null)
+ {
+ // Set out charset for the request
+ request.setCharacterEncoding(UTF_8_CHARSET.name());
+ }
+ else
+ {
+ Charset charset = Charset.forName(characterEncoding);
+ if (!UTF_8_CHARSET.equals(charset))
+ {
+ throw new IllegalRequestException("Charset " +
characterEncoding + " not accepted, it should be UTF8");
+ }
+ }
+
+ //
+ Map<String, String[]> bodyParameterMap = new HashMap<String,
String[]>();
+ for (Map.Entry<String, String[]> entry : ((Map<String,
String[]>)request.getParameterMap()).entrySet())
+ {
+ // Get param name
+ String paramName = entry.getKey();
+
+ // Values that are aggregated from the query string and the body
+ String[] paramValues = entry.getValue();
+
+ // Values decoded from the query string
+ String[] queryValues = queryParameters.get(paramName);
+
+ //
+ if (queryValues != null)
+ {
+ int bodyValuesLength = paramValues.length - queryValues.length;
+ if (bodyValuesLength > 0)
+ {
+ String[] bodyValues = new String[bodyValuesLength];
+ System.arraycopy(paramValues, queryValues.length, bodyValues, 0,
bodyValuesLength);
+ bodyParameterMap.put(paramName, bodyValues);
+ }
+ }
+ else
+ {
+ bodyParameterMap.put(paramName, paramValues);
+ }
+ }
+
+ //
+ body = new Body.Form(request.getCharacterEncoding(), bodyParameterMap);
+ }
+ else
+ {
+ body = new Body.Raw(request.getCharacterEncoding(), request);
+ }
+ }
+ else
+ {
+ body = null;
+ }
+ }
+
+ public MediaType getMediaType()
+ {
+ return mediaType;
+ }
+
+ public Map<String, String[]> getQueryParameters()
+ {
+ return queryParameters;
+ }
+
+ public Body getBody()
+ {
+ return body;
+ }
+}