44import java .io .ByteArrayOutputStream ;
55import java .io .IOException ;
66import java .io .InputStream ;
7- import java .net .URI ;
87import java .nio .charset .StandardCharsets ;
98import java .util .List ;
109import java .util .Map ;
1110import java .util .Map .Entry ;
12- import java .util .Objects ;
1311import java .util .Optional ;
1412import java .util .function .Function ;
15- import java .util .regex .Pattern ;
1613
1714import org .glassfish .jersey .client .ChunkedInput ;
1815import org .glassfish .jersey .client .ClientProperties ;
2320import org .slf4j .LoggerFactory ;
2421import org .springframework .beans .factory .annotation .Autowired ;
2522import org .springframework .boot .ssl .SslBundles ;
26- import org .springframework .util .MultiValueMap ;
2723import org .springframework .util .MultiValueMapAdapter ;
2824
25+ import com ._4point .aem .fluentforms .spring .AemProxyAfSubmission .AfSubmissionHandler ;
26+
2927import jakarta .ws .rs .Consumes ;
3028import jakarta .ws .rs .InternalServerErrorException ;
3129import jakarta .ws .rs .POST ;
@@ -66,7 +64,7 @@ public class AemProxyJerseyAfSubmission {
6664 private static final String CONTENT_FORMS_AF = "content/forms/af/" ;
6765
6866 @ Autowired
69- AfSubmitProcessor submitProcessor ;
67+ JerseyAfSubmitProcessor submitProcessor ;
7068
7169 @ Path (CONTENT_FORMS_AF + "{remainder : .+}" )
7270 @ POST
@@ -126,7 +124,7 @@ private static FormDataMultiPart transformFormData(final FormDataMultiPart inFor
126124 *
127125 */
128126 @ FunctionalInterface
129- public interface AfSubmitProcessor {
127+ public interface JerseyAfSubmitProcessor {
130128 /**
131129 * Processor to process incoming Adaptive Forms submit.
132130 *
@@ -169,7 +167,7 @@ public interface AfFormDataTransformer {
169167 * Spring context.
170168 *
171169 */
172- static class AfSubmitAemProxyProcessor implements AfSubmitProcessor {
170+ static class AfSubmitAemProxyProcessor implements JerseyAfSubmitProcessor {
173171
174172 private final AemConfiguration aemConfig ;
175173 private final Client httpClient ;
@@ -263,215 +261,6 @@ private static byte[] transferFromAem(Response result, Logger logger) {
263261
264262 }
265263
266- /**
267- * Implement this interface in order to provide code that will handle an Adaptive Form submission
268- *
269- */
270- public interface AfSubmissionHandler {
271- /**
272- * Object that contains the data submitted by the Adaptive Form
273- */
274- public record Submission (String formData , String formName , String redirectUrl , MultiValueMap <String , String > headers ) {};
275-
276- /**
277- * Interface that is a tagging interface for the different types of response.
278- */
279- public sealed interface SubmitResponse permits SubmitResponse .Response , SubmitResponse .SeeOther , SubmitResponse .Redirect {
280- /**
281- * A Normal response with a 200 HTTP status code (204 if the responseBytes variable is empty)
282- */
283- public record Response (byte [] responseBytes , String mediaType ) implements SubmitResponse {
284- /**
285- * Creates a text response from a String
286- *
287- * @param text
288- * Text to go into the response.
289- * @return
290- * Response object with a media type of "text/plain"
291- */
292- public static Response text (String text ) { return new Response (text .getBytes (StandardCharsets .UTF_8 ), MediaType .TEXT_PLAIN ); }
293- /**
294- * Creates an HTML response from a String
295- *
296- * @param html
297- * String containing HTML. No checking is done to ensure that this is valid HTML.
298- * @return
299- * Response object with a media type of "text/html"
300- */
301- public static Response html (String html ) { return new Response (html .getBytes (StandardCharsets .UTF_8 ), MediaType .TEXT_HTML ); }
302- /**
303- * Creates an JSON response from a String
304- *
305- * @param json
306- * String containing JSON. No checking is done to ensure that this is valid JSON.
307- * @return
308- * Response object with a media type of "application/html"
309- */
310- public static Response json (String json ) { return new Response (json .getBytes (StandardCharsets .UTF_8 ), MediaType .APPLICATION_JSON ); }
311- /**
312- * Creates an XML response from a String
313- *
314- * @param xml
315- * String containing XML. No checking is done to ensure that this is valid XML.
316- * @return
317- * Response object with a media type of "application/xml"
318- */
319- public static Response xml (String xml ) { return new Response (xml .getBytes (StandardCharsets .UTF_8 ), MediaType .APPLICATION_XML ); }
320- };
321- /**
322- * A Temporary Redirect (302 HTTP status code) response
323- */
324- public record SeeOther (URI redirectUrl ) implements SubmitResponse {};
325- /**
326- * A Temporary Redirect (307 HTTP status code) response
327- */
328- public record Redirect (URI redirectUrl ) implements SubmitResponse {};
329- }
330- /**
331- * Called to determine if this handler can handle a submission from this form.
332- *
333- * The first handler that can handle a form submission (i.e. for which canHandle() returns true) will be selected.
334- *
335- * @param formName
336- * Adaptive Form name (full path under formsanddocuments)
337- * @return
338- * true indicates the this handler can handle the submission, false indicates that it cannot
339- */
340- boolean canHandle (String formName );
341-
342- /**
343- * Called to process the submission
344- *
345- * The incoming submission is parsed into a Submission object and then the first handler that indicates
346- * that is can handle the submission will have its processSubmission method called.
347- *
348- * @param submission
349- * Submission object containing the form submission data
350- * @return
351- * a SubmitResponse object that will be turned into an HTTP response to the submission.
352- */
353- SubmitResponse processSubmission (Submission submission );
354-
355- /**
356- * Creates an AfSubmissionHandler that handles form submissions from a specific form
357- *
358- * @param formName
359- * name of the form (so the form name under FormsAndDocuments)
360- * @param handlerLogic
361- * function that will be called when a form submission for occurs from that form
362- * @return
363- * an AfSubmissionHandler object
364- */
365- public static AfSubmissionHandler canHandleFormNameEquals (String formName , Function <Submission , SubmitResponse > handlerLogic ) {
366- Objects .requireNonNull (formName , "Form Name for submission handler cannot be null." );
367- return new AfSubmissionHandler () {
368-
369- @ Override
370- public boolean canHandle (String formNameIn ) {
371- return formName .equals (formNameIn );
372- }
373-
374- @ Override
375- public SubmitResponse processSubmission (Submission submission ) {
376- return handlerLogic .apply (submission );
377- }
378- };
379- }
380-
381- /**
382- * Creates an AfSubmissionHandler that handles form submissions from one or more specific forms
383- *
384- * @param handlerLogic
385- * function that will be called when a form submission for occurs from that form
386- * @param formNames
387- * one or more form names (can be zero, but then this handler will never be called)
388- * @return
389- * an AfSubmissionHandler object
390- */
391- public static AfSubmissionHandler canHandleFormNameAnyOf (Function <Submission , SubmitResponse > handlerLogic , String ... formNames ) {
392- if (formNames .length < 1 ) {
393- logger .atWarn ().log ("No form names were supplied, so this handler will never be called." );
394- }
395- return new AfSubmissionHandler () {
396-
397- @ Override
398- public boolean canHandle (String formNameIn ) {
399- for (String formName : formNames ) {
400- if (Objects .equals (formName , formNameIn )) {
401- return true ;
402- }
403- }
404- return false ;
405- }
406-
407- @ Override
408- public SubmitResponse processSubmission (Submission submission ) {
409- return handlerLogic .apply (submission );
410- }
411- };
412- }
413-
414- /**
415- * Creates an AfSubmissionHandler that handles form submissions from one or more specific forms
416- *
417- * @param formNames
418- * list of one or more form names (can be empty, but then this handler will never be called)
419- * @param handlerLogic
420- * function that will be called when a form submission for occurs from that form
421- * @return
422- * an AfSubmissionHandler object
423- */
424- public static AfSubmissionHandler canHandleFormNameAnyOf (List <String > formNames , Function <Submission , SubmitResponse > handlerLogic ) {
425- if (formNames .size () < 1 ) {
426- logger .atWarn ().log ("No form names were supplied, so this handler will never be called." );
427- }
428- return new AfSubmissionHandler () {
429-
430- @ Override
431- public boolean canHandle (String formNameIn ) {
432- for (String formName : formNames ) {
433- if (Objects .equals (formName , formNameIn )) {
434- return true ;
435- }
436- }
437- return false ;
438- }
439-
440- @ Override
441- public SubmitResponse processSubmission (Submission submission ) {
442- return handlerLogic .apply (submission );
443- }
444- };
445- }
446-
447- /**
448- * Creates an AfSubmissionHandler that handles form submissions from one or more specific forms
449- *
450- * @param formNameRegEx
451- * a regex that will be applied to the name of the form, if it matches, then the handler will apply
452- * @param handlerLogic
453- * function that will be called when a form submission for occurs from that form
454- * @return
455- * an AfSubmissionHandler object
456- */
457- public static AfSubmissionHandler canHandleFormNameMatchesRegex (String formNameRegEx , Function <Submission , SubmitResponse > handlerLogic ) {
458- final var pattern = Pattern .compile (Objects .requireNonNull (formNameRegEx , "Form Name RegEx for submission handler cannot be null." ));
459-
460- return new AfSubmissionHandler () {
461-
462- @ Override
463- public boolean canHandle (String formNameIn ) {
464- return pattern .matcher (formNameIn ).matches ();
465- }
466-
467- @ Override
468- public SubmitResponse processSubmission (Submission submission ) {
469- return handlerLogic .apply (submission );
470- }
471- };
472- }
473- }
474-
475264 /**
476265 * This processor will process Adaptive Forms submissions locally without sending anything to AEM.
477266 *
@@ -481,7 +270,7 @@ public SubmitResponse processSubmission(Submission submission) {
481270 * ALL - process all handlers that canHandle a request.
482271 *
483272 */
484- static class AfSubmitLocalProcessor implements AfSubmitProcessor {
273+ static class AfSubmitLocalProcessor implements JerseyAfSubmitProcessor {
485274 private final static Logger logger = LoggerFactory .getLogger (AfSubmitLocalProcessor .class );
486275 private static final String REMAINDER_PATH_SUFFIX = "/jcr:content/guideContainer.af.submit.jsp" ;
487276
0 commit comments