@@ -343,7 +343,19 @@ boolean redirectIfNeeded(
343343 final JakartaPostResponse htmlResponse ,
344344 final SlingJakartaHttpServletResponse response )
345345 throws IOException {
346- final String redirectURL = getRedirectUrl (request , htmlResponse );
346+ final String redirectURL = getRedirectUrl (request , htmlResponse , response );
347+ if (redirectURL != null ) {
348+ log .debug ("redirecting to URL [{}]" , redirectURL );
349+ response .sendRedirect (redirectURL );
350+ return true ;
351+ }
352+ return false ;
353+ }
354+
355+ private String encodeRedirectUrl (
356+ final String redirectURL ,
357+ final SlingJakartaHttpServletResponse response ,
358+ final SlingJakartaHttpServletRequest request ) {
347359 if (redirectURL != null ) {
348360 final Matcher m = REDIRECT_WITH_SCHEME_PATTERN .matcher (redirectURL );
349361 final boolean hasScheme = m .matches ();
@@ -356,11 +368,9 @@ boolean redirectIfNeeded(
356368 log .debug ("Request path is [{}]" , request .getPathInfo ());
357369 encodedURL = response .encodeRedirectURL (redirectURL );
358370 }
359- log .debug ("redirecting to URL [{}] - encoded as [{}]" , redirectURL , encodedURL );
360- response .sendRedirect (encodedURL );
361- return true ;
371+ return encodedURL ;
362372 }
363- return false ;
373+ return null ;
364374 }
365375
366376 private static final Pattern REDIRECT_WITH_SCHEME_PATTERN = Pattern .compile ("^(https?://[^/]+)(.*)$" );
@@ -441,12 +451,16 @@ private JakartaPostOperation getSlingPostOperation(final SlingJakartaHttpServlet
441451 * @param ctx the post processor
442452 * @return the redirect location or <code>null</code>
443453 */
444- protected String getRedirectUrl (final SlingJakartaHttpServletRequest request , final JakartaPostResponse ctx ) {
454+ private String getRedirectUrl (
455+ final SlingJakartaHttpServletRequest request ,
456+ final JakartaPostResponse ctx ,
457+ SlingJakartaHttpServletResponse response ) {
445458 // redirect param has priority (but see below, magic star)
446459 String result = request .getParameter (SlingPostConstants .RP_REDIRECT_TO );
447460 if (result != null ) {
448461 try {
449- URI redirectUri = new URI (result );
462+ String encodedURL = encodeRedirectUrl (result , response , request );
463+ URI redirectUri = new URI (encodedURL );
450464 if (redirectUri .getAuthority () != null ) {
451465 // if it has a host information
452466 log .warn (
@@ -462,42 +476,48 @@ protected String getRedirectUrl(final SlingJakartaHttpServletRequest request, fi
462476
463477 log .debug ("redirect requested as [{}] for path [{}]" , result , ctx .getPath ());
464478
465- // redirect to created/modified Resource
466- final int star = result .indexOf ('*' );
467- if (star >= 0 && ctx .getPath () != null ) {
468- final StringBuilder buf = new StringBuilder ();
479+ result = handleStarResource (result , ctx , request );
469480
470- // anything before the star
471- if ( star > 0 ) {
472- buf . append (result . substring ( 0 , star ) );
473- }
481+ log . debug ( "Will redirect to {}" , result );
482+ }
483+ return encodeRedirectUrl (result , response , request );
484+ }
474485
475- // append the name of the manipulated node
476- buf .append (ResourceUtil .getName (ctx .getPath ()));
486+ private String handleStarResource (
487+ String result , final JakartaPostResponse ctx , final SlingJakartaHttpServletRequest request ) {
488+ // redirect to created/modified Resource
489+ final int star = result .indexOf ('*' );
490+ if (star >= 0 && ctx .getPath () != null ) {
491+ final StringBuilder buf = new StringBuilder ();
477492
478- // anything after the star
479- if (star < result . length () - 1 ) {
480- buf .append (result .substring (star + 1 ));
481- }
493+ // anything before the star
494+ if (star > 0 ) {
495+ buf .append (result .substring (0 , star ));
496+ }
482497
483- // Prepend request path if it ends with create suffix and result isn't absolute
484- final String requestPath = request .getPathInfo ();
485- if (requestPath .endsWith (SlingPostConstants .DEFAULT_CREATE_SUFFIX )
486- && buf .charAt (0 ) != '/'
487- && !REDIRECT_WITH_SCHEME_PATTERN .matcher (buf ).matches ()) {
488- buf .insert (0 , requestPath );
489- }
498+ // append the name of the manipulated node
499+ buf .append (ResourceUtil .getName (ctx .getPath ()));
490500
491- // use the created path as the redirect result
492- result = buf .toString ();
501+ // anything after the star
502+ if (star < result .length () - 1 ) {
503+ buf .append (result .substring (star + 1 ));
504+ }
493505
494- } else if (result .endsWith (SlingPostConstants .DEFAULT_CREATE_SUFFIX )) {
495- // if the redirect has a trailing slash, append modified node
496- // name
497- result = result .concat (ResourceUtil .getName (ctx .getPath ()));
506+ // Prepend request path if it ends with create suffix and result isn't absolute
507+ final String requestPath = request .getPathInfo ();
508+ if (requestPath .endsWith (SlingPostConstants .DEFAULT_CREATE_SUFFIX )
509+ && buf .charAt (0 ) != '/'
510+ && !REDIRECT_WITH_SCHEME_PATTERN .matcher (buf ).matches ()) {
511+ buf .insert (0 , requestPath );
498512 }
499513
500- log .debug ("Will redirect to {}" , result );
514+ // use the created path as the redirect result
515+ result = buf .toString ();
516+
517+ } else if (result .endsWith (SlingPostConstants .DEFAULT_CREATE_SUFFIX )) {
518+ // if the redirect has a trailing slash, append modified node
519+ // name
520+ result = result .concat (ResourceUtil .getName (ctx .getPath ()));
501521 }
502522 return result ;
503523 }
0 commit comments