5353import io .undertow .util .AttachmentKey ;
5454import io .undertow .util .HeaderValues ;
5555import io .undertow .util .HttpString ;
56- import javaslang .Lazy ;
57- import javaslang .control .Try ;
5856
5957public class UndertowRequest implements NativeRequest {
6058
6159 public static final AttachmentKey <NativeWebSocket > SOCKET = AttachmentKey
6260 .create (NativeWebSocket .class );
6361
62+ private static final FormData NO_FORM = new FormData (0 );
63+
6464 private HttpServerExchange exchange ;
6565
66- private Config config ;
66+ private Config conf ;
6767
68- private final Lazy < FormData > form ;
68+ private FormData form ;
6969
70- private final String path ;
70+ private String path ;
7171
7272 private Supplier <BlockingHttpExchange > blocking ;
7373
74- public UndertowRequest (final HttpServerExchange exchange , final Config conf )
75- throws IOException {
74+ public UndertowRequest (final HttpServerExchange exchange , final Config conf ) throws IOException {
7675 this .exchange = exchange ;
7776 this .blocking = Suppliers .memoize (() -> this .exchange .startBlocking ());
78- this .config = conf ;
79- this .form = Lazy .of (() -> Try .of (() -> parseForm (exchange , conf .getString ("application.tmpdir" ),
80- conf .getString ("application.charset" ))).get ());
77+ this .conf = conf ;
8178 this .path = URLDecoder .decode (exchange .getRequestPath (), "UTF-8" );
8279 }
8380
@@ -106,7 +103,7 @@ public String rawPath() {
106103 public List <String > paramNames () {
107104 ImmutableList .Builder <String > builder = ImmutableList .<String > builder ();
108105 builder .addAll (exchange .getQueryParameters ().keySet ());
109- FormData formdata = this . form . get ();
106+ FormData formdata = parseForm ();
110107 formdata .forEach (v -> {
111108 // excludes upload from param names.
112109 if (!formdata .getFirst (v ).isFile ()) {
@@ -125,7 +122,7 @@ public List<String> params(final String name) {
125122 query .stream ().forEach (builder ::add );
126123 }
127124 // form params
128- Optional .ofNullable (form . get ().get (name )).ifPresent (values -> {
125+ Optional .ofNullable (parseForm ().get (name )).ifPresent (values -> {
129126 values .stream ().forEach (value -> {
130127 if (!value .isFile ()) {
131128 builder .add (value .getValue ());
@@ -164,7 +161,7 @@ public List<Cookie> cookies() {
164161 @ Override
165162 public List <NativeUpload > files (final String name ) {
166163 Builder <NativeUpload > builder = ImmutableList .builder ();
167- Deque <FormValue > values = form . get ().get (name );
164+ Deque <FormValue > values = parseForm ().get (name );
168165 if (values != null ) {
169166 values .forEach (value -> {
170167 if (value .isFile ()) {
@@ -204,7 +201,7 @@ public boolean secure() {
204201 @ SuppressWarnings ("unchecked" )
205202 public <T > T upgrade (final Class <T > type ) throws Exception {
206203 if (type == NativeWebSocket .class ) {
207- UndertowWebSocket ws = new UndertowWebSocket (config );
204+ UndertowWebSocket ws = new UndertowWebSocket (conf );
208205 exchange .putAttachment (SOCKET , ws );
209206 return (T ) ws ;
210207 }
@@ -222,28 +219,35 @@ public void startAsync(final Executor executor, final Runnable runnable) {
222219 exchange .dispatch (executor , runnable );
223220 }
224221
225- private FormData parseForm (final HttpServerExchange exchange , final String tmpdir ,
226- final String charset ) throws IOException {
227- String value = exchange .getRequestHeaders ().getFirst ("Content-Type" );
228- if (value != null ) {
229- MediaType type = MediaType .valueOf (value );
230- if (MediaType .form .name ().equals (type .name ())) {
231- blocking .get ();
232- return new FormEncodedDataDefinition ()
233- .setDefaultEncoding (charset )
234- .create (exchange )
235- .parseBlocking ();
236- } else if (MediaType .multipart .name ().equals (type .name ())) {
237- blocking .get ();
238- return new MultiPartParserDefinition ()
239- .setTempFileLocation (new File (tmpdir ).toPath ())
240- .setDefaultEncoding (charset )
241- .create (exchange )
242- .parseBlocking ();
243-
222+ private FormData parseForm () {
223+ if (form == null ) {
224+ form = NO_FORM ;
225+ try {
226+ String tmpdir = conf .getString ("application.tmpdir" );
227+ String charset = conf .getString ("application.charset" );
228+ String value = exchange .getRequestHeaders ().getFirst ("Content-Type" );
229+ if (value != null ) {
230+ MediaType type = MediaType .valueOf (value );
231+ if (MediaType .form .name ().equals (type .name ())) {
232+ blocking .get ();
233+ form = new FormEncodedDataDefinition ()
234+ .setDefaultEncoding (charset )
235+ .create (exchange )
236+ .parseBlocking ();
237+ } else if (MediaType .multipart .name ().equals (type .name ())) {
238+ blocking .get ();
239+ form = new MultiPartParserDefinition ()
240+ .setTempFileLocation (new File (tmpdir ).toPath ())
241+ .setDefaultEncoding (charset )
242+ .create (exchange )
243+ .parseBlocking ();
244+
245+ }
246+ }
247+ } catch (IOException x ) {
244248 }
245249 }
246- return new FormData ( 0 ) ;
250+ return form ;
247251 }
248252
249253 private static Cookie cookie (final io .undertow .server .handlers .Cookie c ) {
0 commit comments