Search code examples
javaloggingheartbeat

Prevent HeartbeatServlet from writing nullpointer exception to log


I have the following code:

HeartbeatServlet.java:

public final void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession(false);
    response.setContentType("text/xml");
    response.setHeader("Cache-Control", "no-cache");

    if (session != null) {
        compAjaxResponse ajaxResponse = new compAjaxResponse();

        ajaxResponse.addAction(new JavascriptAction("sendNextBeat", "setTimeout(\"sendHeartBeat()\"," + MILLIS_PER_SECOND * HEARTBEAT_INTERVAL + ")", true));

        if (CSGNotificationTracker.hasNotificationChanged()) {
            ajaxResponse.addAction(new InnerHtmlAjaxAction("importantMessage", CSGNotificationTracker.getNotificationHtml()));
        }

        response.getWriter().write(ajaxResponse.getResponse(request));
    }

    return;
}

GZIPResponseWrapper.java:

public class GZIPResponseWrapper extends HttpServletResponseWrapper {
protected HttpServletResponse origResponse = null;
protected ServletOutputStream stream = null;
protected PrintWriter writer = null;

public GZIPResponseWrapper(HttpServletResponse response) {
  super(response);
  origResponse = response;
}

public ServletOutputStream createOutputStream() throws IOException {
  return (new GZIPResponseStream(origResponse));
}

public void finishResponse() {
  try {
    if (writer != null) {
      writer.close();
    } else {
      if (stream != null) {
        stream.close();
      }
    }
  } catch (IOException e) {}
}

public void flushBuffer() throws IOException {
  stream.flush();
}

public ServletOutputStream getOutputStream() throws IOException {
  if (writer != null) {
    throw new IllegalStateException("getWriter() has already been 
called!");
  }

  if (stream == null)
    stream = createOutputStream();
  return (stream);
}

public PrintWriter getWriter() throws IOException {
  if (writer != null) {
    return (writer);
  }

  if (stream != null) {
    throw new IllegalStateException("getOutputStream() has already been 
called!");
  }

 stream = createOutputStream();
 writer = new PrintWriter(new OutputStreamWriter(stream, "UTF-8"));
 return (writer);
}

public void setContentLength(int length) {}
}

When a user exits the browser without logging out, we get multiple heartbeat nullpointer exceptions written to the log.

Exception:

[7/14/17 9:20:46:326 EDT] 000001e8 webapp        E 
com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet 
Error]-[compHeartbeatServlet]: java.lang.NullPointerException
2017-07-14 11:03:09.340 |   at 
com.csg.service.performance.gzip.GZIPResponseWrapper.flushBuffer 
(GZIPResponseWrapper.java:39)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest 
(ServletWrapper.java:816)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest 
(ServletWrapper.java:480)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest 
(ServletWrapperImpl.java:178)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget 
(WebAppFilterChain.java:136)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter 
(WebAppFilterChain.java:97)
2017-07-14 11:03:09.340 |   at 
com.comp.compservice.security.xss.XssFilter.doFilter(XssFilter.java:33)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter 
(FilterInstanceWrapper.java:195)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter 
(WebAppFilterChain.java:91)
2017-07-14 11:03:09.340 |   at 
com.csg.service.performance.gzip.GZIPFilter.doFilter(GZIPFilter.java:32)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter 
(FilterInstanceWrapper.java:195)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter 
(WebAppFilterChain.java:91)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter 
(WebAppFilterManager.java:967)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters 
(WebAppFilterManager.java:1107)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest 
(CacheServletWrapper.java:87)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:940)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.WSWebContainer.handleRequest 
(WSWebContainer.java:1817)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.channel.WCChannelLink.ready   
(WCChannelLink.java:200)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination 
(HttpInboundLink.java:463)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest 
(HttpInboundLink.java:530)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest 
(HttpInboundLink.java:316)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete 
(HttpICLReadCallback.java:88)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.ssl.channel.impl.SSLReadServiceContext   
$SSLReadCompletedCallback.complete(SSLReadServiceContext.java:1820)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted 
(AioReadCompletionListener.java:175)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.AbstractAsyncFuture.invokeCallback 
(AbstractAsyncFuture.java:217)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.AsyncChannelFuture.fireCompletionActions 
(AsyncChannelFuture.java:161)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.ResultHandler.runEventProcessingLoop 
(ResultHandler.java:775)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1881)

It is fine if we get them, I simply do not want them written to the log as it is causing the log files to be much bigger than they should be. I was wondering if anyone had an efficient way to go about this since I seem to be stuck. Thanks in advance.

Edit: @jmehrens are you thinking something like this?

public void flushBuffer() throws IOException {
       if (stream != null) {
       stream.flush();
      }
}

Solution

  • Using the suggestion given my jmehrens, I added the following code to the GZIPResponseWrapper.java class:

    public void flushBuffer() throws IOException {
       if (stream != null) {
       stream.flush();
      }
    }
    

    This prevents the flushBuffer from running if given a nullpointer exception which in turn would prevent it from writing to the system log. While this isn't the best fix, its a nice temporary solution to prevent the log files from filling up.