1 52 53 package freemarker.ext.servlet; 54 55 import javax.servlet.http.HttpServletRequest ; 56 import javax.servlet.http.HttpServletResponse ; 57 import java.util.*; 58 import freemarker.template.*; 59 60 65 public final class HttpRequestHashModel implements TemplateHashModelEx 66 { 67 private final HttpServletRequest request; 68 private final HttpServletResponse response; 69 private final ObjectWrapper wrapper; 70 71 public HttpRequestHashModel( 72 HttpServletRequest request, ObjectWrapper wrapper) 73 { 74 this(request, null, wrapper); 75 } 76 77 public HttpRequestHashModel( 78 HttpServletRequest request, HttpServletResponse response, 79 ObjectWrapper wrapper) 80 { 81 this.request = request; 82 this.response = response; 83 this.wrapper = wrapper; 84 } 85 86 public TemplateModel get(String key) throws TemplateModelException 87 { 88 return wrapper.wrap(request.getAttribute(key)); 89 } 90 91 public boolean isEmpty() 92 { 93 return !request.getAttributeNames().hasMoreElements(); 94 } 95 96 public int size() { 97 int result = 0; 98 for (Enumeration enumeration = request.getAttributeNames(); enumeration.hasMoreElements();) { 99 enumeration.nextElement(); 100 ++result; 101 } 102 return result; 103 } 104 105 public TemplateCollectionModel keys() { 106 ArrayList keys = new ArrayList(); 107 for (Enumeration enumeration = request.getAttributeNames(); enumeration.hasMoreElements();) { 108 keys.add(enumeration.nextElement()); 109 } 110 return new SimpleCollection(keys.iterator()); 111 } 112 113 public TemplateCollectionModel values() { 114 ArrayList values = new ArrayList(); 115 for (Enumeration enumeration = request.getAttributeNames(); enumeration.hasMoreElements();) { 116 values.add(request.getAttribute((String )enumeration.nextElement())); 117 } 118 return new SimpleCollection(values.iterator(), wrapper); 119 } 120 121 public HttpServletRequest getRequest() 122 { 123 return request; 124 } 125 126 public HttpServletResponse getResponse() 127 { 128 return response; 129 } 130 131 public ObjectWrapper getObjectWrapper() 132 { 133 return wrapper; 134 } 135 } 136 | Popular Tags |