org.apache.juneau.rest.RestMethodParam 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 java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.juneau.*;
import org.apache.juneau.config.*;
import org.apache.juneau.dto.swagger.*;
import org.apache.juneau.http.*;
import org.apache.juneau.http.Date;
import org.apache.juneau.parser.*;
import org.apache.juneau.utils.*;
/**
* REST java method parameter resolver.
*
*
* Used to resolve instances of classes being passed to Java REST methods.
*
*
* By default, the following parameter types can be passed into Java methods in any order:
*
*
Standard top-level objects
*
* - Standard top-level objects
*
* - {@link HttpServletRequest}
*
- {@link RestRequest}
*
- {@link HttpServletResponse}
*
- {@link RestResponse}
*
* - Headers
*
* - {@link Accept}
*
- {@link AcceptCharset}
*
- {@link AcceptEncoding}
*
- {@link AcceptLanguage}
*
- {@link Authorization}
*
- {@link CacheControl}
*
- {@link Connection}
*
- {@link ContentLength}
*
- {@link ContentType}
*
- {@link Date}
*
- {@link Expect}
*
- {@link From}
*
- {@link Host}
*
- {@link IfMatch}
*
- {@link IfModifiedSince}
*
- {@link IfNoneMatch}
*
- {@link IfRange}
*
- {@link IfUnmodifiedSince}
*
- {@link MaxForwards}
*
- {@link Pragma}
*
- {@link ProxyAuthorization}
*
- {@link Range}
*
- {@link Referer}
*
- {@link TE}
*
- {@link TimeZone}
*
- {@link UserAgent}
*
- {@link Upgrade}
*
- {@link Via}
*
- {@link Warning}
*
* - Other objects
*
* - {@link Config}
*
- {@link HttpMethod}
*
- {@link InputStream}
*
- {@link Locale}
*
- {@link MessageBundle}
*
- {@link OutputStream}
*
- {@link Parser}
*
- {@link Reader}
*
- {@link RequestBody}
*
- {@link RequestFormData}
*
- {@link RequestHeaders}
*
- {@link RequestPath}
*
- {@link RequestQuery}
*
- {@link ResourceBundle}
*
- {@link RestContext}
*
- {@link RestLogger}
*
- {@link RequestProperties}
*
- {@link ServletInputStream}
*
- {@link ServletOutputStream}
*
- {@link Swagger}
*
- {@link UriContext}
*
- {@link UriResolver}
*
- {@link Writer}
*
*
*
* See Also:
*
* - {@link RestContext#REST_paramResolvers}
*
- {@doc juneau-rest-server.RestMethod.MethodParameters}
*
*/
public abstract class RestMethodParam {
final RestParamType paramType;
final Method method;
final int index;
final String name;
final Type type;
final Class> c;
/**
* Constructor.
*
* @param paramType The Swagger parameter type.
* @param method The method on which the parameter resides.
* @param index The method parameter index.
* @param name
* The parameter name.
* Can be null if parameter doesn't have a name (e.g. the request body).
* @param type The object type to convert the parameter to.
*/
protected RestMethodParam(RestParamType paramType, Method method, int index, String name, Type type) {
this.paramType = paramType;
this.method = method;
this.index = index;
this.name = name;
this.type = type;
this.c = type instanceof Class ? (Class>)type : type instanceof ParameterizedType ? (Class>)((ParameterizedType)type).getRawType() : null;
}
/**
* Constructor.
*
* @param paramType The Swagger parameter type.
* @param method The method on which the parameter resides.
* @param index The method parameter index.
* @param name
* The parameter name.
* Can be null if parameter doesn't have a name (e.g. the request body).
*/
protected RestMethodParam(RestParamType paramType, Method method, int index, String name) {
this(paramType, method, index, name, method.getGenericParameterTypes()[index]);
}
/**
* Constructor.
*
* @param paramType The Swagger parameter type.
* @param method The method on which the parameter resides.
* @param index The method parameter index.
*/
protected RestMethodParam(RestParamType paramType, Method method, int index) {
this(paramType, method, index, null, method.getGenericParameterTypes()[index]);
}
/**
* Constructor.
*
* @param paramType The Swagger parameter type.
* @param type The object type to convert the parameter to.
*/
protected RestMethodParam(RestParamType paramType, Type type) {
this(paramType, null, -1, null, type);
}
/**
* Constructor.
*
* @param paramType The Swagger parameter type.
* @param name
* The parameter name.
* Can be null if parameter doesn't have a name (e.g. the request body).
* @param type The object type to convert the parameter to.
*/
protected RestMethodParam(RestParamType paramType, String name, Type type) {
this(paramType, null, -1, name, type);
}
/**
* Resolves the parameter object.
*
* @param req The rest request.
* @param res The rest response.
* @return The resolved object.
* @throws Exception
*/
public abstract Object resolve(RestRequest req, RestResponse res) throws Exception;
/**
* Returns the parameter class type that this parameter resolver is meant for.
*
* @return The parameter class type, or null if the type passed in isn't an instance of {@link Class}.
*/
protected Class> forClass() {
if (type instanceof Class)
return (Class>)type;
return null;
}
/**
* Returns the swagger parameter type for this parameter as shown in the Swagger doc.
*
* @return the swagger parameter type for this parameter.
*/
protected RestParamType getParamType() {
return paramType;
}
/**
* Returns the parameter name for this parameter as shown in the Swagger doc.
*
* @return the parameter name for this parameter.
*/
protected String getName() {
return name;
}
/**
* Returns the parameter class type.
*
* @return the parameter class type.
*/
public Type getType() {
return type;
}
/**
* Returns the parameter class type.
*
* @return the parameter class type.
*/
public Class> getTypeClass() {
return c;
}
}