You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/05/24 22:59:22 UTC

svn commit: r778233 - in /incubator/pivot/trunk/web: src/pivot/web/Query.java test/pivot/web/QueryDictionaryTest.java test/pivot/web/test/QueryDictionaryTest.java

Author: gbrown
Date: Sun May 24 20:59:22 2009
New Revision: 778233

URL: http://svn.apache.org/viewvc?rev=778233&view=rev
Log:
Minor updates to web query classes.

Added:
    incubator/pivot/trunk/web/test/pivot/web/test/QueryDictionaryTest.java
      - copied, changed from r778215, incubator/pivot/trunk/web/test/pivot/web/QueryDictionaryTest.java
Removed:
    incubator/pivot/trunk/web/test/pivot/web/QueryDictionaryTest.java
Modified:
    incubator/pivot/trunk/web/src/pivot/web/Query.java

Modified: incubator/pivot/trunk/web/src/pivot/web/Query.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/web/src/pivot/web/Query.java?rev=778233&r1=778232&r2=778233&view=diff
==============================================================================
--- incubator/pivot/trunk/web/src/pivot/web/Query.java (original)
+++ incubator/pivot/trunk/web/src/pivot/web/Query.java Sun May 24 20:59:22 2009
@@ -50,9 +50,9 @@
  * </ul>
  *
  * @param <V>
- *            The type of the value retrieved or sent via the query. For GET
- *            operations, it is {@link Object}; for POST operations, the type is
- *            {@link URL}. For PUT and DELETE, it is {@link Void}.
+ * The type of the value retrieved or sent via the query. For GET operations,
+ * it is {@link Object}; for POST operations, the type is {@link URL}. For PUT
+ * and DELETE, it is {@link Void}.
  *
  * @author gbrown
  * @author tvolkert
@@ -97,18 +97,18 @@
     }
 
     /**
-     * This class allows multiple values to be set against a given key.
+     * Represents a collection of keyed data associated with a query. Allows
+     * multiple values to be set against a given key.
      *
      * @author brindy
      */
