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.common.internal.StringUtils.*;
016
017import java.io.*;
018
019import org.apache.juneau.http.response.*;
020import org.apache.juneau.rest.*;
021import org.apache.juneau.serializer.*;
022import org.apache.juneau.svl.*;
023
024/**
025 * Serialized request attribute variable resolver.
026 *
027 * <p>
028 * The format for this var is <js>"$SA{contentType,key[,defaultValue]}"</js>.
029 *
030 * <p>
031 * This variable resolver requires that a {@link RestRequest} bean be available in the session bean store.
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><ul>
038 *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.SvlVariables">SVL Variables</a>
039 * </ul>
040 */
041public class SerializedRequestAttrVar extends StreamedVar {
042
043   /** The name of this variable. */
044   public static final String NAME = "SA";
045
046   /**
047    * Constructor.
048    */
049   public SerializedRequestAttrVar() {
050      super(NAME);
051   }
052
053   @Override /* Var */
054   public void resolveTo(VarResolverSession session, Writer w, String key) throws Exception {
055      int i = key.indexOf(',');
056      if (i == -1)
057         throw new RuntimeException("Invalid format for $SA var.  Must be of the format $SA{contentType,key[,defaultValue]}");
058      String[] s2 = split(key);
059      RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new);
060      Object o = req.getAttribute(key).orElse(key);
061      Serializer s = req.getOpContext().getSerializers().getSerializer(s2[0]);
062      if (s != null)
063         s.serialize(w, o);
064   }
065
066   @Override  /* Var */
067   protected boolean allowNested() {
068      return false;
069   }
070
071   @Override  /* Var */
072   protected boolean allowRecurse() {
073      return false;
074   }
075
076   @Override /* Var */
077   public boolean canResolve(VarResolverSession session) {
078      return session.getBean(RestRequest.class).isPresent();
079   }
080}