Permalink
Browse files

Added Faces#getRequestParameter/Values(name, type)

  • Loading branch information...
1 parent c267b1c commit 942bf438bc73e765feac009b768710112dfbba8d @BalusC BalusC committed Dec 23, 2016
Showing with 76 additions and 0 deletions.
  1. +27 −0 src/main/java/org/omnifaces/util/Faces.java
  2. +49 −0 src/main/java/org/omnifaces/util/FacesLocal.java
@@ -47,6 +47,7 @@
import javax.faces.context.Flash;
import javax.faces.context.PartialViewContext;
import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
import javax.faces.event.PhaseId;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
@@ -1075,6 +1076,19 @@ public static String getRequestParameter(String name) {
}
/**
+ * Returns the HTTP request parameter value associated with the given name and implicitly convert it to given type.
+ * @param name The HTTP request parameter name.
+ * @return The HTTP request parameter value associated with the given name and implicitly convert it to given type.
+ * @throws ConverterException When conversion fails.
+ * @see ExternalContext#getRequestParameterMap()
+ * @see Faces#createConverter(Class)
+ * @since 2.6
+ */
+ public static <T> T getRequestParameter(String name, Class<T> type) {
+ return FacesLocal.getRequestParameter(getContext(), name, type);
+ }
+
+ /**
* Returns the HTTP request parameter values map.
* @return The HTTP request parameter values map.
* @see ExternalContext#getRequestParameterValuesMap()
@@ -1094,6 +1108,19 @@ public static String getRequestParameter(String name) {
}
/**
+ * Returns the HTTP request parameter values associated with the given name and implicitly convert it to given type.
+ * @param name The HTTP request parameter name.
+ * @return The HTTP request parameter values associated with the given name and implicitly convert it to given type.
+ * @throws ConverterException When conversion fails.
+ * @see ExternalContext#getRequestParameterValuesMap()
+ * @see Faces#createConverter(Class)
+ * @since 2.6
+ */
+ public static <T> T[] getRequestParameterValues(String name, Class<T> type) {
+ return FacesLocal.getRequestParameterValues(getContext(), name, type);
+ }
+
+ /**
* Returns all HTTP request parts, provided that request is of type <code>multipart/form-data</code>. If there are
* no parts, an empty collection is returned.
* @return all HTTP request parts.
@@ -31,6 +31,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
@@ -841,6 +842,27 @@ public static String getRequestParameter(FacesContext context, String name) {
/**
* {@inheritDoc}
+ * @see Faces#getRequestParameter(String, Class)
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T getRequestParameter(FacesContext context, String name, Class<T> type) {
+ String value = getRequestParameter(context, name);
+
+ if (value == null) {
+ return null;
+ }
+
+ Converter converter = createConverter(context, type);
+
+ if (converter == null) {
+ return (T) value;
+ }
+
+ return (T) converter.getAsObject(context, context.getViewRoot(), value);
+ }
+
+ /**
+ * {@inheritDoc}
* @see Faces#getRequestParameterValuesMap()
*/
public static Map<String, String[]> getRequestParameterValuesMap(FacesContext context) {
@@ -857,6 +879,33 @@ public static String getRequestParameter(FacesContext context, String name) {
/**
* {@inheritDoc}
+ * @see Faces#getRequestParameterValues(String, Class)
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T[] getRequestParameterValues(FacesContext context, String name, Class<T> type) {
+ String[] values = getRequestParameterValues(context, name);
+
+ if (values == null) {
+ return null;
+ }
+
+ Converter converter = createConverter(context, type);
+
+ if (converter == null) {
+ return (T[]) values;
+ }
+
+ Object convertedValues = Array.newInstance(type, values.length);
+
+ for (int i = 0; i < values.length; i++) {
+ Array.set(convertedValues, i, converter.getAsObject(context, context.getViewRoot(), values[i]));
+ }
+
+ return (T[]) convertedValues;
+ }
+
+ /**
+ * {@inheritDoc}
* @see Faces#getRequestParts()
*/
public static Collection<Part> getRequestParts(FacesContext context) {

0 comments on commit 942bf43

Please sign in to comment.