1 /* 2 * $Id$ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 package org.apache.struts.upload; 22 23 import java.util.HashMap; 24 25 import org.apache.struts.action.ActionMapping; 26 import org.apache.struts.action.ActionServlet; 27 28 import jakarta.servlet.ServletException; 29 import jakarta.servlet.http.HttpServletRequest; 30 31 /** 32 * MultipartRequestHandler provides an standard interface for struts to deal 33 * with file uploads from forms with enctypes of "multipart/form-data". 34 * Providers must provide a no-argument constructor for initialization. 35 */ 36 public interface MultipartRequestHandler { 37 38 /** 39 * This is the ServletRequest attribute that should be set when a multipart 40 * request is being read and the maximum byte-length or file-count is 41 * exceeded. The value is a Boolean. If the maximum byte-length or file- 42 * count isn't exceeded, this attribute shouldn't be put in the 43 * ServletRequest. It's the job of the implementation to put this attribute 44 * in the request if the maximum byte-length or file-count is exceeded; in 45 * the handleRequest(HttpServletRequest) method. 46 */ 47 public static final String ATTRIBUTE_MAX_LENGTH_EXCEEDED = 48 "org.apache.struts.upload.MaxLengthExceeded"; 49 50 /** 51 * This is the ServletRequest attribute that should be set when a multipart 52 * request is being read and the maximum byte-length is exceeded. The value 53 * is a Boolean. If the maximum length isn't exceeded, this attribute 54 * shouldn't be put in the ServletRequest. It's the job of the 55 * implementation to put this attribute in the request if the maximum byte- 56 * length is exceeded; in the handleRequest(HttpServletRequest) method. 57 */ 58 public static final String ATTRIBUTE_MAX_BYTE_LENGTH_EXCEEDED = 59 "org.apache.struts.upload.MaxByteLengthExceeded"; 60 61 /** 62 * This is the ServletRequest attribute that should be set when a multipart 63 * request is being read and the maximum byte-length of a string parameter 64 * is exceeded. The value is a Boolean. If the maximum length isn't 65 * exceeded, this attribute shouldn't be put in the ServletRequest. It's 66 * the job of the implementation to put this attribute in the request if 67 * the maximum byte-length is exceeded; in the 68 * handleRequest(HttpServletRequest) method. 69 */ 70 public static final String ATTRIBUTE_MAX_STRING_LENGTH_EXCEEDED = 71 "org.apache.struts.upload.MaxStringLengthExceeded"; 72 73 /** 74 * This is the ServletRequest attribute that should be set when a multipart 75 * request is being read and the maximum file-count is exceeded. The value 76 * is a Boolean. If the maximum file-count isn't exceeded, this attribute 77 * shouldn't be put in the ServletRequest. It's the job of the 78 * implementation to put this attribute in the request if the maximum file- 79 * count is exceeded; in the handleRequest(HttpServletRequest) method. 80 */ 81 public static final String ATTRIBUTE_MAX_FILE_COUNT_EXCEEDED = 82 "org.apache.struts.upload.MaxFileCountExceeded"; 83 84 /** 85 * Convenience method to set a reference to a working ActionServlet 86 * instance. 87 */ 88 public void setServlet(ActionServlet servlet); 89 90 /** 91 * Convenience method to set a reference to a working ActionMapping 92 * instance. 93 */ 94 public void setMapping(ActionMapping mapping); 95 96 /** 97 * Get the ActionServlet instance. 98 */ 99 public ActionServlet getServlet(); 100 101 /** 102 * Get the ActionMapping instance for this request. 103 */ 104 public ActionMapping getMapping(); 105 106 /** 107 * After constructed, this is the first method called on by ActionServlet. 108 * Use this method for all your data-parsing of the ServletInputStream in 109 * the request. 110 * 111 * @throws ServletException thrown if something goes wrong 112 */ 113 public void handleRequest(HttpServletRequest request) 114 throws ServletException; 115 116 /** 117 * This method is called on to retrieve all the text input elements of the 118 * request. 119 * 120 * @return A HashMap where the keys and values are the names and values of 121 * the request input parameters 122 */ 123 public HashMap<String, String[]> getTextElements(); 124 125 /** 126 * This method is called on to retrieve all the FormFile input elements of 127 * the request. 128 * 129 * @return A HashMap where the keys are the input names of the files and 130 * the values are FormFile objects. 131 * 132 * @see FormFile 133 */ 134 public HashMap<String, FormFile[]> getFileElements(); 135 136 /** 137 * This method returns all elements of a multipart request. 138 * 139 * @return A HashMap where the keys are input names and values are either 140 * String arrays or FormFiles. 141 */ 142 public HashMap<String, Object> getAllElements(); 143 144 /** 145 * This method is called on when there's some sort of problem and the form 146 * post needs to be rolled back. Providers should remove any FormFiles used 147 * to hold information by setting them to null and also physically delete 148 * them if the implementation calls for writing directly to disk. 149 * 150 * <p>NOTE: Currently implemented but not automatically supported, 151 * ActionForm implementors must call rollback() manually for rolling back 152 * file uploads.</p> 153 */ 154 public void rollback(); 155 156 /** 157 * This method is called on when a successful form post has been made. Some 158 * implementations will use this to destroy temporary files or write to a 159 * database or something of that nature. 160 */ 161 public void finish(); 162 }