Java Code Examples for org.apache.tomcat.util.http.MimeHeaders#size()

The following examples show how to use org.apache.tomcat.util.http.MimeHeaders#size() . 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: Response.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public Collection<String> getHeaderNames() {
    MimeHeaders headers = getCoyoteResponse().getMimeHeaders();
    int n = headers.size();
    List<String> result = new ArrayList<>(n);
    for (int i = 0; i < n; i++) {
        result.add(headers.getName(i).toString());
    }
    return result;

}
 
Example 2
Source File: Response.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Special method for adding a session cookie as we should be overriding
 * any previous.
 *
 * @param cookie The new session cookie to add the response
 */
public void addSessionCookieInternal(final Cookie cookie) {
    if (isCommitted()) {
        return;
    }

    String name = cookie.getName();
    final String headername = "Set-Cookie";
    final String startsWith = name + "=";
    String header = generateCookieString(cookie);
    boolean set = false;
    MimeHeaders headers = getCoyoteResponse().getMimeHeaders();
    int n = headers.size();
    for (int i = 0; i < n; i++) {
        if (headers.getName(i).toString().equals(headername)) {
            if (headers.getValue(i).toString().startsWith(startsWith)) {
                headers.getValue(i).setString(header);
                set = true;
            }
        }
    }
    if (!set) {
        addHeader(headername, header);
    }


}
 
Example 3
Source File: Response.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<String> getHeaderNames() {

    MimeHeaders headers = getCoyoteResponse().getMimeHeaders();
    int n = headers.size();
    List<String> result = new ArrayList<String>(n);
    for (int i = 0; i < n; i++) {
        result.add(headers.getName(i).toString());
    }
    return result;

}
 
Example 4
Source File: Response.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Special method for adding a session cookie as we should be overriding
 * any previous
 * @param cookie
 */
public void addSessionCookieInternal(final Cookie cookie) {
    if (isCommitted()) {
        return;
    }

    String name = cookie.getName();
    final String headername = "Set-Cookie";
    final String startsWith = name + "=";
    final StringBuffer sb = generateCookieString(cookie);
    boolean set = false;
    MimeHeaders headers = getCoyoteResponse().getMimeHeaders();
    int n = headers.size();
    for (int i = 0; i < n; i++) {
        if (headers.getName(i).toString().equals(headername)) {
            if (headers.getValue(i).toString().startsWith(startsWith)) {
                headers.getValue(i).setString(sb.toString());
                set = true;
            }
        }
    }
    if (!set) {
        addHeader(headername, sb.toString());
    }


}
 
Example 5
Source File: Response.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<String> getHeaderNames() {

    MimeHeaders headers = getCoyoteResponse().getMimeHeaders();
    int n = headers.size();
    List<String> result = new ArrayList<String>(n);
    for (int i = 0; i < n; i++) {
        result.add(headers.getName(i).toString());
    }
    return result;

}
 
Example 6
Source File: Response.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Special method for adding a session cookie as we should be overriding
 * any previous
 * @param cookie
 */
public void addSessionCookieInternal(final Cookie cookie) {
    if (isCommitted()) {
        return;
    }

    String name = cookie.getName();
    final String headername = "Set-Cookie";
    final String startsWith = name + "=";
    final StringBuffer sb = generateCookieString(cookie);
    boolean set = false;
    MimeHeaders headers = getCoyoteResponse().getMimeHeaders();
    int n = headers.size();
    for (int i = 0; i < n; i++) {
        if (headers.getName(i).toString().equals(headername)) {
            if (headers.getValue(i).toString().startsWith(startsWith)) {
                headers.getValue(i).setString(sb.toString());
                set = true;
            }
        }
    }
    if (!set) {
        addHeader(headername, sb.toString());
    }


}
 
Example 7
Source File: TomcatService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static ResponseHeaders convertResponse(Response coyoteRes) {
    final ResponseHeadersBuilder headers = ResponseHeaders.builder();
    headers.status(coyoteRes.getStatus());

    final String contentType = coyoteRes.getContentType();
    if (contentType != null && !contentType.isEmpty()) {
        headers.set(HttpHeaderNames.CONTENT_TYPE, contentType);
    }

    final long contentLength = coyoteRes.getBytesWritten(true); // 'true' will trigger flush.
    final String method = coyoteRes.getRequest().method().toString();
    if (!"HEAD".equals(method)) {
        headers.setLong(HttpHeaderNames.CONTENT_LENGTH, contentLength);
    }

    final MimeHeaders cHeaders = coyoteRes.getMimeHeaders();
    final int numHeaders = cHeaders.size();
    for (int i = 0; i < numHeaders; i++) {
        final AsciiString name = toHeaderName(cHeaders.getName(i));
        if (name == null) {
            continue;
        }

        final String value = toHeaderValue(cHeaders.getValue(i));
        if (value == null) {
            continue;
        }

        headers.add(name.toLowerCase(), value);
    }

    return headers.build();
}
 
