001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.protocol.http.servlet;
018
019import java.util.List;
020import java.util.Map;
021
022import javax.servlet.http.HttpServletRequest;
023
024import org.apache.commons.fileupload.FileItem;
025import org.apache.commons.fileupload.FileUploadException;
026import org.apache.wicket.protocol.http.IMultipartWebRequest;
027import org.apache.wicket.request.IRequestParameters;
028import org.apache.wicket.request.Url;
029import org.apache.wicket.util.lang.Args;
030import org.apache.wicket.util.lang.Bytes;
031
032/**
033 * Servlet specific WebRequest subclass for multipart content uploads.
034 *
035 * @author Matej Knopp
036 */
037public abstract class MultipartServletWebRequest extends ServletWebRequest
038        implements
039                IMultipartWebRequest
040{
041        /**
042         *  Maximum size of all uploaded files in bytes in a request.
043         */
044        private Bytes maxSize;
045
046        /**
047         *  Maximum size of file of upload in bytes (if there are more than one) in a request.
048         */
049        private Bytes fileMaxSize;
050
051        /**
052         * Maximum amount of files in request.
053         * A value of -1 indicates no maximum.
054         */
055        private long fileCountMax = -1L;
056
057        /**
058         * Construct.
059         *
060         * @param httpServletRequest
061         * @param filterPrefix
062         */
063        public MultipartServletWebRequest(HttpServletRequest httpServletRequest, String filterPrefix)
064        {
065                super(httpServletRequest, filterPrefix);
066        }
067
068        /**
069         * Construct.
070         *
071         * @param httpServletRequest
072         * @param filterPrefix
073         * @param url
074         */
075        public MultipartServletWebRequest(HttpServletRequest httpServletRequest, String filterPrefix,
076                Url url)
077        {
078                super(httpServletRequest, filterPrefix, url);
079        }
080
081        /**
082         * Parses the multipart body of the request.
083         *
084         * @throws FileUploadException
085         * @since 6.18.0
086         */
087        public abstract void parseFileParts() throws FileUploadException;
088
089        @Override
090        public ServletWebRequest cloneWithUrl(Url url)
091        {
092                return new MultipartServletWebRequest(getContainerRequest(), getFilterPrefix(), url)
093                {
094                        @Override
095                        public void parseFileParts() throws FileUploadException
096                        {
097                                MultipartServletWebRequest.this.parseFileParts();
098                        }
099
100                        @Override
101                        public List<FileItem> getFile(String fieldName)
102                        {
103                                return MultipartServletWebRequest.this.getFile(fieldName);
104                        }
105
106                        @Override
107                        public Map<String, List<FileItem>> getFiles()
108                        {
109                                return MultipartServletWebRequest.this.getFiles();
110                        }
111
112                        @Override
113                        public IRequestParameters getPostParameters()
114                        {
115                                return MultipartServletWebRequest.this.getPostParameters();
116                        }
117                };
118        }
119
120        public Bytes getMaxSize()
121        {
122                return maxSize;
123        }
124
125        public void setMaxSize(Bytes maxSize)
126        {
127                Args.notNull(maxSize, "maxSize");
128                this.maxSize = maxSize;
129        }
130
131        public Bytes getFileMaxSize()
132        {
133                return fileMaxSize;
134        }
135
136        public void setFileMaxSize(Bytes fileMaxSize)
137        {
138                this.fileMaxSize = fileMaxSize;
139        }
140
141        public long getFileCountMax()
142        {
143                return fileCountMax;
144        }
145
146        public void setFileCountMax(long fileCountMax)
147        {
148                this.fileCountMax = fileCountMax;
149        }
150}