org.apache.juneau.rest.RequestPathMatch Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.rest;
import static org.apache.juneau.internal.StringUtils.*;
import java.lang.reflect.*;
import java.util.*;
import org.apache.juneau.*;
import org.apache.juneau.parser.*;
import org.apache.juneau.urlencoding.*;
/**
* Contains information about the matched path on the HTTP request.
*
*
* Provides access to the matched path variables and path match remainder.
*/
@SuppressWarnings("unchecked")
public class RequestPathMatch extends TreeMap {
private static final long serialVersionUID = 1L;
private UrlEncodingParser parser;
private BeanSession beanSession;
private String remainder;
RequestPathMatch() {
super(String.CASE_INSENSITIVE_ORDER);
}
RequestPathMatch setParser(UrlEncodingParser parser) {
this.parser = parser;
return this;
}
RequestPathMatch setBeanSession(BeanSession beanSession) {
this.beanSession = beanSession;
return this;
}
RequestPathMatch setRemainder(String remainder) {
this.remainder = remainder;
return this;
}
/**
* Sets a request query parameter value.
*
* @param name The parameter name.
* @param value The parameter value.
*/
public void put(String name, Object value) {
super.put(name, value.toString());
}
/**
* Returns the specified path parameter converted to a POJO.
*
*
* The type can be any POJO type convertible from a String
(See POJOs Convertible From Strings).
*
*
Examples:
*
* // Parse into an integer.
* int myparam = req.getPathParameter("myparam" , int .class );
*
* // Parse into an int array.
* int [] myparam = req.getPathParameter("myparam" , int [].class );
* // Parse into a bean.
* MyBean myparam = req.getPathParameter("myparam" , MyBean.class );
*
* // Parse into a linked-list of objects.
* List myparam = req.getPathParameter("myparam" , LinkedList.class );
*
* // Parse into a map of object keys/values.
* Map myparam = req.getPathParameter("myparam" , TreeMap.class );
*
*
* @param name The attribute name.
* @param type The class type to convert the attribute value to.
* @param The class type to convert the attribute value to.
* @return The attribute value converted to the specified class type.
* @throws ParseException
*/
public T get(String name, Class type) throws ParseException {
return parse(name, beanSession.getClassMeta(type));
}
/**
* Returns the specified path parameter converted to a POJO.
*
*
* The type can be any POJO type convertible from a String
(See POJOs Convertible From Strings).
*
*
* Use this method if you want to parse into a parameterized Map
/Collection
object.
*
*
Examples:
*
* // Parse into a linked-list of strings.
* List<String> myparam = req.getPathParameter("myparam" , LinkedList.class , String.class );
*
* // Parse into a linked-list of linked-lists of strings.
* List<List<String>> myparam = req.getPathParameter("myparam" , LinkedList.class , LinkedList.class , String.class );
*
* // Parse into a map of string keys/values.
* Map<String,String> myparam = req.getPathParameter("myparam" , TreeMap.class , String.class , String.class );
*
* // Parse into a map containing string keys and values of lists containing beans.
* Map<String,List<MyBean>> myparam = req.getPathParameter("myparam" , TreeMap.class , String.class , List.class , MyBean.class );
*
*
* @param name The attribute name.
* @param type
* The type of object to create.
*
Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType},
* {@link GenericArrayType}
* @param args
* The type arguments of the class if it's a collection or map.
*
Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType},
* {@link GenericArrayType}
*
Ignored if the main type is not a map or collection.
* @param The class type to convert the attribute value to.
* @return The attribute value converted to the specified class type.
* @throws ParseException
*/
public T get(String name, Type type, Type...args) throws ParseException {
return (T)parse(name, beanSession.getClassMeta(type, args));
}
/* Workhorse method */
T parse(String name, ClassMeta cm) throws ParseException {
Object attr = get(name);
T t = null;
if (attr != null)
t = parser.parse(PartType.PATH, attr.toString(), cm);
if (t == null && cm.isPrimitive())
return cm.getPrimitiveDefault();
return t;
}
/**
* Returns the decoded remainder of the URL following any path pattern matches.
*
*
* The behavior of path remainder is shown below given the path pattern "/foo/*":
*
*
* URL
* Path Remainder
*
*
* /foo
* null
*
*
* /foo/
* ""
*
*
* /foo//
* "/"
*
*
* /foo///
* "//"
*
*
* /foo/a/b
* "a/b"
*
*
* /foo//a/b/
* "/a/b/"
*
*
* /foo/a%2Fb
* "a/b"
*
*
*
* Example:
*
* // REST method
* @RestMethod (name=GET ,path="/foo/{bar}/*" )
* public String doGetById(RequestPathParams pathParams, int bar) {
* return pathParams.getRemainder();
* }
*
* // Prints "path/remainder"
* new RestCall(servletPath + "/foo/123/path/remainder" ).connect();
*
*
* @return The path remainder string.
*/
public String getRemainder() {
return urlDecode(remainder);
}
/**
* Same as {@link #getRemainder()} but doesn't decode characters.
*
* @return The un-decoded path remainder.
*/
public String getRemainderUndecoded() {
return remainder;
}
}