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