package net.methodyne.bellvue.core;

import javax.servlet.http.HttpServletRequest;
import java.util.StringTokenizer;
import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/** Parses HTML Form data into a java.util.Properties object with a given total size limit for easy access.
 *<br>The limit is for POST only, GET requests are usually limited elsewhere.
 *@author Yuri
 *@version 1.0
 * <br>Date: 16.09.2002
 * <br>Time: 08:59:44
 * <br> copyright Methodyne GmbH, Zug - Switzerland.
 *
 */

public class FormParser extends Properties {

    int limit = 1024 * 1024;
    String c_set = "ISO-8859-1";

    /**
     * Make a new FormParser with the given limit for bytes POSTed to the server.
     * @param request
     * @param limit
     * @param encoding charcterencoding to use - default is ISO-8859-1
     */
    public FormParser(HttpServletRequest request, int limit, String encoding) {
        super();
        this.c_set = encoding;
        this.limit = limit;
        init(request);
    }


    /**
     * Make a new FormParser with the given limit for bytes POSTed to the server.
     * @param request
     * @param limit to linit the size which can be posted.
     */
    public FormParser(HttpServletRequest request, int limit) {
        super();
        this.limit = limit;
        init(request);
    }

    /**
     * Make a new FormParser with the default POST limit of 1MB.
     * @param request
     */
    public FormParser(HttpServletRequest request) {
        super();
        init(request);

    }

    private void init(HttpServletRequest request) {
        if( request.getCharacterEncoding() != null ) c_set = request.getCharacterEncoding();
        String rdata = null;
        if (request.getMethod().equals("GET") ) {
            rdata = request.getQueryString();
        } else if (request.getMethod().equals("POST") ) {
            try {
                int clen = 0;
                InputStream in = request.getInputStream();
                if (request.getHeader("Content-Length") != null)
                    clen = Integer.parseInt(request.getHeader("Content-Length"));
                if (clen > 0) {
                    if( clen > limit ){
                        System.out.println("FormParser:Error: POST data lenght limit of " + limit + "byte exeeded.");
                    }
                    else{
                        byte uu[] = new byte[clen];
                        in.read(uu, 0, clen);
                        rdata =  new String(uu);
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (rdata == null) {
            //System.out.println("formparser: no input data ");
        } else if (rdata.length() > 0) {
            //System.out.println(rdata);
            StringTokenizer st = new StringTokenizer(rdata, "&");
            String tmp1, tmp2, tmp3;
            StringTokenizer st1;
            while (st.hasMoreTokens()) {
                tmp1 = st.nextToken();
                st1 = new StringTokenizer(tmp1, "=");
                if (st1.hasMoreTokens()) {
                    tmp2 = st1.nextToken();
                    try{
                        if (st1.hasMoreTokens()) {
                            tmp3 = tmp1.substring( tmp1.indexOf("=") +1 );
                            put( URLDecoder.decode( tmp2 ,c_set) , URLDecoder.decode( tmp3 ,c_set));
                        } else
                            put(URLDecoder.decode( tmp2 ,c_set), "");
                    }
                    catch(UnsupportedEncodingException e){ e.printStackTrace();}
                }
            }
        }
    }
}