summaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/util/WebAppUtil.java
diff options
context:
space:
mode:
authorThomas Lenz <thomas.lenz@egiz.gv.at>2013-12-20 12:35:28 +0100
committerThomas Lenz <thomas.lenz@egiz.gv.at>2013-12-20 12:35:28 +0100
commitdefceef8afef538555c13d33e344a89a828a3d97 (patch)
tree24b44f970f161d5b139dde501ca0f5d883f9fdea /src/main/java/at/gv/util/WebAppUtil.java
downloadegovutils-defceef8afef538555c13d33e344a89a828a3d97.tar.gz
egovutils-defceef8afef538555c13d33e344a89a828a3d97.tar.bz2
egovutils-defceef8afef538555c13d33e344a89a828a3d97.zip
inital
Diffstat (limited to 'src/main/java/at/gv/util/WebAppUtil.java')
-rw-r--r--src/main/java/at/gv/util/WebAppUtil.java150
1 files changed, 150 insertions, 0 deletions
diff --git a/src/main/java/at/gv/util/WebAppUtil.java b/src/main/java/at/gv/util/WebAppUtil.java
new file mode 100644
index 0000000..12e287f
--- /dev/null
+++ b/src/main/java/at/gv/util/WebAppUtil.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2011 Federal Chancellery Austria and
+ * Graz University of Technology
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package at.gv.util;
+
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.Enumeration;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import at.gv.util.ex.InternalErrorException;
+
+/**
+ * Class providing several utility methods for the Servlet API.
+ *
+ * @author <a href="mailto:Arne.Tauber@egiz.gv.at">Arne Tauber</a>
+ *
+ */
+public class WebAppUtil {
+
+ private static Logger log = LoggerFactory.getLogger(WebAppUtil.class);
+
+ public static void sendHTTPRedirect(HttpServletResponse response, String redirectURL) {
+ response.setContentLength(0);
+ response.setContentType("text/html");
+ response.setHeader("Location", redirectURL);
+ response.setStatus(HttpServletResponse.SC_FOUND);
+ }
+
+ public static void disableBrowserCacheForResponse(HttpServletResponse response) {
+ log.debug("Disabling browser cache for HttpServletResponse.");
+ response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
+ response.setHeader("Pragma", "no-cache");
+ response.setDateHeader("Expires", 1);
+ }
+
+ public static String getBaseURLUnSecure(HttpServletRequest request) {
+ String url = getBaseURL(request);
+ if (url.startsWith("https")) {
+ return "http" + url.substring(5);
+ }
+ return url;
+ }
+
+ public static String getBaseURL(HttpServletRequest request) {
+ StringBuffer buffer = new StringBuffer(getServerURL(request));
+
+ // add context path if available
+ String contextPath = request.getContextPath();
+ if (MiscUtil.isNotEmpty(contextPath)) {
+ buffer.append(contextPath).append("/");
+ }
+
+ return buffer.toString();
+ }
+
+ public static String getServerURL(HttpServletRequest request) {
+ StringBuffer buffer = new StringBuffer();
+
+ // get protocol
+ String protocol = request.getScheme();
+ buffer.append(protocol).append("://");
+
+ // server name
+ buffer.append(request.getServerName());
+
+ // add port if necessary
+ int port = request.getServerPort();
+ if ((protocol.equals("http") && port != 80)
+ || (protocol.equals("https") && port != 443)) {
+ buffer.append(':');
+ buffer.append(port);
+ }
+
+ return buffer.toString();
+ }
+
+ public static String getRequestURLWithParameters(HttpServletRequest request) {
+ return getRequestURLWithParameters(request, false);
+ }
+
+ public static String getRequestURLWithParameters(HttpServletRequest request,
+ boolean includeRequestParameters) {
+ StringBuffer buffer = request.getRequestURL();
+ if (includeRequestParameters) {
+ Enumeration paramEnum = request.getParameterNames();
+ if (paramEnum.hasMoreElements()) {
+ buffer.append("?");
+ while (paramEnum.hasMoreElements()) {
+ String param = (String) paramEnum.nextElement();
+ try {
+ buffer.append(URLEncoder.encode(param, "UTF-8"));
+ if (!MiscUtil.isEmpty(request.getParameter(param))) {
+ buffer.append("=").append(
+ URLEncoder.encode(request.getParameter(param), "UTF-8"));
+ }
+
+ } catch (UnsupportedEncodingException e) {
+ // should never happen as far as the java installation is ok
+ throw new InternalErrorException(e);
+ }
+ if (paramEnum.hasMoreElements()) {
+ buffer.append("&");
+ }
+ }
+ }
+ } else {
+ String queryString = request.getQueryString();
+ if (MiscUtil.isNotEmpty(queryString)) {
+ buffer.append("?").append(queryString);
+ }
+ }
+ String requestURL = buffer.toString();
+ log.debug("Returning request url "
+ + (includeRequestParameters ? "including request parameters" : "")
+ + " \"" + requestURL + "\".");
+ return requestURL;
+ }
+
+ public static boolean isFullQualifiedURL(String url) {
+ MiscUtil.assertNotNull(url, "URL");
+ try {
+ new URL(url);
+ return true;
+ } catch (MalformedURLException e) {
+ return false;
+ }
+ }
+
+}