org.apache.juneau.rest.vars.RequestVar 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.vars;
import javax.servlet.http.*;
import org.apache.juneau.*;
import org.apache.juneau.rest.*;
import org.apache.juneau.svl.*;
/**
* Request attribute variable resolver.
*
*
* The format for this var is "$R{key[,defaultValue]}" .
*
*
* The possible values are:
*
* "contextPath" - Value returned by {@link RestRequest#getContextPath()}
* "method" - Value returned by {@link RestRequest#getMethod()}
* "methodDescription" - Value returned by {@link RestRequest#getMethodDescription()}
* "methodSummary" - Value returned by {@link RestRequest#getMethodSummary()}
* "pathInfo" - Value returned by {@link RestRequest#getPathInfo()}
* "requestParentURI" - Value returned by {@link UriContext#getRootRelativePathInfoParent()}
* "requestURI" - Value returned by {@link RestRequest#getRequestURI()}
* "servletDescription" - Value returned by {@link RestRequest#getServletDescription()}
* "servletParentURI" - Value returned by {@link UriContext#getRootRelativeServletPathParent()}
* "servletPath" - See {@link RestRequest#getServletPath()}
* "servletTitle" - See {@link RestRequest#getServletTitle()}
* "servletURI" - See {@link UriContext#getRootRelativeServletPath()}
* "siteName" - See {@link RestRequest#getSiteName()}
* "Attribute.x" - Value returned by {@link HttpServletRequest#getAttribute(String)}.
* "FormData.x" - Value returned by {@link RestRequest#getFormData(String)}.
* "Header.x" - Value returned by {@link RestRequest#getHeader(String)}.
* "Path.x" - Value returned by {@link RestRequest#getPath(String)}.
* "Query.x" = Value returned by {@link RestRequest#getQuery(String)}.
*
*
* This variable resolver requires that a {@link RestRequest} object be set as a context object on the resolver or a
* session object on the resolver session.
*
*
* Since this is a {@link SimpleVar}, any variables contained in the result will be recursively resolved.
* Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var.
*
* @see org.apache.juneau.svl
*/
public class RequestVar extends DefaultingVar {
/**
* The name of the session or context object that identifies the {@link RestRequest} object.
*/
public static final String SESSION_req = "req";
/** The name of this variable. */
public static final String NAME = "R";
/**
* Constructor.
*/
public RequestVar() {
super(NAME);
}
@Override /* Parameter */
public String resolve(VarResolverSession session, String key) {
RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req);
if (key.length() > 0) {
String k = key.toString();
int i = k.indexOf('.');
if (i != -1) {
String prefix = k.substring(0, i);
String remainder = k.substring(i+1);
Object o = req.resolveProperty(null, prefix, remainder);
if (o != null)
return o.toString();
} else {
Object o = req.resolveProperty(null, "Request", key);
if (o != null)
return o.toString();
}
Object o = req.getProperties().get(key);
if (o != null)
return o.toString();
return req.getPathMatch().get(key);
}
return null;
}
}