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 019import static org.apache.juneau.common.utils.Utils.*; 020 021import java.io.*; 022 023import org.apache.juneau.http.response.*; 024import org.apache.juneau.rest.*; 025import org.apache.juneau.serializer.*; 026import org.apache.juneau.svl.*; 027 028/** 029 * Serialized request attribute variable resolver. 030 * 031 * <p> 032 * The format for this var is <js>"$SA{contentType,key[,defaultValue]}"</js>. 033 * 034 * <p> 035 * This variable resolver requires that a {@link RestRequest} bean be available in the session bean store. 036 * 037 * <p> 038 * Since this is a {@link SimpleVar}, any variables contained in the result will be recursively resolved. 039 * Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var. 040 * 041 * <h5 class='section'>See Also:</h5><ul> 042 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerSvlVariables">SVL Variables</a> 043 * </ul> 044 */ 045public class SerializedRequestAttrVar extends StreamedVar { 046 047 /** The name of this variable. */ 048 public static final String NAME = "SA"; 049 050 /** 051 * Constructor. 052 */ 053 public SerializedRequestAttrVar() { 054 super(NAME); 055 } 056 057 @Override /* Var */ 058 public void resolveTo(VarResolverSession session, Writer w, String key) throws Exception { 059 int i = key.indexOf(','); 060 if (i == -1) 061 throw new IllegalArgumentException("Invalid format for $SA var. Must be of the format $SA{contentType,key[,defaultValue]}"); 062 String[] s2 = splita(key); 063 RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new); 064 Object o = req.getAttribute(key).orElse(key); 065 Serializer s = req.getOpContext().getSerializers().getSerializer(s2[0]); 066 if (s != null) 067 s.serialize(w, o); 068 } 069 070 @Override /* Var */ 071 protected boolean allowNested() { 072 return false; 073 } 074 075 @Override /* Var */ 076 protected boolean allowRecurse() { 077 return false; 078 } 079 080 @Override /* Var */ 081 public boolean canResolve(VarResolverSession session) { 082 return session.getBean(RestRequest.class).isPresent(); 083 } 084}