Skip to content

Build a service interceptor in Eclipse

This walk-through demonstrates how to create, build, and deploy a service interceptor using Eclipse. 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 Eclipse, complete the following steps:

  1. Open Eclipse and click File > New > Maven Project. The New Maven Project window appears.

  2. Uncheck Create a simple project (skip archetype selection) and click Next.

  3. If the server-services-interceptor-archetype has not been added, click Add Archetype, and in the Add Archetype window that appears, 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.

    Note: If the server-services-interceptor-archetype has been added, you can skip this step.

    Create the service interceptor project

    The server-services-interceptor-archetype has been added. Click this arhectype and click Next.

  4. Provide the following archetype parameter values:

    • Enter entsdksamples in the GroupId field.
    • Enter AuditLogInterceptor in the ArtifactId field.
    • Enter entsdksamples in the Package field.
    • Enter AuditLogInterceptor as the value for interceptorName.
    Archetype parameter values

    The service interceptor project has been created, and the AuditLogInterceptor class has been generated in the \src\main\java folder.

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 Build:

    i. Right-click the project and click Run As > Run Configuration. The Run configurations window appears.

    ii. Expand Maven Build and click New_configuration.

    iii. Click Workspace to set the current project as the workspace.

    iv. Enter the following command in the Goals field: clean install.

    v. Click Apply and click Run.

    The project is built successfully with detailed log messages in the Console. (see C).

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

    i. Click Terminal (see D). If you can't find the Terminal tab, add it from Window > Show View > Other > Terminal (see E).

    ii. Click Open a Terminal 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 F).

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.