Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id BEF07200C55 for ; Thu, 13 Apr 2017 22:45:12 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id BDA4B160BA7; Thu, 13 Apr 2017 20:45:12 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 52A25160BB0 for ; Thu, 13 Apr 2017 22:45:10 +0200 (CEST) Received: (qmail 76471 invoked by uid 500); 13 Apr 2017 20:45:09 -0000 Mailing-List: contact commits-help@juneau.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@juneau.incubator.apache.org Delivered-To: mailing list commits@juneau.incubator.apache.org Received: (qmail 76437 invoked by uid 99); 13 Apr 2017 20:45:09 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Apr 2017 20:45:09 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id F3030182398 for ; Thu, 13 Apr 2017 20:45:08 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.222 X-Spam-Level: X-Spam-Status: No, score=-4.222 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id O9v0IdVRXR-H for ; Thu, 13 Apr 2017 20:44:55 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id 97FD660DA7 for ; Thu, 13 Apr 2017 20:44:48 +0000 (UTC) Received: (qmail 75512 invoked by uid 99); 13 Apr 2017 20:44:47 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Apr 2017 20:44:47 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A504EE2F41; Thu, 13 Apr 2017 20:44:47 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: jamesbognar@apache.org To: commits@juneau.incubator.apache.org Date: Thu, 13 Apr 2017 20:45:00 -0000 Message-Id: <9a650c8a08f8493ca1d819353220af27@git.apache.org> In-Reply-To: <334f427b2c734202bd1eea85428a3242@git.apache.org> References: <334f427b2c734202bd1eea85428a3242@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [14/42] incubator-juneau-website git commit: Update javadocs. archived-at: Thu, 13 Apr 2017 20:45:12 -0000 http://git-wip-us.apache.org/repos/asf/incubator-juneau-website/blob/80ff9b74/content/site/apidocs/src-html/org/apache/juneau/rest/RestRequest.html ---------------------------------------------------------------------- diff --git a/content/site/apidocs/src-html/org/apache/juneau/rest/RestRequest.html b/content/site/apidocs/src-html/org/apache/juneau/rest/RestRequest.html index ad54f0e..f552737 100644 --- a/content/site/apidocs/src-html/org/apache/juneau/rest/RestRequest.html +++ b/content/site/apidocs/src-html/org/apache/juneau/rest/RestRequest.html @@ -23,2059 +23,2068 @@ 015import static java.util.Collections.*; 016import static java.util.logging.Level.*; 017import static javax.servlet.http.HttpServletResponse.*; -018 -019import java.io.*; -020import java.lang.reflect.*; -021import java.net.*; -022import java.nio.charset.*; -023import java.text.*; -024import java.util.*; -025import java.util.logging.*; -026 -027import javax.servlet.*; -028import javax.servlet.http.*; -029 -030import org.apache.juneau.*; -031import org.apache.juneau.dto.swagger.*; -032import org.apache.juneau.encoders.*; -033import org.apache.juneau.encoders.Encoder; -034import org.apache.juneau.ini.*; -035import org.apache.juneau.internal.*; -036import org.apache.juneau.parser.*; -037import org.apache.juneau.parser.ParseException; -038import org.apache.juneau.serializer.*; -039import org.apache.juneau.svl.*; -040import org.apache.juneau.uon.*; -041import org.apache.juneau.urlencoding.*; -042import org.apache.juneau.utils.*; -043 -044/** -045 * Represents an HTTP request for a REST resource. -046 * <p> -047 * Equivalent to {@link HttpServletRequest} except with some additional convenience methods. -048 * <p> -049 * For reference, given the URL <js>"http://localhost:9080/contextRoot/servletPath/foo?bar=baz#qux"</js>, the -050 * following methods return the following values.... -051 * <table class='styled'> -052 * <tr><th>Method</th><th>Value</th></tr> -053 * <tr><td>{@code getContextPath()}</td><td>{@code /contextRoot}</td></tr> -054 * <tr><td>{@code getPathInfo()}</td><td>{@code /foo}</td></tr> -055 * <tr><td>{@code getPathTranslated()}</td><td>{@code path-to-deployed-war-on-filesystem/foo}</td></tr> -056 * <tr><td>{@code getQueryString()}</td><td>{@code bar=baz}</td></tr> -057 * <tr><td>{@code getRequestURI()}</td><td>{@code /contextRoot/servletPath/foo}</td></tr> -058 * <tr><td>{@code getRequestURL()}</td><td>{@code http://localhost:9080/contextRoot/servletPath/foo}</td></tr> -059 * <tr><td>{@code getServletPath()}</td><td>{@code /servletPath}</td></tr> -060 * </table> -061 * <p> -062 * Refer to <a class="doclink" href="package-summary.html#TOC">REST Servlet API</a> for information about using this class. -063 */ -064@SuppressWarnings("unchecked") -065public final class RestRequest extends HttpServletRequestWrapper { -066 -067 private final RestContext context; -068 -069 private final String method; -070 private String pathRemainder; -071 private byte[] body; -072 private Method javaMethod; -073 private ObjectMap properties; -074 private SerializerGroup serializerGroup; -075 private ParserGroup parserGroup; -076 private EncoderGroup encoders; -077 private Encoder encoder; -078 private int contentLength; -079 private final boolean debug; -080 private UrlEncodingParser urlEncodingParser; // The parser used to parse URL attributes and parameters (beanContext also used to parse headers) -081 private BeanSession beanSession; -082 private VarResolverSession varSession; -083 private final Map<String,String[]> queryParams; -084 private final Map<String,String> defaultServletHeaders; -085 private Map<String,String> defaultMethodHeaders, overriddenHeaders, overriddenQueryParams, overriddenFormDataParams, pathParameters; -086 private boolean isPost; -087 private String servletURI, relativeServletURI; -088 private String charset, defaultCharset; -089 private ObjectMap headers; -090 private ConfigFile cf; -091 private Swagger swagger, fileSwagger; -092 private String pageTitle, pageText, pageLinks; -093 -094 /** -095 * Constructor. -096 */ -097 RestRequest(RestContext context, HttpServletRequest req) throws ServletException { -098 super(req); -099 this.context = context; -100 -101 try { -102 isPost = req.getMethod().equalsIgnoreCase("POST"); -103 -104 // If this is a POST, we want to parse the query parameters ourselves to prevent -105 // the servlet code from processing the HTTP body as URL-Encoded parameters. -106 if (isPost) -107 queryParams = context.getUrlEncodingParser().parseIntoSimpleMap(getQueryString()); -108 else { -109 queryParams = req.getParameterMap(); -110 } -111 -112 // Get the HTTP method. -113 // Can be overridden through a "method" GET attribute. -114 String _method = super.getMethod(); -115 -116 String m = getQueryParameter("method"); -117 if (context.allowMethodParam(m)) -118 _method = m; -119 -120 method = _method; +018import static org.apache.juneau.internal.StringUtils.*; +019 +020import java.io.*; +021import java.lang.reflect.*; +022import java.net.*; +023import java.nio.charset.*; +024import java.text.*; +025import java.util.*; +026import java.util.logging.*; +027 +028import javax.servlet.*; +029import javax.servlet.http.*; +030 +031import org.apache.juneau.*; +032import org.apache.juneau.dto.swagger.*; +033import org.apache.juneau.encoders.*; +034import org.apache.juneau.encoders.Encoder; +035import org.apache.juneau.ini.*; +036import org.apache.juneau.internal.*; +037import org.apache.juneau.parser.*; +038import org.apache.juneau.parser.ParseException; +039import org.apache.juneau.serializer.*; +040import org.apache.juneau.svl.*; +041import org.apache.juneau.uon.*; +042import org.apache.juneau.urlencoding.*; +043import org.apache.juneau.utils.*; +044 +045/** +046 * Represents an HTTP request for a REST resource. +047 * <p> +048 * Equivalent to {@link HttpServletRequest} except with some additional convenience methods. +049 * <p> +050 * For reference, given the URL <js>"http://localhost:9080/contextRoot/servletPath/foo?bar=baz#qux"</js>, the +051 * following methods return the following values.... +052 * <table class='styled'> +053 * <tr><th>Method</th><th>Value</th></tr> +054 * <tr><td>{@code getContextPath()}</td><td>{@code /contextRoot}</td></tr> +055 * <tr><td>{@code getPathInfo()}</td><td>{@code /foo}</td></tr> +056 * <tr><td>{@code getPathTranslated()}</td><td>{@code path-to-deployed-war-on-filesystem/foo}</td></tr> +057 * <tr><td>{@code getQueryString()}</td><td>{@code bar=baz}</td></tr> +058 * <tr><td>{@code getRequestURI()}</td><td>{@code /contextRoot/servletPath/foo}</td></tr> +059 * <tr><td>{@code getRequestURL()}</td><td>{@code http://localhost:9080/contextRoot/servletPath/foo}</td></tr> +060 * <tr><td>{@code getServletPath()}</td><td>{@code /servletPath}</td></tr> +061 * </table> +062 * <p> +063 * Refer to <a class="doclink" href="package-summary.html#TOC">REST Servlet API</a> for information about using this class. +064 */ +065@SuppressWarnings("unchecked") +066public final class RestRequest extends HttpServletRequestWrapper { +067 +068 private final RestContext context; +069 +070 private final String method; +071 private String pathRemainder; +072 private byte[] body; +073 private Method javaMethod; +074 private ObjectMap properties; +075 private SerializerGroup serializerGroup; +076 private ParserGroup parserGroup; +077 private EncoderGroup encoders; +078 private Encoder encoder; +079 private int contentLength; +080 private final boolean debug; +081 private UrlEncodingParser urlEncodingParser; // The parser used to parse URL attributes and parameters (beanContext also used to parse headers) +082 private BeanSession beanSession; +083 private VarResolverSession varSession; +084 private final Map<String,String[]> queryParams; +085 private Map<String,String> formData; +086 private final Map<String,String> defaultServletHeaders; +087 private Map<String,String> defaultMethodHeaders, overriddenHeaders, overriddenQueryParams, overriddenFormDataParams, pathParameters; +088 private boolean isPost; +089 private String servletURI, relativeServletURI; +090 private String charset, defaultCharset; +091 private ObjectMap headers; +092 private ConfigFile cf; +093 private Swagger swagger, fileSwagger; +094 private String pageTitle, pageText, pageLinks; +095 +096 /** +097 * Constructor. +098 */ +099 RestRequest(RestContext context, HttpServletRequest req) throws ServletException { +100 super(req); +101 this.context = context; +102 +103 try { +104 isPost = req.getMethod().equalsIgnoreCase("POST"); +105 +106 // If this is a POST, we want to parse the query parameters ourselves to prevent +107 // the servlet code from processing the HTTP body as URL-Encoded parameters. +108 if (isPost) +109 queryParams = context.getUrlEncodingParser().parseIntoSimpleMap(getQueryString()); +110 else { +111 queryParams = req.getParameterMap(); +112 } +113 +114 // Get the HTTP method. +115 // Can be overridden through a "method" GET attribute. +116 String _method = super.getMethod(); +117 +118 String m = getQueryParameter("method"); +119 if (context.allowMethodParam(m)) +120 _method = m; 121 -122 if (context.isAllowBodyParam()) { -123 String b = getQueryParameter("body"); -124 if (b != null) { -125 setHeader("Content-Type", UonSerializer.DEFAULT.getResponseContentType()); -126 this.body = b.getBytes(IOUtils.UTF8); -127 } -128 } -129 -130 defaultServletHeaders = context.getDefaultRequestHeaders(); +122 method = _method; +123 +124 if (context.isAllowBodyParam()) { +125 String b = getQueryParameter("body"); +126 if (b != null) { +127 setHeader("Content-Type", UonSerializer.DEFAULT.getResponseContentType()); +128 this.body = b.getBytes(IOUtils.UTF8); +129 } +130 } 131 -132 debug = "true".equals(getQueryParameter("debug", "false")) || "true".equals(getHeader("Debug", "false")); +132 defaultServletHeaders = context.getDefaultRequestHeaders(); 133 -134 } catch (RestException e) { -135 throw e; -136 } catch (Exception e) { -137 throw new ServletException(e); -138 } -139 } -140 -141 /* -142 * Called from RestServlet after a match has been made but before the guard or method invocation. -143 */ -144 @SuppressWarnings("hiding") -145 final void init(Method javaMethod, String pathRemainder, ObjectMap properties, Map<String,String> mDefaultRequestHeaders, String defaultCharset, SerializerGroup mSerializers, ParserGroup mParsers, UrlEncodingParser mUrlEncodingParser, EncoderGroup encoders, String pageTitle, String pageText, String pageLinks) { -146 this.javaMethod = javaMethod; -147 this.pathRemainder = pathRemainder; -148 this.properties = properties; -149 this.defaultMethodHeaders = mDefaultRequestHeaders; -150 this.serializerGroup = mSerializers; -151 this.parserGroup = mParsers; -152 this.urlEncodingParser = mUrlEncodingParser; -153 this.beanSession = urlEncodingParser.getBeanContext().createSession(); -154 this.defaultCharset = defaultCharset; -155 this.encoders = encoders; -156 this.pageTitle = pageTitle; -157 this.pageText = pageText; -158 this.pageLinks = pageLinks; -159 -160 if (debug) { -161 String msg = "" -162 + "\n=== HTTP Request (incoming) ====================================================" -163 + toString() -164 + "\n=== END ========================================================================"; -165 context.getLogger().log(Level.WARNING, msg); -166 } -167 } -168 -169 /** -170 * Returns a string of the form <js>"HTTP method-name full-url"</js> -171 * -172 * @return A description of the request. -173 */ -174 public String getDescription() { -175 String qs = getQueryString(); -176 return "HTTP " + getMethod() + " " + getRequestURI() + (qs == null ? "" : "?" + qs); -177 } -178 -179 -180 //-------------------------------------------------------------------------------- -181 // Properties +134 debug = "true".equals(getQueryParameter("debug", "false")) || "true".equals(getHeader("Debug", "false")); +135 +136 } catch (RestException e) { +137 throw e; +138 } catch (Exception e) { +139 throw new ServletException(e); +140 } +141 } +142 +143 /* +144 * Called from RestServlet after a match has been made but before the guard or method invocation. +145 */ +146 @SuppressWarnings("hiding") +147 final void init(Method javaMethod, String pathRemainder, ObjectMap properties, Map<String,String> mDefaultRequestHeaders, String defaultCharset, SerializerGroup mSerializers, ParserGroup mParsers, UrlEncodingParser mUrlEncodingParser, EncoderGroup encoders, String pageTitle, String pageText, String pageLinks) { +148 this.javaMethod = javaMethod; +149 this.pathRemainder = pathRemainder; +150 this.properties = properties; +151 this.defaultMethodHeaders = mDefaultRequestHeaders; +152 this.serializerGroup = mSerializers; +153 this.parserGroup = mParsers; +154 this.urlEncodingParser = mUrlEncodingParser; +155 this.beanSession = urlEncodingParser.getBeanContext().createSession(); +156 this.defaultCharset = defaultCharset; +157 this.encoders = encoders; +158 this.pageTitle = pageTitle; +159 this.pageText = pageText; +160 this.pageLinks = pageLinks; +161 +162 if (debug) { +163 String msg = "" +164 + "\n=== HTTP Request (incoming) ====================================================" +165 + toString() +166 + "\n=== END ========================================================================"; +167 context.getLogger().log(Level.WARNING, msg); +168 } +169 } +170 +171 /** +172 * Returns a string of the form <js>"HTTP method-name full-url"</js> +173 * +174 * @return A description of the request. +175 */ +176 public String getDescription() { +177 String qs = getQueryString(); +178 return "HTTP " + getMethod() + " " + getRequestURI() + (qs == null ? "" : "?" + qs); +179 } +180 +181 182 //-------------------------------------------------------------------------------- -183 -184 /** -185 * Servlet calls this method to initialize the properties. -186 */ -187 RestRequest setProperties(ObjectMap properties) { -188 this.properties = properties; -189 return this; -190 } -191 -192 /** -193 * Retrieve the properties active for this request. -194 * <p> -195 * These properties can be modified by the request. -196 * -197 * @return The properties active for this request. -198 */ -199 public ObjectMap getProperties() { -200 return this.properties; -201 } -202 -203 -204 //-------------------------------------------------------------------------------- -205 // Headers +183 // Properties +184 //-------------------------------------------------------------------------------- +185 +186 /** +187 * Servlet calls this method to initialize the properties. +188 */ +189 RestRequest setProperties(ObjectMap properties) { +190 this.properties = properties; +191 return this; +192 } +193 +194 /** +195 * Retrieve the properties active for this request. +196 * <p> +197 * These properties can be modified by the request. +198 * +199 * @return The properties active for this request. +200 */ +201 public ObjectMap getProperties() { +202 return this.properties; +203 } +204 +205 206 //-------------------------------------------------------------------------------- -207 -208 /** -209 * Sets a request header value. -210 * -211 * @param name The header name. -212 * @param value The header value. -213 */ -214 public void setHeader(String name, Object value) { -215 if (overriddenHeaders == null) -216 overriddenHeaders = new TreeMap<String,String>(String.CASE_INSENSITIVE_ORDER); -217 overriddenHeaders.put(name, StringUtils.toString(value)); -218 } -219 -220 -221 /** -222 * Returns the specified header value, or <jk>null</jk> if the header doesn't exist. -223 * <p> -224 * If {@code allowHeaderParams} init parameter is <jk>true</jk>, then first looks for {@code &HeaderName=x} in the URL query string. -225 */ -226 @Override /* ServletRequest */ -227 public String getHeader(String name) { -228 return getHeader(name, (String)null); -229 } -230 -231 /** -232 * Returns the specified header value, or a default value if the header doesn't exist. -233 * <p> -234 * If {@code allowHeaderParams} init parameter is <jk>true</jk>, then first looks for {@code &HeaderName=x} in the URL query string. -235 * -236 * @param name The HTTP header name. -237 * @param def The default value to return if the header value isn't found. -238 * @return The header value, or the default value if the header isn't present. -239 */ -240 public String getHeader(String name, String def) { -241 String h = getOverriddenHeader(name); -242 if (h != null) -243 return h; -244 h = super.getHeader(name); -245 if (h != null && ! h.isEmpty()) -246 return h; -247 if (defaultMethodHeaders != null) { -248 h = defaultMethodHeaders.get(name); -249 if (h != null) -250 return h; -251 } -252 h = defaultServletHeaders.get(name); -253 if (h != null) -254 return h; -255 return def; -256 } -257 -258 /** -259 * Returns the specified header value converted to a POJO. -260 * <p> -261 * The type can be any POJO type convertable from a <code>String</code> -262 * (See <a class="doclink" href="package-summary.html#PojosConvertableFromString">POJOs Convertable From Strings</a>). -263 * <p> -264 * <h5 class='section'>Examples:</h5> -265 * <p class='bcode'> -266 * <jc>// Parse into an integer.</jc> -267 * <jk>int</jk> myheader = req.getHeader(<js>"My-Header"</js>, <jk>int</jk>.<jk>class</jk>); -268 * -269 * <jc>// Parse a UUID.</jc> -270 * UUID myheader = req.getHeader(<js>"My-Header"</js>, UUID.<jk>class</jk>); -271 * </p> -272 * -273 * @param name The HTTP header name. -274 * @param type The class type to convert the header value to. -275 * @param <T> The class type to convert the header value to. -276 * @return The parameter value converted to the specified class type. -277 */ -278 public <T> T getHeader(String name, Class<T> type) { -279 String h = getHeader(name); -280 return beanSession.convertToType(h, type); -281 } -282 -283 /** -284 * Same as {@link #getHeader(String, Class)} but returns a default value if not found. -285 * -286 * @param name The HTTP header name. -287 * @param def The default value if the header was not specified or is <jk>null</jk>. -288 * @param type The class type to convert the header value to. -289 * @param <T> The class type to convert the header value to. -290 * @return The parameter value converted to the specified class type. -291 */ -292 public <T> T getHeader(String name, T def, Class<T> type) { -293 String h = getHeader(name); -294 if (h == null) -295 return def; -296 return beanSession.convertToType(h, type); -297 } -298 -299 /** -300 * Returns the specified header value converted to a POJO. -301 * <p> -302 * The type can be any POJO type convertable from a <code>String</code> -303 * (See <a class="doclink" href="package-summary.html#PojosConvertableFromString">POJOs Convertable From Strings</a>). -304 * <p> -305 * <h5 class='section'>Examples:</h5> -306 * <p class='bcode'> -307 * <jc>// Parse into a linked-list of strings.</jc> -308 * List&lt;String&gt; myheader = req.getHeader(<js>"My-Header"</js>, LinkedList.<jk>class</jk>, String.<jk>class</jk>); -309 * </p> -310 * -311 * @param name The HTTP header name. -312 * @param type The type of object to create. -313 * <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType} -314 * @param args The type arguments of the class if it's a collection or map. +207 // Headers +208 //-------------------------------------------------------------------------------- +209 +210 /** +211 * Sets a request header value. +212 * +213 * @param name The header name. +214 * @param value The header value. +215 */ +216 public void setHeader(String name, Object value) { +217 if (overriddenHeaders == null) +218 overriddenHeaders = new TreeMap<String,String>(String.CASE_INSENSITIVE_ORDER); +219 overriddenHeaders.put(name, StringUtils.toString(value)); +220 } +221 +222 +223 /** +224 * Returns the specified header value, or <jk>null</jk> if the header doesn't exist. +225 * <p> +226 * If {@code allowHeaderParams} init parameter is <jk>true</jk>, then first looks for {@code &HeaderName=x} in the URL query string. +227 */ +228 @Override /* ServletRequest */ +229 public String getHeader(String name) { +230 return getHeader(name, (String)null); +231 } +232 +233 /** +234 * Returns the specified header value, or a default value if the header doesn't exist. +235 * <p> +236 * If {@code allowHeaderParams} init parameter is <jk>true</jk>, then first looks for {@code &HeaderName=x} in the URL query string. +237 * +238 * @param name The HTTP header name. +239 * @param def The default value to return if the header value isn't found. +240 * @return The header value, or the default value if the header isn't present. +241 */ +242 public String getHeader(String name, String def) { +243 String h = getOverriddenHeader(name); +244 if (h != null) +245 return h; +246 h = super.getHeader(name); +247 if (h != null && ! h.isEmpty()) +248 return h; +249 if (defaultMethodHeaders != null) { +250 h = defaultMethodHeaders.get(name); +251 if (h != null) +252 return h; +253 } +254 h = defaultServletHeaders.get(name); +255 if (h != null) +256 return h; +257 return def; +258 } +259 +260 /** +261 * Returns the specified header value converted to a POJO. +262 * <p> +263 * The type can be any POJO type convertable from a <code>String</code> +264 * (See <a class="doclink" href="package-summary.html#PojosConvertableFromString">POJOs Convertable From Strings</a>). +265 * <p> +266 * <h5 class='section'>Examples:</h5> +267 * <p class='bcode'> +268 * <jc>// Parse into an integer.</jc> +269 * <jk>int</jk> myheader = req.getHeader(<js>"My-Header"</js>, <jk>int</jk>.<jk>class</jk>); +270 * +271 * <jc>// Parse a UUID.</jc> +272 * UUID myheader = req.getHeader(<js>"My-Header"</js>, UUID.<jk>class</jk>); +273 * </p> +274 * +275 * @param name The HTTP header name. +276 * @param type The class type to convert the header value to. +277 * @param <T> The class type to convert the header value to. +278 * @return The parameter value converted to the specified class type. +279 */ +280 public <T> T getHeader(String name, Class<T> type) { +281 String h = getHeader(name); +282 return beanSession.convertToType(h, type); +283 } +284 +285 /** +286 * Same as {@link #getHeader(String, Class)} but returns a default value if not found. +287 * +288 * @param name The HTTP header name. +289 * @param def The default value if the header was not specified or is <jk>null</jk>. +290 * @param type The class type to convert the header value to. +291 * @param <T> The class type to convert the header value to. +292 * @return The parameter value converted to the specified class type. +293 */ +294 public <T> T getHeader(String name, T def, Class<T> type) { +295 String h = getHeader(name); +296 if (h == null) +297 return def; +298 return beanSession.convertToType(h, type); +299 } +300 +301 /** +302 * Returns the specified header value converted to a POJO. +303 * <p> +304 * The type can be any POJO type convertable from a <code>String</code> +305 * (See <a class="doclink" href="package-summary.html#PojosConvertableFromString">POJOs Convertable From Strings</a>). +306 * <p> +307 * <h5 class='section'>Examples:</h5> +308 * <p class='bcode'> +309 * <jc>// Parse into a linked-list of strings.</jc> +310 * List&lt;String&gt; myheader = req.getHeader(<js>"My-Header"</js>, LinkedList.<jk>class</jk>, String.<jk>class</jk>); +311 * </p> +312 * +313 * @param name The HTTP header name. +314 * @param type The type of object to create. 315 * <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType} -316 * <br>Ignored if the main type is not a map or collection. -317 * @param <T> The class type to convert the header value to. -318 * @return The parameter value converted to the specified class type. -319 */ -320 public <T> T getHeader(String name, Type type, Type...args) { -321 String h = getHeader(name); -322 return (T)beanSession.convertToType(null, h, beanSession.getClassMeta(type, args)); -323 } -324 -325 /** -326 * Returns all the request headers as an {@link ObjectMap}. -327 * <p> -328 * Altering entries in this map does not alter headers in the underlying request. -329 * -330 * @return The request headers. Never <jk>null</jk>. -331 */ -332 public ObjectMap getHeaders() { -333 if (headers == null) { -334 headers = new ObjectMap(); -335 for (Enumeration<String> e = getHeaderNames(); e.hasMoreElements();) { -336 String key = e.nextElement(); -337 headers.put(key, getHeader(key)); -338 } -339 } -340 return headers; -341 } -342 -343 @Override /* ServletRequest */ -344 public Enumeration<String> getHeaders(String name) { -345 String h = getOverriddenHeader(name); -346 if (h != null) -347 return enumeration(singleton(h)); -348 return super.getHeaders(name); -349 } -350 -351 /** -352 * Returns the <code>Content-Type</code> header value on the request, stripped -353 * of any parameters such as <js>";charset=X"</js>. -354 * <p> -355 * Example: <js>"text/json"</js>. -356 * <p> -357 * If the content type is not specified, and the content is specified via a -358 * <code>&amp;body</code> query parameter, the content type is assumed to be -359 * <js>"text/uon"</js>. Otherwise, the content type is assumed to be <js>"text/json"</js>. -360 * -361 * @return The <code>Content-Type</code> media-type header value on the request. -362 */ -363 public MediaType getMediaType() { -364 String cm = getHeader("Content-Type"); -365 if (cm == null) { -366 if (body != null) -367 return MediaType.UON; -368 return MediaType.JSON; -369 } -370 return MediaType.forString(cm); -371 } -372 -373 /** -374 * Returns the <code>Time-Zone</code> header value on the request if there is one. -375 * <p> -376 * Example: <js>"GMT"</js>. -377 * -378 * @return The <code>Time-Zone</code> header value on the request, or <jk>null</jk> if not present. -379 */ -380 public TimeZone getTimeZone() { -381 String tz = getHeader("Time-Zone"); -382 if (tz != null) -383 return TimeZone.getTimeZone(tz); -384 return null; -385 } -386 -387 /** -388 * Returns the media types that are valid for <code>Content-Type</code> headers on the request. -389 * -390 * @return The set of media types registered in the parser group of this request. -391 */ -392 public List<MediaType> getSupportedMediaTypes() { -393 return parserGroup.getSupportedMediaTypes(); -394 } -395 -396 /** -397 * Sets the charset to expect on the request body. -398 */ -399 @Override /* ServletRequest */ -400 public void setCharacterEncoding(String charset) { -401 this.charset = charset; -402 } -403 -404 /** -405 * Returns the charset specified on the <code>Content-Type</code> header, or <js>"UTF-8"</js> if not specified. -406 */ -407 @Override /* ServletRequest */ -408 public String getCharacterEncoding() { -409 if (charset == null) { -410 // Determine charset -411 // NOTE: Don't use super.getCharacterEncoding() because the spec is implemented inconsistently. -412 // Jetty returns the default charset instead of null if the character is not specified on the request. -413 String h = getHeader("Content-Type"); -414 if (h != null) { -415 int i = h.indexOf(";charset="); -416 if (i > 0) -417 charset = h.substring(i+9).trim(); -418 } -419 if (charset == null) -420 charset = defaultCharset; -421 if (! Charset.isSupported(charset)) -422 throw new RestException(SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported charset in header ''Content-Type'': ''{0}''", h); -423 } -424 return charset; -425 } -426 -427 @Override /* ServletRequest */ -428 public Locale getLocale() { -429 String h = getOverriddenHeader("Accept-Language"); -430 if (h != null) { -431 MediaRange[] mr = MediaRange.parse(h); -432 if (mr.length > 0) -433 return toLocale(mr[0].getMediaType().getType()); -434 } -435 return super.getLocale(); -436 } -437 -438 @Override /* ServletRequest */ -439 public Enumeration<Locale> getLocales() { -440 String h = getOverriddenHeader("Accept-Language"); -441 if (h != null) { -442 MediaRange[] mr = MediaRange.parse(h); -443 if (mr.length > 0) { -444 List<Locale> l = new ArrayList<Locale>(mr.length); -445 for (MediaRange r : mr) -446 l.add(toLocale(r.getMediaType().getType())); -447 return enumeration(l); -448 } -449 } -450 return super.getLocales(); -451 } -452 -453 -454 //-------------------------------------------------------------------------------- -455 // Query parameters -456 //-------------------------------------------------------------------------------- -457 -458 /** -459 * Sets a request query parameter value. -460 * -461 * @param name The parameter name. -462 * @param value The parameter value. -463 */ -464 public void setQueryParameter(String name, Object value) { -465 if (overriddenQueryParams == null) -466 overriddenQueryParams = new TreeMap<String,String>(String.CASE_INSENSITIVE_ORDER); -467 overriddenQueryParams.put(name, value == null ? null : value.toString()); -468 } -469 -470 /** -471 * Returns a query parameter value. -472 * <p> -473 * Same as {@link #getParameter(String)} except only looks in the URL string, not parameters from URL-Encoded FORM posts. -474 * <p> -475 * This method can be used to retrieve a parameter without triggering the underlying servlet API to load and parse the request body. -476 * -477 * @param name The URL parameter name. -478 * @return The parameter value, or <jk>null</jk> if parameter not specified or has no value (e.g. <js>"&amp;foo"</js>. -479 */ -480 public String getQueryParameter(String name) { -481 String s = null; -482 if (overriddenQueryParams != null) -483 s = overriddenQueryParams.get(name); -484 if (s != null) -485 return s; -486 String[] v = queryParams.get(name); -487 if (v == null || v.length == 0) -488 return null; -489 if (v.length == 1 && v[0] != null && v[0].isEmpty()) { -490 // Fix for behavior difference between Tomcat and WAS. -491 // getParameter("foo") on "&foo" in Tomcat returns "". -492 // getParameter("foo") on "&foo" in WAS returns null. -493 if (queryParams.containsKey(name)) -494 return null; -495 } -496 return v[0]; -497 } -498 -499 /** -500 * Same as {@link #getQueryParameter(String)} but returns the specified default value if the query parameter was not specified. -501 * -502 * @param name The URL parameter name. -503 * @param def The default value. -504 * @return The parameter value, or the default value if parameter not specified or has no value (e.g. <js>"&amp;foo"</js>. -505 */ -506 public String getQueryParameter(String name, String def) { -507 String s = getQueryParameter(name); -508 return s == null ? def : s; -509 } -510 -511 /** -512 * Returns the specified query parameter value converted to a POJO. -513 * <p> -514 * This method can be used to retrieve a parameter without triggering the underlying servlet API to load and parse the request body. -515 * <p> -516 * <h5 class='section'>Examples:</h5> -517 * <p class='bcode'> -518 * <jc>// Parse into an integer.</jc> -519 * <jk>int</jk> myparam = req.getQueryParameter(<js>"myparam"</js>, <jk>int</jk>.<jk>class</jk>); -520 * -521 * <jc>// Parse into an int array.</jc> -522 * <jk>int</jk>[] myparam = req.getQueryParameter(<js>"myparam"</js>, <jk>int</jk>[].<jk>class</jk>); -523 -524 * <jc>// Parse into a bean.</jc> -525 * MyBean myparam = req.getQueryParameter(<js>"myparam"</js>, MyBean.<jk>class</jk>); -526 * -527 * <jc>// Parse into a linked-list of objects.</jc> -528 * List myparam = req.getQueryParameter(<js>"myparam"</js>, LinkedList.<jk>class</jk>); +316 * @param args The type arguments of the class if it's a collection or map. +317 * <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType} +318 * <br>Ignored if the main type is not a map or collection. +319 * @param <T> The class type to convert the header value to. +320 * @return The parameter value converted to the specified class type. +321 * @throws ParseException If the header could not be converted to the specified type. +322 */ +323 public <T> T getHeader(String name, Type type, Type...args) throws ParseException { +324 String h = getHeader(name); +325 return urlEncodingParser.parsePart(h, type, args); +326 } +327 +328 /** +329 * Returns all the request headers as an {@link ObjectMap}. +330 * <p> +331 * Altering entries in this map does not alter headers in the underlying request. +332 * +333 * @return The request headers. Never <jk>null</jk>. +334 */ +335 public ObjectMap getHeaders() { +336 if (headers == null) { +337 headers = new ObjectMap(); +338 for (Enumeration<String> e = getHeaderNames(); e.hasMoreElements();) { +339 String key = e.nextElement(); +340 headers.put(key, getHeader(key)); +341 } +342 } +343 return headers; +344 } +345 +346 @Override /* ServletRequest */ +347 public Enumeration<String> getHeaders(String name) { +348 String h = getOverriddenHeader(name); +349 if (h != null) +350 return enumeration(singleton(h)); +351 return super.getHeaders(name); +352 } +353 +354 /** +355 * Returns the <code>Content-Type</code> header value on the request, stripped +356 * of any parameters such as <js>";charset=X"</js>. +357 * <p> +358 * Example: <js>"text/json"</js>. +359 * <p> +360 * If the content type is not specified, and the content is specified via a +361 * <code>&amp;body</code> query parameter, the content type is assumed to be +362 * <js>"text/uon"</js>. Otherwise, the content type is assumed to be <js>"text/json"</js>. +363 * +364 * @return The <code>Content-Type</code> media-type header value on the request. +365 */ +366 public MediaType getMediaType() { +367 String cm = getHeader("Content-Type"); +368 if (cm == null) { +369 if (body != null) +370 return MediaType.UON; +371 return MediaType.JSON; +372 } +373 return MediaType.forString(cm); +374 } +375 +376 /** +377 * Returns the <code>Time-Zone</code> header value on the request if there is one. +378 * <p> +379 * Example: <js>"GMT"</js>. +380 * +381 * @return The <code>Time-Zone</code> header value on the request, or <jk>null</jk> if not present. +382 */ +383 public TimeZone getTimeZone() { +384 String tz = getHeader("Time-Zone"); +385 if (tz != null) +386 return TimeZone.getTimeZone(tz); +387 return null; +388