Intercepting requests on your server - marcos8154/SocketAppServer GitHub Wiki

The framework allows you to implement request interception mechanisms to execute some code before Action is executed. To do this, you must first create a class that implements the "IHandlerInterceptor" interface, located in the "MobileAppServer.ServerObjects" namespace.

    public class MySimpleInterceptor : IHandlerInterceptor
    {
        public string ControllerName { get; }
        public string ActionName { get; }

        public InterceptorHandleResult PreHandle(SocketRequest socketRequest)
        {
            throw new NotImplementedException();
        }
    }

Note that the interface implementation generated 2 properties and one method. The "ControllerName" property must return the class name of the controller you want to trap. If the rule applies to ALL server controllers, you must return an empty string (string.empty)

The "ActionName" property must be the name of the action method within the Controller that will respond for the trap. Again, if the rule applies to ALL actions within the CONTROLLER, an empty string (string.empty) must be returned.

Let's look at an example of how to fill in the properties:

        public string ControllerName { get { return "ProductController"; } }
        public string ActionName { get { return "GetProduct"; } }

        public InterceptorHandleResult PreHandle(SocketRequest socketRequest)
        {
            throw new NotImplementedException();
        }

In the example above, our interceptor will only act for the controller product, and for the "GetProduct" action that will get a GUID parameter, and we want to check if this Guid is valid.

Understanding the PreHandle () Method

The PreHandle() method will provide you with information from the request body through the socketRequest parameter.

       public InterceptorHandleResult PreHandle(SocketRequest socketRequest)
        {
            RequestParameter param = socketRequest.FindParameter("productId");
            if (param == null)
                return new InterceptorHandleResult(true, false, "ProductId parameter not found", "");

            Guid guid = new Guid();
            if (Guid.TryParse(param.Value.ToString(), out guid))
                return new InterceptorHandleResult(false, true, "Valid ProductId :)", "");
            else
                return new InterceptorHandleResult(true, false, "Invalid productId :(", "");
        }

First, we get the request parameter that we want to validate by doing this through socketRequest.FindParameter ("productId"); That is, we are trying to retrieve the representation of the parameter whose name is "productId"

      if (param == null)
        return new InterceptorHandleResult(true, false, "ProductId parameter not found", "");

If the parameter was not entered in the request, the RequestParameter object will be null. If the parameter has been entered, the object instance will be valid, and then we can access the property "Value" (object) Having access to it, we can perform our checks and then approve or disapprove the request.

Approving or Disapproving Requests

Now that we know how the interceptor works, approving or rejecting requests is a simple task: just return an instance of "InterceptorHandleResult" containing the constructor parameters that determine the intercept result.

The constructor signature is as follows:

public InterceptorHandleResult(bool cancelActionInvoke, bool responseSuccess, string message, object data);
  • cancelActionInvoke: Determines whether the request will be declined.
  • responseSuccess: Determines whether the client response will be a success or an error.
  • message: Reply a custom message in the reply body.
  • data: Custom data in request response

Enabling the interceptor on the server

Having implemented your trap rules, simply register your trap class on the server as follows:

server.RegisterInterceptor(new MySimpleInterceptor());