Author: lindner
Date: Tue Mar 24 18:39:16 2009
New Revision: 757944
URL: http://svn.apache.org/viewvc?rev=757944&view=rev
Log:
SHINDIG-990 | Add attributes bag for request so listeners can add
request-scoped data
Modified:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/BaseRequestItem.java
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/RequestItem.java
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/BaseRequestItemTest.java
Modified:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/BaseRequestItem.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/BaseRequestItem.java?rev=757944&r1=757943&r2=757944&view=diff
==============================================================================
---
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/BaseRequestItem.java
(original)
+++
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/BaseRequestItem.java
Tue Mar 24 18:39:16 2009
@@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
+import com.google.common.base.Preconditions;
import org.joda.time.DateTime;
import org.json.JSONArray;
import org.json.JSONException;
@@ -50,6 +51,7 @@
final Map<String,Object> parameters;
final Map<String, FormDataItem> formItems;
final BeanJsonConverter jsonConverter;
+ Map<String,Object> attributes;
public BaseRequestItem(Map<String, String[]> parameters,
SecurityToken token,
@@ -58,6 +60,7 @@
this.token = token;
this.converter = converter;
this.parameters = Maps.newHashMap();
+
for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
if (entry.getValue() == null) {
setParameter(entry.getKey(), null);
@@ -192,12 +195,6 @@
return converter.convertToObject(getParameter(parameterName),
dataTypeClass);
}
- /**
- * Assume that all the parameters in the request belong to single aggregate
- * type and convert to it.
- * @param dataTypeClass
- * @return Typed request object
- */
public <T> T getTypedRequest(Class<T> dataTypeClass) {
return jsonConverter.convertToObject(new
JSONObject(this.parameters).toString(),
dataTypeClass);
@@ -285,4 +282,25 @@
return null;
}
}
+
+ private Map<String,Object> getAttributeMap() {
+ if (this.attributes == null){
+ this.attributes = Maps.newHashMap();
+ }
+ return attributes;
+ }
+
+ public Object getAttribute(String val) {
+ Preconditions.checkNotNull(val);
+ return getAttributeMap().get(val);
+ }
+
+ public void setAttribute(String val, Object obj) {
+ Preconditions.checkNotNull(val);
+ if (obj == null) {
+ getAttributeMap().remove(val);
+ } else {
+ getAttributeMap().put(val, obj);
+ }
+ }
}
Modified:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/RequestItem.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/RequestItem.java?rev=757944&r1=757943&r2=757944&view=diff
==============================================================================
---
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/RequestItem.java
(original)
+++
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/RequestItem.java
Tue Mar 24 18:39:16 2009
@@ -45,39 +45,142 @@
int DEFAULT_COUNT = 20;
String APP_SUBSTITUTION_TOKEN = "@app";
+ /**
+ * Gets the Opensocial App ID for this request
+ * @return an app ID
+ */
String getAppId();
+ /**
+ * Gets the value of the updatedSince parameter
+ * @return A Date representing the updatedSince value
+ */
Date getUpdatedSince();
+ /**
+ * Gets the value of the startIndex parameter
+ * @return An integer containing the value of startIndex
+ */
int getStartIndex();
+ /**
+ * Gets the value of the count parameter
+ * @return An integer containing the value of count
+ */
int getCount();
+ /**
+ * Gets the value of the sortBy parameter
+ * @return the value of the sortBy parameter
+ */
String getSortBy();
+ /**
+ * Gets the value of the sortOrder parameter
+ * @return a SortOrder enum value representing the sortOrder parameter
+ */
SortOrder getSortOrder();
+ /**
+ * Gets the value of the filterBy parameter
+ * @return the value of the filterBy parameter
+ */
String getFilterBy();
+ /**
+ * Gets the value of the filterOperation parameter
+ * @return a SortOrder enum value representing the filterOperation parameter
+ */
FilterOperation getFilterOperation();
+ /**
+ * Gets the value of the filterValue parameter
+ * @return the value of the filterValue parameter
+ */
String getFilterValue();
+ /**
+ * Gets the unique set of fields from the request
+ *
+ * @return Set of field names, empty if no fields specified.
+ */
Set<String> getFields();
+ /**
+ * Get the unique set of fields from the request with defaults
+ * @param defaultValue returned if no fields are specified in the request.
+ * @return specified set of fields or default value
+ */
Set<String> getFields(Set<String> defaultValue);
+ /**
+ * Returns the security token of this request
+ * @return the token
+ */
SecurityToken getToken();
+ /**
+ * Converts a parameter into an object using a converter
+ * @param parameterName the name of the parameter with data to convert
+ * @param dataTypeClass The class to make
+ * @param <T> The type of this object
+ * @return A Valid Object of the given type
+ */
+
<T> T getTypedParameter(String parameterName, Class<T> dataTypeClass);
+ /**
+ * Assume that all the parameters in the request belong to single aggregate
+ * type and convert to it.
+ * @param dataTypeClass the class to convert to
+ * @return Typed request object
+ */
+
<T> T getTypedRequest(Class<T> dataTypeClass);
+ /**
+ * Gets the specified parameter as a string
+ * @param paramName the param name to get
+ * @return the paramName value or null if the parameter is not found
+ */
String getParameter(String paramName);
+ /**
+ * Gets the specified parameter as a string, with a default value
+ * @param paramName the param name to get
+ * @param defaultValue the default value of the parameter
+ * @return the paramName value or defaultValue if the parameter is not found
+ */
String getParameter(String paramName, String defaultValue);
+ /**
+ * Tries to get a list of values for a specified parameter. This can
include splitting
+ * text on commas, dereferencing a json array and more.
+ * @param paramName The parameter
+ * @return A list of strings for the given parameter
+ */
List<String> getListParameter(String paramName);
-
+
+ /**
+ * Returns MIME content data for multipart/mixed form submissions
+ * @param partName the part name to retrieve
+ * @return The FormDataItem for this part.
+ */
FormDataItem getFormMimePart(String partName);
+
+ /**
+ * Gets an attribute for this request. Attributes are a place to store
per-request values that persist across the
+ * life cycle.
+ *
+ * @param val the localized string variable for this request
+ * @return the object associated with this requested string value or null if
not found
+ */
+ Object getAttribute(String val);
+
+ /**
+ * Sets an attribute on this request object
+ * @param val string value
+ * @param obj an object
+ */
+ void setAttribute(String val, Object obj);
}
Modified:
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/BaseRequestItemTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/BaseRequestItemTest.java?rev=757944&r1=757943&r2=757944&view=diff
==============================================================================
---
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/BaseRequestItemTest.java
(original)
+++
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/BaseRequestItemTest.java
Tue Mar 24 18:39:16 2009
@@ -110,6 +110,14 @@
assertEquals(Lists.newArrayList("huey", "dewey", "louie"),
request.getListParameter("fields"));
}
+ public void testAttributes() throws Exception {
+ assertNull(request.getAttribute("undefined"));
+ request.setAttribute("test", "value");
+ assertEquals((String)request.getAttribute("test"), "value");
+ request.setAttribute("test", null);
+ assertNull(request.getAttribute("undefined"));
+ }
+
public static class InputData {
String name;
int id;