moesif-servlet
is a Java Servlet Filter that logs incoming API calls and sends to Moesif for API analytics and monitoring.
The SDK is implemented as a Java EE Servlet Filter without importing framework specific dependencies. Any framework built on Java Servlet API such as Spring, Struts, Jersey, etc can use this SDK with minimal configuration.
Add the Moesif dependency to your project's pom.xml file:
<!-- Include jcenter repository if you don't already have it. -->
<dependency>
<groupId>com.moesif.servlet</groupId>
<artifactId>moesif-servlet</artifactId>
<version>1.7.2</version>
</dependency>
Add the Moesif dependency to your project's build.gradle file:
dependencies {
compile 'com.moesif.servlet:moesif-servlet:1.7.2'
}
Different Java web frameworks have different way of configuring filters. Go to your specific framework's instructions below:
Your Moesif Application Id can be found in the Moesif Portal. After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps.
You can always find your Moesif Application Id at any time by logging into the Moesif Portal, click on the top right menu, and then clicking Installation.
In your Spring configuration file, install the Moesif Filter object.
import com.moesif.servlet.MoesifFilter;
import javax.servlet.Filter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.context.annotation.*;
import org.springframework.http.converter.*;
@Configuration
public class MyConfig extends WebMvcConfigurerAdapter {
@Bean
public Filter moesifFilter() {
return new MoesifFilter("Your Moesif Application Id");
}
}
To customize the filter, pass in a object that implements MoesifConfiguration
such
as MoesifConfigurationAdapter
For details regarding MoesifConfiguration
, see the configuration options.
@Configuration
public class MyConfig extends WebMvcConfigurerAdapter {
MoesifConfiguration config = new MoesifConfigurationAdapter() {
@Override
public String getSessionToken(HttpServletRequest request, HttpServletResponse response) {
return request.getHeader("Authorization");
}
};
@Bean
public Filter moesifFilter() {
return new MoesifFilter("Your Moesif Application Id", config);
}
}
In order to run this example you will need to have Java 7+ and Maven installed.
Before starting, check that your maven version is 3.0.x or above:
mvn -v
-
Clone the repository
git clone https://github.com/Moesif/moesif-servlet
cd moesif-servlet ```
-
Update MyConfig to use your own Moesif ApplicationId (Register for an account on moesif.com)
vim spring-example/src/main/java/com/moesif/servlet/spring/MyConfig.java
-
Compile the example
cd spring-example mvn clean install
-
Run spring-example (from the spring-example dir)
java -jar target/moesif-spring-example.jar
Alternatively:
mvn spring-boot:run
-
Using Postman or CURL, make a few API calls to
http://localhost:8080/api
or the port that Spring Boot is running on. -
Verify the API calls are logged to your Moesif account
import com.moesif.servlet.MoesifFilter;
import javax.servlet.Filter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Filter[] getServletFilters() {
return new Filter[]{new MoesifFilter("Your Moesif Application Id")};
}
}
In Spring MVC + XML configuration, you can register the filters via web.xml
In web.xml
file:
<filter>
<filter-name>MoesifFilter</filter-name>
<filter-class>com.moesif.servlet.MoesifFilter</filter-class>
<init-param>
<param-name>application-id</param-name>
<param-value>Your Moesif Application Id</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>logBody</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MoesifFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
You may have to override onStartup()
to pass in the MoesifConfiguration object.
There are multiple ways to run Jersey, as a Java Servlet or embedded with a Java NIO framework like Grizzly. This subsection focuses on running Jersey as a Servlet.
Edit the web.xml file to add your Moesif Application Id that you obtained from your Moesif Account.
<filter>
<filter-name>MoesifFilter</filter-name>
<filter-class>com.moesif.servlet.MoesifFilter</filter-class>
<init-param>
<param-name>application-id</param-name>
<param-value>Your Moesif Application Id</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>logBody</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MoesifFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
In order to run this example you will need to have Java 7+ and Maven installed.
Before starting, check that your maven version is 3.0.x or above:
mvn -v
-
Clone the repository
git clone https://github.com/Moesif/moesif-servlet
cd moesif-servlet ```
-
Update web.xml to use your own Moesif ApplicationId (Register for an account on moesif.com)
vim jersey-servlet-example/src/main/webapp/WEB-INF/web.xml
-
Run jersey-servlet-example
cd jersey-servlet-example mvn tomcat7:run
-
Go to
http://localhost:3099/api/demo
or the port that Tomcat is running on.
In your Moesif Account, you should see event logged and monitored.
Shut it down manually with Ctrl-C.
There are multiple ways to run Spark, as a Java Servlet or embedded with a server like Jetty. This subsection focuses on running Spark as a Servlet.
Edit the web.xml file to add your Moesif Application Id that you obtained from your Moesif Account.
<filter>
<filter-name>MoesifFilter</filter-name>
<filter-class>com.moesif.servlet.MoesifFilter</filter-class>
<init-param>
<param-name>application-id</param-name>
<param-value>Your Moesif Application Id</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>logBody</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MoesifFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
In order to run this example you will need to have Java 8+ and Maven installed.
Before starting, check that your maven version is 3.0.x or above:
mvn -v
-
Clone the repository
git clone https://github.com/Moesif/moesif-servlet
cd moesif-servlet ```
-
Update web.xml to use your own Moesif ApplicationId (Register for an account on moesif.com)
vim spark-servlet-example/src/main/webapp/WEB-INF/web.xml
-
Run spark-servlet-example
cd spark-servlet-example mvn tomcat7:run
-
Go to
http://localhost:3099/api/demo
or the port that Tomcat is running on.
In your Moesif Account, you should see event logged and monitored.
Shut it down manually with Ctrl-C.
Edit the web.xml file to add your Moesif Application Id that you obtained from your Moesif Account.
<filter>
<filter-name>MoesifFilter</filter-name>
<filter-class>com.moesif.servlet.MoesifFilter</filter-class>
<init-param>
<param-name>application-id</param-name>
<param-value>Your Moesif Application Id</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>logBody</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MoesifFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
This example implements the Servlet Filter directly in a generic Servlet app rather than using a higher level framework like Spring MVC or Spring Boot.
In order to run this example you will need to have Java 7+ and Maven installed.
Before starting, check that your maven version is 3.0.x or above:
mvn -v
-
Clone the repository
git clone https://github.com/Moesif/moesif-servlet
cd moesif-servlet ```
-
Update web.xml to use your own Moesif ApplicationId (Register for an account on moesif.com)
vim servlet-example/src/main/webapp/WEB-INF/web.xml
-
Run servlet-example
cd servlet-example mvn tomcat7:run
-
Go to
http://localhost:3099/api/demo
or the port that Tomcat is running on.
In your Moesif Account, you should see event logged and monitored.
Shut it down manually with Ctrl-C.
To configure the filter, extend the MoesifConfigurationAdapter
class to override a few config params or implement the entire
MoesifConfiguration
interface.
Both will achieve similar results.
Override the following parameters, if needed.
Name | Required | Type & Default Value | Description |
---|---|---|---|
batchSize | False | Type: number Default 100 |
BatchSize of API events that will trigger flushing of queue and sending the data to Moesif. |
batchMaxTime | False | Type: number in seconds Default 5 . |
This is the maximum wait time (approximately) before triggering flushing of the queue and sending to Moesif. |
queueSize | False | Type: number Default 5000 |
Maximum queue capacity to hold events in memory. |
retry | False | Type: number Default: 0 |
Number of time to retry if fails to post to Moesif. If set, must be a number between 0 to 3. |
updateConfigTime | False | Type: number Default: 300 |
This is the maximum wait time (approximately) to pull the latest app config and update the cache. |
Override following methods, if needed.
Return true
if you want to skip logging a
request to Moesif i.e. to skip boring requests like health probes.
@Override
public boolean skip(HttpServletRequest request, HttpServletResponse response) {
// Skip logging health probes
return request.getRequestURI().contains("health/probe");
}
Return a Java Object that allows you to add custom metadata to the event like instanceId or traceId. The metadata must be a simple Java object that can be converted to JSON.
public Object getMetadata(HttpRequest request, ClientHttpResponse response) {
Map<String, Object> customMetadata = new HashMap<String, Object>();
customMetadata.put("service_name", System.getProperty("app_name"));
return customMetadata;
}
Highly recommended. Returns a userId as a String.
This enables Moesif to attribute API requests to individual users so you can understand who calling your API.
This can be used simultaneously with identifyCompany
to track both individual customers and the companies that they are a part of.
@Override
public String identifyUser(HttpServletRequest request, HttpServletResponse response) {
if (request.getUserPrincipal() == null) {
return null;
}
return request.getUserPrincipal().getName();
}
Returns a companyId as a String. If your business is B2B, this enables Moesif to attribute API requests to specific companies or organizations so you can understand which accounts are calling your API. This can be used simultaneously with identifyUser to track both individual customers and the companies their a part of.
@Override
public String identifyCompany(HttpServletRequest request, HttpServletResponse response) {
return "12345";
}
Moesif automatically detects the end user's session token or API key, but you can manually define the token for finer control.
@Override
public String getSessionToken(HttpServletRequest request, HttpServletResponse response) {
return request.getHeader("Authorization");
}
A second example if want to use the session id
@Override
public String getSessionToken(HttpServletRequest request, HttpServletResponse response) {
return request.getRequestedSessionId();
}
Returns a String to tag requests with a specific version of your API.
@Override
public String getApiVersion(HttpServletRequest request, HttpServletResponse response) {
return request.getHeader("X-Api-Version");
}
If you want to remove any sensitive data in the HTTP headers or body before sending to Moesif, you can do so with maskContent
If you are contributing to moesif-servlet, you can build it locally and install in local Maven Repo:
cd moesif-servlet
mvn clean install
The below methods to update user and company are accessible via the Moesif Java API lib which Moesif Play Filter already imports as a dependency.
Create or update a user profile in Moesif.
The metadata field can be any customer demographic or other info you want to store.
Only the userId
field is required.
This method is a convenient helper that calls the Moesif API lib.
For details, visit the Java API Reference.
MoesifFilter filter = new MoesifFilter("Your Moesif Application Id", new MoesifConfiguration());
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#users for campaign schema
CampaignModel campaign = new CampaignBuilder()
.utmSource("google")
.utmCampaign("cpc")
.utmMedium("adwords")
.utmTerm("api+tooling")
.utmContent("landing")
.build();
// Only userId is required
// metadata can be any custom object
UserModel user = new UserBuilder()
.userId("12345")
.companyId("67890") // If set, associate user with a company object
.campaign(campaign)
.metadata(APIHelper.deserialize("{" +
"\"email\": \"johndoe@acmeinc.com\"," +
"\"first_name\": \"John\"," +
"\"last_name\": \"Doe\"," +
"\"title\": \"Software Engineer\"," +
"\"sales_info\": {" +
"\"stage\": \"Customer\"," +
"\"lifetime_value\": 24000," +
"\"account_owner\": \"mary@contoso.com\"" +
"}" +
"}"))
.build();
filter.updateUser(user);
Similar to UpdateUser, but used to update a list of users in one batch.
Only the userId
field is required.
This method is a convenient helper that calls the Moesif API lib.
For details, visit the Java API Reference.
You can update users synchronously or asynchronously on a background thread. Unless you require synchronous behavior, we recommend the async versions.
MoesifFilter filter = new MoesifFilter("Your Moesif Application Id", new MoesifConfiguration());
List<UserModel> users = new ArrayList<UserModel>();
UserModel userA = new UserBuilder()
.userId("12345")
.companyId("67890")
.campaign(campaign)
.metadata(APIHelper.deserialize("{" +
"\"email\": \"johndoe@acmeinc.com\"," +
"\"first_name\": \"John\"," +
"\"last_name\": \"Doe\"," +
"\"title\": \"Software Engineer\"," +
"\"sales_info\": {" +
"\"stage\": \"Customer\"," +
"\"lifetime_value\": 24000," +
"\"account_owner\": \"mary@contoso.com\"" +
"}" +
"}"))
.build();
users.add(userA);
UserModel userB = new UserBuilder()
.userId("54321")
.companyId("67890")
.campaign(campaign)
.metadata(APIHelper.deserialize("{" +
"\"email\": \"johndoe@acmeinc.com\"," +
"\"first_name\": \"John\"," +
"\"last_name\": \"Doe\"," +
"\"title\": \"Software Engineer\"," +
"\"sales_info\": {" +
"\"stage\": \"Customer\"," +
"\"lifetime_value\": 24000," +
"\"account_owner\": \"mary@contoso.com\"" +
"}" +
"}"))
.build();
users.add(userB);
filter.updateUsersBatch(users, callBack);
Create or update a company profile in Moesif.
The metadata field can be any company demographic or other info you want to store.
Only the company_id
field is required.
This method is a convenient helper that calls the Moesif API lib.
For details, visit the Java API Reference.
MoesifFilter filter = new MoesifFilter("Your Moesif Application Id", new MoesifConfiguration());
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#update-a-company for campaign schema
CampaignModel campaign = new CampaignBuilder()
.utmSource("google")
.utmCampaign("cpc")
.utmMedium("adwords")
.utmTerm("api+tooling")
.utmContent("landing")
.build();
// Only companyId is required
// metadata can be any custom object
CompanyModel company = new CompanyBuilder()
.companyId("67890")
.companyDomain("acmeinc.com") // If set, Moesif will enrich your profiles with publicly available info
.campaign(campaign)
.metadata(APIHelper.deserialize("{" +
"\"org_name\": \"Acme, Inc\"," +
"\"plan_name\": \"Free\"," +
"\"deal_stage\": \"Lead\"," +
"\"mrr\": 24000," +
"\"demographics\": {" +
"\"alexa_ranking\": 500000," +
"\"employee_count\": 47" +
"}" +
"}"))
.build();
filter.updateCompany(company);
Similar to updateCompany, but used to update a list of companies in one batch.
Only the company_id
field is required.
This method is a convenient helper that calls the Moesif API lib.
For details, visit the Java API Reference.
You can update users synchronously or asynchronously on a background thread. Unless you require synchronous behavior, we recommend the async versions.
MoesifFilter filter = new MoesifFilter("Your Moesif Application Id", new MoesifConfiguration());
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#update-a-company for campaign schema
CampaignModel campaign = new CampaignBuilder()
.utmSource("google")
.utmCampaign("cpc")
.utmMedium("adwords")
.utmTerm("api+tooling")
.utmContent("landing")
.build();
// Only companyId is required
// metadata can be any custom object
CompanyModel company = new CompanyBuilder()
.companyId("67890")
.companyDomain("acmeinc.com") // If set, Moesif will enrich your profiles with publicly available info
.campaign(campaign)
.metadata(APIHelper.deserialize("{" +
"\"org_name\": \"Acme, Inc\"," +
"\"plan_name\": \"Free\"," +
"\"deal_stage\": \"Lead\"," +
"\"mrr\": 24000," +
"\"demographics\": {" +
"\"alexa_ranking\": 500000," +
"\"employee_count\": 47" +
"}" +
"}"))
.build();
filter.updateCompaniesBatch(companies);
- Manually clone the git repo
- Invoke
mvn clean install -U -Dgpg.skip
if you haven't done so. - Add your own application id to 'src/test/java/com/moesif/servlet/MoesifServletTests.java'. You can find your Moesif Application Id from Moesif Dashboard -> Top Right Menu -> Installation
- From terminal/cmd navigate to the root directory of the moesif-servlet.
- Invoke
mvn -Dtest=MoesifServletTests test
to run the tests.
To view more documentation on integration options, please visit the Integration Options Documentation.