-    public final class QueryDictionary implements Dictionary<String, String>,
-            Iterable<String> {
-
-        private HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
+    public static final class QueryDictionary implements Dictionary<String, String>,
+        Iterable<String> {
+        private HashMap<String, ArrayList<String>> map =  new HashMap<String, ArrayList<String>>();
 
         public String get(String key) {
             ArrayList<String> list = map.get(key);
-            if (null != list && list.getLength() > 0) {
+            if (list != null && list.getLength() > 0) {
                 return list.get(0);
             }
             return null;
@@ -116,8 +116,8 @@
 
         public String get(String key, int index) {
             ArrayList<String> list = map.get(key);
-            if (null == list || list.getLength() <= index) {
-                throw new ArrayIndexOutOfBoundsException(index);
+            if (list == null || list.getLength() <= index) {
+                throw new IndexOutOfBoundsException();
             }
             return list.get(index);
         }
@@ -126,9 +126,9 @@
             ArrayList<String> list = new ArrayList<String>();
             list.add(value);
 
-            ArrayList<String> old = map.put(key, list);
-            if (old != null && old.getLength() > 0) {
-                return old.get(0);
+            ArrayList<String> previous = map.put(key, list);
+            if (previous != null && previous.getLength() > 0) {
+                return previous.get(0);
             }
 
             return null;
@@ -136,7 +136,7 @@
 
         public int add(String key, String value) {
             ArrayList<String> list = map.get(key);
-            if (null == list) {
+            if (list == null) {
                 put(key, value);
                 return 0;
             }
@@ -149,17 +149,16 @@
             ArrayList<String> list = map.get(key);
 
             // e.g if index = 0 and length = 0, throw an exception
-            if (null == list || list.getLength() <= index) {
-                throw new ArrayIndexOutOfBoundsException(index);
+            if (list == null || list.getLength() <= index) {
+                throw new IndexOutOfBoundsException();
             }
 
             list.insert(value, index);
         }
 
         public String remove(String key) {
-
             ArrayList<String> list = map.remove(key);
-            if (null != list && list.getLength() > 0) {
+            if (list != null && list.getLength() > 0) {
                 return list.get(0);
             }
 
@@ -168,8 +167,8 @@
 
         public String remove(String key, int index) {
             ArrayList<String> list = map.get(key);
-            if (null == list || list.getLength() <= index) {
-                throw new ArrayIndexOutOfBoundsException(index);
+            if (list == null || list.getLength() <= index) {
+                throw new IndexOutOfBoundsException();
             }
             return list.get(index);
         }
@@ -188,7 +187,7 @@
 
         public int getLength(String key) {
             ArrayList<String> list = map.get(key);
-            if (null == list) {
+            if (list == null) {
                 return 0;
             }
             return list.getLength();
@@ -204,9 +203,8 @@
      *
      * @author tvolkert
      */
-    private static class QueryListenerList<V> extends
-            SynchronizedListenerList<QueryListener<V>> implements
-            QueryListener<V> {
+    private static class QueryListenerList<V> extends SynchronizedListenerList<QueryListener<V>>
+        implements QueryListener<V> {
         public synchronized void connected(Query<V> query) {
             for (QueryListener<V> listener : this) {
                 listener.connected(query);
@@ -266,7 +264,7 @@
 
         try {
             locationContext = new URL(secure ? HTTPS_PROTOCOL : HTTP_PROTOCOL,
-                    hostname, port, path);
+                hostname, port, path);
         } catch (MalformedURLException exception) {
             throw new IllegalArgumentException(
                     "Unable to construct context URL.", exception);
@@ -305,21 +303,18 @@
         StringBuilder queryStringBuilder = new StringBuilder();
 
         for (String key : parameters) {
-
             for (int index = 0; index < parameters.getLength(key); index++) {
                 try {
                     if (queryStringBuilder.length() > 0) {
                         queryStringBuilder.append("&");
                     }
 
-                    queryStringBuilder.append(URLEncoder.encode(key,
-                            URL_ENCODING)
-                            + "="
-                            + URLEncoder.encode(parameters.get(key, index),
-                                    URL_ENCODING));
+                    queryStringBuilder.append(URLEncoder.encode(key, URL_ENCODING)
+                        + "=" + URLEncoder.encode(parameters.get(key, index),
+                            URL_ENCODING));
                 } catch (UnsupportedEncodingException exception) {
-                    throw new IllegalStateException(
-                            "Unable to construct query string.", exception);
+                    throw new IllegalStateException("Unable to construct query string.",
+                        exception);
                 }
             }
         }
@@ -327,15 +322,12 @@
         URL location = null;
         try {
             String queryString = queryStringBuilder.length() > 0 ? "?"
-                    + queryStringBuilder.toString() : "";
+                + queryStringBuilder.toString() : "";
 
-            location = new URL(locationContext.getProtocol(), locationContext
-                    .getHost(), locationContext.getPort(), locationContext
-                    .getPath()
-                    + queryString);
+            location = new URL(locationContext.getProtocol(), locationContext.getHost(),
+                locationContext.getPort(), locationContext.getPath() + queryString);
         } catch (MalformedURLException exception) {
-            throw new IllegalStateException("Unable to construct query URL.",
-                    exception);
+            throw new IllegalStateException("Unable to construct query URL.", exception);
         }
 
         return location;
@@ -366,27 +358,6 @@
     }
 
     /**
-     * @deprecated use {@link #getParameters()} instead
-     */
-    public QueryDictionary getArguments() {
-        return parameters;
-    }
-
-    /**
-     * @deprecated use {@link #getRequestHeaders()} instead
-     */
-    public QueryDictionary getRequestProperties() {
-        return requestHeaders;
-    }
-
-    /**
-     * @deprecated use {@link #getResponseHeaders()} instead
-     */
-    public QueryDictionary getResponseProperties() {
-        return responseHeaders;
-    }
-
-    /**
      * Returns the status of the most recent execution.
      *
      * @return An HTTP code representing the most recent execution status.
@@ -497,20 +468,18 @@
 
             // Set the request headers
             if (method == Method.POST || method == Method.PUT) {
-                connection.setRequestProperty("Content-Type", serializer
-                        .getMIMEType(value));
+                connection.setRequestProperty("Content-Type", serializer.getMIMEType(value));
             }
+
             for (String key : requestHeaders) {
                 for (int index = 0; index < requestHeaders.getLength(key); index++) {
-                    connection.setRequestProperty(key, requestHeaders.get(key,
-                            index));
+                    connection.setRequestProperty(key, requestHeaders.get(key, index));
                 }
             }
 
             // Set the input/output state
             connection.setDoInput(true);
-            connection.setDoOutput(method == Method.POST
-                    || method == Method.PUT);
+            connection.setDoOutput(method == Method.POST || method == Method.PUT);
 
             // Connect to the server
             connection.connect();
@@ -521,8 +490,7 @@
                 OutputStream outputStream = null;
                 try {
                     outputStream = connection.getOutputStream();
-                    serializer.writeObject(value, new MonitoredOutputStream(
-                            outputStream));
+                    serializer.writeObject(value, new MonitoredOutputStream(outputStream));
                 } finally {
                     if (outputStream != null) {
                         outputStream.close();
@@ -548,8 +516,8 @@
 
             // NOTE Header indexes start at 1, not 0
             int i = 1;
-            for (String key = connection.getHeaderFieldKey(i); key != null; key = connection
-                    .getHeaderFieldKey(++i)) {
+            for (String key = connection.getHeaderFieldKey(i); key != null;
+                key = connection.getHeaderFieldKey(++i)) {
                 responseHeaders.add(key, connection.getHeaderField(i));
             }
 
@@ -558,8 +526,7 @@
                 InputStream inputStream = null;
                 try {
                     inputStream = connection.getInputStream();
-                    value = serializer.readObject(new MonitoredInputStream(
-                            inputStream));
+                    value = serializer.readObject(new MonitoredInputStream(inputStream));
                 } finally {
                     if (inputStream != null) {
                         inputStream.close();

Copied: incubator/pivot/trunk/web/test/pivot/web/test/QueryDictionaryTest.java (from r778215, incubator/pivot/trunk/web/test/pivot/web/QueryDictionaryTest.java)
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/web/test/pivot/web/test/QueryDictionaryTest.java?p2=incubator/pivot/trunk/web/test/pivot/web/test/QueryDictionaryTest.java&p1=incubator/pivot/trunk/web/test/pivot/web/QueryDictionaryTest.java&r1=778215&r2=778233&rev=778233&view=diff
==============================================================================
--- incubator/pivot/trunk/web/test/pivot/web/QueryDictionaryTest.java (original)
+++ incubator/pivot/trunk/web/test/pivot/web/test/QueryDictionaryTest.java Sun May 24 20:59:22 2009
@@ -1,17 +1,17 @@
-package pivot.web;
+package pivot.web.test;
 
 import java.util.HashSet;
 import java.util.Set;
 
 import junit.framework.TestCase;
+import pivot.web.Query;
 import pivot.web.Query.QueryDictionary;
 
 public class QueryDictionaryTest extends TestCase {
 
 	@SuppressWarnings("unchecked")
 	public void testQueryDictionary() {
-		GetQuery query = new GetQuery("localhost", "path");
-		QueryDictionary dict = query.new QueryDictionary();
+		QueryDictionary dict = new Query.QueryDictionary();
 
 		assertNull(dict.get("key"));