package votorola.g.web; // Copyright 2011-2013, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import votorola.g.lang.*; /** Servlet request utilities. * * @see HttpServletRequest */ public @ThreadSafe final class HTTPServletRequestX { private HTTPServletRequestX() {} /** Constructs a standard log message summarizing the request, and returns it in a * string builder. */ public static StringBuilder buildRequestSummary( final HttpServletRequest reqHS ) { final StringBuilder b = getRequestURLFull( reqHS ); b.insert( 0, ": " ); b.insert( 0, reqHS.getRemoteAddr() ); b.insert( 0, "Request from " ); return b; } /** Returns the boolean value of a request parameter, where true is encoded as a 'Y', * 'y' or empty value '', and false as an 'N', 'n' or omitted value (null). * * @throws HTTPRequestException if the value is present and malformed. */ public static boolean getBooleanParameter( final String key, final ServletRequest reqS ) throws HTTPRequestException { final String value = reqS.getParameter( key ); if( value == null || "N".equals(value) || "n".equals(value) ) return false; if( "".equals(value) || "Y".equals(value) || "y".equals(value) ) return true; throw new HTTPRequestException( HttpServletResponse.SC_BAD_REQUEST, // 400 "Unrecognized '" + key + "' value: " + value ); } /** Returns the apparent address of the remote client after taking into account any * proxying declared in the x-forwarded-for header. No attempt is made * to authenticate the proxying declaration, which may easily be bogus. Therefore do * not depend on the returned value being correct, or even well formed. */ public static String getForwardedRemoteAddr( final HttpServletRequest reqHS ) { // http://johannburkard.de/blog/programming/java/x-forwarded-for-http-header.html String a = reqHS.getHeader( "X-Forwarded-For" ); if( a == null ) a = reqHS.getRemoteAddr(); else { final int c = a.indexOf( ',' ); if( c >= 0 ) a = a.substring( 0, c ); // first address in list is most remote } return a; } // /** Returns the value of a request parameter, or a default value if the parameter is // * not specified. // * // * @param defaultValue the value to return in the event the parameter is not // * specified. // * // * @throws HTTPRequestException if the actual value is the empty string "". // */ // public static String getParameter( final String key, final String defaultValue, // final ServletRequest reqS ) throws HTTPRequestException // { // String value = getParameterNonEmpty( key, reqS ); // if( value == null ) value = defaultValue; // return value; // } /** Returns the non-empty value of a request parameter, or null if the parameter is * not specified. * * @throws HTTPRequestException if the value is the empty string "". * * @see votorola.a.web.wic.VPage#stringNonEmpty(org.apache.wicket.request.mapper.parameter.PageParameters,String) * @see votorola.g.web.wic.PageParametersX#getStringNonEmpty(org.apache.wicket.request.mapper.parameter.PageParameters,String) */ public static String getParameterNonEmpty( final String key, final ServletRequest reqS ) throws HTTPRequestException { final String value = reqS.getParameter( key ); if( !"".equals( value )) return value; throw new HTTPRequestException( HttpServletResponse.SC_BAD_REQUEST, // 400 "Empty request parameter '" + key + "'" ); } /** Returns the non-null, non-empty value of a request parameter. * * @throws HTTPRequestException if the value is either null or the empty string "". * * @see votorola.a.web.wic.VPage#stringRequired(org.apache.wicket.request.mapper.parameter.PageParameters,String) * @see votorola.g.web.wic.PageParametersX#getStringRequired(org.apache.wicket.request.mapper.parameter.PageParameters,String) */ public static String getParameterRequired( final String key, final ServletRequest reqS ) throws HTTPRequestException { final String value = reqS.getParameter( key ); if( value != null && !"".equals( value )) return value; throw new HTTPRequestException( HttpServletResponse.SC_BAD_REQUEST, // 400 "Missing request parameter '" + key + "'" ); } /** Reconstructs the full URL the client used to make the request, including any query * portion, and returns it in a string builder. * * @see HttpServletRequest#getRequestURL() */ public static StringBuilder getRequestURLFull( final HttpServletRequest reqHS ) { final StringBuilder b = new StringBuilder( reqHS.getRequestURL().toString() ); final String q = reqHS.getQueryString(); if( q != null ) b.append( '?' ).append( q ); return b; } /** Copies headers of the specified name from an incoming client request (reqHS) to an * outgoing server request (http). */ public static void relayHeaders( final String name, final HttpServletRequest reqHS, final URLConnection http ) { final Enumeration values = reqHS.getHeaders( name ); while( values.hasMoreElements() ) { http.setRequestProperty( name, values.nextElement().toString() ); } } }