⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pi3servletutils.java

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 JAVA
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission. 

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Servlet/org/pi3/servlet/util/Pi3ServletUtils.java,v $
 * $Date: 2003/05/13 18:42:22 $
 *
 Description:
	Utility class, providing some usefull methods.

\*____________________________________________________________________________*/

package org.pi3.servlet.util;

import java.io.Writer;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Locale;
import java.util.StringTokenizer;
import javax.servlet.http.Cookie;
import javax.servlet.SingleThreadModel;



final public class Pi3ServletUtils {

    private int hobj;
    private int hsrv;




	/** This constructor is used by the native side */
    public Pi3ServletUtils(int pihttp, int hservlet) {
		hobj = pihttp;
	    hsrv = hservlet;
	}




	/** The method to get the PiHttp object from the native side */
	protected int getPiHttp() {
		return hobj;
	}




	/** The method to get the Servlet handler object from the native side */
	protected int getServletHandler() {
		return hsrv;
	}




    public String grabStackTrace( Throwable t ) {
        StringWriter sw = new StringWriter();
        PrintWriter w = new PrintWriter(sw);
        if ( t != null ) t.printStackTrace(w);
        return sw.toString();
    }




	public String getDefaultCharset() {
	    return org.pi3.servlet.core.Constants.DEFAULT_CHARSET;
	}




	public String getDefaultLanguage() {
	    return org.pi3.servlet.core.Constants.DEFAULT_LOCALE;
	}




	public Enumeration getLocales() {
        String acceptLanguage = getRequestVariable("Accept-Language");
        if (acceptLanguage == null) {
            Vector def = new Vector();
            def.addElement(Locale.getDefault());
            return def.elements();
        }

        Hashtable languages = new Hashtable();

        StringTokenizer languageTokenizer =
            new StringTokenizer(acceptLanguage, ",");

        while (languageTokenizer.hasMoreTokens()) {
            String language = languageTokenizer.nextToken().trim();
            int qValueIndex = language.indexOf(';');
            int qIndex = language.indexOf('q');
            int equalIndex = language.indexOf('=');
            Double qValue = new Double(1);

            if (qValueIndex > -1 &&
                qValueIndex < qIndex &&
                qIndex < equalIndex) {
	        String qValueStr = language.substring(qValueIndex + 1);

                language = language.substring(0, qValueIndex);
                qValueStr = qValueStr.trim().toLowerCase();
                qValueIndex = qValueStr.indexOf('=');
                qValue = new Double(0);

                if (qValueStr.startsWith("q") &&
                    qValueIndex > -1) {
                    qValueStr = qValueStr.substring(qValueIndex + 1);

                    try {
                        qValue = new Double(qValueStr.trim());
                    } catch (NumberFormatException nfe) {
                    }
                }
            }

	    // XXX
	    // may need to handle "*" at some point in time

	    if (! language.equals("*")) {
	        String key = qValue.toString();
		Vector v = (Vector)((languages.containsKey(key)) ?
		    languages.get(key) : new Vector());

		v.addElement(language);
		languages.put(key, v);
	    }
        }

        if (languages.size() == 0) {
            Vector v = new Vector();

            v.addElement(org.pi3.servlet.core.Constants.DEFAULT_LOCALE);
            languages.put("1.0", v);
        }

        Vector l = new Vector();
        Enumeration e = languages.keys();

        while (e.hasMoreElements()) {
            String key = (String)e.nextElement();
            Vector v = (Vector)languages.get(key);
            Enumeration le = v.elements();

            while (le.hasMoreElements()) {
	        String language = (String)le.nextElement();
		String country = "";
		int countryIndex = language.indexOf("-");

		if (countryIndex > -1) {
		    country = language.substring(countryIndex + 1).trim();
		    language = language.substring(0, countryIndex).trim();
		}

                l.addElement(new Locale(language, country));
            }
        }

        return l.elements();
	}




	public boolean isSingleThread(Object obj) {
		return (obj instanceof SingleThreadModel);
	}




    public native String getRequestVariable(String name);
	public native void getRequestVariables(Hashtable hreq);
    public native String getResponseVariable(String name);
    public native void addResponseVariable(String name, String value);
    public native void setResponseVariable(String name, String value);
    public native String getHttpDate(long date);
    public native long getHttpDate(String date);
    public native int sendError(int sc) throws IOException;
    public native void addCookie(Cookie cookie);

    static {
        System.loadLibrary("Pi3ServletJNI");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -