Java Code Examples for org.apache.catalina.connector.Response#getBytesWritten()

The following examples show how to use org.apache.catalina.connector.Response#getBytesWritten() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    // Don't need to flush since trigger for log message is after the
    // response has been committed
    long length = response.getBytesWritten(false);
    if (length <= 0) {
        // Protect against nulls and unexpected types as these values
        // may be set by untrusted applications
        Object start = request.getAttribute(
                Globals.SENDFILE_FILE_START_ATTR);
        if (start instanceof Long) {
            Object end = request.getAttribute(
                    Globals.SENDFILE_FILE_END_ATTR);
            if (end instanceof Long) {
                length = ((Long) end).longValue() -
                        ((Long) start).longValue();
            }
        }
    }
    if (length <= 0 && conversion) {
        buf.append('-');
    } else {
        buf.append(Long.toString(length));
    }
}
 
Example 2
Source File: AccessLogValve.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void addElement(StringBuilder buf, Date date, Request request,
        Response response, long time) {
    // Don't need to flush since trigger for log message is after the
    // response has been committed
    long length = response.getBytesWritten(false);
    if (length <= 0) {
        // Protect against nulls and unexpected types as these values
        // may be set by untrusted applications
        Object start = request.getAttribute(
                Globals.SENDFILE_FILE_START_ATTR);
        if (start instanceof Long) {
            Object end = request.getAttribute(
                    Globals.SENDFILE_FILE_END_ATTR);
            if (end instanceof Long) {
                length = ((Long) end).longValue() -
                        ((Long) start).longValue();
            }
        }
    }
    if (length <= 0 && conversion) {
        buf.append('-');
    } else {
        buf.append(length);
    }
}
 
Example 3
Source File: AccessLogValve.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void addElement(StringBuilder buf, Date date, Request request,
        Response response, long time) {
    // Don't need to flush since trigger for log message is after the
    // response has been committed
    long length = response.getBytesWritten(false);
    if (length <= 0) {
        // Protect against nulls and unexpected types as these values
        // may be set by untrusted applications
        Object start = request.getAttribute(
                Globals.SENDFILE_FILE_START_ATTR);
        if (start instanceof Long) {
            Object end = request.getAttribute(
                    Globals.SENDFILE_FILE_END_ATTR);
            if (end instanceof Long) {
                length = ((Long) end).longValue() -
                        ((Long) start).longValue();
            }
        }
    }
    if (length <= 0 && conversion) {
        buf.append('-');
    } else {
        buf.append(length);
    }
}
 
Example 4
Source File: MongoAccessLogValve.java    From tomcat-mongo-access-log with Apache License 2.0 6 votes vote down vote up
@Override
public void addElement(StringBuilder buf, DBObject result, Date date, Request request,
        Response response, long time) {
    // Don't need to flush since trigger for log message is after the
    // response has been committed
    long length = response.getBytesWritten(false);
    if (length <= 0) {
        // Protect against nulls and unexpected types as these values
        // may be set by untrusted applications
        Object start = request.getAttribute(
                Globals.SENDFILE_FILE_START_ATTR);
        if (start instanceof Long) {
            Object end = request.getAttribute(
                    Globals.SENDFILE_FILE_END_ATTR);
            if (end instanceof Long) {
                length = ((Long) end).longValue() -
                        ((Long) start).longValue();
            }
        }
    }
    if (length <= 0 && conversion) {
        result.put("bytesSent", '-');
    } else {
        result.put("bytesSent", length);
    }
}
 
Example 5
Source File: JDBCAccessLogValve.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void log(Request request, Response response, long time) {
    if (!getState().isAvailable()) {
        return;
    }

    final String EMPTY = "" ;

    String remoteHost;
    if(resolveHosts) {
        if (requestAttributesEnabled) {
            Object host = request.getAttribute(REMOTE_HOST_ATTRIBUTE);
            if (host == null) {
                remoteHost = request.getRemoteHost();
            } else {
                remoteHost = (String) host;
            }
        } else {
            remoteHost = request.getRemoteHost();
        }
    } else {
        if (requestAttributesEnabled) {
            Object addr = request.getAttribute(REMOTE_ADDR_ATTRIBUTE);
            if (addr == null) {
                remoteHost = request.getRemoteAddr();
            } else {
                remoteHost = (String) addr;
            }
        } else {
            remoteHost = request.getRemoteAddr();
        }
    }
    String user = request.getRemoteUser();
    String query=request.getRequestURI();

    long bytes = response.getBytesWritten(true);
    if(bytes < 0) {
        bytes = 0;
    }
    int status = response.getStatus();
    String virtualHost = EMPTY;
    String method = EMPTY;
    String referer = EMPTY;
    String userAgent = EMPTY;
    String logPattern = pattern;
    if (logPattern.equals("combined")) {
        virtualHost = request.getServerName();
        method = request.getMethod();
        referer = request.getHeader("referer");
        userAgent = request.getHeader("user-agent");
    }
    synchronized (this) {
      int numberOfTries = 2;
      while (numberOfTries>0) {
        try {
            open();

            ps.setString(1, remoteHost);
            ps.setString(2, user);
            ps.setTimestamp(3, new Timestamp(getCurrentTimeMillis()));
            ps.setString(4, query);
            ps.setInt(5, status);

            if(useLongContentLength) {
                ps.setLong(6, bytes);
            } else {
                if (bytes > Integer.MAX_VALUE) {
                    bytes = -1 ;
                }
                ps.setInt(6, (int) bytes);
            }
            if (logPattern.equals("combined")) {
                  ps.setString(7, virtualHost);
                  ps.setString(8, method);
                  ps.setString(9, referer);
                  ps.setString(10, userAgent);
            }
            ps.executeUpdate();
            return;
          } catch (SQLException e) {
            // Log the problem for posterity
              container.getLogger().error(sm.getString("jdbcAccessLogValve.exception"), e);

            // Close the connection so that it gets reopened next time
            if (conn != null) {
                close();
            }
          }
          numberOfTries--;
       }
    }

}
 
