Permalink
Browse files

Added Faces#getRequestParameter/Values(name, type)

  • Loading branch information...
BalusC committed Dec 23, 2016
1 parent c267b1c commit 942bf438bc73e765feac009b768710112dfbba8d
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;
@@ -1074,6 +1075,19 @@ public static String getRequestParameter(String name) {
return FacesLocal.getRequestParameterMap(getContext()).get(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.
@@ -1093,6 +1107,19 @@ public static String getRequestParameter(String name) {
return FacesLocal.getRequestParameterValuesMap(getContext()).get(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.
@@ -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;
@@ -839,6 +840,27 @@ public static String getRequestParameter(FacesContext context, String name) {
return getRequestParameterMap(context).get(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()
@@ -855,6 +877,33 @@ public static String getRequestParameter(FacesContext context, String name) {
return getRequestParameterValuesMap(context).get(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()

0 comments on commit 942bf43

Please sign in to comment.