Skip to content

Commit

Permalink
Merge pull request #242 from rollbar/add-struts2-integration
Browse files Browse the repository at this point in the history
Add struts2 integration with examples
  • Loading branch information
basoko authored Dec 9, 2020
2 parents d834a1e + 23a9499 commit 1ad54e2
Show file tree
Hide file tree
Showing 17 changed files with 586 additions and 1 deletion.
29 changes: 29 additions & 0 deletions examples/rollbar-struts2-spring/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
buildscript {
repositories {
jcenter()
}

dependencies {
classpath 'com.bmuschko:gradle-tomcat-plugin:2.3'
}
}

apply plugin: 'war'
apply plugin: 'com.bmuschko.tomcat'

dependencies {
implementation project(":rollbar-struts2")

implementation 'javax.servlet:javax.servlet-api:3.1.0'
implementation 'org.apache.struts:struts2-core:2.5.25'
implementation 'org.apache.struts:struts2-spring-plugin:2.5.25'

def tomcatVersion = '7.0.57'

tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}")
}

tomcatRun.contextPath = '/'
tomcatRunWar.contextPath = '/'
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.rollbar.struts2.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Inject;
import com.rollbar.notifier.Rollbar;
import com.rollbar.struts.RollbarFactory;
import java.util.concurrent.atomic.AtomicInteger;


/**
* Struts2 action with Spring CDI.
*/
public class HelloRollbarAction extends ActionSupport {

private static final AtomicInteger counter = new AtomicInteger(1);

private final Rollbar rollbar;

@Inject
public HelloRollbarAction(RollbarFactory rollbarFactory) {
this.rollbar = rollbarFactory.build();
}

public String index() {
rollbar.info("Executing index action in HelloRollbarAction");

return SUCCESS;
}

public String hello() {
int current = counter.getAndAdd(1);
if (current % 2 == 0) {
throw new RuntimeException("Fatal error at hello rollbar action. Number: " + current);
}
return SUCCESS;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
<bean id="rollbarFactory" class="com.rollbar.struts.RollbarFactory" scope="singleton" >
<property name="accessToken" value="[ACCESS_TOKEN]" />
<property name="captureIp" value="true" />
<property name="environment" value="production" />
<property name="codeVersion" value="1.1.2" />
</bean>
</beans>

31 changes: 31 additions & 0 deletions examples/rollbar-struts2-spring/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true" />

<package name="basicstruts2" extends="struts-default" namespace="/">
<interceptors>
<interceptor name="rollbarExceptionInterceptor"
class="com.rollbar.struts.interceptor.RollbarExceptionInterceptor" />

<interceptor-stack name="appStack">
<interceptor-ref name="rollbarExceptionInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="appStack"/>

<default-action-ref name="index" />

<action name="index" class="com.example.rollbar.struts2.action.HelloRollbarAction" method="index">
<result type="chain">hello</result>
</action>

<action name="hello" class="com.example.rollbar.struts2.action.HelloRollbarAction" method="hello">
<result>/WEB-INF/jsp/index.jsp</result>
</action>

</package>
</struts>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@taglib prefix="s" uri="/struts-tags" %>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Hello Rollbar struts2 example application</title>
<s:head />
</head>
<body>
Hello Rollbar!
</body>
</html>

29 changes: 29 additions & 0 deletions examples/rollbar-struts2-spring/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Rollbar listener and filter -->
<listener>
<listener-class>com.rollbar.web.listener.RollbarRequestListener</listener-class>
</listener>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
28 changes: 28 additions & 0 deletions examples/rollbar-struts2/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
buildscript {
repositories {
jcenter()
}

dependencies {
classpath 'com.bmuschko:gradle-tomcat-plugin:2.3'
}
}

apply plugin: 'war'
apply plugin: 'com.bmuschko.tomcat'

dependencies {
implementation project(":rollbar-struts2")

implementation 'org.apache.struts:struts2-core:2.5.25'
implementation 'javax.servlet:javax.servlet-api:3.1.0'

def tomcatVersion = '7.0.57'

tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}")
}

tomcatRun.contextPath = '/'
tomcatRunWar.contextPath = '/'
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.rollbar.struts2.action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.concurrent.atomic.AtomicInteger;


/**
* Struts2 action that rises an error every even request number.
*/
public class HelloRollbarAction extends ActionSupport {

private static final AtomicInteger counter = new AtomicInteger(1);

public String execute() {
int current = counter.getAndAdd(1);
if (current % 2 == 0) {
throw new RuntimeException("Fatal error at hello rollbar action. Number: " + current);
}
return SUCCESS;
}


}
34 changes: 34 additions & 0 deletions examples/rollbar-struts2/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true" />

<package name="basicstruts2" extends="struts-default" namespace="/">

<interceptors>
<interceptor name="rollbarExceptionInterceptor"
class="com.rollbar.struts.interceptor.RollbarExceptionInterceptor">
<param name="accessToken">[ACCESS_TOKEN]</param>
<param name="captureIp">true</param>
<param name="environment">production</param>
<param name="codeVersion">1.1.2</param>
</interceptor>

<interceptor-stack name="appStack">
<interceptor-ref name="rollbarExceptionInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>

<default-interceptor-ref name="appStack"/>

<default-action-ref name="index" />

<action name="index" class="com.example.rollbar.struts2.action.HelloRollbarAction" method="execute">
<result>/WEB-INF/jsp/index.jsp</result>
</action>

</package>
</struts>
16 changes: 16 additions & 0 deletions examples/rollbar-struts2/src/main/webapp/WEB-INF/jsp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@taglib prefix="s" uri="/struts-tags" %>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Hello Rollbar struts2 example application</title>
<s:head />
</head>
<body>
Hello Rollbar!
</body>
</html>

20 changes: 20 additions & 0 deletions examples/rollbar-struts2/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<!-- Rollbar listener and filter -->
<listener>
<listener-class>com.rollbar.web.listener.RollbarRequestListener</listener-class>
</listener>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
8 changes: 8 additions & 0 deletions rollbar-struts2/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ext {
struts2Version = '2.5.25'
}

dependencies {
api project(":rollbar-web")
implementation 'org.apache.struts:struts2-core:' + struts2Version
}
Loading

0 comments on commit 1ad54e2

Please sign in to comment.