/* * 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 Arne Tauber * */ 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; } } }