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 static org.apache.juneau.internal.StringUtils.*;
016
017import java.io.*;
018
019import org.apache.juneau.rest.*;
020import org.apache.juneau.serializer.*;
021import org.apache.juneau.svl.*;
022
023/**
024 * Serialized request attribute variable resolver.
025 *
026 * <p>
027 * The format for this var is <js>"$SA{contentType,key[,defaultValue]}"</js>.
028 *
029 * <p>
030 * This variable resolver requires that a {@link RestRequest} object be set as a context object on the resolver or a
031 * session object on the resolver session.
032 *
033 * <p>
034 * Since this is a {@link SimpleVar}, any variables contained in the result will be recursively resolved.
035 * Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var.
036 *
037 * <h5 class='section'>See Also:</h5>
038 * <ul>
039 *    <li class='link'>{@doc juneau-rest-server.SvlVariables}
040 * </ul>
041 */
042public class SerializedRequestAttrVar extends StreamedVar {
043
044   /** The name of this variable. */
045   public static final String NAME = "SA";
046
047   /**
048    * Constructor.
049    */
050   public SerializedRequestAttrVar() {
051      super(NAME);
052   }
053
054   @Override /* Parameter */
055   public void resolveTo(VarResolverSession session, Writer w, String key) throws Exception {
056      int i = key.indexOf(',');
057      if (i == -1)
058         throw new RuntimeException("Invalid format for $SA var.  Must be of the format $SA{contentType,key[,defaultValue]}");
059      String[] s2 = split(key);
060      RestRequest req = session.getSessionObject(RestRequest.class, RequestVar.SESSION_req, true);
061      if (req != null) {
062         Object o = req.getAttribute(key);
063         if (o == null)
064            o = key;
065         Serializer s = req.getSerializers().getSerializer(s2[0]);
066         if (s != null)
067            s.serialize(w, o);
068      }
069   }
070
071   @Override  /* Var */
072   protected boolean allowNested() {
073      return false;
074   }
075
076   @Override  /* Var */
077   protected boolean allowRecurse() {
078      return false;
079   }
080}