Java Code Examples for org.apache.catalina.connector.Request#getRequestURI()

The following examples show how to use org.apache.catalina.connector.Request#getRequestURI() . 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: WebappAuthenticationValve.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
private boolean isContextSkipped(Request request) {
    Context context = request.getContext();
    String ctx = context == null ? null :context.getPath();
    if (ctx == null || "".equals(ctx)) {
        ctx = request.getContextPath();
        if (ctx == null || "".equals(ctx)) {
            String requestUri = request.getRequestURI();
            if ("/".equals(requestUri)) {
                return true;
            }
            StringTokenizer tokenizer = new StringTokenizer(request.getRequestURI(), "/");
            if (!tokenizer.hasMoreTokens()) {
                return false;
            }
            ctx = tokenizer.nextToken();
        }
    }
    return ("carbon".equalsIgnoreCase(ctx) || "services".equalsIgnoreCase(ctx));
}
 
Example 2
Source File: WebappAuthenticationValve.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
private boolean isNonSecuredEndPoint(Request request) {
    if (request.getCoyoteRequest() != null && request.getCoyoteRequest().getMimeHeaders() !=
            null && request.getCoyoteRequest().getMimeHeaders().getValue(Constants
            .HTTPHeaders.HEADER_HTTP_AUTHORIZATION) != null) {
        //This is to handle the DEP behaviours of the same endpoint being non-secured in the
        // first call and then being secured in the second call which comes with the basic
        // auth header.
        return false;
    }
    String uri = request.getRequestURI();
    if (uri == null) {
        uri = "";
    }
    if (!uri.endsWith("/")) {
        uri = uri + "/";
    }
    String contextPath = request.getContextPath();
    //Check the contextPath in nonSecuredEndpoints. If so it means cache is not populated for this web-app.
    if (!nonSecuredEndpoints.containsKey(contextPath)) {
        String param = request.getContext().findParameter("nonSecuredEndPoints");
        String skippedEndPoint;
        if (param != null && !param.isEmpty()) {
            //Add the nonSecured end-points to cache
            StringTokenizer tokenizer = new StringTokenizer(param, ",");
            nonSecuredEndpoints.put(contextPath, "true");
            while (tokenizer.hasMoreTokens()) {
                skippedEndPoint = tokenizer.nextToken();
                skippedEndPoint = skippedEndPoint.replace("\n", "").replace("\r", "").trim();
                if (!skippedEndPoint.endsWith("/")) {
                    skippedEndPoint = skippedEndPoint + "/";
                }
                nonSecuredEndpoints.put(skippedEndPoint, "true");
            }
        }
    }
    return nonSecuredEndpoints.containsKey(uri);
}
 
Example 3
Source File: MongoAccessLogValve.java    From tomcat-mongo-access-log with Apache License 2.0 5 votes vote down vote up
@Override
public void log(Request request, Response response, long time) {
    if (!getState().isAvailable() || logElements == null
            || condition != null
            && null != request.getRequest().getAttribute(condition)
            || conditionIf != null
            && null == request.getRequest().getAttribute(conditionIf)) {
        return;
    }
    String uri = request.getRequestURI();
    if (uri != null) {
      for(String pattern : this.excludePatterns) {
        if (uri.indexOf(pattern) != -1) {
          return;
        }
      }
    }
    /**
     * XXX This is a bit silly, but we want to have start and stop time and
     * duration consistent. It would be better to keep start and stop
     * simply in the request and/or response object and remove time
     * (duration) from the interface.
     */
    long start = request.getCoyoteRequest().getStartTime();
    Date date = getDate(start + time);

    StringBuilder buf = new StringBuilder();
    DBObject result = new BasicDBObject(10);
    for (int i = 0; i < logElements.length; i++) {
        logElements[i].addElement(buf, result, date, request, response, time);
    }

    log(result);
}
 
Example 4
Source File: LoadBalancerDrainingValve.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
    if  ("DIS".equals(request.getAttribute(ATTRIBUTE_KEY_JK_LB_ACTIVATION)) &&
            !request.isRequestedSessionIdValid()) {

        if (containerLog.isDebugEnabled()) {
            containerLog.debug("Load-balancer is in DISABLED state; draining this node");
        }

        boolean ignoreRebalance = false;
        Cookie sessionCookie = null;

        final Cookie[] cookies = request.getCookies();

        final String sessionCookieName = SessionConfig.getSessionCookieName(request.getContext());

        if (null != cookies) {
            for (Cookie cookie : cookies) {
                final String cookieName = cookie.getName();
                if (containerLog.isTraceEnabled()) {
                    containerLog.trace("Checking cookie " + cookieName + "=" + cookie.getValue());
                }

                if (sessionCookieName.equals(cookieName) &&
                        request.getRequestedSessionId().equals(cookie.getValue())) {
                    sessionCookie = cookie;
                } else if (null != _ignoreCookieName &&
                        _ignoreCookieName.equals(cookieName) &&
                        null != _ignoreCookieValue &&
                        _ignoreCookieValue.equals(cookie.getValue())) {
                    // The client presenting a valid ignore-cookie value?
                    ignoreRebalance = true;
                }
            }
        }

        if (ignoreRebalance) {
            if (containerLog.isDebugEnabled()) {
                containerLog.debug("Client is presenting a valid " + _ignoreCookieName +
                        " cookie, re-balancing is being skipped");
            }

            getNext().invoke(request, response);

            return;
        }

        // Kill any session cookie that was found
        // TODO: Consider implications of SSO cookies
        if (null != sessionCookie) {
            sessionCookie.setPath(SessionConfig.getSessionCookiePath(request.getContext()));
            sessionCookie.setMaxAge(0); // Delete
            sessionCookie.setValue(""); // Purge the cookie's value
            response.addCookie(sessionCookie);
        }

        // Re-write the URI if it contains a ;jsessionid parameter
        String uri = request.getRequestURI();
        String sessionURIParamName = SessionConfig.getSessionUriParamName(request.getContext());
        if (uri.contains(";" + sessionURIParamName + "=")) {
            uri = uri.replaceFirst(";" + sessionURIParamName + "=[^&?]*", "");
        }

        String queryString = request.getQueryString();

        if (null != queryString) {
            uri = uri + "?" + queryString;
        }

        // NOTE: Do not call response.encodeRedirectURL or the bad
        // sessionid will be restored
        response.setHeader("Location", uri);
        response.setStatus(_redirectStatusCode);
    } else {
        getNext().invoke(request, response);
    }
}
 
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--;
       }
    }

}