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 9DE41200C6E for ; Mon, 8 May 2017 17:32:42 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 9C9A8160BCA; Mon, 8 May 2017 15:32:42 +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 1E0CE160BA5 for ; Mon, 8 May 2017 17:32:40 +0200 (CEST) Received: (qmail 17277 invoked by uid 500); 8 May 2017 15:32:40 -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 17244 invoked by uid 99); 8 May 2017 15:32:40 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 May 2017 15:32:40 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id E15881A7AC3 for ; Mon, 8 May 2017 15:32:39 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -3.822 X-Spam-Level: X-Spam-Status: No, score=-3.822 tagged_above=-999 required=6.31 tests=[KAM_LINEPADDING=1.2, 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 (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id N-lvSjEBNMR4 for ; Mon, 8 May 2017 15:32:34 +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 2650F5FDC4 for ; Mon, 8 May 2017 15:32:32 +0000 (UTC) Received: (qmail 16999 invoked by uid 99); 8 May 2017 15:32:31 -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; Mon, 08 May 2017 15:32:31 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 6F991DFF36; Mon, 8 May 2017 15:32:31 +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: Mon, 08 May 2017 15:32:40 -0000 Message-Id: <36593f46e0be40cd80dd35a458f6071f@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [10/51] [partial] incubator-juneau-website git commit: Support for passing in more class types to REST methods. archived-at: Mon, 08 May 2017 15:32:42 -0000 http://git-wip-us.apache.org/repos/asf/incubator-juneau-website/blob/f1f17bc3/content/site/apidocs/src-html/org/apache/juneau/rest/RequestFormData.html ---------------------------------------------------------------------- diff --git a/content/site/apidocs/src-html/org/apache/juneau/rest/RequestFormData.html b/content/site/apidocs/src-html/org/apache/juneau/rest/RequestFormData.html new file mode 100644 index 0000000..2a41e35 --- /dev/null +++ b/content/site/apidocs/src-html/org/apache/juneau/rest/RequestFormData.html @@ -0,0 +1,343 @@ + + + +Source code + + + +
+
001// ***************************************************************************************************************************
+002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
+003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
+004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
+005// * with the License.  You may obtain a copy of the License at                                                              *
+006// *                                                                                                                         *
+007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
+008// *                                                                                                                         *
+009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
+010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
+011// * specific language governing permissions and limitations under the License.                                              *
+012// ***************************************************************************************************************************
+013package org.apache.juneau.rest;
+014
+015import java.lang.reflect.*;
+016import java.util.*;
+017
+018import org.apache.juneau.*;
+019import org.apache.juneau.internal.*;
+020import org.apache.juneau.parser.*;
+021import org.apache.juneau.urlencoding.*;
+022
+023/**
+024 * Represents the parsed form data parameters in an HTTP request.
+025 */
+026@SuppressWarnings("unchecked")
+027public class RequestFormData extends LinkedHashMap<String,String[]> {
+028   private static final long serialVersionUID = 1L;
+029
+030   private UrlEncodingParser parser;
+031   private BeanSession beanSession;
+032
+033   RequestFormData setParser(UrlEncodingParser parser) {
+034      this.parser = parser;
+035      return this;
+036   }
+037
+038   RequestFormData setBeanSession(BeanSession beanSession) {
+039      this.beanSession = beanSession;
+040      return this;
+041   }
+042
+043   /**
+044    * Sets a request form data parameter value.
+045    *
+046    * @param name The parameter name.
+047    * @param value The parameter value.
+048    */
+049   public void put(String name, Object value) {
+050      super.put(name, new String[]{StringUtils.toString(value)});
+051   }
+052
+053   /**
+054    * Returns a form data parameter value.
+055    * <p>
+056    * Parameter lookup is case-insensitive (consistent with WAS, but differs from Tomcat).
+057    * <p>
+058    * <h5 class='section'>Notes:</h5>
+059    * <ul>
+060    *    <li>Calling this method on URL-Encoded FORM posts causes the body content to be loaded and parsed by the underlying servlet API.
+061    *    <li>This method returns the raw unparsed value, and differs from calling <code>getFormDataParameter(name, String.<jk>class</js>)</code>
+062    *       which will convert the value from UON notation:
+063    *       <ul>
+064    *          <li><js>"null"</js> =&gt; <jk>null</jk>
+065    *          <li><js>"'null'"</js> =&gt; <js>"null"</js>
+066    *          <li><js>"'foo bar'"</js> =&gt; <js>"foo bar"</js>
+067    *          <li><js>"foo~~bar"</js> =&gt; <js>"foo~bar"</js>
+068    *       </ul>
+069    * </ul>
+070    *
+071    * @param name The form data parameter name.
+072    * @return The parameter value, or <jk>null</jk> if parameter does not exist.
+073    */
+074   public String getFirst(String name) {
+075      String[] v = get(name);
+076      if (v == null || v.length == 0)
+077         return null;
+078      if (v.length == 1 && v[0] != null && v[0].isEmpty()) {
+079         // Fix for behavior difference between Tomcat and WAS.
+080         // getParameter("foo") on "&foo" in Tomcat returns "".
+081         // getParameter("foo") on "&foo" in WAS returns null.
+082         if (containsKey(name))
+083            return null;
+084      }
+085      return v[0];
+086   }
+087
+088   /**
+089    * Same as {@link #getFirst(String)} except returns a default value if <jk>null</jk> or empty.
+090    *
+091    * @param name The form data parameter name.
+092    * @param def The default value.
+093    * @return The parameter value, or the default value if <jk>null</jk> or empty.
+094    */
+095   public String getFirst(String name, String def) {
+096      String val = getFirst(name);
+097      if (val == null || val.isEmpty())
+098         return def;
+099      return val;
+100   }
+101
+102   /**
+103    * Returns the specified form data parameter value converted to a POJO using the
+104    *    {@link UrlEncodingParser} registered with this servlet.
+105    * <p>
+106    * <h5 class='section'>Examples:</h5>
+107    * <p class='bcode'>
+108    *    <jc>// Parse into an integer.</jc>
+109    *    <jk>int</jk> myparam = req.getFormDataParameter(<js>"myparam"</js>, <jk>int</jk>.<jk>class</jk>);
+110    *
+111    *    <jc>// Parse into an int array.</jc>
+112    *    <jk>int</jk>[] myparam = req.getFormDataParameter(<js>"myparam"</js>, <jk>int</jk>[].<jk>class</jk>);
+113
+114    *    <jc>// Parse into a bean.</jc>
+115    *    MyBean myparam = req.getFormDataParameter(<js>"myparam"</js>, MyBean.<jk>class</jk>);
+116    *
+117    *    <jc>// Parse into a linked-list of objects.</jc>
+118    *    List myparam = req.getFormDataParameter(<js>"myparam"</js>, LinkedList.<jk>class</jk>);
+119    *
+120    *    <jc>// Parse into a map of object keys/values.</jc>
+121    *    Map myparam = req.getFormDataParameter(<js>"myparam"</js>, TreeMap.<jk>class</jk>);
+122    * </p>
+123    * <p>
+124    * <h5 class='section'>Notes:</h5>
+125    * <ul>
+126    *    <li>Calling this method on URL-Encoded FORM posts causes the body content to be loaded and parsed by the underlying servlet API.
+127    * </ul>
+128    *
+129    * @param name The parameter name.
+130    * @param type The class type to convert the parameter value to.
+131    * @param <T> The class type to convert the parameter value to.
+132    * @return The parameter value converted to the specified class type.
+133    * @throws ParseException
+134    */
+135   public <T> T get(String name, Class<T> type) throws ParseException {
+136      return parse(name, beanSession.getClassMeta(type));
+137   }
+138
+139   /**
+140    * Same as {@link #get(String, Class)} except returns a default value if not specified.
+141    *
+142    * @param name The parameter name.
+143    * @param def The default value if the parameter was not specified or is <jk>null</jk>.
+144    * @param type The class type to convert the parameter value to.
+145    * @param <T> The class type to convert the parameter value to.
+146    * @return The parameter value converted to the specified class type.
+147    * @throws ParseException
+148    */
+149   public <T> T get(String name, T def, Class<T> type) throws ParseException {
+150      return parse(name, def, beanSession.getClassMeta(type));
+151   }
+152
+153   /**
+154    * Same as {@link #get(String, Class)} except for use on multi-part parameters
+155    *    (e.g. <js>"key=1&amp;key=2&amp;key=3"</js> instead of <js>"key=(1,2,3)"</js>)
+156    * <p>
+157    * This method must only be called when parsing into classes of type Collection or array.
+158    *
+159    * @param name The parameter name.
+160    * @param type The class type to convert the parameter value to.
+161    * @return The parameter value converted to the specified class type.
+162    * @throws ParseException
+163    */
+164   public <T> T getAll(String name, Class<T> type) throws ParseException {
+165      return parseAll(name, beanSession.getClassMeta(type));
+166   }
+167
+168   /**
+169    * Returns the specified form data parameter value converted to a POJO using the
+170    *    {@link UrlEncodingParser} registered with this servlet.
+171    * <p>
+172    * <h5 class='section'>Notes:</h5>
+173    * <ul>
+174    *    <li>Calling this method on URL-Encoded FORM posts causes the body content to be loaded and parsed by the underlying servlet API.
+175    *    <li>Use this method if you want to parse into a parameterized <code>Map</code>/<code>Collection</code> object.
+176    * </ul>
+177    * <p>
+178    * <h5 class='section'>Examples:</h5>
+179    * <p class='bcode'>
+180    *    <jc>// Parse into a linked-list of strings.</jc>
+181    *    List&lt;String&gt; myparam = req.getFormDataParameter(<js>"myparam"</js>, LinkedList.<jk>class</jk>, String.<jk>class</jk>);
+182    *
+183    *    <jc>// Parse into a linked-list of linked-lists of strings.</jc>
+184    *    List&lt;List&lt;String&gt;&gt; myparam = req.getFormDataParameter(<js>"myparam"</js>, LinkedList.<jk>class</jk>, LinkedList.<jk>class</jk>, String.<jk>class</jk>);
+185    *
+186    *    <jc>// Parse into a map of string keys/values.</jc>
+187    *    Map&lt;String,String&gt; myparam = req.getFormDataParameter(<js>"myparam"</js>, TreeMap.<jk>class</jk>, String.<jk>class</jk>, String.<jk>class</jk>);
+188    *
+189    *    <jc>// Parse into a map containing string keys and values of lists containing beans.</jc>
+190    *    Map&lt;String,List&lt;MyBean&gt;&gt; myparam = req.getFormDataParameter(<js>"myparam"</js>, TreeMap.<jk>class</jk>, String.<jk>class</jk>, List.<jk>class</jk>, MyBean.<jk>class</jk>);
+191    * </p>
+192    *
+193    * @param name The parameter name.
+194    * @param type The type of object to create.
+195    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
+196    * @param args The type arguments of the class if it's a collection or map.
+197    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
+198    *    <br>Ignored if the main type is not a map or collection.
+199    * @return The parameter value converted to the specified class type.
+200    * @throws ParseException
+201    */
+202   public <T> T get(String name, Type type, Type...args) throws ParseException {
+203      return (T)parse(name, beanSession.getClassMeta(type, args));
+204   }
+205
+206   /**
+207    * Same as {@link #get(String, Type, Type...)} except for use on multi-part parameters
+208    *    (e.g. <js>"key=1&amp;key=2&amp;key=3"</js> instead of <js>"key=(1,2,3)"</js>)
+209    * <p>
+210    * This method must only be called when parsing into classes of type Collection or array.
+211    *
+212    * @param name The parameter name.
+213    * @param type The type of object to create.
+214    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
+215    * @param args The type arguments of the class if it's a collection or map.
+216    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
+217    *    <br>Ignored if the main type is not a map or collection.
+218    * @return The parameter value converted to the specified class type.
+219    * @throws ParseException
+220    */
+221   public <T> T getAll(String name, Type type, Type...args) throws ParseException {
+222      return (T)parseAll(name, beanSession.getClassMeta(type, args));
+223   }
+224
+225   /* Workhorse method */
+226   <T> T parse(String name, T def, ClassMeta<T> cm) throws ParseException {
+227      String val = getFirst(name);
+228      if (val == null)
+229         return def;
+230      return parseValue(val, cm);
+231   }
+232
+233   /* Workhorse method */
+234   <T> T parse(String name, ClassMeta<T> cm) throws ParseException {
+235      String val = getFirst(name);
+236      if (cm.isPrimitive() && (val == null || val.isEmpty()))
+237         return cm.getPrimitiveDefault();
+238      return parseValue(val, cm);
+239   }
+240
+241   /* Workhorse method */
+242   @SuppressWarnings("rawtypes")
+243   <T> T parseAll(String name, ClassMeta<T> cm) throws ParseException {
+244      String[] p = get(name);
+245      if (p == null)
+246         return null;
+247      if (cm.isArray()) {
+248         List c = new ArrayList();
+249         for (int i = 0; i < p.length; i++)
+250            c.add(parseValue(p[i], cm.getElementType()));
+251         return (T)ArrayUtils.toArray(c, cm.getElementType().getInnerClass());
+252      } else if (cm.isCollection()) {
+253         try {
+254            Collection c = (Collection)(cm.canCreateNewInstance() ? cm.newInstance() : new ObjectList());
+255            for (int i = 0; i < p.length; i++)
+256               c.add(parseValue(p[i], cm.getElementType()));
+257            return (T)c;
+258         } catch (ParseException e) {
+259            throw e;
+260         } catch (Exception e) {
+261            // Typically an instantiation exception.
+262            throw new ParseException(e);
+263         }
+264      }
+265      throw new ParseException("Invalid call to getParameters(String, ClassMeta).  Class type must be a Collection or array.");
+266   }
+267
+268   private <T> T parseValue(String val, ClassMeta<T> c) throws ParseException {
+269      return parser.parsePart(val, c);
+270   }
+271}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +