Skip to content

Commit

Permalink
Fix: Remove catalina dependency, work with JSP
Browse files Browse the repository at this point in the history
1. Remove dependency on catalina
2. Ensure works with JSP and string writer
3. Remove Java 1.8 Code
4. Set Principle by default
  • Loading branch information
dgilling committed Aug 6, 2019
1 parent eb46fd6 commit eaf23c4
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 8,636 deletions.
51 changes: 6 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@

## Introduction

`moesif-servlet` is a Java SDK that logs API calls and sends to [Moesif](https://www.moesif.com) for API analytics and log analysis.
`moesif-servlet` is a Java Servlet Filter that logs API calls and sends to [Moesif](https://www.moesif.com) for API analytics and log analysis.

The SDK is implemented as a Java EE [Servlet Filter](https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/Filter.html)
without importing large framework specific dependencies.
Many frameworks are built on top of the Servlet API such as Spring, Apache Struts, Jersey, etc.

If you're using a web framework that is built on the
[Servlet API](https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/Servlet.html)
such as Spring Boot, Spring MVC, Jersey, and Apache Struts, then you can enable this SDK with minimal configuration.
without importing framework specific dependencies. Any framework built on Java [Servlet API](https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/Servlet.html)
such as Spring, Struts, Jersey, etc can use this SDK with minimal configuration.

[Source Code on GitHub](https://github.com/moesif/moesif-servlet)

Expand All @@ -37,7 +33,7 @@ Add the Moesif dependency to your project's pom.xml file:
<dependency>
<groupId>com.moesif.servlet</groupId>
<artifactId>moesif-servlet</artifactId>
<version>1.5.9</version>
<version>1.6.0</version>
</dependency>
```

Expand All @@ -52,7 +48,7 @@ repositories {
}
dependencies {
compile 'com.moesif.servlet:moesif-servlet:1.5.9'
compile 'com.moesif.servlet:moesif-servlet:1.6.0'
}
```

Expand Down Expand Up @@ -169,7 +165,7 @@ mvn -v
```


5. Go to `http://localhost:8080/greeting` or the port that Spring Boot is running on.
5. Go to `http://localhost:8080/api` or the port that Spring Boot is running on.

### Spring MVC (Java Config)

Expand Down Expand Up @@ -225,41 +221,6 @@ In `web.xml` file:
```
You may have to override `onStartup()` to pass in the MoesifConfiguration object.

#### Running the Spring MVC example

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:

```sh
mvn -v
```

1. Clone the repository

```sh
git clone https://github.com/Moesif/moesif-servlet
cd moesif-servlet
```

2. Update MyConfig to use your own Moesif ApplicationId
(Register for an account on [moesif.com](https://www.moesif.com))

```sh
vim spring-mvc-example/src/main/webapp/WEB-INF/web.xml
```

3. Run spring-mvc-example

```sh
cd spring-mvc-example
mvn jetty:run
```

4. Go to `http://localhost:8080/api/json`. In your Moesif Account, you should see event logged and monitored.

Shut it down manually with Ctrl-C.

### Jersey Servlet

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.
Expand Down
10 changes: 2 additions & 8 deletions moesif-servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.moesif.servlet</groupId>
<artifactId>moesif-servlet</artifactId>
<version>1.5.9</version>
<version>1.6.0</version>
<packaging>jar</packaging>
<name>moesif-servlet</name>
<description>Moesif SDK for Java Servlet to log and analyze API calls</description>
Expand All @@ -24,7 +24,7 @@
<developers>
<developer>
<id>moesif</id>
<name>Moesif API Insights</name>
<name>Moesif API Analytics</name>
<email>[email protected]</email>
<url>https://www.moesif.com</url>
<organization>Moesif, Inc.</organization>
Expand Down Expand Up @@ -78,12 +78,6 @@
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>catalina</artifactId>
<version>6.0.16</version>
</dependency>


<!-- testing -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@

public class MoesifConfiguration {

public boolean skip(HttpServletRequest request, HttpServletResponse response) {
return false;
}

public EventModel maskContent(EventModel eventModel) {
return eventModel;
}

public String identifyUser(HttpServletRequest request, HttpServletResponse response) {
return null;
try {
if (request.getUserPrincipal() == null) {
return null;
}
return request.getUserPrincipal().getName();
} catch (Exception e) {
return null;
}
}

public String identifyCompany(HttpServletRequest request, HttpServletResponse response) {
Expand All @@ -26,11 +25,6 @@ public String getSessionToken(HttpServletRequest request, HttpServletResponse re
return null;
}

@Deprecated
public String getTags(HttpServletRequest request, HttpServletResponse response) {
return null;
}

public String getApiVersion(HttpServletRequest request, HttpServletResponse response) {
return null;
}
Expand All @@ -39,5 +33,18 @@ public Object getMetadata(HttpServletRequest request, HttpServletResponse respon
return null;
}

public boolean skip(HttpServletRequest request, HttpServletResponse response) {
return false;
}

public EventModel maskContent(EventModel eventModel) {
return eventModel;
}

public boolean disableTransactionId = false;

@Deprecated
public String getTags(HttpServletRequest request, HttpServletResponse response) {
return null;
}
}
70 changes: 25 additions & 45 deletions moesif-servlet/src/main/java/com/moesif/servlet/MoesifFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.moesif.servlet.wrappers.LoggingHttpServletRequestWrapper;
import com.moesif.servlet.wrappers.LoggingHttpServletResponseWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.catalina.connector.ResponseFacade;

public class MoesifFilter implements Filter {

Expand Down Expand Up @@ -214,25 +213,17 @@ public int getAppConfig(String cachedConfigEtag) throws Throwable {
try {
Map<String, Object> appConfig = this.configDict.get(responseConfigEtag);
// Get the sample rate and update last updated time
if (!appConfig.isEmpty()) {
sampleRate = (int) appConfig.getOrDefault("sample_rate", 100);
this.lastUpdatedTime = new Date();
if (!appConfig.isEmpty() && appConfig.containsKey("sample_rate")) {
sampleRate = (Integer) appConfig.get("sample_rate");
}
else {
// Upate last updated time
this.lastUpdatedTime = new Date();
}
}
catch(Exception e) {
// Upate last updated time
this.lastUpdatedTime = new Date();
}
} catch(Exception e) {
logger.warning("getConfig() call failed " + e.toString());
}
} catch(Exception e) {
// Upate last updated time
logger.warning("getConfig call failed " + e.toString());
this.lastUpdatedTime = new Date();
logger.warning("getConfig() call failed " + e.toString());
}
return sampleRate;
this.lastUpdatedTime = new Date();
return sampleRate;
}

public void updateUser(UserModel userModel) throws Throwable{
Expand Down Expand Up @@ -384,35 +375,24 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
startDate, config.getApiVersion(httpRequest, httpResponse), transactionId);

// pass to next step in the chain.
filterChain.doFilter(requestWrapper, responseWrapper);

if(!(responseWrapper.delegate instanceof ResponseFacade)) {

if (responseWrapper.getHeaders() != null &&
!responseWrapper.getHeaders().isEmpty() &&
responseWrapper.getHeaders().get("Content-Type").toLowerCase().contains("html")) {
if (debug) {
logger.warning("MoesifFilter was called for html response, skipping send Event to Moesif");
}
filterChain.doFilter(request, response);
return;
}
try {
filterChain.doFilter(requestWrapper, responseWrapper);
} finally {
Date endDate = new Date();
EventResponseModel eventResponseModel = getEventResponseModel(responseWrapper, endDate);

if (!(responseWrapper.getResponse() instanceof LoggingHttpServletResponseWrapper)) {
sendEvent(
eventRequestModel,
eventResponseModel,
config.identifyUser(httpRequest, httpResponse),
config.identifyCompany(httpRequest, httpResponse),
config.getSessionToken(httpRequest, httpResponse),
config.getTags(httpRequest, httpResponse),
config.getMetadata(httpRequest, httpResponse)
);
}
}

Date endDate = new Date();
EventResponseModel eventResponseModel = getEventResponseModel(responseWrapper, endDate);

httpResponse.getOutputStream().write(responseWrapper.getContentAsBytes());

sendEvent(
eventRequestModel,
eventResponseModel,
config.identifyUser(httpRequest, httpResponse),
config.identifyCompany(httpRequest, httpResponse),
config.getSessionToken(httpRequest, httpResponse),
config.getTags(httpRequest, httpResponse),
config.getMetadata(httpRequest, httpResponse)
);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public class LoggingHttpServletRequestWrapper extends HttpServletRequestWrapper {

Expand Down Expand Up @@ -111,7 +109,7 @@ public String getContent() {

private byte[] getContentFromParameterMap(Map<String, String[]> parameterMap) {

List<String> result = new ArrayList<>();
List<String> result = new ArrayList<String>();
for (Map.Entry<String, String[]> e: parameterMap.entrySet()) {
String[] value = e.getValue();
result.add(e.getKey() + "=" + (value.length == 1 ? value[0] : Arrays.toString(value)));
Expand Down
Loading

0 comments on commit eaf23c4

Please sign in to comment.