From ae52d6a2ff40a8a7c4702236a3d1c6ffa4dd1df9 Mon Sep 17 00:00:00 2001 From: vovasty Date: Tue, 17 Feb 2009 19:09:51 +0300 Subject: [PATCH] commons pages behavoir (utf8, errors, etc.) --- aramzamzam-commons/commons-pages-behavoir/pom.xml | 25 +++++++++ .../pagesbehavoir/services/HttpStatusCode.java | 39 ++++++++++++++ .../services/PagesBehavoirModule.java | 60 ++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 aramzamzam-commons/commons-pages-behavoir/pom.xml create mode 100644 aramzamzam-commons/commons-pages-behavoir/src/main/java/net/aramzamzam/commons/pagesbehavoir/services/HttpStatusCode.java create mode 100644 aramzamzam-commons/commons-pages-behavoir/src/main/java/net/aramzamzam/commons/pagesbehavoir/services/PagesBehavoirModule.java diff --git a/aramzamzam-commons/commons-pages-behavoir/pom.xml b/aramzamzam-commons/commons-pages-behavoir/pom.xml new file mode 100644 index 0000000..4ca6d59 --- /dev/null +++ b/aramzamzam-commons/commons-pages-behavoir/pom.xml @@ -0,0 +1,25 @@ + + + net.aramzamzam.commons + aramzamzam-commons + 1.0-SNAPSHOT + + 4.0.0 + commons-pages-behavoir + commons-tokens + http://maven.apache.org + + + org.apache.tapestry + tapestry-core + ${tapestry-release-version} + + + javax.servlet + servlet-api + 2.5 + provided + false + + + diff --git a/aramzamzam-commons/commons-pages-behavoir/src/main/java/net/aramzamzam/commons/pagesbehavoir/services/HttpStatusCode.java b/aramzamzam-commons/commons-pages-behavoir/src/main/java/net/aramzamzam/commons/pagesbehavoir/services/HttpStatusCode.java new file mode 100644 index 0000000..a436d2a --- /dev/null +++ b/aramzamzam-commons/commons-pages-behavoir/src/main/java/net/aramzamzam/commons/pagesbehavoir/services/HttpStatusCode.java @@ -0,0 +1,39 @@ +package net.aramzamzam.commons.pagesbehavoir.services; + +import java.io.Serializable; +import java.net.URL; + +import org.apache.tapestry5.Link; + +public class HttpStatusCode implements Serializable { + private static final long serialVersionUID = 7324260894398442878L; + + private final int statusCode; + private final String location; + + public HttpStatusCode(int statusCode) { + this.statusCode = statusCode; + this.location = ""; + } + + public HttpStatusCode(int statusCode, String location) { + this.statusCode = statusCode; + this.location = location; + } + + public HttpStatusCode(int statusCode, Link link) { + this(statusCode, link.toRedirectURI()); + } + + public HttpStatusCode(int statusCode, URL url) { + this(statusCode, url.toExternalForm()); + } + + public int getStatusCode() { + return statusCode; + } + + public String getLocation() { + return location; + } +} \ No newline at end of file diff --git a/aramzamzam-commons/commons-pages-behavoir/src/main/java/net/aramzamzam/commons/pagesbehavoir/services/PagesBehavoirModule.java b/aramzamzam-commons/commons-pages-behavoir/src/main/java/net/aramzamzam/commons/pagesbehavoir/services/PagesBehavoirModule.java new file mode 100644 index 0000000..a403738 --- /dev/null +++ b/aramzamzam-commons/commons-pages-behavoir/src/main/java/net/aramzamzam/commons/pagesbehavoir/services/PagesBehavoirModule.java @@ -0,0 +1,60 @@ +package net.aramzamzam.commons.pagesbehavoir.services; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tapestry5.ioc.MappedConfiguration; +import org.apache.tapestry5.ioc.OrderedConfiguration; +import org.apache.tapestry5.ioc.annotations.InjectService; +import org.apache.tapestry5.services.ComponentEventResultProcessor; +import org.apache.tapestry5.services.HttpServletRequestFilter; +import org.apache.tapestry5.services.HttpServletRequestHandler; +import org.apache.tapestry5.services.Response; + +public class PagesBehavoirModule { + + @SuppressWarnings("unchecked") + public static void contributeComponentEventResultProcessor( + MappedConfiguration configuration, + final Response response) { + configuration.add(HttpStatusCode.class, + new ComponentEventResultProcessor() { + public void processResultValue(HttpStatusCode value) + throws IOException { + if (!value.getLocation().isEmpty()) + response.setHeader("Location", value.getLocation()); + response.sendError(value.getStatusCode(), ""); + } + }); + } + + /** + * UTF-8 в tapestry http://wiki.apache.org/tapestry/Tapestry5Utf8Encoding + * @param requestGlobals + * @return + */ + + public HttpServletRequestFilter buildUtf8Filter() + { + return new HttpServletRequestFilter() + { + @Override + public boolean service(HttpServletRequest request, + HttpServletResponse response, + HttpServletRequestHandler handler) throws IOException { + request.setCharacterEncoding("UTF-8"); + return handler.service(request, response); + } + }; + } + + public void contributeHttpServletRequestHandler( + OrderedConfiguration configuration, + @InjectService("Utf8Filter") + HttpServletRequestFilter utf8Filter) + { + configuration.add("Utf8Filter", utf8Filter, "before:MultipartFilter"); + } +} -- 2.11.4.GIT