/**
  * Method that implements the interceptors functionality.
  *
  * @param AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
  *
  * @return string|null The action result
  */
 protected function execute(MethodInvocationInterface $methodInvocation)
 {
     try {
         // proceed invocation chain
         $result = $methodInvocation->proceed();
         // get the action, methods and servlet request
         $action = $this->getAction();
         // query whether we want to validate
         if ($action instanceof Validateable) {
             $action->validate();
         }
         // query whether the action is validation aware
         if ($action instanceof ValidationAware) {
             // query whether the action has errors or not
             if ($action->hasErrors()) {
                 // attach the action errors to the servlet request
                 $this->getServletRequest()->setAttribute('error.messages', $action->getErrors());
                 // return a failure
                 $result = ActionInterface::FAILURE;
             }
         }
     } catch (\Exception $e) {
         // if not add an error message
         $this->getServletRequest()->setAttribute('error.messages', array('critical' => $e->getMessage()));
         // action invocation has failed
         $result = ActionInterface::FAILURE;
     }
     // return the result
     return $result;
 }
 /**
  * Tries to JSON decode the servlet request body content into parameters.
  *
  * Then it tries to find and invoke a setter with the param that matches
  * the setters name.
  *
  * @param AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
  *
  * @return string|null The action result
  */
 protected function execute(MethodInvocationInterface $methodInvocation)
 {
     // get the action, methods and servlet request
     $action = $this->getAction();
     $methods = $this->getActionMethods();
     $servletRequest = $this->getServletRequest();
     // query whether we've a body content
     if ($bodyContent = $servletRequest->getBodyContent()) {
         // only process if request has valid JSON
         if (is_object(json_decode($bodyContent))) {
             // try to inject the request parameters by using the class setters
             foreach (json_decode($bodyContent) as $key => $value) {
                 // prepare the setter method name
                 $methodName = sprintf('set%s', ucfirst($key));
                 // query whether the class has the setter implemented
                 if (in_array($methodName, $methods) === false) {
                     continue;
                 }
                 try {
                     // set the value by using the setter
                     $action->{$methodName}($value);
                 } catch (\Exception $e) {
                     $action->addFieldError($key, $e->getMessage());
                 }
             }
         }
     }
     // proceed invocation chain
     return $methodInvocation->proceed();
 }
 /**
  * Advice used to encode the response data.
  *
  * @param \AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
  *
  * @return void
  */
 public function intercept(MethodInvocationInterface $methodInvocation)
 {
     // load the method invocation parameters
     $this->setParameters($methodInvocation->getParameters());
     // load the servlet instance
     $servlet = $methodInvocation->getContext();
     // query whether we've to validate the servlet or not
     if ($servlet instanceof ValidationAwareInterface) {
         // create a reflection object instance
         $reflectionObject = new ReflectionObject($servlet);
         // iterate over all methods to one with the apropriate validation annotations
         foreach ($reflectionObject->getMethods() as $reflectionMethod) {
             // prepare the request method name
             $requestMethod = ucfirst(strtolower($this->getServletRequest()->getMethod()));
             // invoke the valiation method only on the Doppelgaenger methods
             if ($reflectionMethod->hasAnnotation(sprintf('ValidateOn%s', $requestMethod)) && stripos($reflectionMethod->getMethodName(), 'DOPPELGAENGEROriginal') === false) {
                 $reflectionMethod->invoke($servlet);
             }
         }
     }
     // proceed method invocation
     return $methodInvocation->proceed();
 }
 /**
  * Empty dummy "Around" advice
  *
  * @param \AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
  *
  * @return null
  *
  * @Around
  */
 public function basicAroundAdvice(MethodInvocationInterface $methodInvocation)
 {
     $methodInvocation->proceed();
 }
 /**
  * Advice used to proceed a method and increase an invocation counter.
  * This is done to test if the advice code gets executed BEFORE proceeding
  *
  * @param \AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
  *
  * @return null
  */
 public static function countedBeforeAdvice(MethodInvocationInterface $methodInvocation)
 {
     $result = $methodInvocation->proceed();
     self::$counter++;
     self::$testableState2 = self::$counter;
     return $result;
 }
 /**
  * Advice used to encode the response data.
  *
  * @param \AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
  *
  * @return void
  */
 public function intercept(MethodInvocationInterface $methodInvocation)
 {
     // load the method invocation parameters
     $this->setParameters($methodInvocation->getParameters());
     // load the servlet instance
     $servlet = $methodInvocation->getContext();
     // query whether or not we've to process errors
     if ($servlet instanceof ValidationAwareInterface && $servlet->hasErrors()) {
         $servlet->processErrors($this->getServletRequest(), $this->getServletResponse());
     }
     // query whether or not we've to encode the request
     if ($servlet instanceof EncodingAwareInterface) {
         $servlet->encode($this->getServletRequest(), $this->getServletResponse());
     }
 }
 /**
  * Basic Around advice always returning TRUE. Used to test if methods got wrapped correctly
  *
  * @param \AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
  *
  * @return boolean
  *
  * @Around("pointcut(PointcutReferencingSeveralRegexedMethods)")
  */
 public static function severalRegexedMethodsAdvice(MethodInvocationInterface $methodInvocation)
 {
     $methodInvocation->proceed();
     return true;
 }
 /**
  * Iterates over all servlet request parameters and tries to find and
  * invoke a setter with the param that matches the setters name.
  *
  * @param AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
  *
  * @return string|null The action result
  */
 public function intercept(MethodInvocationInterface $methodInvocation)
 {
     // load the method invocation parameters
     $this->setParameters($methodInvocation->getParameters());
     // load the servlet instance
     $servlet = $methodInvocation->getContext();
     // get the servlet's methods
     $methods = get_class_methods($servlet);
     // try to inject the request parameters by using the class setters
     foreach ($this->getServletRequest()->getParameterMap() as $key => $value) {
         // prepare the setter method name
         $methodName = sprintf('set%s', ucfirst($key));
         // query whether the class has the setter implemented
         if (in_array($methodName, $methods) === false) {
             continue;
         }
         try {
             // set the value by using the setter
             $servlet->{$methodName}($value);
         } catch (\Exception $e) {
             $servlet->addError(array($key => $e->getMessage()));
         }
     }
     // proceed method invocation
     return $methodInvocation->proceed();
 }
 /**
  * Basic Around advice incrementing the param of the wrapped method
  *
  * @param \AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
  *
  * @return void
  *
  * @Before("pointcut(\AppserverIo\Doppelgaenger\Tests\Data\Aspects\PointcutReferencingTestAspect->PointcutReferencingIHaveTwoBeforeAdvicesIncrementingTheParam)")
  */
 public static function paramIncrementingAdvice(MethodInvocationInterface $methodInvocation)
 {
     $param = $methodInvocation->getParameters()['param'];
     $param++;
     $methodInvocation->setParameters(array('param' => $param));
 }
 /**
  * Method that implements the interceptors functionality.
  *
  * @param AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
  *
  * @return string|null The action result
  */
 public function intercept(MethodInvocationInterface $methodInvocation)
 {
     try {
         // load the action instance
         $this->setAction($methodInvocation->getContext());
         // load the method invocation parameters
         $this->setParameters($methodInvocation->getParameters());
         // execute the custom interceptor functionality
         return $this->execute($methodInvocation);
     } catch (\Exception $e) {
         // add the catched exception
         $this->getAction()->addFieldError('unknown', $e->getMessage());
         // return the key for a failed action invocation
         return ActionInterface::FAILURE;
     }
 }