Added:
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/BasicChain.java
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/BasicRequestService.java
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/Chain.java
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/Hook.java
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/Request.java
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/RequestService.java
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/Response.java
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/util
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/util/MapUtil.java
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/test/java/org/mycontainer/commons/http
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/test/java/org/mycontainer/commons/http/request
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/test/java/org/mycontainer/commons/http/request/BasicRequestServiceTest.java
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/test/java/org/mycontainer/commons/http/request/TestHookDouble.java
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/test/java/org/mycontainer/commons/http/request/TestHookReply.java
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/test/java/org/mycontainer/commons/http/request/TestHookSquare.java
Modified:
/trunk/devel/implementation/mycontainer-components/mycontainer-servlet-commons/src/main/java/org/mycontainer/commons/servlet/ServletUtil.java
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/BasicChain.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,22 @@
+package org.mycontainer.commons.http.request;
+
+import java.util.Iterator;
+
+public class BasicChain implements Chain {
+
+ private final Iterator<Hook> it;
+
+ public BasicChain(Iterator<Hook> it) {
+ this.it = it;
+ }
+
+ public Response proceed(Request request) {
+ if (!this.it.hasNext()) {
+ throw new RuntimeException("chain finished");
+ }
+
+ Hook hook = this.it.next();
+ return hook.invoke(request, this);
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/BasicRequestService.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,19 @@
+package org.mycontainer.commons.http.request;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class BasicRequestService implements RequestService {
+
+ private final List<Hook> hooks = new ArrayList<Hook>();
+
+ public void addHook(Hook hook) {
+ this.hooks.add(hook);
+ }
+
+ public Response invoke(Request request) {
+ BasicChain chain = new BasicChain(new ArrayList<Hook>(hooks).iterator());
+ return chain.proceed(request);
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/Chain.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,7 @@
+package org.mycontainer.commons.http.request;
+
+public interface Chain {
+
+ public Response proceed(Request request);
+
+}
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/Hook.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,7 @@
+package org.mycontainer.commons.http.request;
+
+public interface Hook {
+
+ public Response invoke(Request request, Chain chain);
+
+}
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/Request.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,64 @@
+package org.mycontainer.commons.http.request;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Request {
+
+ private String method;
+
+ private String uri;
+
+ private Map<String, List<String>> queryString = new HashMap<String,
List<String>>();
+
+ private Map<String, List<String>> headers = new HashMap<String,
List<String>>();
+
+ private Object content;
+
+ public String getMethod() {
+ return method;
+ }
+
+ public Request setMethod(String method) {
+ this.method = method;
+ return this;
+ }
+
+ public String getUri() {
+ return uri;
+ }
+
+ public Request setUri(String uri) {
+ this.uri = uri;
+ return this;
+ }
+
+ public Map<String, List<String>> getQueryString() {
+ return queryString;
+ }
+
+ public Request setQueryString(Map<String, List<String>> queryString) {
+ this.queryString = queryString;
+ return this;
+ }
+
+ public Map<String, List<String>> getHeaders() {
+ return headers;
+ }
+
+ public Request setHeaders(Map<String, List<String>> headers) {
+ this.headers = headers;
+ return this;
+ }
+
+ public Object getContent() {
+ return content;
+ }
+
+ public Request setContent(Object content) {
+ this.content = content;
+ return this;
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/RequestService.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,7 @@
+package org.mycontainer.commons.http.request;
+
+public interface RequestService {
+
+ public Response invoke(Request request);
+
+}
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/http/request/Response.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,49 @@
+package org.mycontainer.commons.http.request;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Response {
+
+ private Integer code;
+
+ private String codeMessage;
+
+ private Map<String, List<String>> headers = new HashMap<String,
List<String>>();
+
+ private Object content;
+
+ public Integer getCode() {
+ return code;
+ }
+
+ public void setCode(Integer code) {
+ this.code = code;
+ }
+
+ public String getCodeMessage() {
+ return codeMessage;
+ }
+
+ public void setCodeMessage(String codeMessage) {
+ this.codeMessage = codeMessage;
+ }
+
+ public Map<String, List<String>> getHeaders() {
+ return headers;
+ }
+
+ public void setHeaders(Map<String, List<String>> headers) {
+ this.headers = headers;
+ }
+
+ public Object getContent() {
+ return content;
+ }
+
+ public void setContent(Object content) {
+ this.content = content;
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/main/java/org/mycontainer/commons/util/MapUtil.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,42 @@
+package org.mycontainer.commons.util;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class MapUtil {
+
+ @SuppressWarnings("unchecked")
+ public static <K, V> Map<K, List<V>> populateList(Map<K, List<V>> map,
+ Object... array) {
+ if (map == null) {
+ map = new HashMap<K, List<V>>();
+ }
+ for (int i = 0; i < array.length; i += 2) {
+ K name = (K) array[i];
+ List<V> list = map.get(name);
+ if (list == null) {
+ list = new ArrayList<V>();
+ map.put(name, list);
+ }
+ V value = (V) array[i + 1];
+ list.add(value);
+ }
+ return map;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <K, V> Map<K, V> populate(Map<K, V> map, Object... array) {
+ if (map == null) {
+ map = new HashMap<K, V>();
+ }
+ for (int i = 0; i < array.length; i += 2) {
+ K name = (K) array[i];
+ V value = (V) array[i + 1];
+ map.put(name, value);
+ }
+ return map;
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/test/java/org/mycontainer/commons/http/request/BasicRequestServiceTest.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,63 @@
+package org.mycontainer.commons.http.request;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.junit.Test;
+import org.mycontainer.commons.util.MapUtil;
+
+public class BasicRequestServiceTest {
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testRequestReply() {
+ BasicRequestService service = new BasicRequestService();
+ service.addHook(new TestHookReply());
+
+ Request req = new Request();
+ req.setMethod("GET").setUri("my/uri").setContent(5l);
+ MapUtil.populateList(req.getQueryString(), "k1", "v11", "k2", "v2",
+ "k1", "v12");
+ MapUtil.populateList(req.getHeaders(), "n1", "v11", "n2", "v2", "n1",
+ "v12");
+ Response resp = service.invoke(req);
+ assertEquals(200, resp.getCode());
+ assertEquals("OK", resp.getCodeMessage());
+ assertEquals("v11", resp.getHeaders().get("n1").get(0));
+ assertEquals("v12", resp.getHeaders().get("n1").get(1));
+ assertEquals("v2", resp.getHeaders().get("n2").get(0));
+ Map<String, Object> content = (Map<String, Object>) resp.getContent();
+ assertEquals("k1=v11&k1=v12&k2=v2&", content.get("queryString"));
+ assertEquals(5l, content.get("result"));
+
+ service.addHook(new TestHookDouble());
+ service.addHook(new TestHookSquare());
+
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testRequestChain() {
+ BasicRequestService service = new BasicRequestService();
+ service.addHook(new TestHookDouble());
+ service.addHook(new TestHookSquare());
+ service.addHook(new TestHookReply());
+
+ Request req = new Request();
+ req.setMethod("GET").setUri("my/uri").setContent(5l);
+ MapUtil.populateList(req.getQueryString(), "k1", "v11", "k2", "v2",
+ "k1", "v12");
+ MapUtil.populateList(req.getHeaders(), "n1", "v11", "n2", "v2", "n1",
+ "v12");
+ Response resp = service.invoke(req);
+ assertEquals(200, resp.getCode());
+ assertEquals("OK", resp.getCodeMessage());
+ assertEquals("v11", resp.getHeaders().get("n1").get(0));
+ assertEquals("v12", resp.getHeaders().get("n1").get(1));
+ assertEquals("v2", resp.getHeaders().get("n2").get(0));
+ Map<String, Object> content = (Map<String, Object>) resp.getContent();
+ assertEquals("k1=v11&k1=v12&k2=v2&", content.get("queryString"));
+ assertEquals(100l, content.get("result"));
+ }
+}
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/test/java/org/mycontainer/commons/http/request/TestHookDouble.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,14 @@
+package org.mycontainer.commons.http.request;
+
+public class TestHookDouble implements Hook {
+
+ public void invoke(Request request, Response response, Chain chain) {
+ }
+
+ public Response invoke(Request request, Chain chain) {
+ Long content = (Long) request.getContent();
+ request.setContent(content * 2);
+ return chain.proceed(request);
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/test/java/org/mycontainer/commons/http/request/TestHookReply.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,32 @@
+package org.mycontainer.commons.http.request;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+public class TestHookReply implements Hook {
+
+ public Response invoke(Request request, Chain chain) {
+ Response response = new Response();
+ response.setCode(200);
+ response.setCodeMessage("OK");
+ response.getHeaders().putAll(request.getHeaders());
+ Set<Entry<String, List<String>>> set = request.getQueryString()
+ .entrySet();
+ StringBuilder sb = new StringBuilder();
+ for (Entry<String, List<String>> entry : set) {
+ List<String> values = entry.getValue();
+ for (String value : values) {
+ sb.append(entry.getKey()).append('=').append(value).append('&');
+ }
+ }
+ Map<String, Object> content = new HashMap<String, Object>();
+ content.put("queryString", sb.toString());
+ content.put("result", request.getContent());
+ response.setContent(content);
+ return response;
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-commons/src/test/java/org/mycontainer/commons/http/request/TestHookSquare.java
Sat Jun 25 11:17:50 2011
@@ -0,0 +1,11 @@
+package org.mycontainer.commons.http.request;
+
+public class TestHookSquare implements Hook {
+
+ public Response invoke(Request request, Chain chain) {
+ Long content = (Long) request.getContent();
+ request.setContent(content * content);
+ return chain.proceed(request);
+ }
+
+}
=======================================
---
/trunk/devel/implementation/mycontainer-components/mycontainer-servlet-commons/src/main/java/org/mycontainer/commons/servlet/ServletUtil.java
Thu Jun 23 11:30:24 2011
+++
/trunk/devel/implementation/mycontainer-components/mycontainer-servlet-commons/src/main/java/org/mycontainer/commons/servlet/ServletUtil.java
Sat Jun 25 11:17:50 2011
@@ -1,8 +1,13 @@
package org.mycontainer.commons.servlet;
import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
@@ -68,5 +73,58 @@
throw new RuntimeException(e);
}
}
+
+ public static String checkParameter(HttpServletRequest req,
+ HttpServletResponse resp, String name, String... requires) {
+ String value = req.getParameter(name);
+ if (value == null) {
+ value = "";
+ }
+ value = value.trim();
+ for (String require : requires) {
+ if (require.equals(value)) {
+ return (require.length() == 0 ? requires[0] : require);
+ }
+ }
+ StringBuilder sb = new StringBuilder();
+ sb.append("parameter required '").append(name).append("'");
+ if (requires.length > 0) {
+ sb.append(" with one of these: ").append(Arrays.toString(requires));
+ } else {
+ sb.append(" with any value");
+ }
+ sb.append(", but was: '").append(value).append("'");
+ throw new RuntimeException(sb.toString());
+ }
+
+ public static <T> List<T> getParameters(HttpServletRequest req,
+ String name, Class<T> clazz) {
+ try {
+ String[] values = req.getParameterValues(name);
+ if (values == null) {
+ return new ArrayList<T>();
+ }
+ ArrayList<T> ret = new ArrayList<T>(values.length);
+ for (String value : values) {
+ Constructor<T> cons = clazz
+ .getConstructor(new Class[] { String.class });
+ T parsed = cons.newInstance(value);
+ ret.add(parsed);
+ }
+ return ret;
+ } catch (SecurityException e) {
+ throw new RuntimeException(e);
+ } catch (IllegalArgumentException e) {
+ throw new RuntimeException(e);
+ } catch (NoSuchMethodException e) {
+ throw new RuntimeException(e);
+ } catch (InstantiationException e) {
+ throw new RuntimeException(e);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
+ }
}