001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.vars;
014
015import org.apache.juneau.*;
016import org.apache.juneau.rest.*;
017import org.apache.juneau.svl.*;
018
019/**
020 * URL variable resolver.
021 *
022 * <p>
023 * The format for this var is <js>"$U{uri}"</js>.
024 *
025 * <p>
026 * The advantage of using this variable is that you can resolve URLs with special protocols such as
027 * <js>"servlet:/xxx"</js>.
028 *
029 * <p>
030 * See {@link UriResolver} for the kinds of URIs that can be resolved.
031 *
032 * <p>
033 * Uses the URI resolver returned by {@link RestRequest#getUriResolver()}.
034 *
035 * <ul class='seealso'>
036 *    <li class='link'>{@doc juneau-rest-server.SvlVariables}
037 * </ul>
038 */
039public class UrlVar extends SimpleVar {
040
041   private static final String SESSION_req = "req";
042
043   /** The name of this variable. */
044   public static final String NAME = "U";
045
046   /**
047    * Constructor.
048    */
049   public UrlVar() {
050      super(NAME);
051   }
052
053   @Override /* Var */
054   public String resolve(VarResolverSession session, String key) {
055      RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, true);
056      return req.getUriResolver().resolve(key);
057   }
058
059   @Override /* Var */
060   public boolean canResolve(VarResolverSession session) {
061      return session.hasSessionObject(SESSION_req);
062   }
063}