KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > servlet > multipart > RejectedPart


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.servlet.multipart;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * An upload part that was rejected because request length exceeded the maximum upload size.
24  *
25  * @version $Id: RejectedPart.java 280876 2005-09-14 15:44:29Z sylvain $
26  * @since 2.1.8
27  */

28 public class RejectedPart extends Part {
29     
30     private int size;
31     public int contentLength;
32     public int maxContentLength;
33
34     public RejectedPart(Map JavaDoc headers, int partSize, int contentLength, int maxContentLength) {
35         super(headers);
36         this.size = partSize;
37         this.contentLength = contentLength;
38         this.maxContentLength = maxContentLength;
39     }
40
41     public String JavaDoc getFileName() {
42         return (String JavaDoc) headers.get("filename");
43     }
44
45     /**
46      * Get the size of this part.
47      *
48      * @return the size in bytes
49      */

50     public int getSize() {
51         return this.size;
52     }
53     
54     /**
55      * Get the maximum allowed upload size. Not that this applies to the full request content length,
56      * including multipart boundaries and other form data values.
57      * <p>
58      * This means that an upload part can be rejected although it's individual size is (a bit) smaller
59      * than the maximum size. It is therefore advisable to use {@link #getContentLength()} to build
60      * error messages rather than {@link #getSize()}.
61      *
62      * @return the maximum content length in bytes
63      */

64     public int getMaxContentLength() {
65         return this.maxContentLength;
66     }
67
68     /**
69      * Get the content length of the request that cause this part to be rejected.
70      *
71      * @return the content length in bytes
72      */

73     public int getContentLength() {
74         return this.contentLength;
75     }
76     /**
77      * Always throw an <code>IOException</code> as this part was rejected.
78      */

79     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
80         throw new IOException JavaDoc("Multipart element '" + getFileName() + "' is too large (" +
81                 this.size + " bytes) and was discarded.");
82     }
83
84     /**
85      * Always return <code>true</code>
86      */

87     public boolean isRejected() {
88         return true;
89     }
90
91     public void dispose() {
92         // nothing
93
}
94 }
95
Popular Tags