Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f03c851

Browse files
committed
Improve the getParameter method.
1 parent 62637e1 commit f03c851

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/main/java/org/tinystruct/http/servlet/RequestBuilder.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.tinystruct.ApplicationException;
88
import org.tinystruct.data.Attachment;
99
import org.tinystruct.data.FileEntity;
10-
import org.tinystruct.data.component.Builder;
1110
import org.tinystruct.http.*;
1211
import org.tinystruct.transfer.http.upload.ContentDisposition;
1312

@@ -40,6 +39,7 @@ public class RequestBuilder extends RequestWrapper<HttpServletRequest, ServletIn
4039
private static final Logger logger = Logger.getLogger(RequestBuilder.class.getName());
4140
private String bodyCache;
4241
private List<FileEntity> list = new ArrayList<>();
42+
private String contentType;
4343

4444
public RequestBuilder(HttpServletRequest request, boolean secure) {
4545
super(request);
@@ -85,6 +85,7 @@ public RequestBuilder(HttpServletRequest request, boolean secure) {
8585
}
8686

8787
this.secure = secure;
88+
this.contentType = request.getContentType();
8889

8990
try {
9091
parseRequest();
@@ -121,10 +122,13 @@ public String query() {
121122
}
122123

123124
private boolean isJsonRequest() {
124-
String contentType = this.request.getContentType();
125125
return contentType != null && contentType.contains("application/json");
126126
}
127127

128+
private boolean isMultipartRequest() {
129+
return contentType != null && contentType.contains("multipart/form-data");
130+
}
131+
128132
/**
129133
* @return body string.
130134
*/
@@ -231,23 +235,32 @@ public Request<HttpServletRequest, ServletInputStream> setUri(String uri) {
231235

232236
@Override
233237
public String getParameter(String name) {
238+
Object value;
239+
if ((value = this.request.getParameter(name)) != null) {
240+
return value.toString();
241+
}
242+
234243
if (this.isJsonRequest()) {
235244
String queryString = this.query();
236245
if (queryString != null && queryString.contains(name + "=")) {
237246
// Find the value of the parameter
238-
String value = queryString.substring(queryString.indexOf(name + "=") + name.length() + 1);
247+
String _value = queryString.substring(queryString.indexOf(name + "=") + name.length() + 1);
239248
try {
240249
// Decode the value to handle any encoded characters (e.g., '%20' for spaces)
241-
return URLDecoder.decode(value, "UTF-8");
250+
return URLDecoder.decode(_value, "UTF-8");
242251
} catch (UnsupportedEncodingException e) {
243252
// Log the error and return the raw value if decoding fails
244253
logger.log(Level.WARNING, "Failed to decode query parameter: " + name, e);
245-
return value;
254+
return _value;
246255
}
247256
}
248257
return null;
258+
} else if (isMultipartRequest()) {
259+
if ((value = request.getAttribute(name)) != null)
260+
return value.toString();
249261
}
250-
return this.request.getParameter(name);
262+
263+
return null;
251264
}
252265

253266
/**
@@ -271,7 +284,6 @@ public String getCharacterEncoding() {
271284

272285
private void parseRequest() throws ApplicationException {
273286
// Check if the content type is multipart/form-data
274-
String contentType = this.request.getContentType();
275287
if (contentType == null || !contentType.toLowerCase().startsWith("multipart/form-data")) {
276288
return; // Not a multipart form, nothing to parse
277289
}

0 commit comments

Comments
 (0)