Search in sources :

Example 6 with HttpResponse

use of org.apache.catalina.HttpResponse in project Payara by payara.

the class JDBCAccessLogValve method postInvoke.

@Override
public void postInvoke(Request request, Response response) throws IOException, ServletException {
    ServletRequest req = request.getRequest();
    HttpServletRequest hreq = null;
    if (req instanceof HttpServletRequest)
        hreq = (HttpServletRequest) req;
    String remoteHost = "";
    if (resolveHosts)
        remoteHost = req.getRemoteHost();
    else
        remoteHost = req.getRemoteAddr();
    String user = "";
    if (hreq != null)
        user = hreq.getRemoteUser();
    String query = "";
    if (hreq != null)
        query = hreq.getRequestURI();
    int bytes = response.getContentCount();
    if (bytes < 0)
        bytes = 0;
    int status = ((HttpResponse) response).getStatus();
    synchronized (ps) {
        try {
            ps.setString(1, remoteHost);
            ps.setString(2, user);
            ps.setTimestamp(3, new Timestamp(getCurrentTimeMillis()));
            ps.setString(4, query);
            ps.setInt(5, status);
            ps.setInt(6, bytes);
        } catch (SQLException e) {
            throw new ServletException(e);
        }
        if (pattern.equals("common")) {
            try {
                ps.executeUpdate();
            } catch (SQLException e) {
                throw new ServletException(e);
            }
        } else if (pattern.equals("combined")) {
            String virtualHost = "";
            if (hreq != null)
                virtualHost = hreq.getServerName();
            String method = "";
            if (hreq != null)
                method = hreq.getMethod();
            String referer = "";
            if (hreq != null)
                referer = hreq.getHeader("referer");
            String userAgent = "";
            if (hreq != null)
                userAgent = hreq.getHeader("user-agent");
            try {
                ps.setString(7, virtualHost);
                ps.setString(8, method);
                ps.setString(9, referer);
                ps.setString(10, userAgent);
                ps.executeUpdate();
            } catch (SQLException e) {
                throw new ServletException(e);
            }
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpResponse(org.apache.catalina.HttpResponse)

Example 7 with HttpResponse

use of org.apache.catalina.HttpResponse in project Payara by payara.

the class ErrorReportValve method report.

// ------------------------------------------------------ Protected Methods
/**
 * Prints out an error report.
 *
 * @param request The request being processed
 * @param response The response being generated
 * @param throwable The exception that occurred (which possibly wraps
 *  a root cause exception
 */
protected void report(Request request, Response response, Throwable throwable) throws IOException {
    /* GlassFish 6386229
        // Do nothing on non-HTTP responses
        if (!(response instanceof HttpResponse))
            return;
        */
    HttpResponse hresponse = (HttpResponse) response;
    /* GlassFish 6386229
        if (!(response instanceof HttpServletResponse))
            return;
        */
    HttpServletResponse hres = (HttpServletResponse) response;
    int statusCode = hresponse.getStatus();
    // Do nothing if anything has been written already
    if (statusCode < 400 || (response.getContentCount() > 0))
        return;
    Throwable rootCause = null;
    if (throwable != null) {
        if (throwable instanceof ServletException)
            rootCause = ((ServletException) throwable).getRootCause();
    }
    String message = hresponse.getMessage();
    // BEGIN S1AS 4878272
    if (message == null) {
        message = hresponse.getDetailMessage();
    }
    if (message == null) {
        message = "";
    } else {
        message = HtmlEntityEncoder.encodeXSS(message);
    }
    // END S1AS 4878272
    // Do nothing if there is no report for the specified status code
    String report = null;
    try {
        /* SJSAS 6412710
            report = sm.getString("http." + statusCode, message);
            */
        // START SJSAS 6412710
        report = sm.getString("http." + statusCode, hres.getLocale());
    // END SJSAS 6412710
    } catch (Throwable t) {
        ;
    }
    if (report == null)
        return;
    String errorPage = makeErrorPage(statusCode, message, throwable, rootCause, report, hres);
    // START SJSAS 6412710
    /*
         * If throwable is not null, we've already preserved any non-default
         * response encoding in postInvoke(), so that the throwable's exception
         * message can be delivered to the client without any loss of
         * information. The following call to ServletResponse.setLocale()
         * will not override the response encoding in this case.
         * For all other cases, the response encoding will be set according to
         * the resource bundle locale.
         */
    hres.setLocale(sm.getResourceBundleLocale(hres.getLocale()));
    /* PWC 6254469
        // set the charset part of content type before getting the writer
        */
    try {
        hres.setContentType("text/html");
    /* PWC 6254469
            hres.setCharacterEncoding("UTF-8");
            */
    } catch (Throwable t) {
        if (debug >= 1)
            log(rb.getString(LogFacade.SET_CONTENT_TYPE_EXCEPTION), t);
    }
    try {
        Writer writer = response.getReporter();
        if (writer != null) {
            // If writer is null, it's an indication that the response has
            // been hard committed already, which should never happen
            writer.write(errorPage);
        }
    } catch (IOException | IllegalStateException e) {
        ;
    }
}
Also used : ServletException(javax.servlet.ServletException) HttpResponse(org.apache.catalina.HttpResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Writer(java.io.Writer)

Aggregations

HttpResponse (org.apache.catalina.HttpResponse)7 ServletException (javax.servlet.ServletException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Realm (org.apache.catalina.Realm)3 IOException (java.io.IOException)2 Principal (java.security.Principal)2 Cookie (javax.servlet.http.Cookie)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpRequest (org.apache.catalina.HttpRequest)2 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)2 InputStream (java.io.InputStream)1 Writer (java.io.Writer)1 Locale (java.util.Locale)1 ServletRequest (javax.servlet.ServletRequest)1 Auditor (org.apache.catalina.Auditor)1 LifecycleException (org.apache.catalina.LifecycleException)1 Session (org.apache.catalina.Session)1 StandardContext (org.apache.catalina.core.StandardContext)1 LoginConfig (org.apache.catalina.deploy.LoginConfig)1 ByteChunk (org.glassfish.grizzly.http.util.ByteChunk)1