Skip to content

Commit 3fc7dc6

Browse files
committed
✨ Separate Jersey Autoconfiguration project now passes all tests.
1 parent b487384 commit 3fc7dc6

9 files changed

Lines changed: 311 additions & 240 deletions

File tree

spring/fluentforms-jersey-spring-boot-autoconfigure/src/main/java/com/_4point/aem/fluentforms/spring/AemProxyJerseyAfSubmission.java

Lines changed: 6 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
import java.io.ByteArrayOutputStream;
55
import java.io.IOException;
66
import java.io.InputStream;
7-
import java.net.URI;
87
import java.nio.charset.StandardCharsets;
98
import java.util.List;
109
import java.util.Map;
1110
import java.util.Map.Entry;
12-
import java.util.Objects;
1311
import java.util.Optional;
1412
import java.util.function.Function;
15-
import java.util.regex.Pattern;
1613

1714
import org.glassfish.jersey.client.ChunkedInput;
1815
import org.glassfish.jersey.client.ClientProperties;
@@ -23,9 +20,10 @@
2320
import org.slf4j.LoggerFactory;
2421
import org.springframework.beans.factory.annotation.Autowired;
2522
import org.springframework.boot.ssl.SslBundles;
26-
import org.springframework.util.MultiValueMap;
2723
import org.springframework.util.MultiValueMapAdapter;
2824

25+
import com._4point.aem.fluentforms.spring.AemProxyAfSubmission.AfSubmissionHandler;
26+
2927
import jakarta.ws.rs.Consumes;
3028
import jakarta.ws.rs.InternalServerErrorException;
3129
import 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

spring/fluentforms-jersey-spring-boot-autoconfigure/src/main/java/com/_4point/aem/fluentforms/spring/AemProxyJerseyAutoConfiguration.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.glassfish.jersey.servlet.ServletProperties;
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.boot.autoconfigure.AutoConfiguration;
9+
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
910
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
1011
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1112
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -19,11 +20,11 @@
1920
import org.springframework.core.task.SimpleAsyncTaskExecutor;
2021
import org.springframework.core.task.TaskExecutor;
2122

22-
import com._4point.aem.fluentforms.spring.AemProxyJerseyAfSubmission.AfSubmissionHandler;
23+
import com._4point.aem.fluentforms.spring.AemProxyAfSubmission.AfSubmissionHandler;
2324
import com._4point.aem.fluentforms.spring.AemProxyJerseyAfSubmission.AfSubmitAemProxyProcessor;
2425
import com._4point.aem.fluentforms.spring.AemProxyJerseyAfSubmission.AfSubmitLocalProcessor;
2526
import com._4point.aem.fluentforms.spring.AemProxyJerseyAfSubmission.AfSubmitLocalProcessor.InternalAfSubmitAemProxyProcessor;
26-
import com._4point.aem.fluentforms.spring.AemProxyJerseyAfSubmission.AfSubmitProcessor;
27+
import com._4point.aem.fluentforms.spring.AemProxyJerseyAfSubmission.JerseyAfSubmitProcessor;
2728

2829
/**
2930
* AutoConfiguration for the Reverse Proxy Library which reverse proxies secondary
@@ -32,9 +33,24 @@
3233
@AutoConfiguration
3334
@ConditionalOnWebApplication(type=Type.SERVLET)
3435
@ConditionalOnProperty(prefix="fluentforms.rproxy", name="enabled", havingValue="true", matchIfMissing=true )
36+
@ConditionalOnProperty(prefix="fluentforms.rproxy", name="type", havingValue="jersey", matchIfMissing=true )
3537
@EnableConfigurationProperties({AemConfiguration.class, AemProxyConfiguration.class})
38+
@ConditionalOnMissingBean(AemProxyImplemention.class)
39+
@AutoConfigureBefore(AemProxyAutoConfiguration.class)
3640
public class AemProxyJerseyAutoConfiguration {
3741

42+
/**
43+
* Marker bean to indicate that the Jersey-based AEM Proxy implementation is being used.
44+
*
45+
* @return
46+
*/
47+
@Bean
48+
AemProxyImplemention aemProxyImplemention() {
49+
return new AemProxyImplemention() {
50+
// This is just a marker bean.
51+
};
52+
}
53+
3854
/**
3955
* Configures the JAX-RS resources associated with reverse proxying resources and submissions from
4056
* Adaptive Forms.
@@ -93,10 +109,10 @@ public TaskExecutor aemProxyTaskExecutor() {
93109
* Processor that will call the first submission handler that says that it can
94110
* process this request.
95111
*/
96-
@ConditionalOnMissingBean(AfSubmitProcessor.class)
112+
@ConditionalOnMissingBean(JerseyAfSubmitProcessor.class)
97113
@ConditionalOnBean(AfSubmissionHandler.class)
98114
@Bean
99-
public AfSubmitProcessor localSubmitProcessor(List<AfSubmissionHandler> submissionHandlers, InternalAfSubmitAemProxyProcessor aemProxyProcessor) {
115+
public JerseyAfSubmitProcessor localSubmitProcessor(List<AfSubmissionHandler> submissionHandlers, InternalAfSubmitAemProxyProcessor aemProxyProcessor) {
100116
return new AfSubmitLocalProcessor(submissionHandlers, aemProxyProcessor);
101117
}
102118

@@ -112,9 +128,9 @@ public AfSubmitProcessor localSubmitProcessor(List<AfSubmissionHandler> submissi
112128
* @return
113129
* Processor that forwards all submissions on to AEM.
114130
*/
115-
@ConditionalOnMissingBean({AfSubmitProcessor.class, AfSubmissionHandler.class})
131+
@ConditionalOnMissingBean({JerseyAfSubmitProcessor.class, AfSubmissionHandler.class})
116132
@Bean()
117-
public AfSubmitProcessor aemSubmitProcessor(AemConfiguration aemConfig, @Autowired(required = false) SslBundles sslBundles) {
133+
public JerseyAfSubmitProcessor aemSubmitProcessor(AemConfiguration aemConfig, @Autowired(required = false) SslBundles sslBundles) {
118134
return new AfSubmitAemProxyProcessor(aemConfig, sslBundles);
119135
}
120136

spring/fluentforms-jersey-spring-boot-autoconfigure/src/main/java/com/_4point/aem/fluentforms/spring/FluentFormsJerseyAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.boot.autoconfigure.AutoConfiguration;
55
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
67
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
78
import org.springframework.boot.context.properties.EnableConfigurationProperties;
89
import org.springframework.boot.ssl.SslBundles;
@@ -12,7 +13,6 @@
1213

1314
import com._4point.aem.docservices.rest_services.client.helpers.Builder.RestClientFactory;
1415
import com._4point.aem.docservices.rest_services.client.jersey.JerseyRestClient;
15-
import com.ulisesbocchio.jasyptspringboot.annotation.ConditionalOnMissingBean;
1616

1717
import jakarta.ws.rs.client.Client;
1818

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
com._4point.aem.fluentforms.spring.FluentFormsJerseyAutoConfiguration
2+
com._4point.aem.fluentforms.spring.AemProxyJerseyAutoConfiguration

0 commit comments

Comments
 (0)