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.server.vars;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020import static org.apache.juneau.commons.utils.StringUtils.*;
021
022import java.io.*;
023
024import org.apache.juneau.commons.svl.*;
025import org.apache.juneau.http.response.*;
026import org.apache.juneau.marshall.serializer.*;
027import org.apache.juneau.rest.server.*;
028
029/**
030 * Serialized request attribute variable resolver.
031 *
032 * <p>
033 * The format for this var is <js>"${SA:contentType,key[,defaultValue]}"</js>.
034 * The comma separates the content type, the attribute name and the optional default (which is written when the attribute is not set).
035 * This var is not a multipart var, so the body reaches it whole: the commas are part of its own syntax and are not subject
036 * to the one-comma-per-key restriction of defaulting and multipart vars.
037 *
038 * <p>
039 * This variable resolver requires that a {@link RestRequest} bean be available in the session bean store.
040 *
041 * <p>
042 * Since this is a {@link SimpleVar}, any variables contained in the result will be recursively resolved.
043 * Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var.
044 *
045 * <h2 class='section'>See Also:</h2><ul>
046 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerSvlVariables">REST Server SVL Variables</a>
047 * </ul>
048 */
049public class SerializedRequestAttrVar extends StreamedVar {
050
051   /** The name of this variable. */
052   public static final String NAME = "SA";
053
054   /**
055    * Constructor.
056    */
057   public SerializedRequestAttrVar() {
058      super(NAME);
059   }
060
061   @Override /* Overridden from Var */
062   public boolean canResolve(VarResolverSession session) {
063      return session.getBean(RestRequest.class).isPresent();
064   }
065
066   @Override /* Overridden from Var */
067   public void resolveTo(VarResolverSession session, Writer w, String key) throws Exception {
068      var i = key.indexOf(',');
069      if (i == -1)
070         throw iaex("Invalid format for $SA var. Must be of the format ${SA:contentType,key[,defaultValue]}");
071      var s2 = splita(key, ',', 3);
072      var req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new);
073      var dflt = s2.length > 2 ? s2[2] : null;
074      var o = req.getAttribute(s2[1]).orElse(dflt);
075      Serializer s = req.getOpContext().getSerializers().getSerializer(s2[0]).orElse(null);
076      if (nn(s) && nn(o))
077         s.write(o, w);
078   }
079
080   @Override /* Overridden from Var */
081   protected boolean allowNested() {
082      return false;
083   }
084
085   @Override /* Overridden from Var */
086   protected boolean allowRecurse() {
087      return false;
088   }
089}