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 * <ul class='seealso'> 038 * <li class='link'>{@doc juneau-rest-server.SvlVariables} 039 * </ul> 040 */ 041public class SerializedRequestAttrVar extends StreamedVar { 042 043 private static final String SESSION_req = "req"; 044 045 /** The name of this variable. */ 046 public static final String NAME = "SA"; 047 048 /** 049 * Constructor. 050 */ 051 public SerializedRequestAttrVar() { 052 super(NAME); 053 } 054 055 @Override /* Var */ 056 public void resolveTo(VarResolverSession session, Writer w, String key) throws Exception { 057 int i = key.indexOf(','); 058 if (i == -1) 059 throw new RuntimeException("Invalid format for $SA var. Must be of the format $SA{contentType,key[,defaultValue]}"); 060 String[] s2 = split(key); 061 RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, true); 062 if (req != null) { 063 Object o = req.getAttribute(key); 064 if (o == null) 065 o = key; 066 Serializer s = req.getSerializers().getSerializer(s2[0]); 067 if (s != null) 068 s.serialize(w, o); 069 } 070 } 071 072 @Override /* Var */ 073 protected boolean allowNested() { 074 return false; 075 } 076 077 @Override /* Var */ 078 protected boolean allowRecurse() { 079 return false; 080 } 081 082 @Override /* Var */ 083 public boolean canResolve(VarResolverSession session) { 084 return session.hasSessionObject(SESSION_req); 085 } 086}