Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ServletRequestContext |
|
| 1.1428571428571428;1.143 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * 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.commons.fileupload.servlet; | |
18 | ||
19 | import static java.lang.String.format; | |
20 | ||
21 | import java.io.IOException; | |
22 | import java.io.InputStream; | |
23 | ||
24 | import javax.servlet.http.HttpServletRequest; | |
25 | ||
26 | import org.apache.commons.fileupload.FileUploadBase; | |
27 | import org.apache.commons.fileupload.UploadContext; | |
28 | ||
29 | /** | |
30 | * <p>Provides access to the request information needed for a request made to | |
31 | * an HTTP servlet.</p> | |
32 | * | |
33 | * @since FileUpload 1.1 | |
34 | */ | |
35 | public class ServletRequestContext implements UploadContext { | |
36 | ||
37 | // ----------------------------------------------------- Instance Variables | |
38 | ||
39 | /** | |
40 | * The request for which the context is being provided. | |
41 | */ | |
42 | private final HttpServletRequest request; | |
43 | ||
44 | // ----------------------------------------------------------- Constructors | |
45 | ||
46 | /** | |
47 | * Construct a context for this request. | |
48 | * | |
49 | * @param request The request to which this context applies. | |
50 | */ | |
51 | 38 | public ServletRequestContext(HttpServletRequest request) { |
52 | 38 | this.request = request; |
53 | 38 | } |
54 | ||
55 | // --------------------------------------------------------- Public Methods | |
56 | ||
57 | /** | |
58 | * Retrieve the character encoding for the request. | |
59 | * | |
60 | * @return The character encoding for the request. | |
61 | */ | |
62 | @Override | |
63 | public String getCharacterEncoding() { | |
64 | 36 | return request.getCharacterEncoding(); |
65 | } | |
66 | ||
67 | /** | |
68 | * Retrieve the content type of the request. | |
69 | * | |
70 | * @return The content type of the request. | |
71 | */ | |
72 | @Override | |
73 | public String getContentType() { | |
74 | 38 | return request.getContentType(); |
75 | } | |
76 | ||
77 | /** | |
78 | * Retrieve the content length of the request. | |
79 | * | |
80 | * @return The content length of the request. | |
81 | * @deprecated 1.3 Use {@link #contentLength()} instead | |
82 | */ | |
83 | @Override | |
84 | @Deprecated | |
85 | public int getContentLength() { | |
86 | 37 | return request.getContentLength(); |
87 | } | |
88 | ||
89 | /** | |
90 | * Retrieve the content length of the request. | |
91 | * | |
92 | * @return The content length of the request. | |
93 | * @since 1.3 | |
94 | */ | |
95 | @Override | |
96 | public long contentLength() { | |
97 | long size; | |
98 | try { | |
99 | 37 | size = Long.parseLong(request.getHeader(FileUploadBase.CONTENT_LENGTH)); |
100 | 37 | } catch (NumberFormatException e) { |
101 | 37 | size = request.getContentLength(); |
102 | 0 | } |
103 | 37 | return size; |
104 | } | |
105 | ||
106 | /** | |
107 | * Retrieve the input stream for the request. | |
108 | * | |
109 | * @return The input stream for the request. | |
110 | * | |
111 | * @throws IOException if a problem occurs. | |
112 | */ | |
113 | @Override | |
114 | public InputStream getInputStream() throws IOException { | |
115 | 36 | return request.getInputStream(); |
116 | } | |
117 | ||
118 | /** | |
119 | * Returns a string representation of this object. | |
120 | * | |
121 | * @return a string representation of this object. | |
122 | */ | |
123 | @Override | |
124 | public String toString() { | |
125 | 0 | return format("ContentLength=%s, ContentType=%s", |
126 | 0 | Long.valueOf(this.contentLength()), |
127 | 0 | this.getContentType()); |
128 | } | |
129 | ||
130 | } |