Example 6
Source File: JDBCAccessLogValve.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public void log(Request request, Response response, long time) {
    if (!getState().isAvailable()) {
        return;
    }

    final String EMPTY = "" ;

    String remoteHost;
    if(resolveHosts) {
        if (requestAttributesEnabled) {
            Object host = request.getAttribute(REMOTE_HOST_ATTRIBUTE);
            if (host == null) {
                remoteHost = request.getRemoteHost();
            } else {
                remoteHost = (String) host;
            }
        } else {
            remoteHost = request.getRemoteHost();
        }
    } else {
        if (requestAttributesEnabled) {
            Object addr = request.getAttribute(REMOTE_ADDR_ATTRIBUTE);
            if (addr == null) {
                remoteHost = request.getRemoteAddr();
            } else {
                remoteHost = (String) addr;
            }
        } else {
            remoteHost = request.getRemoteAddr();
        }
    }
    String user = request.getRemoteUser();
    String query=request.getRequestURI();

    long bytes = response.getBytesWritten(true);
    if(bytes < 0) {
        bytes = 0;
    }
    int status = response.getStatus();
    String virtualHost = EMPTY;
    String method = EMPTY;
    String referer = EMPTY;
    String userAgent = EMPTY;
    String logPattern = pattern;
    if (logPattern.equals("combined")) {
        virtualHost = request.getServerName();
        method = request.getMethod();
        referer = request.getHeader("referer");
        userAgent = request.getHeader("user-agent");
    }
    synchronized (this) {
      int numberOfTries = 2;
      while (numberOfTries>0) {
        try {
            open();

            ps.setString(1, remoteHost);
            ps.setString(2, user);
            ps.setTimestamp(3, new Timestamp(getCurrentTimeMillis()));
            ps.setString(4, query);
            ps.setInt(5, status);

            if(useLongContentLength) {
                ps.setLong(6, bytes);
            } else {
                if (bytes > Integer.MAX_VALUE) {
                    bytes = -1 ;
                }
                ps.setInt(6, (int) bytes);
            }
            if (logPattern.equals("combined")) {
                  ps.setString(7, virtualHost);
                  ps.setString(8, method);
                  ps.setString(9, referer);
                  ps.setString(10, userAgent);
            }
            ps.executeUpdate();
            return;
          } catch (SQLException e) {
            // Log the problem for posterity
              container.getLogger().error(sm.getString("jdbcAccessLogValve.exception"), e);

            // Close the connection so that it gets reopened next time
            if (conn != null) {
                close();
            }
          }
          numberOfTries--;
       }
    }

}
 
Example 7
Source File: JDBCAccessLogValve.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void log(Request request, Response response, long time) {
    if (!getState().isAvailable()) {
        return;
    }

    final String EMPTY = "" ;

    String remoteHost;
    if(resolveHosts) {
        if (requestAttributesEnabled) {
            Object host = request.getAttribute(REMOTE_HOST_ATTRIBUTE);
            if (host == null) {
                remoteHost = request.getRemoteHost();
            } else {
                remoteHost = (String) host;
            }
        } else {
            remoteHost = request.getRemoteHost();
        }
    } else {
        if (requestAttributesEnabled) {
            Object addr = request.getAttribute(REMOTE_ADDR_ATTRIBUTE);
            if (addr == null) {
                remoteHost = request.getRemoteAddr();
            } else {
                remoteHost = (String) addr;
            }
        } else {
            remoteHost = request.getRemoteAddr();
        }
    }
    String user = request.getRemoteUser();
    String query=request.getRequestURI();

    long bytes = response.getBytesWritten(true);
    if(bytes < 0) {
        bytes = 0;
    }
    int status = response.getStatus();
    String virtualHost = EMPTY;
    String method = EMPTY;
    String referer = EMPTY;
    String userAgent = EMPTY;
    String logPattern = pattern;
    if (logPattern.equals("combined")) {
        virtualHost = request.getServerName();
        method = request.getMethod();
        referer = request.getHeader("referer");
        userAgent = request.getHeader("user-agent");
    }
    synchronized (this) {
      int numberOfTries = 2;
      while (numberOfTries>0) {
        try {
            open();

            ps.setString(1, remoteHost);
            ps.setString(2, user);
            ps.setTimestamp(3, new Timestamp(getCurrentTimeMillis()));
            ps.setString(4, query);
            ps.setInt(5, status);

            if(useLongContentLength) {
                ps.setLong(6, bytes);
            } else {
                if (bytes > Integer.MAX_VALUE) {
                    bytes = -1 ;
                }
                ps.setInt(6, (int) bytes);
            }
            if (logPattern.equals("combined")) {
                  ps.setString(7, virtualHost);
                  ps.setString(8, method);
                  ps.setString(9, referer);
                  ps.setString(10, userAgent);
            }
            ps.executeUpdate();
            return;
          } catch (SQLException e) {
            // Log the problem for posterity
              container.getLogger().error(sm.getString("jdbcAccessLogValve.exception"), e);

            // Close the connection so that it gets reopened next time
            if (conn != null) {
                close();
            }
          }
          numberOfTries--;
       }
    }

}