|
9 | 9 | import java.util.List; |
10 | 10 | import java.util.Map; |
11 | 11 | import java.util.Map.Entry; |
| 12 | +import java.util.Objects; |
12 | 13 | import java.util.Optional; |
13 | 14 | import java.util.function.Function; |
| 15 | +import java.util.regex.Pattern; |
14 | 16 |
|
15 | 17 | import org.glassfish.jersey.client.ChunkedInput; |
16 | 18 | import org.glassfish.jersey.client.ClientProperties; |
@@ -267,6 +269,12 @@ private static byte[] transferFromAem(Response result, Logger logger) { |
267 | 269 | * Implement this interface in order to provide code that will handle an Adaptive Form submission |
268 | 270 | * |
269 | 271 | */ |
| 272 | + /** |
| 273 | + * |
| 274 | + */ |
| 275 | + /** |
| 276 | + * |
| 277 | + */ |
270 | 278 | public interface AfSubmissionHandler { |
271 | 279 | /** |
272 | 280 | * Object that contains the data submitted by the Adaptive Form |
@@ -347,6 +355,125 @@ public record Redirect(URI redirectUrl) implements SubmitResponse {}; |
347 | 355 | * a SubmitResponse object that will be turned into an HTTP response to the submission. |
348 | 356 | */ |
349 | 357 | SubmitResponse processSubmission(Submission submission); |
| 358 | + |
| 359 | + /** |
| 360 | + * Creates an AfSubmissionHandler that handles form submissions from a specific form |
| 361 | + * |
| 362 | + * @param formName |
| 363 | + * name of the form (so the form name under FormsAndDocuments) |
| 364 | + * @param handlerLogic |
| 365 | + * function that will be called when a form submission for occurs from that form |
| 366 | + * @return |
| 367 | + * an AfSubmissionHandler object |
| 368 | + */ |
| 369 | + public static AfSubmissionHandler canHandleFormNameEquals(String formName, Function<Submission, SubmitResponse> handlerLogic) { |
| 370 | + Objects.requireNonNull(formName, "Form Name for submission handler cannot be null."); |
| 371 | + return new AfSubmissionHandler() { |
| 372 | + |
| 373 | + @Override |
| 374 | + public boolean canHandle(String formNameIn) { |
| 375 | + return formName.equals(formNameIn); |
| 376 | + } |
| 377 | + |
| 378 | + @Override |
| 379 | + public SubmitResponse processSubmission(Submission submission) { |
| 380 | + return handlerLogic.apply(submission); |
| 381 | + } |
| 382 | + }; |
| 383 | + } |
| 384 | + |
| 385 | + /** |
| 386 | + * Creates an AfSubmissionHandler that handles form submissions from one or more specific forms |
| 387 | + * |
| 388 | + * @param handlerLogic |
| 389 | + * function that will be called when a form submission for occurs from that form |
| 390 | + * @param formNames |
| 391 | + * one or more form names (can be zero, but then this handler will never be called) |
| 392 | + * @return |
| 393 | + * an AfSubmissionHandler object |
| 394 | + */ |
| 395 | + public static AfSubmissionHandler canHandleFormNameAnyOf(Function<Submission, SubmitResponse> handlerLogic, String... formNames) { |
| 396 | + if (formNames.length < 1) { |
| 397 | + logger.atWarn().log("No form names were supplied, so this handler will never be called."); |
| 398 | + } |
| 399 | + return new AfSubmissionHandler() { |
| 400 | + |
| 401 | + @Override |
| 402 | + public boolean canHandle(String formNameIn) { |
| 403 | + for (String formName : formNames) { |
| 404 | + if (Objects.equals(formName, formNameIn)) { |
| 405 | + return true; |
| 406 | + } |
| 407 | + } |
| 408 | + return false; |
| 409 | + } |
| 410 | + |
| 411 | + @Override |
| 412 | + public SubmitResponse processSubmission(Submission submission) { |
| 413 | + return handlerLogic.apply(submission); |
| 414 | + } |
| 415 | + }; |
| 416 | + } |
| 417 | + |
| 418 | + /** |
| 419 | + * Creates an AfSubmissionHandler that handles form submissions from one or more specific forms |
| 420 | + * |
| 421 | + * @param formNames |
| 422 | + * list of one or more form names (can be empty, but then this handler will never be called) |
| 423 | + * @param handlerLogic |
| 424 | + * function that will be called when a form submission for occurs from that form |
| 425 | + * @return |
| 426 | + * an AfSubmissionHandler object |
| 427 | + */ |
| 428 | + public static AfSubmissionHandler canHandleFormNameAnyOf(List<String> formNames, Function<Submission, SubmitResponse> handlerLogic) { |
| 429 | + if (formNames.size() < 1) { |
| 430 | + logger.atWarn().log("No form names were supplied, so this handler will never be called."); |
| 431 | + } |
| 432 | + return new AfSubmissionHandler() { |
| 433 | + |
| 434 | + @Override |
| 435 | + public boolean canHandle(String formNameIn) { |
| 436 | + for (String formName : formNames) { |
| 437 | + if (Objects.equals(formName, formNameIn)) { |
| 438 | + return true; |
| 439 | + } |
| 440 | + } |
| 441 | + return false; |
| 442 | + } |
| 443 | + |
| 444 | + @Override |
| 445 | + public SubmitResponse processSubmission(Submission submission) { |
| 446 | + return handlerLogic.apply(submission); |
| 447 | + } |
| 448 | + }; |
| 449 | + } |
| 450 | + |
| 451 | + /** |
| 452 | + * Creates an AfSubmissionHandler that handles form submissions from one or more specific forms |
| 453 | + * |
| 454 | + * @param formNameRegEx |
| 455 | + * a regex that will be applied to the name of the form, if it matches, then the handler will apply |
| 456 | + * @param handlerLogic |
| 457 | + * function that will be called when a form submission for occurs from that form |
| 458 | + * @return |
| 459 | + * an AfSubmissionHandler object |
| 460 | + */ |
| 461 | + public static AfSubmissionHandler canHandleFormNameMatchesRegex(String formNameRegEx, Function<Submission, SubmitResponse> handlerLogic) { |
| 462 | + final var pattern = Pattern.compile(Objects.requireNonNull(formNameRegEx, "Form Name RegEx for submission handler cannot be null.")); |
| 463 | + |
| 464 | + return new AfSubmissionHandler() { |
| 465 | + |
| 466 | + @Override |
| 467 | + public boolean canHandle(String formNameIn) { |
| 468 | + return pattern.matcher(formNameIn).matches(); |
| 469 | + } |
| 470 | + |
| 471 | + @Override |
| 472 | + public SubmitResponse processSubmission(Submission submission) { |
| 473 | + return handlerLogic.apply(submission); |
| 474 | + } |
| 475 | + }; |
| 476 | + } |
350 | 477 | } |
351 | 478 |
|
352 | 479 | /** |
|
0 commit comments