Java Code Examples for org.apache.tomcat.util.buf.MessageBytes#indexOf()

The following examples show how to use org.apache.tomcat.util.buf.MessageBytes#indexOf() . 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: AbstractHttp11Processor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the resource could be compressed, if the client supports it.
 */
private boolean isCompressable() {

    // Check if content is not already gzipped
    MessageBytes contentEncodingMB =
        response.getMimeHeaders().getValue("Content-Encoding");

    if ((contentEncodingMB != null)
        && (contentEncodingMB.indexOf("gzip") != -1)) {
        return false;
    }

    // If force mode, always compress (test purposes only)
    if (compressionLevel == 2) {
        return true;
    }

    // Check if sufficient length to trigger the compression
    long contentLength = response.getContentLengthLong();
    if ((contentLength == -1)
        || (contentLength > compressionMinSize)) {
        // Check for compatible MIME-TYPE
        if (compressableMimeTypes != null) {
            return (startsWithStringArray(compressableMimeTypes,
                                          response.getContentType()));
        }
    }

    return false;
}
 
Example 2
Source File: AbstractHttp11Processor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Check if compression should be used for this resource. Already checked
 * that the resource could be compressed if the client supports it.
 */
private boolean useCompression() {

    // Check if browser support gzip encoding
    MessageBytes acceptEncodingMB =
        request.getMimeHeaders().getValue("accept-encoding");

    if ((acceptEncodingMB == null)
        || (acceptEncodingMB.indexOf("gzip") == -1)) {
        return false;
    }

    // If force mode, always compress (test purposes only)
    if (compressionLevel == 2) {
        return true;
    }

    // Check for incompatible Browser
    if (noCompressionUserAgents != null) {
        MessageBytes userAgentValueMB =
            request.getMimeHeaders().getValue("user-agent");
        if(userAgentValueMB != null) {
            String userAgentValue = userAgentValueMB.toString();

            if (noCompressionUserAgents != null &&
                    noCompressionUserAgents.matcher(userAgentValue).matches()) {
                    return false;
            }
        }
    }

    return true;
}
 
Example 3
Source File: AbstractHttp11Processor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the resource could be compressed, if the client supports it.
 */
private boolean isCompressable() {

    // Check if content is not already gzipped
    MessageBytes contentEncodingMB =
        response.getMimeHeaders().getValue("Content-Encoding");

    if ((contentEncodingMB != null)
        && (contentEncodingMB.indexOf("gzip") != -1)) {
        return false;
    }

    // If force mode, always compress (test purposes only)
    if (compressionLevel == 2) {
        return true;
    }

    // Check if sufficient length to trigger the compression
    long contentLength = response.getContentLengthLong();
    if ((contentLength == -1)
        || (contentLength > compressionMinSize)) {
        // Check for compatible MIME-TYPE
        if (compressableMimeTypes != null) {
            return (startsWithStringArray(compressableMimeTypes,
                                          response.getContentType()));
        }
    }

    return false;
}
 
Example 4
Source File: AbstractHttp11Processor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Check if compression should be used for this resource. Already checked
 * that the resource could be compressed if the client supports it.
 */
private boolean useCompression() {

    // Check if browser support gzip encoding
    MessageBytes acceptEncodingMB =
        request.getMimeHeaders().getValue("accept-encoding");

    if ((acceptEncodingMB == null)
        || (acceptEncodingMB.indexOf("gzip") == -1)) {
        return false;
    }

    // If force mode, always compress (test purposes only)
    if (compressionLevel == 2) {
        return true;
    }

    // Check for incompatible Browser
    if (noCompressionUserAgents != null) {
        MessageBytes userAgentValueMB =
            request.getMimeHeaders().getValue("user-agent");
        if(userAgentValueMB != null) {
            String userAgentValue = userAgentValueMB.toString();

            if (noCompressionUserAgents.matcher(userAgentValue).matches()) {
                return false;
            }
        }
    }

    return true;
}
 
Example 5
Source File: CrossSubdomainSessionValve.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
protected void replaceCookie(Request request, Response response, Cookie cookie) {

        Delegator delegator = (Delegator) request.getAttribute("delegator");
        // copy the existing session cookie, but use a different domain (only if domain is valid)
        String cookieDomain = null;
        cookieDomain = EntityUtilProperties.getPropertyValue("url", "cookie.domain", "", delegator);

        if (UtilValidate.isEmpty(cookieDomain)) {
            String serverName = request.getServerName();
            String[] domainArray = serverName.split("\\.");
            // check that the domain isn't an IP address
            if (domainArray.length == 4) {
                boolean isIpAddress = true;
                for (String domainSection : domainArray) {
                    if (!UtilValidate.isIntegerInRange(domainSection, 0, 255)) {
                        isIpAddress = false;
                        break;
                    }
                }
                if (isIpAddress) {
                    return;
                }
            }
            if (domainArray.length > 2) {
                cookieDomain = "." + domainArray[domainArray.length - 2] + "." + domainArray[domainArray.length - 1];
            }
        }


        if (UtilValidate.isNotEmpty(cookieDomain)) {
            Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue());
            if (cookie.getPath() != null) {
                newCookie.setPath(cookie.getPath());
            }
            newCookie.setDomain(cookieDomain);
            newCookie.setMaxAge(cookie.getMaxAge());
            newCookie.setVersion(cookie.getVersion());
            if (cookie.getComment() != null) {
                newCookie.setComment(cookie.getComment());
            }
            newCookie.setSecure(cookie.getSecure());
            newCookie.setHttpOnly(cookie.isHttpOnly());

            // if the response has already been committed, our replacement strategy will have no effect
            if (response.isCommitted()) {
                Debug.logError("CrossSubdomainSessionValve: response was already committed!", module);
            }

            // find the Set-Cookie header for the existing cookie and replace its value with new cookie
            MimeHeaders mimeHeaders = request.getCoyoteRequest().getMimeHeaders();
            for (int i = 0, size = mimeHeaders.size(); i < size; i++) {
                if (mimeHeaders.getName(i).equals("Set-Cookie")) {
                    MessageBytes value = mimeHeaders.getValue(i);
                    if (value.indexOf(cookie.getName()) >= 0) {
                        String newCookieValue = request.getContext().getCookieProcessor().generateHeader(newCookie);
                        if (Debug.verboseOn()) Debug.logVerbose("CrossSubdomainSessionValve: old Set-Cookie value: " + value.toString(), module);
                        if (Debug.verboseOn()) Debug.logVerbose("CrossSubdomainSessionValve: new Set-Cookie value: " + newCookieValue, module);
                        value.setString(newCookieValue);
                    }
                }
            }
        }
    }