Example 8
Source File: AjpProcessor.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * When committing the response, we have to validate the set of headers, as
 * well as setup the response filters.
 */
@SuppressWarnings("deprecation")
@Override
protected final void prepareResponse() throws IOException {

    response.setCommitted(true);

    tmpMB.recycle();
    responseMsgPos = -1;
    responseMessage.reset();
    responseMessage.appendByte(Constants.JK_AJP13_SEND_HEADERS);

    // Responses with certain status codes are not permitted to include a
    // response body.
    int statusCode = response.getStatus();
    if (statusCode < 200 || statusCode == 204 || statusCode == 205 ||
            statusCode == 304) {
        // No entity body
        swallowResponse = true;
    }

    // Responses to HEAD requests are not permitted to include a response
    // body.
    MessageBytes methodMB = request.method();
    if (methodMB.equals("HEAD")) {
        // No entity body
        swallowResponse = true;
    }

    // HTTP header contents
    responseMessage.appendInt(statusCode);
    if (sendReasonPhrase) {
        String message = null;
        if (org.apache.coyote.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER &&
                HttpMessages.isSafeInHttpHeader(response.getMessage())) {
            message = response.getMessage();
        }
        if (message == null) {
            message = HttpMessages.getInstance(
                    response.getLocale()).getMessage(response.getStatus());
        }
        if (message == null) {
            // mod_jk + httpd 2.x fails with a null status message - bug 45026
            message = Integer.toString(response.getStatus());
        }
        tmpMB.setString(message);
    } else {
        // Reason phrase is optional but mod_jk + httpd 2.x fails with a null
        // reason phrase - bug 45026
        tmpMB.setString(Integer.toString(response.getStatus()));
    }
    responseMessage.appendBytes(tmpMB);

    // Special headers
    MimeHeaders headers = response.getMimeHeaders();
    String contentType = response.getContentType();
    if (contentType != null) {
        headers.setValue("Content-Type").setString(contentType);
    }
    String contentLanguage = response.getContentLanguage();
    if (contentLanguage != null) {
        headers.setValue("Content-Language").setString(contentLanguage);
    }
    long contentLength = response.getContentLengthLong();
    if (contentLength >= 0) {
        headers.setValue("Content-Length").setLong(contentLength);
    }

    // Other headers
    int numHeaders = headers.size();
    responseMessage.appendInt(numHeaders);
    for (int i = 0; i < numHeaders; i++) {
        MessageBytes hN = headers.getName(i);
        int hC = Constants.getResponseAjpIndex(hN.toString());
        if (hC > 0) {
            responseMessage.appendInt(hC);
        }
        else {
            responseMessage.appendBytes(hN);
        }
        MessageBytes hV=headers.getValue(i);
        responseMessage.appendBytes(hV);
    }

    // Write to buffer
    responseMessage.end();
    socketWrapper.write(true, responseMessage.getBuffer(), 0, responseMessage.getLen());
    socketWrapper.flush(true);
}
 
Example 9
Source File: AbstractAjpProcessor.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * When committing the response, we have to validate the set of headers, as
 * well as setup the response filters.
 */
