1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/* ------------------------------------ JavaOnTracks Thibaut Colar tcolar-jot AT colar DOT net Artistic Licence 2.0 http://www.javaontracks.net ------------------------------------ */ package net.jot.web; import java.io.File; import java.io.UnsupportedEncodingException; import java.util.Hashtable; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import net.jot.logger.JOTLogger; import net.jot.web.multipart.JOTMultiPartItem; import net.jot.web.multipart.JOTMultiPartParser; /** * This is a custom extension of the standard HttpServletRequestWrapper with added functionality. * In particular it supports a setParameter() method that allows to overide/manipulates parameter values. * It also provides basic for multipartRequest support (ie: file uploads from forms) * @author thibautc * */ public class JOTFlowRequest extends HttpServletRequestWrapper { Hashtable customParams = new Hashtable(); // possible data about uploaded files from multipart request Hashtable files = new Hashtable(); File tempMultiPartFile = null; boolean isMultiPartParsed = false; public JOTFlowRequest(HttpServletRequest request) { super(request); try { setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { JOTLogger.logException(this, "Encoding exception", e); } } public Hashtable getCustomParams() { return customParams; } public void setCustomParams(Hashtable customParams) { this.customParams = customParams; } /** * SetParameter will save the new parameter value (valid through the java request.) * @param name * @param value */ public void setParameter(String name, String value) { customParams.put(name, value); } /** * Overload of getParameter, to take into account our custom setRequest feature */ public String getParameter(String name) { String param = super.getParameter(name); if (customParams.containsKey(name)) { // If there is a custom value, return that one. param = (String) customParams.get(name); } return param; } /** * Call this if you want to parse the request as a multipart content. * ie: data is coming from a form with enctype="multipart/form-data" * Usually this is a file upload form. * By default it is not done automatically for performance/security reasons. * So you will want to call this method manually to parse the multipart form. * This will: * - add the multipart variables as request parameters, so you can read them with getParameter() * - uplload the files data to a temporary folder, so you can get/save the files getFileItem().xxx() * * @tempDataFolder: is the folder the temp data will be stored temporarely (raw multipart data) * @maxSize: maximum data size in bytes to accept. */ public void parseMultiPartContent(String tempDataFolder, long maxSize) throws Exception { // this should only be done once. if (!isMultiPartParsed) { isMultiPartParsed = true; try { tempMultiPartFile = File.createTempFile("mpart", null, new File(tempDataFolder)); tempMultiPartFile.deleteOnExit(); JOTMultiPartParser.parse(this, tempMultiPartFile, maxSize); } catch (Exception e) { getInputStream().close(); tempMultiPartFile.delete(); throw e; } } } /** * When the request is completed/terminated tries to delete the temporary multipart data (uploaded files) * @throws java.lang.Throwable */ public void finalize() throws Throwable { // removes the temp data when request ends. if (tempMultiPartFile != null && tempMultiPartFile.exists()) { JOTLogger.log(JOTLogger.DEBUG_LEVEL, this, "Removing temporary multipart data: " + tempMultiPartFile.getAbsolutePath()); tempMultiPartFile.delete(); } super.finalize(); } /** * Called internally by JOTMultiPartParser, if a file was uploaded from the form. * @param item */ public void addFile(JOTMultiPartItem item) { JOTLogger.log(JOTLogger.DEBUG_LEVEL, this, "Adding a file to the request: " + item.getName()); files.put(item.getName(), item); } /** * Return a file handle(JOTMultiPartItem) from the request(multipart request) for an uploaded file, which you can then save somewhere * NOTE: parseMultiPartContent() MUST have been called first otherwise it will return nothing(null). * @param name : the name of the file (HTML input name) * @return : Handle to the uplaoded file. * see javadoc of net.jot.web.multipart.JOTMultiPartParser for an example */ public JOTMultiPartItem getFile(String name) { return (JOTMultiPartItem) files.get(name); } }