Skip to content

Commit 03d04b8

Browse files
committed
Added "See Other" response (HTTP 303).
1 parent 4e99b43 commit 03d04b8

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public record Submission(String formData, String formName, String redirectUrl, M
278278
/**
279279
* Interface that is a tagging interface for the different types of response.
280280
*/
281-
public sealed interface SubmitResponse permits SubmitResponse.Response, SubmitResponse.Redirect {
281+
public sealed interface SubmitResponse permits SubmitResponse.Response, SubmitResponse.SeeOther, SubmitResponse.Redirect {
282282
/**
283283
* A Normal response with a 200 HTTP status code (204 if the responseBytes variable is empty)
284284
*/
@@ -320,6 +320,10 @@ public record Response(byte[] responseBytes, String mediaType) implements Submit
320320
*/
321321
public static Response xml(String xml) { return new Response(xml.getBytes(StandardCharsets.UTF_8), MediaType.APPLICATION_XML); }
322322
};
323+
/**
324+
* A Temporary Redirect (302 HTTP status code) response
325+
*/
326+
public record SeeOther(URI redirectUrl) implements SubmitResponse {};
323327
/**
324328
* A Temporary Redirect (307 HTTP status code) response
325329
*/
@@ -568,6 +572,8 @@ private Response formulateResponse(AfSubmissionHandler.SubmitResponse submitResp
568572
var builder = response.responseBytes().length > 0 ? Response.ok().entity(response.responseBytes()).type(response.mediaType())
569573
: Response.noContent();
570574
return builder.build();
575+
} else if (submitResponse instanceof AfSubmissionHandler.SubmitResponse.SeeOther redirectFound) {
576+
return Response.seeOther(redirectFound.redirectUrl()).build();
571577
} else if (submitResponse instanceof AfSubmissionHandler.SubmitResponse.Redirect redirect) {
572578
return Response.temporaryRedirect(redirect.redirectUrl()).build();
573579
} else {

spring/fluentforms-spring-boot-autoconfigure/src/test/java/com/_4point/aem/fluentforms/spring/AemProxyAfSubmissionTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,19 @@ void testRedirect() {
225225
assertThat(response, allOf(isStatus(Response.Status.TEMPORARY_REDIRECT), doesNotHaveEntity()));
226226
}
227227

228+
@Test
229+
void testSeeOther() {
230+
final FormDataMultiPart getPdfForm = mockFormData("foo3", "bar");
231+
232+
Response response = jrc.target
233+
.path(SUBMIT_ADAPTIVE_FORM_SERVICE_PATH)
234+
.request()
235+
.accept(MediaType.TEXT_PLAIN_TYPE)
236+
.post(Entity.entity(getPdfForm, getPdfForm.getMediaType()));
237+
238+
assertThat(response, allOf(isStatus(Response.Status.SEE_OTHER), doesNotHaveEntity()));
239+
}
240+
228241
@Test
229242
void testProxy() {
230243
final FormDataMultiPart getPdfForm = mockFormData("foo2", "bar");
@@ -256,13 +269,18 @@ public SubmitResponse processSubmission(Submission submission) {
256269
assertAll(
257270
()->assertEquals(AF_TEMPLATE_NAME, submission.formName()),
258271
()->assertEquals("bar", submission.formData()),
259-
()->assertThat(submission.redirectUrl(), anyOf(equalTo("foo1"), equalTo("foo2"))),
272+
()->assertThat(submission.redirectUrl(), anyOf(equalTo("foo1"), equalTo("foo2"), equalTo("foo3"))),
260273
()->assertEquals(MediaType.TEXT_PLAIN, submission.headers().getFirst("accept")),
261274
()->assertTrue(MediaType.MULTIPART_FORM_DATA_TYPE.isCompatible(MediaType.valueOf(submission.headers().getFirst("content-type"))))
262275
);
263276
try {
264-
return "foo2".equals(submission.redirectUrl()) ? new SubmitResponse.Redirect(new URI("http://localhost/"))
265-
: new SubmitResponse.Response(AF_SUBMIT_LOCAL_PROCESSOR_RESPONSE.getBytes(), "text/plain");
277+
String redirectUrl = submission.redirectUrl();
278+
return switch(redirectUrl) {
279+
case "foo1" -> new SubmitResponse.Response(AF_SUBMIT_LOCAL_PROCESSOR_RESPONSE.getBytes(), "text/plain");
280+
case "foo2" -> new SubmitResponse.Redirect(new URI("http://localhost/"));
281+
case "foo3" -> new SubmitResponse.SeeOther(new URI("http://localhost/"));
282+
default -> throw new UnsupportedOperationException("Unexpected value in redirectUrl (%s)".formatted(redirectUrl));
283+
};
266284
} catch (URISyntaxException e) {
267285
throw new IllegalStateException("Bad URI -- ", e);
268286
}

0 commit comments

Comments
 (0)