1 46 package groovy.servlet; 47 48 import groovy.lang.Binding; 49 import groovy.xml.MarkupBuilder; 50 51 import java.io.IOException ; 52 import java.util.Enumeration ; 53 import java.util.HashMap ; 54 import java.util.Map ; 55 56 import javax.servlet.ServletContext ; 57 import javax.servlet.http.HttpServletRequest ; 58 import javax.servlet.http.HttpServletResponse ; 59 60 88 public class ServletBinding extends Binding { 89 90 private final Binding binding; 91 92 private final ServletContext context; 93 94 private final HttpServletRequest request; 95 96 private final HttpServletResponse response; 97 98 private MarkupBuilder html; 99 100 103 public ServletBinding(HttpServletRequest request, HttpServletResponse response, ServletContext context) { 104 this.binding = new Binding(); 105 this.request = request; 106 this.response = response; 107 this.context = context; 108 109 112 binding.setVariable("request", request); 113 binding.setVariable("response", response); 114 binding.setVariable("context", context); 115 binding.setVariable("application", context); 116 117 121 binding.setVariable("session", request.getSession(false)); 122 123 128 Map params = new HashMap (); 129 for (Enumeration names = request.getParameterNames(); names.hasMoreElements();) { 130 String name = (String ) names.nextElement(); 131 if (!binding.getVariables().containsKey(name)) { 132 String [] values = request.getParameterValues(name); 133 if (values.length == 1) { 134 params.put(name, values[0]); 135 } else { 136 params.put(name, values); 137 } 138 } 139 } 140 binding.setVariable("params", params); 141 142 145 Map headers = new HashMap (); 146 for (Enumeration names = request.getHeaderNames(); names.hasMoreElements();) { 147 String headerName = (String ) names.nextElement(); 148 String headerValue = request.getHeader(headerName); 149 headers.put(headerName, headerValue); 150 } 151 binding.setVariable("headers", headers); 152 } 153 154 public void setVariable(String name, Object value) { 155 158 if (name == null) { 159 throw new IllegalArgumentException ("Can't bind variable to null key."); 160 } 161 if (name.length() == 0) { 162 throw new IllegalArgumentException ("Can't bind variable to blank key name. [length=0]"); 163 } 164 167 if ("out".equals(name)) { 168 throw new IllegalArgumentException ("Can't bind variable to key named '" + name + "'."); 169 } 170 if ("sout".equals(name)) { 171 throw new IllegalArgumentException ("Can't bind variable to key named '" + name + "'."); 172 } 173 if ("html".equals(name)) { 174 throw new IllegalArgumentException ("Can't bind variable to key named '" + name + "'."); 175 } 176 179 180 183 binding.setVariable(name, value); 184 } 185 186 public Map getVariables() { 187 return binding.getVariables(); 188 } 189 190 193 public Object getVariable(String name) { 194 197 if (name == null) { 198 throw new IllegalArgumentException ("Can't bind variable to null key."); 199 } 200 if (name.length() == 0) { 201 throw new IllegalArgumentException ("Can't bind variable to blank key name. [length=0]"); 202 } 203 206 try { 207 if ("out".equals(name)) { 208 return response.getWriter(); 209 } 210 if ("sout".equals(name)) { 211 return response.getOutputStream(); 212 } 213 if ("html".equals(name)) { 214 if (html == null) { 215 html = new MarkupBuilder(response.getWriter()); 216 } 217 return html; 218 } 219 } catch (IOException e) { 220 String message = "Failed to get writer or output stream from response."; 221 context.log(message, e); 222 throw new RuntimeException (message, e); 223 } 224 227 return binding.getVariable(name); 228 } 229 } 230 | Popular Tags |