@SuppressWarnings( "unchecked" )
protected void handleActionRequest( final HttpServletRequest request, final HttpServletResponse response,
final IOutputHandler outputHandler, final HttpServletRequestHandler requestHandler, OutputStream outputStream,
final IContentItem contentItem ) throws ServletException, IOException {
IRuntimeContext runtime = null;
try {
runtime = requestHandler.handleActionRequest( 0, 0 );
if ( runtime == null ) {
StringBuffer buffer = new StringBuffer();
for ( String message : (List<String>) requestHandler.getMessages() ) {
buffer.append( message );
}
outputStream.write( buffer.toString().getBytes( LocaleHelper.getSystemEncoding() ) );
return;
}
/*
* the flag "hasResponse" should be set if the outputHandler is expected to serve a response back via either the
* "response.content" output (a final content output), or an intermediate response such as a form to request
* parameters such as from a SecureFilterComponent.
*/
boolean hasResponse = outputHandler.isResponseExpected();
IContentItem responseContentItem =
outputHandler.getOutputContentItem( IOutputHandler.RESPONSE, IOutputHandler.CONTENT, null, null );
boolean success = ( runtime != null && runtime.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS );
boolean debugMessages = doMessages( request );
boolean printSuccess = ( runtime != null ) && success && ( !hasResponse || debugMessages );
boolean printError = ( runtime != null ) && !success && !response.isCommitted();
if ( printSuccess || printError ) {
final String htmlMimeType = "text/html"; //$NON-NLS-1$
responseContentItem.setMimeType( htmlMimeType );
// this is going to be the response output stream unless you are in debug mode
outputStream = responseContentItem.getOutputStream( null );
response.setContentType( htmlMimeType );
StringBuffer buffer = new StringBuffer();
IMessageFormatter formatter = PentahoSystem.get( IMessageFormatter.class, PentahoSessionHolder.getSession() );
if ( printSuccess ) {
boolean doWrapper = !( "false".equals( request.getParameter( "wrapper" ) ) ); //$NON-NLS-1$ //$NON-NLS-2$
formatter.formatSuccessMessage( htmlMimeType, runtime, buffer, debugMessages, doWrapper );
} else {
response.resetBuffer();
formatter.formatFailureMessage( htmlMimeType, runtime, buffer, requestHandler.getMessages() );
}
outputStream.write( buffer.toString().getBytes( LocaleHelper.getSystemEncoding() ) );
responseContentItem.closeOutputStream();
}
} finally {
if ( runtime != null ) {
runtime.dispose();
}
}
}