001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.hadoop.lib.wsrs;
019
020 import org.apache.hadoop.classification.InterfaceAudience;
021
022 import com.google.common.collect.Lists;
023
024 import java.util.List;
025 import java.util.Map;
026
027 /**
028 * Class that contains all parsed JAX-RS parameters.
029 * <p/>
030 * Instances are created by the {@link ParametersProvider} class.
031 */
032 @InterfaceAudience.Private
033 public class Parameters {
034 private Map<String, List<Param<?>>> params;
035
036 /**
037 * Constructor that receives the request parsed parameters.
038 *
039 * @param params the request parsed parameters.
040 */
041 public Parameters(Map<String, List<Param<?>>> params) {
042 this.params = params;
043 }
044
045 /**
046 * Returns the value of a request parsed parameter.
047 *
048 * @param name parameter name.
049 * @param klass class of the parameter, used for value casting.
050 * @return the value of the parameter.
051 */
052 @SuppressWarnings("unchecked")
053 public <V, T extends Param<V>> V get(String name, Class<T> klass) {
054 List<Param<?>> multiParams = (List<Param<?>>)params.get(name);
055 if (multiParams != null && multiParams.size() > 0) {
056 return ((T) multiParams.get(0)).value(); // Return first value;
057 }
058 return null;
059 }
060
061 /**
062 * Returns the values of a request parsed parameter.
063 *
064 * @param name parameter name.
065 * @param klass class of the parameter, used for value casting.
066 * @return List<V> the values of the parameter.
067 */
068 @SuppressWarnings("unchecked")
069 public <V, T extends Param<V>> List<V> getValues(String name, Class<T> klass) {
070 List<Param<?>> multiParams = (List<Param<?>>)params.get(name);
071 List<V> values = Lists.newArrayList();
072 if (multiParams != null) {
073 for (Param<?> param : multiParams) {
074 V value = ((T) param).value();
075 if (value != null) {
076 values.add(value);
077 }
078 }
079 }
080 return values;
081 }
082 }