Permalink
Browse files

J2EE / servlet Java probe implementation based on web.xml / error-pag…

…e tag (#6)
  • Loading branch information...
dcieslak committed Oct 18, 2013
1 parent d10ca98 commit 07d652f89ba01cb7a48dad13ac12de35617c4c62
View
@@ -1,3 +1,5 @@
classes
*.class
var
doc/diagrams/*.png
*.o
View
@@ -1,11 +1,13 @@
all: build src/server/var test
default: build
build:
make -C src/probes/c
make -C src/probes/java
test:
test: src/server/var
make -C src/probes/c test
make -C src/probes/php test
make -C src/probes/java test
wget -qO - $$RANDOMTEST_URL
src/server/var:
@@ -15,7 +17,13 @@ src/server/var:
clean:
make -C src/probes/c clean
make -C src/probes/java clean
rm -f src/server/var/*
sleep:
sleep 3
all: clean build sleep test
-include local.mak
View
@@ -0,0 +1,14 @@
Web resources that helped a bit with probes:
Servlets - Exception Handling
http://www.tutorialspoint.com/servlets/servlets-exception-handling.htm
Web.xml Servlet Configuration
http://tutorials.jenkov.com/java-servlets/web-xml.html#initParams
Java - sending HTTP parameters via POST method easily
http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily
PrintWriter for creating Strings
http://www.jroller.com/ethdsy/entry/printwriter_for_creating_strings
@@ -0,0 +1,19 @@
package com.randomtest.test;
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
// Extend HttpServlet class
public class ExceptionTest extends HttpServlet
{
// Method to handle GET method request.
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throw new ServletException("Exception details");
}
}
View
@@ -0,0 +1,22 @@
WAR_DEPLOY_DIR=$(JBOSS_HOME)/server/default/deploy/randomtest1.war
all:
mkdir -p classes
javac -source 1.6 -target 1.6 \
-cp $(JBOSS_HOME)/client/jboss-servlet-*_spec.jar -d classes \
*.java
rm -rf $(WAR_DEPLOY_DIR)
mkdir -p $(WAR_DEPLOY_DIR)/WEB-INF
cp -r classes $(WAR_DEPLOY_DIR)/WEB-INF/
sed 's!%RANDOMTEST_URL%!$(RANDOMTEST_URL)!' web.xml > $(WAR_DEPLOY_DIR)/WEB-INF/web.xml
test:
wget -qO - http://localhost:8080/randomtest1/ExceptionTest || true
WEB-INF/classes/com/randomtest/RandomTestErrorHandler.class: RandomTestErrorHandler.java
clean:
rm -rf WEB-INF/classes
rm -rf $(JBOSS_HOME)/server/default/deploy/randomtest1.war
@@ -0,0 +1,90 @@
package com.randomtest;
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
// Extend HttpServlet class
public class RandomTestErrorHandler extends HttpServlet
{
private String RANDOMTEST_URL;
public void init(ServletConfig servletConfig) throws ServletException{
RANDOMTEST_URL = servletConfig.getInitParameter("RANDOMTEST_URL");
if (RANDOMTEST_URL == null) {
throw new ServletException("Missing RANDOMTEST_URL servlet parameter");
}
}
// Method to handle GET method request.
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
assert RANDOMTEST_URL != null;
// Analyze the servlet exception
Throwable throwable = (Throwable)
request.getAttribute ("javax.servlet.error.exception");
Integer statusCode = (Integer)
request.getAttribute ("javax.servlet.error.status_code");
String servletName = (String)
request.getAttribute ("javax.servlet.error.servlet_name");
if (servletName == null) {
servletName = "Unknown";
}
String requestUri = (String) request.getAttribute ("javax.servlet.error.request_uri");
if (requestUri == null) {
requestUri = "Unknown";
}
// Set response content type
response.setContentType ("text/plain");
if (throwable != null) {
StringWriter str = new StringWriter();
throwable.printStackTrace(new PrintWriter(str));
URL url = new URL(RANDOMTEST_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes("stacktrace=" + URLEncoder.encode(str.toString()));
wr.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
wr.close();
connection.disconnect();
PrintWriter out = response.getWriter ();
out.println ("Error information");
out.println ("RANDOMTEST_URL = " + RANDOMTEST_URL);
out.println ("Servlet Name : " + servletName + "</br></br>");
out.println ("Exception Type : " +
throwable.getClass ().getName () + "</br></br>");
out.println ("The request URI: " + requestUri + "<br><br>");
out.println ("The exception message: " + throwable.getMessage ());
throwable.printStackTrace(out);
}
}
// Method to handle POST method request.
public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet (request, response);
}
}
View
@@ -0,0 +1,44 @@
<?xml version="1.0"?>
<web-app>
<!-- sample servlets -->
<servlet>
<display-name>ExceptionTest</display-name>
<servlet-name>ExceptionTest</servlet-name>
<servlet-class>com.randomtest.test.ExceptionTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExceptionTest</servlet-name>
<url-pattern>/ExceptionTest</url-pattern>
</servlet-mapping>
<!-- randomtest infrastructure -->
<servlet>
<display-name>RandomTestErrorHandler</display-name>
<servlet-name>RandomTestErrorHandler</servlet-name>
<servlet-class>com.randomtest.RandomTestErrorHandler</servlet-class>
<init-param>
<param-name>RANDOMTEST_URL</param-name>
<param-value>%RANDOMTEST_URL%</param-value>
</init-param>
<!-- To show any config problems instantly -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RandomTestErrorHandler</servlet-name>
<url-pattern>/RandomTestErrorHandler</url-pattern>
</servlet-mapping>
<!-- Error types to be handled by randomtest -->
<error-page>
<error-code>404</error-code>
<location>/RandomTestErrorHandler</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type >
<location>/RandomTestErrorHandler</location>
</error-page>
</web-app>

0 comments on commit 07d652f

Please sign in to comment.