1 2 20 21 package za.org.coefficient.core; 22 23 import org.apache.commons.fileupload.FileUploadException; 24 import org.apache.commons.httpclient.Cookie; 25 26 import za.org.coefficient.util.common.MultipartRequest; 27 28 import java.io.IOException ; 29 import java.io.Serializable ; 30 31 import java.util.Enumeration ; 32 import java.util.Iterator ; 33 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 48 public class CoefficientWebContext extends BaseCoefficientContext implements Serializable { 49 50 52 public CoefficientWebContext(HttpServletRequest request, 53 HttpServletResponse response) throws IOException { 54 55 this.requestURL = request.getRequestURL().toString(); 57 58 javax.servlet.http.Cookie [] cookies = request.getCookies(); 60 if(cookies == null) { 61 cookies = new javax.servlet.http.Cookie [0]; 62 } 63 serializableExistingCookies = new Cookie[cookies.length]; 64 for(int i = 0; i < cookies.length; i++) { 65 Cookie serializableCookie = new Cookie(cookies[i].getDomain(), 66 cookies[i].getName(), 67 cookies[i].getValue(), 68 cookies[i].getPath(), 69 cookies[i].getMaxAge(), 70 cookies[i].getSecure()); 71 serializableExistingCookies[i] = serializableCookie; 72 } 73 74 for(Enumeration enumer = request.getParameterNames(); 76 enumer.hasMoreElements(); ) { 77 String paramName = (String )enumer.nextElement(); 78 String [] values = request.getParameterValues(paramName); 79 if(values.length == 1) { 80 requestData.put(paramName, values[0]); 81 } else { 82 requestData.put(paramName, values); 83 } 84 } 85 86 for(Enumeration enumer = request.getAttributeNames(); 88 enumer.hasMoreElements(); ) { 89 String attrName = (String )enumer.nextElement(); 90 Object value = request.getAttribute(attrName); 91 if(value instanceof Serializable ) { 93 requestAttributes.put(attrName, value); 94 } 95 } 96 97 for(Enumeration enumer = request.getSession().getAttributeNames(); 99 enumer.hasMoreElements(); ) { 100 String attrName = (String )enumer.nextElement(); 101 Object value = request.getSession().getAttribute(attrName); 102 if(value instanceof Serializable ) { 104 session.put(attrName, value); 105 } 106 } 107 108 try { 109 multipartRequest = new MultipartRequest(request, 110 Integer.parseInt(Constants.MAX_FILE_UPLOAD_SIZE_BYTES)); 111 requestData.putAll(multipartRequest.getParameterMap()); 112 } catch (NumberFormatException e) { 113 setError("MAX_FILE_UPLOAD_SIZE_BYTES is not an integer!"); 114 } catch(FileUploadException fux) { 115 multipartRequest = null; 116 } 118 } 119 120 121 123 public void synchronize(HttpServletRequest request, 124 HttpServletResponse response) throws IOException { 125 126 if(invalidateSession) { 128 request.getSession().invalidate(); 129 } 130 131 for(Iterator it = session.keySet().iterator(); it.hasNext(); ) { 133 String sessKey = (String )it.next(); 134 Object value = session.get(sessKey); 135 request.getSession().setAttribute(sessKey, value); 136 } 137 138 for(Iterator it = removedSessionKeys.iterator(); it.hasNext(); ) { 141 String key = (String )it.next(); 142 request.getSession().removeAttribute(key); 143 } 144 145 for(Iterator it = serializableNewCookies.iterator(); it.hasNext(); ) { 147 Cookie cookie = (Cookie)it.next(); 148 javax.servlet.http.Cookie cookieToSet = 149 new javax.servlet.http.Cookie (cookie.getName(), cookie.getValue()); 150 long expire = cookie.getExpiryDate().getTime() 151 - System.currentTimeMillis(); 152 if(expire < 0) { 153 expire = 0; 154 } 155 cookieToSet.setMaxAge((int)expire); 156 response.addCookie(cookieToSet); 157 } 158 159 if(redirectionURL != null) { 161 response.sendRedirect(redirectionURL); 162 } 163 } 164 165 } 166 | Popular Tags |