1 package org.kohsuke.stapler; 2 3 import org.apache.commons.beanutils.BeanUtils; 4 import org.apache.commons.beanutils.PropertyUtils; 5 import org.apache.commons.beanutils.Converter; 6 import org.apache.commons.beanutils.ConvertUtils; 7 import org.kohsuke.stapler.jelly.JellyClassTearOff; 8 9 import javax.servlet.RequestDispatcher ; 10 import javax.servlet.ServletContext ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletRequestWrapper ; 13 import javax.servlet.http.HttpServletResponse ; 14 import java.io.IOException ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.lang.reflect.Field ; 17 import java.text.ParseException ; 18 import java.text.SimpleDateFormat ; 19 import java.util.ArrayList ; 20 import java.util.Calendar ; 21 import java.util.Date ; 22 import java.util.Enumeration ; 23 import java.util.List ; 24 import java.util.StringTokenizer ; 25 import java.beans.PropertyDescriptor ; 26 27 30 class RequestImpl extends HttpServletRequestWrapper implements StaplerRequest { 31 35 public final TokenList tokens; 36 40 public final List <AncestorImpl> ancestors; 41 42 private final Stapler stapler; 43 44 private String rest; 46 47 private final String originalRequestURI; 48 49 public RequestImpl(Stapler stapler, HttpServletRequest request, List <AncestorImpl> ancestors, TokenList tokens) { 50 super(request); 51 this.stapler = stapler; 52 this.ancestors = ancestors; 53 this.tokens = tokens; 54 this.originalRequestURI = request.getRequestURI(); 55 } 56 57 public Stapler getStapler() { 58 return stapler; 59 } 60 61 public String getRestOfPath() { 62 if(rest==null) 63 rest = assembleRestOfPath(tokens); 64 return rest; 65 } 66 67 public ServletContext getServletContext() { 68 return stapler.getServletContext(); 69 } 70 71 private static String assembleRestOfPath(TokenList tokens) { 72 StringBuffer buf = new StringBuffer (); 73 for( int i=tokens.idx; i<tokens.length(); i++ ) { 74 buf.append('/'); 75 buf.append(tokens.get(i)); 76 } 77 return buf.toString(); 78 } 79 80 public RequestDispatcher getView(Object it,String viewName) throws IOException { 81 RequestDispatcher rd = stapler.getResourceDispatcher(it, viewName); 83 if(rd!=null) return rd; 84 85 try { 87 rd = MetaClass.get(it.getClass()).loadTearOff(JellyClassTearOff.class).createDispatcher(it,viewName); 88 if(rd!=null) 89 return rd; 90 } catch (LinkageError e) { 91 } 93 94 return null; 95 } 96 97 public String getRootPath() { 98 StringBuffer buf = super.getRequestURL(); 99 int idx = 0; 100 for( int i=0; i<3; i++ ) 101 idx = buf.substring(idx).indexOf("/")+1; 102 buf.setLength(idx-1); 103 buf.append(super.getContextPath()); 104 return buf.toString(); 105 } 106 107 public List getAncestors() { 108 return ancestors; 109 } 110 111 public String getOriginalRequestURI() { 112 return originalRequestURI; 113 } 114 115 public boolean checkIfModified(long lastModified, StaplerResponse rsp) { 116 if(lastModified<=0) 117 return false; 118 119 String since = getHeader("If-Modified-Since"); 121 SimpleDateFormat format = Stapler.HTTP_DATE_FORMAT.get(); 122 if(since!=null) { 123 try { 124 long ims = format.parse(since).getTime(); 125 if(lastModified<ims+1000) { 126 rsp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 128 return true; 129 } 130 } catch (NumberFormatException e) { 131 } catch (ParseException e) { 133 } 135 } 136 rsp.setHeader("Last-Modified",format.format(new Date (lastModified))); 137 return false; 138 } 139 140 public boolean checkIfModified(Date timestampOfResource, StaplerResponse rsp) { 141 return checkIfModified(timestampOfResource.getTime(),rsp); 142 } 143 144 public boolean checkIfModified(Calendar timestampOfResource, StaplerResponse rsp) { 145 return checkIfModified(timestampOfResource.getTimeInMillis(),rsp); 146 } 147 148 public void bindParameters(Object bean) { 149 bindParameters(bean,""); 150 } 151 152 public void bindParameters(Object bean, String prefix) { 153 Enumeration e = getParameterNames(); 154 while(e.hasMoreElements()) { 155 String name = (String )e.nextElement(); 156 if(name.startsWith(prefix)) 157 fill(bean,name.substring(prefix.length()), getParameter(name) ); 158 } 159 } 160 161 public <T> 162 List <T> bindParametersToList(Class <T> type, String prefix) { 163 List <T> r = new ArrayList <T>(); 164 165 int len = Integer.MAX_VALUE; 166 167 Enumeration e = getParameterNames(); 168 while(e.hasMoreElements()) { 169 String name = (String )e.nextElement(); 170 if(name.startsWith(prefix)) 171 len = Math.min(len,getParameterValues(name).length); 172 } 173 174 try { 175 for( int i=0; i<len; i++ ) { 176 T t = type.newInstance(); 177 r.add(t); 178 179 e = getParameterNames(); 180 while(e.hasMoreElements()) { 181 String name = (String )e.nextElement(); 182 if(name.startsWith(prefix)) 183 fill(t, name.substring(prefix.length()), getParameterValues(name)[i] ); 184 } 185 } 186 } catch (InstantiationException x) { 187 throw new InstantiationError (x.getMessage()); 188 } catch (IllegalAccessException x) { 189 throw new IllegalAccessError (x.getMessage()); 190 } 191 192 return r; 193 } 194 195 private static void fill(Object bean, String key, String value) { 196 StringTokenizer tokens = new StringTokenizer (key); 197 while(tokens.hasMoreTokens()) { 198 String token = tokens.nextToken(); 199 boolean last = !tokens.hasMoreTokens(); 201 try { 202 if(last) { 203 copyProperty(bean,token,value); 204 } else { 205 bean = BeanUtils.getProperty(bean,token); 206 } 207 } catch (IllegalAccessException x) { 208 throw new IllegalAccessError (x.getMessage()); 209 } catch (InvocationTargetException x) { 210 Throwable e = x.getTargetException(); 211 if(e instanceof RuntimeException ) 212 throw (RuntimeException )e; 213 if(e instanceof Error ) 214 throw (Error )e; 215 throw new RuntimeException (x); 216 } catch (NoSuchMethodException e) { 217 } 219 } 220 } 221 222 private static void copyProperty(Object bean, String name, Object value) throws IllegalAccessException , InvocationTargetException { 223 PropertyDescriptor propDescriptor; 224 try { 225 propDescriptor = 226 PropertyUtils.getPropertyDescriptor(bean, name); 227 } catch (NoSuchMethodException e) { 228 propDescriptor = null; 229 } 230 if ((propDescriptor != null) && 231 (propDescriptor.getWriteMethod() == null)) { 232 propDescriptor = null; 233 } 234 if (propDescriptor != null) { 235 Converter converter = ConvertUtils.lookup(propDescriptor.getPropertyType()); 236 if (converter != null) 237 value = converter.convert(propDescriptor.getPropertyType(), value); 238 try { 239 PropertyUtils.setSimpleProperty(bean, name, value); 240 } catch (NoSuchMethodException e) { 241 throw new NoSuchMethodError (e.getMessage()); 242 } 243 return; 244 } 245 246 try { 248 Field field = bean.getClass().getField(name); 249 Converter converter = ConvertUtils.lookup(field.getType()); 250 if (converter != null) 251 value = converter.convert(field.getType(), value); 252 field.set(bean,value); 253 } catch (NoSuchFieldException e) { 254 } 256 } 257 } 258 | Popular Tags |