private Optional < @ NotNull Boolean > isParagraphValid ( Resource resource ) { // use reflection to access the method "getModelFromResource" from ModelFactory, as it is not present in earlier version // but we want still to support earlier versions of AEM as well which do not contain this method // validation is disabled in this case Method getModelFromResourceMethod ; try { getModelFromResourceMethod = ModelFactory . class . getDeclaredMethod ( "getModelFromResource" , Resource . class ) ; } catch ( NoSuchMethodException | SecurityException ex ) { // seems to be an earlier version of AEM/Sling models not supporting this method log . debug ( "ModelFactory does not support method 'getModelFromResource' - skip paragraph validation." ) ; return Optional . empty ( ) ; } try { // try to get model associated with the resource, and check if it implements the ParsysItem interface Object model = getModelFromResourceMethod . invoke ( modelFactory , resource ) ; if ( model instanceof ParsysItem ) { return Optional . of ( ( ( ParsysItem ) model ) . isValid ( ) ) ; } } catch ( ModelClassException ex ) { // ignore if no model was registered for this resource type } catch ( InvocationTargetException ex ) { if ( ex . getCause ( ) instanceof ModelClassException ) { // ignore if no model was registered for this resource type } else { log . warn ( "Unable to invoke ModelFactory.getModelFromResource." , ex ) ; } } catch ( IllegalAccessException ex ) { log . warn ( "Unable to access ModelFactory.getModelFromResource." , ex ) ; } return Optional . empty ( ) ; }
Checks if the given paragraph is valid .