Skip to content

Build a service interceptor in IntelliJ

This walk-through demonstrates how to create, build, and deploy a service interceptor using IntelliJ IDEA. Before you begin, ensure that all prerequisites mentioned in Systems Requirements are installed on your development machine.

Create the service interceptor project

To create a service interceptor project using IntelliJ, complete the following steps:

  1. Open IntelliJ and click File > New > Project.

  2. Under Generators, select Maven Archetype.

  3. Enter AuditLogInterceptor in the Name field.

  4. In the Location field, specify a location on the machine where you want to store the project.

  5. Choose JDK 17 from the JDK drop-down menu.

  6. Under Catalog, ensure that the default internal catalog is selected.

  7. Click Add.

  8. In the Add Archetype window, do the following:

    • Enter com.esri.arcgis.enterprise.sdk in the GroupId field.
    • Enter server-services-interceptor-archetype in the ArtifactId field.
    • Enter 11.5.0 in the Version field.
  9. Click Add.

    Create the service interceptor project

    The service interceptor project is created, and the server-services-interceptor-archetype is added. Once you add the archetype, interceptorName will appear under Additional Properties.

  10. To configure advanced settings, expand Advanced Settings and provide the required information.

    Configure advanced settings
  11. Click Create.

If you see any warnings, you can ignore them as long as the project has successfully generated the AuditLogInterceptor class in the \src\main\java folder. Those warnings will be addressed during the project build.

Modify the code and package the service interceptor project

Building the service interceptor project will package the project’s classes, dependencies, and resources into a .interceptor file, which can be deployed to ArcGIS Server. Since it’s a Maven project, the Maven build lifecycle must be followed to build the service interceptor.

You can build the service interceptor project from either the Maven tool window or the Terminal tool window.

Build the service interceptor project

Since the project is created from server-services-interceptor-archetype, it automatically loads the boilerplate code that implements a ready-to-use service interceptor.

  1. Open the AuditLogInterceptor class located in src\main\java (see A).

  2. Adding commons-lang3 artifact to the <dependencies> section as below in pom.xml file.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
      <dependencies>
         <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
          <version>3.11</version>
       </dependency>
     </dependencies>
  3. Paste the following code into the AuditLogInterceptor.java class.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    import com.esri.arcgis.enterprise.interceptor.Interceptor;
    import com.esri.arcgis.enterprise.interceptor.server.IServerServicesInterceptor;
    import com.esri.arcgis.enterprise.interceptor.IInterceptorRequest;
    import com.esri.arcgis.enterprise.interceptor.IInterceptorResponse;
    import com.esri.arcgis.enterprise.interceptor.server.IServerServicesInterceptorChain;
    import com.esri.arcgis.enterprise.interceptor.server.IServerServicesInterceptorHelper;
    import com.esri.arcgis.enterprise.interceptor.IInterceptorConfig;
    import com.esri.arcgis.enterprise.interceptor.server.IServerInterceptorLogger;
    
    import org.apache.commons.lang3.StringUtils;
    import jakarta.servlet.ServletException;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    @Interceptor(
            name = "AuditLogInterceptor",
            displayName = "Enterprise Interceptor",
            description = "Enterprise Interceptor description",
            urlPatterns = {"/services"},
            properties = {}
    
    )
    public class AuditLogInterceptor implements IServerServicesInterceptor {
        private IInterceptorConfig interceptorConfig;
        private IServerServicesInterceptorHelper interceptorHelper;
        private IServerInterceptorLogger logger;
    
    
        @Override
        public void init(IInterceptorConfig interceptorConfig,
                        IServerServicesInterceptorHelper interceptorHelper,
                        IServerInterceptorLogger logger){
            this.interceptorConfig = interceptorConfig;
            this.interceptorHelper = interceptorHelper;
            this.logger = logger;
        }
    
        @Override
        public void intercept(
                IInterceptorRequest request,
                IInterceptorResponse response,
                IServerServicesInterceptorChain filterChain) throws IOException, ServletException {
    
            if(filterChain.isRestRequestChain()){
                //To intercept the request, write your code here
                logger.info("Inside TestSOI.intercept");
                logUserAction(request, response);
                filterChain.intercept(request, response);
                //To intercept the response, write your code here
            }else{
                filterChain.intercept(request, response);
            }
        }
    
        private void logUserAction(
                IInterceptorRequest request,
                IInterceptorResponse response){
            String serviceName = interceptorHelper.getServiceName(request, response);
            String serviceType = interceptorHelper.getServiceType(request, response);
            String operationName = interceptorHelper.getOperationName(request, response);
            String username = interceptorHelper.getUsername(request, response);
    
            StringBuilder sb = new StringBuilder();
            sb.append("User '" + username + "' ");
            sb.append("accessed the service " + serviceName + "." + serviceType + ". ");
            if(!StringUtils.isEmpty(operationName))
                sb.append("Performed the operation - '" + operationName + "'.");
    
            logger.info(sb.toString());
        }
    
    }
  4. Set the displayName and description (see B).

  5. Follow these steps to build the project from the Maven tool window:

    i. On the toolbar on the right, click Maven (see C). The Maven tool window appears.

    ii. Expand AuditLogInterceptor.

    Under AuditLogInterceptor, you should see the Lifecycle, Plugins, and Dependencies sections. If you do not see the Dependencies section, click Reimport All Maven Projects on the top left of the Maven tool window (see D).

    iii. Expand Lifecycle and click install. You can also click clean and then click install for a clean build of the project.

    The project is built successfully with detailed log messages in the Run tool window (see E).

    Alternatively, follow these steps to build the project from the Terminal tool window.

    i. On the toolbar on the right, click the Terminal (see F) and ensure the directory is pointing to the project's root directory.

    ii. Run the following command to create a clean build of the project: mvn clean install.

  6. Once the build is complete, a new folder named target appears in the project's root directory, where you can find the AuditLogInterceptor.interceptor file (see G).

If you want to add third-party libraries as dependencies, you can add them to the project's pom.xml (see H). The POM contains project configuration information, including the Java compiler (JDK) and plug-ins used, as well as the project version and dependencies. To learn more about POM dependency management, see Overview of Maven integration.

Deploy the service interceptor to ArcGIS Server

To deploy the service interceptor to ArcGIS Server, upload the interceptor and then register the interceptor.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.