01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.servlet.multipart;
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.util.Map;
22:
23: /**
24: * An upload part that was rejected because request length exceeded the maximum upload size.
25: *
26: * @version $Id: RejectedPart.java 433543 2006-08-22 06:22:54Z crossley $
27: * @since 2.1.8
28: */
29: public class RejectedPart extends Part {
30:
31: private int size;
32: public int contentLength;
33: public int maxContentLength;
34:
35: public RejectedPart(Map headers, int partSize, int contentLength,
36: int maxContentLength) {
37: super (headers);
38: this .size = partSize;
39: this .contentLength = contentLength;
40: this .maxContentLength = maxContentLength;
41: }
42:
43: public String getFileName() {
44: return (String) headers.get("filename");
45: }
46:
47: /**
48: * Get the size of this part.
49: *
50: * @return the size in bytes
51: */
52: public int getSize() {
53: return this .size;
54: }
55:
56: /**
57: * Get the maximum allowed upload size. Not that this applies to the full request content length,
58: * including multipart boundaries and other form data values.
59: * <p>
60: * This means that an upload part can be rejected although it's individual size is (a bit) smaller
61: * than the maximum size. It is therefore advisable to use {@link #getContentLength()} to build
62: * error messages rather than {@link #getSize()}.
63: *
64: * @return the maximum content length in bytes
65: */
66: public int getMaxContentLength() {
67: return this .maxContentLength;
68: }
69:
70: /**
71: * Get the content length of the request that cause this part to be rejected.
72: *
73: * @return the content length in bytes
74: */
75: public int getContentLength() {
76: return this .contentLength;
77: }
78:
79: /**
80: * Always throw an <code>IOException</code> as this part was rejected.
81: */
82: public InputStream getInputStream() throws IOException {
83: throw new IOException("Multipart element '" + getFileName()
84: + "' is too large (" + this .size
85: + " bytes) and was discarded.");
86: }
87:
88: /**
89: * Always return <code>true</code>
90: */
91: public boolean isRejected() {
92: return true;
93: }
94:
95: public void dispose() {
96: // nothing
97: }
98: }
|