7
7
import org .tinystruct .ApplicationException ;
8
8
import org .tinystruct .data .Attachment ;
9
9
import org .tinystruct .data .FileEntity ;
10
- import org .tinystruct .data .component .Builder ;
11
10
import org .tinystruct .http .*;
12
11
import org .tinystruct .transfer .http .upload .ContentDisposition ;
13
12
@@ -40,6 +39,7 @@ public class RequestBuilder extends RequestWrapper<HttpServletRequest, ServletIn
40
39
private static final Logger logger = Logger .getLogger (RequestBuilder .class .getName ());
41
40
private String bodyCache ;
42
41
private List <FileEntity > list = new ArrayList <>();
42
+ private String contentType ;
43
43
44
44
public RequestBuilder (HttpServletRequest request , boolean secure ) {
45
45
super (request );
@@ -85,6 +85,7 @@ public RequestBuilder(HttpServletRequest request, boolean secure) {
85
85
}
86
86
87
87
this .secure = secure ;
88
+ this .contentType = request .getContentType ();
88
89
89
90
try {
90
91
parseRequest ();
@@ -121,10 +122,13 @@ public String query() {
121
122
}
122
123
123
124
private boolean isJsonRequest () {
124
- String contentType = this .request .getContentType ();
125
125
return contentType != null && contentType .contains ("application/json" );
126
126
}
127
127
128
+ private boolean isMultipartRequest () {
129
+ return contentType != null && contentType .contains ("multipart/form-data" );
130
+ }
131
+
128
132
/**
129
133
* @return body string.
130
134
*/
@@ -231,23 +235,32 @@ public Request<HttpServletRequest, ServletInputStream> setUri(String uri) {
231
235
232
236
@ Override
233
237
public String getParameter (String name ) {
238
+ Object value ;
239
+ if ((value = this .request .getParameter (name )) != null ) {
240
+ return value .toString ();
241
+ }
242
+
234
243
if (this .isJsonRequest ()) {
235
244
String queryString = this .query ();
236
245
if (queryString != null && queryString .contains (name + "=" )) {
237
246
// 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 );
239
248
try {
240
249
// 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" );
242
251
} catch (UnsupportedEncodingException e ) {
243
252
// Log the error and return the raw value if decoding fails
244
253
logger .log (Level .WARNING , "Failed to decode query parameter: " + name , e );
245
- return value ;
254
+ return _value ;
246
255
}
247
256
}
248
257
return null ;
258
+ } else if (isMultipartRequest ()) {
259
+ if ((value = request .getAttribute (name )) != null )
260
+ return value .toString ();
249
261
}
250
- return this .request .getParameter (name );
262
+
263
+ return null ;
251
264
}
252
265
253
266
/**
@@ -271,7 +284,6 @@ public String getCharacterEncoding() {
271
284
272
285
private void parseRequest () throws ApplicationException {
273
286
// Check if the content type is multipart/form-data
274
- String contentType = this .request .getContentType ();
275
287
if (contentType == null || !contentType .toLowerCase ().startsWith ("multipart/form-data" )) {
276
288
return ; // Not a multipart form, nothing to parse
277
289
}
0 commit comments