FileDocCategorySizeDatePackage
PwcCoyoteRequest.javaAPI DocGlassfish v2 API12354Fri May 04 22:35:30 BST 2007com.sun.enterprise.web.connector.coyote

PwcCoyoteRequest

public class PwcCoyoteRequest extends org.apache.coyote.tomcat5.CoyoteRequest
Customized version of the Tomcat 5 CoyoteRequest This is required for supporting Web Programmatic Login and setting the request encoding (charset).
author
Jeanfrancois Arcand
author
Jan Luehe

Fields Summary
private static Logger
logger
private boolean
sunWebXmlChecked
private byte[]
formData
private int
formDataLen
Constructors Summary
Methods Summary
public voidconfigureSessionCookie(javax.servlet.http.Cookie cookie)


        super.configureSessionCookie(cookie);

        PwcWebModule wm = (PwcWebModule) getContext();
        SessionCookieConfig cookieConfig = wm.getSessionCookieConfig();

        if (cookieConfig != null) {

            String name = cookieConfig.getName();
            if (name != null && !name.equals(Globals.SESSION_COOKIE_NAME)) {
                logger.log(Level.WARNING,
                           "pe_coyote.request.illegal_cookie_name",
                           new String[] { name, Globals.SESSION_COOKIE_NAME });
            }
     
            if (cookieConfig.getPath() != null) {
                cookie.setPath(cookieConfig.getPath());
            }

            cookie.setMaxAge(cookieConfig.getMaxAge());

            if (cookieConfig.getDomain() != null) {
                cookie.setDomain(cookieConfig.getDomain());
            }

            if (cookieConfig.getComment() != null) {
                cookie.setVersion(1);
                cookie.setComment(cookieConfig.getComment());
            }

            if (!cookieConfig.getSecure().equalsIgnoreCase(SessionCookieConfig.DYNAMIC_SECURE)) {
                cookie.setSecure(Boolean.parseBoolean(cookieConfig.getSecure()));
            }
        }
    
public java.lang.StringgetCharacterEncoding()
Return the character encoding for this Request. If there is no request charset specified in the request, determines and sets the request charset using the locale-charset-info, locale-charset-map, and parameter-encoding elements provided in the sun-web.xml.

        String enc = super.getCharacterEncoding();
        if (enc != null) {
            return enc;
        }
    
        boolean encodingFound = setRequestEncodingFromSunWebXml();
        if (encodingFound) {
            return super.getCharacterEncoding();
        } else {
            return null;
        }
    
private java.lang.StringgetFormHintFieldEncoding(com.sun.enterprise.web.PwcWebModule wm)


        String encoding = null;

        String formHintField = wm.getFormHintField();
        if (formHintField == null){
            return null;
        }

        if ("POST".equalsIgnoreCase(getMethod())) {
            // POST
            encoding = getPostDataEncoding(formHintField);
        } else {
            String query = getQueryString();
            if (query != null) {
                encoding = parseFormHintField(query, formHintField);
            }
        }

        return encoding;
    
protected byte[]getPostBody()
Gets the POST body of this request.

return
The POST body of this request


        if (formDataLen > 0) {
            // POST body already read
            return formData;
        } else {
            return super.getPostBody();
        } 
    
private java.lang.StringgetPostDataEncoding(java.lang.String formHintField)


        if (!getMethod().equalsIgnoreCase("POST")) {
            return null;
        }

        String contentType = getContentType();
        if (contentType == null)
            contentType = "";
        int semicolon = contentType.indexOf(';");
        if (semicolon >= 0) {
            contentType = contentType.substring(0, semicolon).trim();
        } else {
            contentType = contentType.trim();
        }
        if (!("application/x-www-form-urlencoded".equals(contentType))) {
            return null;
        }

        int len = getContentLength();
        if (len <= 0) {
            return null;
        }
        int maxPostSize = ((CoyoteConnector) connector).getMaxPostSize();
        if ((maxPostSize > 0) && (len > maxPostSize)) {
            logger.log(Level.WARNING, "peCoyoteRequest.postTooLarge");
            throw new IllegalStateException("Post too large");
        }

        String encoding = null;

        try {
            formData = null;
            if (len < CACHED_POST_LEN) {
                if (postData == null)
                    postData = new byte[CACHED_POST_LEN];
                formData = postData;
            } else {
                formData = new byte[len];
            }
            int actualLen = readPostBody(formData, len);
            if (actualLen == len) {
                // START SJSAS 6346738
                formDataLen = actualLen;
                // END SJSAS 6346738
                String formDataString = new String(formData).substring(0, len);
                encoding = parseFormHintField(formDataString, formHintField);
            }
        } catch (Throwable t) {
            ; // Ignore
        }

        return encoding;
    
public java.io.BufferedReadergetReader()

        if (super.getCharacterEncoding() == null) {
            setRequestEncodingFromSunWebXml();
        }
        return super.getReader();
    
protected javax.servlet.http.CookiemakeCookie(org.apache.tomcat.util.http.ServerCookie scookie)


        PwcWebModule wm = (PwcWebModule) getContext();
        boolean encodeCookies = false;
        if (wm != null && wm.getEncodeCookies()) {
            encodeCookies = true;
        }

        return makeCookie(scookie, encodeCookies);
    
private java.lang.StringparseFormHintField(java.lang.String paramsString, java.lang.String formHintField)


        String encoding = null;

        formHintField += "=";            
        int index = paramsString.indexOf(formHintField);
        if (index != -1) {
            int endIndex = paramsString.indexOf('&", index);
            if (endIndex != -1) {
                encoding = paramsString.substring(
                    index + formHintField.length(), endIndex);
            } else {
                encoding = paramsString.substring(
                    index + formHintField.length());
            }
        }

        return encoding;
    
public voidrecycle()

        super.recycle();
        formDataLen = 0;
        sunWebXmlChecked = false;
    
public voidsetContext(org.apache.catalina.Context ctx)

    // END SJSAS 6346738

        
        if (ctx == null) {
            // Invalid request. Response will be handled by
            // the StandardEngineValve
            return;
        }
        
        super.setContext(ctx);
        CoyoteResponse response = (CoyoteResponse) getResponse();
        // Assert response!=null
        if (response != null) {
            String[] cacheControls = ((PwcWebModule) ctx).getCacheControls();
            for (int i=0; cacheControls!=null && i<cacheControls.length; i++) {
                response.addHeader("Cache-Control", cacheControls[i]);
            }
        }

        sunWebXmlChecked = false;
    
private booleansetRequestEncodingFromSunWebXml()
Determines and sets the request charset using the locale-charset-info, locale-charset-map, and parameter-encoding elements provided in the sun-web.xml.

return
true if a request encoding has been determined and set, false otherwise


        if (sunWebXmlChecked) {
            return false;
        }

        sunWebXmlChecked = true;

        PwcWebModule wm = (PwcWebModule) getContext();

        String encoding = getFormHintFieldEncoding(wm);
        if (encoding == null) {
            encoding = wm.getDefaultCharset();
            if (encoding == null && wm.hasLocaleToCharsetMapping()) {
                encoding = wm.mapLocalesToCharset(getLocales());
            }
        }

        if (encoding != null) {
            try {
                setCharacterEncoding(encoding);
            } catch (UnsupportedEncodingException uee) {
                logger.log(Level.WARNING, "pe_coyote.request.encoding", uee);
            }
        }

        return (encoding != null);