protected void prepareResponse() throws IOException {

    response.setCommitted(true);

    responseMessage.reset();
    responseMessage.appendByte(Constants.JK_AJP13_SEND_HEADERS);

    // Responses with certain status codes are not permitted to include a
    // response body.
    int statusCode = response.getStatus();
    if (statusCode < 200 || statusCode == 204 || statusCode == 205 ||
            statusCode == 304) {
        // No entity body
        swallowResponse = true;
    }

    // Responses to HEAD requests are not permitted to incude a response
    // body.
    MessageBytes methodMB = request.method();
    if (methodMB.equals("HEAD")) {
        // No entity body
        swallowResponse = true;
    }

    // HTTP header contents
    responseMessage.appendInt(statusCode);
    String message = null;
    if (org.apache.coyote.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER &&
            HttpMessages.isSafeInHttpHeader(response.getMessage())) {
        message = response.getMessage();
    }
    if (message == null){
        message = HttpMessages.getInstance(
                response.getLocale()).getMessage(response.getStatus());
    }
    if (message == null) {
        // mod_jk + httpd 2.x fails with a null status message - bug 45026
        message = Integer.toString(response.getStatus());
    }
    tmpMB.setString(message);
    responseMessage.appendBytes(tmpMB);

    // Special headers
    MimeHeaders headers = response.getMimeHeaders();
    String contentType = response.getContentType();
    if (contentType != null) {
        headers.setValue("Content-Type").setString(contentType);
    }
    String contentLanguage = response.getContentLanguage();
    if (contentLanguage != null) {
        headers.setValue("Content-Language").setString(contentLanguage);
    }
    long contentLength = response.getContentLengthLong();
    if (contentLength >= 0) {
        headers.setValue("Content-Length").setLong(contentLength);
    }

    // Other headers
    int numHeaders = headers.size();
    responseMessage.appendInt(numHeaders);
    for (int i = 0; i < numHeaders; i++) {
        MessageBytes hN = headers.getName(i);
        int hC = Constants.getResponseAjpIndex(hN.toString());
        if (hC > 0) {
            responseMessage.appendInt(hC);
        }
        else {
            responseMessage.appendBytes(hN);
        }
        MessageBytes hV=headers.getValue(i);
        responseMessage.appendBytes(hV);
    }

    // Write to buffer
    responseMessage.end();
    output(responseMessage.getBuffer(), 0,
            responseMessage.getLen());
}
 
Example 10
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);
                    }
                }
            }
        }
    }
 
Example 11
Source File: AbstractAjpProcessor.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * When committing the response, we have to validate the set of headers, as
 * well as setup the response filters.
 */
protected void prepareResponse() throws IOException {

    response.setCommitted(true);

    responseMessage.reset();
    responseMessage.appendByte(Constants.JK_AJP13_SEND_HEADERS);

    // Responses with certain status codes are not permitted to include a
    // response body.
    int statusCode = response.getStatus();
    if (statusCode < 200 || statusCode == 204 || statusCode == 205 ||
            statusCode == 304) {
        // No entity body
        swallowResponse = true;
    }

    // Responses to HEAD requests are not permitted to incude a response
    // body.
    MessageBytes methodMB = request.method();
    if (methodMB.equals("HEAD")) {
        // No entity body
        swallowResponse = true;
    }

    // HTTP header contents
    responseMessage.appendInt(statusCode);
    String message = null;
    if (org.apache.coyote.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER &&
            HttpMessages.isSafeInHttpHeader(response.getMessage())) {
        message = response.getMessage();
    }
    if (message == null){
        message = HttpMessages.getInstance(
                response.getLocale()).getMessage(response.getStatus());
    }
    if (message == null) {
        // mod_jk + httpd 2.x fails with a null status message - bug 45026
        message = Integer.toString(response.getStatus());
    }
    tmpMB.setString(message);
    responseMessage.appendBytes(tmpMB);

    // Special headers
    MimeHeaders headers = response.getMimeHeaders();
    String contentType = response.getContentType();
    if (contentType != null) {
        headers.setValue("Content-Type").setString(contentType);
    }
    String contentLanguage = response.getContentLanguage();
    if (contentLanguage != null) {
        headers.setValue("Content-Language").setString(contentLanguage);
    }
    long contentLength = response.getContentLengthLong();
    if (contentLength >= 0) {
        headers.setValue("Content-Length").setLong(contentLength);
    }

    // Other headers
    int numHeaders = headers.size();
    responseMessage.appendInt(numHeaders);
    for (int i = 0; i < numHeaders; i++) {
        MessageBytes hN = headers.getName(i);
        int hC = Constants.getResponseAjpIndex(hN.toString());
        if (hC > 0) {
            responseMessage.appendInt(hC);
        }
        else {
            responseMessage.appendBytes(hN);
        }
        MessageBytes hV=headers.getValue(i);
        responseMessage.appendBytes(hV);
    }

    // Write to buffer
    responseMessage.end();
    output(responseMessage.getBuffer(), 0,
            responseMessage.getLen());
}