1 16 17 package org.apache.velocity.tools.view.context; 18 19 import java.util.HashMap ; 20 21 import org.apache.velocity.VelocityContext; 22 import org.apache.velocity.context.Context; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 import javax.servlet.ServletContext ; 28 29 30 66 public class ChainedContext extends VelocityContext implements ViewContext 67 { 68 69 72 private HttpServletRequest request; 73 74 77 private HttpServletResponse response; 78 79 82 private HttpSession session; 83 84 87 private ServletContext application; 88 89 92 private ToolboxContext toolboxContext = null; 93 94 95 98 public ChainedContext(Context ctx, 99 HttpServletRequest request, 100 HttpServletResponse response, 101 ServletContext application) 102 { 103 super(null, ctx ); 104 105 this.request = request; 106 this.response = response; 107 this.session = request.getSession(false); 108 this.application = application; 109 } 110 111 112 117 public void setToolbox(ToolboxContext box) 118 { 119 toolboxContext = box; 120 session = request.getSession(false); 124 } 125 126 127 136 public Object internalGet( String key ) 137 { 138 Object o = null; 139 140 if (toolboxContext != null) 142 { 143 o = toolboxContext.get(key); 144 if (o != null) 145 { 146 return o; 147 } 148 } 149 150 if ( key.equals( REQUEST )) 152 { 153 return request; 154 } 155 else if( key.equals(RESPONSE) ) 156 { 157 return response; 158 } 159 else if ( key.equals(SESSION) ) 160 { 161 return session; 162 } 163 else if ( key.equals(APPLICATION)) 164 { 165 return application; 166 } 167 168 o = super.internalGet(key); 170 if (o != null) 171 { 172 return o; 173 } 174 175 return getAttribute(key); 177 } 178 179 180 187 public Object getAttribute(String key) 188 { 189 Object o = request.getAttribute(key); 190 if (o == null) 191 { 192 if (session != null) 193 { 194 o = session.getAttribute(key); 195 } 196 197 if (o == null) 198 { 199 o = application.getAttribute(key); 200 } 201 } 202 return o; 203 } 204 205 206 209 public HttpServletRequest getRequest() 210 { 211 return request; 212 } 213 214 215 218 public HttpServletResponse getResponse() 219 { 220 return response; 221 } 222 223 224 227 public ServletContext getServletContext() 228 { 229 return application; 230 } 231 232 233 236 public Context getVelocityContext() 237 { 238 return this; 239 } 240 241 242 } | Popular Tags |