Package anvil.util

Source Code of anvil.util.RequestUtils

/*
* $Id: RequestUtils.java,v 1.7 2002/09/16 08:05:07 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.util;

import anvil.Log;
import java.util.Enumeration;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

/**
*
* @author Jaripekka Salminen
*/
public class RequestUtils {

    public static final String HEADER_ACCEPT      = "accept";
    public static final String HEADER_USER_AGENT  = "user-agent";
    public static final String ACCEPT_WML         = "wml";
    public static final String DOCUMENT_TYPE_WML  = "wml";
    public static final String DOCUMENT_TYPE_HTML = "html";
    public static final String CONTENT_TYPE_WML   = "text/vnd.wap.wml";
    public static final String CONTENT_TYPE_HTML  = "text/html";

    /**
     * Examples of request headers:
     * <pre>
     * user-agent=Nokia-WAP-Toolkit/1.3beta
     * accept=text/vnd.wap.wml,text/vnd.wap.wmlscript,application/vnd.wap.wmlc,
     *        application/vnd.wap.wmlscriptc, image/vnd.wap.wbmp, image/gif
     *
     * user-agent=Mozilla/4.71 [en] (WinNT; I)
     * accept=image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png,
     *        (asterisk)/(asterisk)
     *
     * Nokia 7110 wap phone over iobox demo gateway
     * user-agent=null
     * accept=null
     *
     * </pre>
     *
     * @return "html" or "wml" depending on request
     */
    public static String getDocumentType(HttpServletRequest req) {
        String accept = req.getHeader(HEADER_ACCEPT);
        if (accept != null && accept.indexOf(ACCEPT_WML) >= 0) {
            return DOCUMENT_TYPE_WML;
        }
        // Nokia WAP phone etc
        if (accept == null) {
            return DOCUMENT_TYPE_WML;
        }
        return DOCUMENT_TYPE_HTML;
    }

    /**
     * @param documentType "html" or "wml"
     * @return "text/html" or "text/vnd.wap.wml" depending on document type
     */
    public static String getContentType(String documentType) {
        if (documentType.equals(DOCUMENT_TYPE_WML)) {
            return CONTENT_TYPE_WML;
        } else {
            return CONTENT_TYPE_HTML;
        }
    }

    public static String getContentType(HttpServletRequest req) {
        return getContentType(getDocumentType(req));
    }

    public static void debugRequest(ServletConfig config, HttpServletRequest request) {

     try {
        anvil.Log.log().debug("Init Parameters");
        if (config != null) {
          Enumeration e = config.getInitParameterNames();
          if (e != null) {
            while (e.hasMoreElements()) {
                String key = (String)e.nextElement();
                String value = config.getInitParameter(key);
                anvil.Log.log().debug("   " + key + " = " + value);
            }
          }
        }

      /***
        anvil.Log.log().debug("Context attributes:");
        ServletContext context = config.getServletContext();
        Enumeration enum = context.getAttributeNames();
        while (enum.hasMoreElements()) {
            String key = (String)enum.nextElement();
            Object value = context.getAttribute(key);
            anvil.Log.log().debug("   " + key + " = " + value);
        }
      ***/

        anvil.Log.log().debug("javax.servlet.Servlet methods");

      /***
        anvil.Log.log().debug("Attribute names in this request:");
        e = request.getAttributeNames();
        while (e.hasMoreElements()) {
            String key = (String)e.nextElement();
            Object value = request.getAttribute(key);
            anvil.Log.log().debug("   " + key + " = " + value);
        }
      ***/

        anvil.Log.log().debug("Protocol: " + request.getProtocol());
        anvil.Log.log().debug("Scheme: " + request.getScheme());
        anvil.Log.log().debug("Server Name: " + request.getServerName());
        anvil.Log.log().debug("Server Port: " + request.getServerPort());
        try {
          anvil.Log.log().debug("Server Info: " + config.getServletContext().getServerInfo());
        } catch (Exception ex) {
        }
        anvil.Log.log().debug("Remote Addr: " + request.getRemoteAddr());
        anvil.Log.log().debug("Remote Host: " + request.getRemoteHost());
        anvil.Log.log().debug("Character Encoding: " + request.getCharacterEncoding());
        anvil.Log.log().debug("Content Length: " + request.getContentLength());
        anvil.Log.log().debug("Content Type: "+ request.getContentType());
        anvil.Log.log().debug("Parameter names in this request");
        Enumeration e = request.getParameterNames();
        while (e.hasMoreElements()) {
            String key = (String)e.nextElement();
            String[] values = request.getParameterValues(key);
            anvil.Log.log().debug("   " + key + " = ");
            for(int i = 0; i < values.length; i++) {
                anvil.Log.log().debug(values[i]);
            }
        }
        anvil.Log.log().debug("javax.servlet.http.HttpServletRequest methods");
        anvil.Log.log().debug("Headers in this request");
        e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String key = (String)e.nextElement();
            String value = request.getHeader(key);
            anvil.Log.log().debug("   " + key + " : " + value);
        }
        anvil.Log.log().debug("Cookies in this request");
        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            anvil.Log.log().debug("   " + cookie.getName() + " = " + cookie.getValue());
        }
        anvil.Log.log().debug("Auth Type: " + request.getAuthType());
        anvil.Log.log().debug("HTTP Method: " + request.getMethod());
        anvil.Log.log().debug("Path Info: " + request.getPathInfo());
        anvil.Log.log().debug("Path Trans: " + request.getPathTranslated());
        anvil.Log.log().debug("Query String: " + request.getQueryString());
        anvil.Log.log().debug("Remote User: " + request.getRemoteUser());
        anvil.Log.log().debug("Session Id: " + request.getRequestedSessionId());
        anvil.Log.log().debug("Request URI: " + request.getRequestURI());
        anvil.Log.log().debug("Servlet Path: " + request.getServletPath());

        anvil.Log.log().debug("Session Information");
        HttpSession session = request.getSession(true);
        anvil.Log.log().debug("Created: " + session.getCreationTime());
        anvil.Log.log().debug("ID: " + session.getId());
        anvil.Log.log().debug("Last Accessed: " + session.getLastAccessedTime());
      /***
        anvil.Log.log().debug("Max Inactive Interval: " +
                    session.getMaxInactiveInterval());
      ***/
        anvil.Log.log().debug("Values: ");
        String[] valueNames = session.getValueNames();
        for (int i = 0; i < valueNames.length; i++) {
            anvil.Log.log().debug("   " + valueNames[i] + " = " +
                        session.getValue(valueNames[i]));
        }
      } catch (Exception e) {
          anvil.Log.log().error(e.toString());
      }
    }
}
TOP

Related Classes of anvil.util.RequestUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.