HttpServletResponse httpResponse = RequestCycle.get().getResponse();
String modelId = httpRequest.getParameter( AjaxListenerHelper.PARAM_MODEL_ID );
if ( modelId != null )
{
Page page = RequestCycle.get().getInjector().getInstance( getPageClass() );
// check model parameter
String modelParameter = httpRequest.getParameter( AjaxListenerHelper.PARAM_METHOD_MODEL );
if ( modelParameter == null )
{
log.severe( "Model parameter ['" + AjaxListenerHelper.PARAM_METHOD_MODEL + "'] must be set." );
return;
}
// check json parameter
String jsonParameter = httpRequest.getParameter( AjaxListenerHelper.PARAM_JSON );
if ( jsonParameter == null )
{
log.severe( "Json parameter ['" + AjaxListenerHelper.PARAM_JSON + "'] must be set." );
return;
}
// set model class form model parameter
Class<?> modelClass = Class.forName( modelParameter );
for ( Method method : page.getClass().getDeclaredMethods() )
{
SetModel setModelAnnotation = method.getAnnotation( SetModel.class );
if ( setModelAnnotation != null && setModelAnnotation.name().equals( modelId ) )
{
// parse json parameter into model object
Object model = getModelObject( page, modelId );
MAPPER.updatingReader( model ).readValue( jsonParameter );
try
{
Response response = ( Response ) method.invoke( page, model );
response.setMessages( RequestCycle.get().getMessages() );
response.setModel( model );
httpResponse.getWriter().write( MAPPER.writeValueAsString( response ) );
}
catch ( InvocationTargetException e )
{
throw ( Exception ) e.getCause();
}
return;
}
}
log.severe( "No method with annotation " + SetModel.class.getName() + "(name = '" + modelId + "') found in class " + page.getClass().getName() );
}
}