/******************************************************************************* * Copyright 2014 Federal Chancellery Austria * MOA-ID has been developed in a cooperation between BRZ, the Federal * Chancellery Austria - ICT staff unit, and Graz University of Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. ******************************************************************************/ /* * Copyright 2003 Federal Chancellery Austria * MOA-ID has been developed in a cooperation between BRZ, the Federal * Chancellery Austria - ICT staff unit, and Graz University of Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package at.gv.egiz.eaaf.core.impl.utils; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; /** * * @author Rudolf Schamberger * */ public class HTTPUtils { // /** // * Utility used to obtainin correct encoded HTTP content. // * Reads a given Content adressed by HTTP-URL into String. // * Content encoding is considered by using the Content-Type HTTP header charset value. // * @param URL HTTP URL to read from. // * @return String representation of content // * @throws IOException on data-reading problems // */ // public static String readHttpURL(String URL) // throws IOException { // // URL url = new URL(URL); // HttpURLConnection conn = (HttpURLConnection)url.openConnection(); // conn.setRequestMethod("GET"); // String contentType = conn.getContentType(); // RE regExp = null; // try { // regExp = new RE("(;.*charset=)(\"*)(.*[^\"])"); // } catch (RESyntaxException e) { // //RESyntaxException is not possible = expr. is costant // } // boolean charsetSupplied = regExp.match(contentType); // String encoding = "ISO-8859-1"; //default HTTP encoding // if (charsetSupplied) { // encoding = regExp.getParen(3); // } // InputStream instream = new BufferedInputStream(conn.getInputStream()); // InputStreamReader isr = new InputStreamReader(instream, encoding); // Reader in = new BufferedReader(isr); // int ch; // StringBuffer buffer = new StringBuffer(); // while ((ch = in.read()) > -1) { // buffer.append((char)ch); // } // in.close(); // conn.disconnect(); // return buffer.toString(); // } /** * Helper method to retrieve server URL including context path * @param request HttpServletRequest * @return Server URL including context path (e.g. http://localhost:8443/moa-id-auth */ public static String getBaseURL(HttpServletRequest request) { StringBuffer buffer = new StringBuffer(getServerURL(request)); // add context path if available String contextPath = request.getContextPath(); if (!StringUtils.isEmpty(contextPath)) { buffer.append(contextPath); } return buffer.toString(); } /** * Helper method to retrieve server URL * @param request HttpServletRequest * @return Server URL (e.g. http://localhost:8443) */ 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(); } /** * Extract the IDP PublicURLPrefix from authrequest * * @param req HttpServletRequest * @return PublicURLPrefix which ends always without / */ public static String extractAuthURLFromRequest(HttpServletRequest req) { String authURL = req.getScheme() + "://" + req.getServerName(); if ((req.getScheme().equalsIgnoreCase("https") && req.getServerPort()!=443) || (req.getScheme().equalsIgnoreCase("http") && req.getServerPort()!=80)) { authURL = authURL.concat(":" + req.getServerPort()); } authURL = authURL.concat(req.getContextPath()); return authURL; } /** * Extract the IDP requested URL from authrequest * * @param req HttpServletRequest * @return RequestURL which ends always without / */ public static String extractAuthServletPathFromRequest(HttpServletRequest req) { return extractAuthURLFromRequest(req).concat(req.getServletPath()); } public static String addURLParameter(String url, String paramname, String paramvalue) { String param = paramname + "=" + paramvalue; if (url.indexOf("?") < 0) return url + "?" + param; else return url + "&" + param; } }