001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.vars; 018 019 020import org.apache.juneau.*; 021import org.apache.juneau.common.utils.*; 022import org.apache.juneau.http.response.*; 023import org.apache.juneau.rest.*; 024import org.apache.juneau.svl.*; 025 026/** 027 * Request attribute variable resolver. 028 * 029 * <p> 030 * The format for this var is <js>"$R{key1[,key2...]}"</js>. 031 * <br>When multiple keys are used, returns the first non-null/empty value. 032 * 033 * <p> 034 * The possible values are: 035 * <ul> 036 * <li><js>"authorityPath"</js> - Value returned by {@link RestRequest#getAuthorityPath()} 037 * <li><js>"contextPath"</js> - Value returned by {@link RestRequest#getContextPath()} 038 * <li><js>"method"</js> - Value returned by {@link RestRequest#getMethod()} 039 * <li><js>"pathInfo"</js> - Value returned by {@link RestRequest#getPathInfo()} 040 * <li><js>"requestParentURI"</js> - Value returned by {@link UriContext#getRootRelativePathInfoParent()} 041 * <li><js>"requestURI"</js> - Value returned by {@link RestRequest#getRequestURI()} 042 * <li><js>"servletParentURI"</js> - Value returned by {@link UriContext#getRootRelativeServletPathParent()} 043 * <li><js>"servletPath"</js> - See {@link RestRequest#getServletPath()} 044 * <li><js>"servletURI"</js> - See {@link UriContext#getRootRelativeServletPath()} 045 * </ul> 046 * 047 * <h5 class='section'>Example:</h5> 048 * <p class='bjava'> 049 * String <jv>servletClass</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$R{servletClass}"</js>); 050 * </p> 051 * 052 * <h5 class='section'>Notes:</h5><ul> 053 * <li class='note'> 054 * This variable resolver requires that a {@link RestRequest} bean be available in the session bean store. 055 * <li class='note'> 056 * For security reasons, nested and recursive variables are not resolved. 057 * </ul> 058 * 059 * <h5 class='section'>See Also:</h5><ul> 060 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerSvlVariables">SVL Variables</a> 061 * </ul> 062 */ 063public class RequestVar extends MultipartResolvingVar { 064 065 /** The name of this variable. */ 066 public static final String NAME = "R"; 067 068 /** 069 * Constructor. 070 */ 071 public RequestVar() { 072 super(NAME); 073 } 074 075 @Override /* Var */ 076 protected boolean allowNested() { 077 return false; 078 } 079 080 @Override /* Var */ 081 protected boolean allowRecurse() { 082 return false; 083 } 084 085 @Override /* Var */ 086 public String resolve(VarResolverSession session, String key) { 087 RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new); 088 char c = StringUtils.charAt(key, 0); 089 if (c == 'a') { 090 if ("authorityPath".equals(key)) 091 return req.getAuthorityPath(); 092 } else if (c == 'c') { 093 if ("contextPath".equals(key)) 094 return req.getContextPath(); 095 } else if (c == 'm') { 096 if ("method".equals(key)) 097 return req.getMethod(); 098 } else if (c == 'p') { 099 if ("pathInfo".equals(key)) 100 return req.getPathInfo(); 101 } else if (c == 'r') { 102 if ("requestParentURI".equals(key)) 103 return req.getUriContext().getRootRelativePathInfoParent(); 104 if ("requestURI".equals(key)) 105 return req.getRequestURI(); 106 } else if (c == 's') { 107 if ("servletClass".equals(key)) 108 return req.getContext().getResourceClass().getName(); 109 if ("servletClassSimple".equals(key)) 110 return req.getContext().getResourceClass().getSimpleName(); 111 if ("servletParentURI".equals(key)) 112 return req.getUriContext().getRootRelativeServletPathParent(); 113 if ("servletPath".equals(key)) 114 return req.getServletPath(); 115 if ("servletURI".equals(key)) 116 return req.getUriContext().getRootRelativeServletPath(); 117 } 118 return req.getAttributes().get(key).asString().orElse(null); 119 } 120 121 @Override /* Var */ 122 public boolean canResolve(VarResolverSession session) { 123 return session.getBean(RestRequest.class).isPresent(); 124 } 125}