Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public boolean handle(Request request, Response response, Callback callback) thr

FormsHandling formHandling = new FormsHandling(hookSystem);

final AtomicReference<FormResponse> formResponse = new AtomicReference<>();
try {
if (MimeTypes.Type.FORM_ENCODED.is(contentType)) {
FormFields.onFields(request, StandardCharsets.UTF_8, new Promise.Invocable<Fields>() {
@Override
public void succeeded(Fields fields) {
FormResponse formResponse;
try {
final String formName = fields.get("form").getValue();
var form = FormsLifecycleExtension.FORMSCONFIG.findForm(formName).get();
Expand All @@ -78,16 +78,21 @@ public void succeeded(Fields fields) {
}
return field;
});
formResponse.set(new FormResponse(false));
formResponse = new FormResponse(false);
response.setStatus(HttpStatus.OK_200);
} catch (FormHandlingException fhe) {
log.error(null, fhe);
formResponse.set(new FormResponse(true));
formResponse = new FormResponse(true);
response.setStatus(HttpStatus.BAD_REQUEST_400);
}
Content.Sink.write(response, true, GSON.toJson(formResponse), callback);
}

@Override
public void failed(Throwable x) {
formResponse.set(new FormResponse(true));
var formResponse = new FormResponse(true);
response.setStatus(HttpStatus.BAD_REQUEST_400);
Content.Sink.write(response, true, GSON.toJson(formResponse), callback);
}
});
} else if (contentType.startsWith(MimeTypes.Type.MULTIPART_FORM_DATA.asString())) {
Expand All @@ -98,11 +103,14 @@ public void failed(Throwable x) {
parser.parse(request, new Promise.Invocable<MultiPartFormData.Parts>() {
@Override
public void failed(Throwable x) {
formResponse.set(new FormResponse(true));
var formResponse = new FormResponse(true);
response.setStatus(HttpStatus.BAD_REQUEST_400);
Content.Sink.write(response, true, GSON.toJson(formResponse), callback);
}

@Override
public void succeeded(MultiPartFormData.Parts parts) {
FormResponse formResponse;
try {

String formName = parts.getFirst("form").getContentAsString(StandardCharsets.UTF_8);
Expand All @@ -114,26 +122,28 @@ public void succeeded(MultiPartFormData.Parts parts) {
return field;
});

formResponse.set(new FormResponse(false));

formResponse = new FormResponse(false);
response.setStatus(HttpStatus.OK_200);
} catch (FormHandlingException fhe) {
log.error(null, fhe);
formResponse.set(new FormResponse(true));
formResponse = new FormResponse(true);
response.setStatus(HttpStatus.BAD_REQUEST_400);
}
Content.Sink.write(response, true, GSON.toJson(formResponse), callback);
}

});
} else {
var formResponse = new FormResponse(true);
response.setStatus(HttpStatus.BAD_REQUEST_400);
Content.Sink.write(response, true, GSON.toJson(formResponse), callback);
}
} catch (Exception e) {
log.error("error processing form", e);
}

if (formResponse.get().error()) {
var formResponse = new FormResponse(true);
response.setStatus(HttpStatus.BAD_REQUEST_400);
} else {
response.setStatus(HttpStatus.OK_200);
Content.Sink.write(response, true, GSON.toJson(formResponse), callback);
}
Content.Sink.write(response, true, GSON.toJson(formResponse.get()), callback);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public boolean handle(Request request, Response response, Callback callback) thr
public void failed(Throwable x) {
response.getHeaders().add("Location", FormsLifecycleExtension.FORMSCONFIG.getRedirects().getError());
response.setStatus(HttpStatus.MOVED_TEMPORARILY_302);
callback.succeeded();
}

@Override
Expand All @@ -101,10 +102,11 @@ public void succeeded(Fields fields) {
response.getHeaders().add("Location", FormsLifecycleExtension.FORMSCONFIG.getRedirects().getError());
response.setStatus(HttpStatus.MOVED_TEMPORARILY_302);
}
} finally {
callback.succeeded();
}
}
});
return true;
} else if (contentType.startsWith(MimeTypes.Type.MULTIPART_FORM_DATA.asString())) {
String boundary = MultiPart.extractBoundary(contentType);
MultiPartFormData.Parser parser = new MultiPartFormData.Parser(boundary);
Expand All @@ -114,7 +116,7 @@ public void succeeded(Fields fields) {
public void failed(Throwable x) {
response.getHeaders().add("Location", FormsLifecycleExtension.FORMSCONFIG.getRedirects().getError());
response.setStatus(HttpStatus.MOVED_TEMPORARILY_302);

callback.succeeded();
}

@Override
Expand Down Expand Up @@ -143,19 +145,23 @@ public void succeeded(MultiPartFormData.Parts parts) {
response.getHeaders().add("Location", FormsLifecycleExtension.FORMSCONFIG.getRedirects().getError());
response.setStatus(HttpStatus.MOVED_TEMPORARILY_302);
}
} finally {
callback.succeeded();
}
}

});
return true;
} else {
response.getHeaders().add("Location", FormsLifecycleExtension.FORMSCONFIG.getRedirects().getError());
response.setStatus(HttpStatus.MOVED_TEMPORARILY_302);
callback.succeeded();
}
} catch (Exception e) {
log.error("error processing form", e);
response.getHeaders().add("Location", FormsLifecycleExtension.FORMSCONFIG.getRedirects().getError());
response.setStatus(HttpStatus.MOVED_TEMPORARILY_302);
callback.succeeded();
}

response.getHeaders().add("Location", FormsLifecycleExtension.FORMSCONFIG.getRedirects().getError());
response.setStatus(HttpStatus.MOVED_TEMPORARILY_302);
callback.succeeded();
return true;
}
}