com.pureinfo.srm.RequestUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.RequestUtils.java

Source

/**
 * PureInfo Quake
 * @(#)RequestUtils.java   1.0 Oct 10, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.srm;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.log4j.Logger;

import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.object.PureProperties;

/**
 * <P>
 * Created on Oct 10, 2005 9:23:45 AM <BR>
 * Last modified on Oct 10, 2005
 * </P>
 * 
 * @author Freeman.Hu
 * @version 1.0, Oct 10, 2005
 * @since Quake 1.0
 */
public class RequestUtils {
    private final static Logger logger = Logger.getLogger(RequestUtils.class.getName());

    public static PureProperties parse(HttpServletRequest _request) throws PureException {
        logger.debug("enti");
        PureProperties props = new PureProperties();

        Enumeration names = _request.getParameterNames();
        logger.debug("enti11111111111#" + names.hasMoreElements());
        while (names.hasMoreElements()) {
            String sName = (String) names.nextElement();
            String[] values = _request.getParameterValues(sName);
            if (values.length == 1) {
                props.setProperty(sName, values[0]);
            } else {
                props.setProperty(sName, values);
            }

        }

        String sContentType = _request.getContentType();
        if (sContentType != null && sContentType.startsWith("multipart/form-data")) {
            logger.debug("enti111");
            DiskFileUpload upload = new DiskFileUpload();
            List items;
            try {
                items = upload.parseRequest(_request);
            } catch (FileUploadException ex) {
                throw new PureException(PureException.UNKNOWN, "upload error", ex);
            }
            logger.debug("enti111111111111" + items.size());
            for (Iterator iter = items.iterator(); iter.hasNext();) {
                FileItem item = (FileItem) iter.next();
                if (item.getName() == null) {
                    props.setProperty(item.getFieldName(), item.getString());
                } else {
                    props.setProperty(item.getFieldName(), item);
                }
                logger.debug("name:" + item.getFieldName() + "-value:" + props.getProperty(item.getFieldName()));
            }
        }

        return props;
    }
}