Last active
October 2, 2025 14:54
-
-
Save hugithordarson/357f7ac603e2d9a4b69daa495d4ece11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public WOResponse dispatchRequest( final WORequest request ) { | |
final WOResponse originalResponse = super.dispatchRequest( request ); | |
final boolean parseResponse = request.formValueKeys().contains( "woparse" ) || request.uri().endsWith( ".wo.css" ); | |
if( parseResponse ) { | |
final String responseContentString = stringFromResponse( originalResponse ); | |
final WOResponse parsedResponse = new StringTemplateComponent( responseContentString ).generateResponse(); | |
parsedResponse.setHeader( originalResponse.headerForKey( "content-type" ), "content-type" ); | |
return parsedResponse; | |
} | |
return originalResponse; | |
} | |
/** | |
* A component with a template constructed from a template string | |
*/ | |
public static class StringTemplateComponent extends WOComponent { | |
private WOElement _template; | |
public StringTemplateComponent( final String templateString ) { | |
super( ERXWOContext.newContext() ); | |
_template = new Parsley( templateString ).parse(); | |
} | |
@Override | |
public WOElement template() { | |
return _template; | |
} | |
} | |
/** | |
* @return The string content of the given response | |
* | |
* FIXME: Currently only handles streaming responses (what we expect from the resource request handler) | |
*/ | |
private static String stringFromResponse( final WOResponse response ) { | |
try { | |
return new String( response.contentInputStream().readAllBytes() ); | |
} | |
catch( IOException e ) { | |
throw new UncheckedIOException( e ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment