Skip to content
Browse files

[jmdb] Redirection to in memory cache working

  • Loading branch information...
1 parent 0f4af42 commit 0471dbcec6efc020a3d3ffc408345e1a1ad380c0 @jimbarritt jimbarritt committed
View
2 http/src/main/java/ixcode/platform/http/server/resource/DataConsumer.java
@@ -3,5 +3,5 @@
import ixcode.platform.http.protocol.request.Request;
public interface DataConsumer {
- void consumeRequest(Request request);
+ void consumeRequest(Request request, RedirectionParameters redirectionParameters);
}
View
15 http/src/main/java/ixcode/platform/http/server/resource/Redirection.java
@@ -8,6 +8,8 @@
private final String requestUrl;
private final String requestPath;
+ private String redirectionUri;
+
public static Redirection redirect(String requestUrl, String requestPath) {
return new Redirection(requestUrl, requestPath);
}
@@ -17,12 +19,21 @@ public static Redirection redirect(String requestUrl, String requestPath) {
this.requestPath = requestPath;
}
- public URI to(String redirectPath) {
+ public Redirection to(String redirectPath) {
String redirectTo = redirectPath;
if (redirectPath.startsWith(".")) {
redirectTo = requestPath.substring(0, requestPath.lastIndexOf("/")) + redirectPath.substring(1);
}
String urlRoot = requestUrl.substring(0, requestUrl.length() - requestPath.length());
- return new UriFormat().parseString(urlRoot + redirectTo);
+ redirectionUri = urlRoot + redirectTo;
+ return this;
+ }
+
+ public URI withParameters(RedirectionParameters redirectionParameters) {
+ return new UriFormat().parseString(redirectionUri + redirectionParameters.toUrlSection());
+ }
+
+ public URI withNoParameters() {
+ return withParameters(new RedirectionParameters());
}
}
View
26 http/src/main/java/ixcode/platform/http/server/resource/RedirectionParameters.java
@@ -0,0 +1,26 @@
+package ixcode.platform.http.server.resource;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+public class RedirectionParameters {
+
+ private Map<String, Object> parameters = new LinkedHashMap<String, Object>();
+
+ public RedirectionParameters() {
+ this.parameters = parameters;
+ }
+
+ public RedirectionParameters add(String key, Object value) {
+ parameters.put(key, value);
+ return this;
+ }
+
+ public String toUrlSection() {
+ StringBuilder sb = new StringBuilder();
+ for (String key : parameters.keySet()) {
+ sb.append("/").append(parameters.get(key));
+ }
+ return sb.toString();
+ }
+}
View
19 http/src/main/java/ixcode/platform/http/server/resource/TemplateConfig.java
@@ -25,7 +25,9 @@
public TemplatedPage toEntry(String path, String templateName,
File sourceFile,
InjectionContext injectionContext) {
- return new TemplatedPage(path,
+ String pathWithParameters = addParametersTo(path);
+
+ return new TemplatedPage(pathWithParameters,
templateName,
sourceFile,
(Map<String, Object>) get.get("data"),
@@ -35,6 +37,21 @@ public TemplatedPage toEntry(String path, String templateName,
injectionContext);
}
+ private String addParametersTo(String path) {
+ List<String> parameters = (List<String>)get.get("parameters");
+
+ if (parameters == null) {
+ return path;
+ }
+
+ StringBuilder sb = new StringBuilder(path);
+
+ for (String parameter : parameters) {
+ sb.append("/{").append(parameter).append("}");
+ }
+ return sb.toString();
+ }
+
private static List<DataProvider> loadDataProviders(List<String> providerNames, InjectionContext di) {
List<DataProvider> providers = new ArrayList<DataProvider>();
if (providerNames == null) {
View
4 http/src/main/java/ixcode/platform/http/server/resource/TemplatedPage.java
@@ -104,9 +104,9 @@ public synchronized TemplatedPage autoRefresh() {
}
- public void handlePOST(Request request) {
+ public void handlePOST(Request request, RedirectionParameters redirectionParameters) {
for (DataConsumer consumer : dataConsumers) {
- consumer.consumeRequest(request);
+ consumer.consumeRequest(request, redirectionParameters);
}
}
}
View
6 http/src/main/java/ixcode/platform/http/server/resource/TemplatedPageResource.java
@@ -48,10 +48,12 @@ public void POST(Request request,
page = page.autoRefresh();
- page.handlePOST(request);
+ RedirectionParameters redirectionParameters = new RedirectionParameters();
+
+ page.handlePOST(request, redirectionParameters);
URI redirectUri = redirect(request.getUrl(), request.getPath())
- .to(page.redirectTo);
+ .to(page.redirectTo).withParameters(redirectionParameters);
respondWith.status().seeOther(redirectUri)
.contentType().html()
View
13 http/src/test/unit/java/ixcode/platform/http/server/resource/RedirectionTest.java
@@ -10,10 +10,21 @@
public class RedirectionTest {
+
+
@Test
public void redirects_with_a_relative_path() {
- URI to = redirect("http://some/path", "/some/path").to("./newpath");
+ URI to = redirect("http://some/path", "/some/path").to("./newpath").withNoParameters();
assertThat(to.toString(), is("http://some/newpath"));
}
+
+ @Test
+ public void redirects_with_parameters() {
+ RedirectionParameters parameters = new RedirectionParameters().add("foo", "bar");
+
+ URI to = redirect("http://some/path", "/some/path").to("./newpath").withParameters(parameters);
+
+ assertThat(to.toString(), is("http://some/newpath/bar"));
+ }
}

0 comments on commit 0471dbc

Please sign in to comment.
Something went wrong with that request. Please try again.