0

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();
      }
}
  • 1
    Why not just add a null check to `flushBuffer` like you have everywhere else? – jmehrens Jul 21 '17 at 13:13
  • 1
    Right. That should stop the NPE but that is probably a symptom of a larger problem. – jmehrens Jul 21 '17 at 13:46
  • I agree however that's what I'm being told to do haha. I figure I'll look into it more once I finish some other projects I'm working on. This seems to be working so I'm gonna close the question. Thanks for the help! – M. Winnicki Jul 21 '17 at 13:59
  • What logging framework are you using and what servlet container is this running under? – jmehrens Jul 21 '17 at 14:24
  • Servlet container is IBM Websphere, logging framework is logback – M. Winnicki Jul 21 '17 at 15:40

2 Answers2

0

One option would be to set the logger level to OFF for the logger that is reporting this error. However, that might hide other errors that you want to see.

A better option would be to configure EvaluatorFilter on the fileappender that is reporting this error or write your own filter.

jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • My understanding from the higher ups in the company is that we still want to receive this error, we simply don't want to write it out to the system out log. That's why it is my understanding that your original answer will the null check would be the best option in this particular case. In terms of overall performance and fixing the underlying issue? Absolutely not, however like I said this is a peculiar case. Thanks again for the help. – M. Winnicki Jul 21 '17 at 19:38
-2

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.