All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jodd.upload.MultipartStreamParser Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.upload;

import jodd.io.FastByteArrayOutputStream;
import jodd.upload.impl.MemoryFileUploadFactory;
import jodd.util.ArraysUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Generic, serlvets-free multipart request input stream parser.
 */
public class MultipartStreamParser {

	protected FileUploadFactory fileUploadFactory;
	protected Map requestParameters;
	protected Map requestFiles;

	public MultipartStreamParser() {
		this(null);
	}

	public MultipartStreamParser(FileUploadFactory fileUploadFactory) {
		this.fileUploadFactory = (fileUploadFactory == null ? new MemoryFileUploadFactory() : fileUploadFactory);
	}

	private boolean parsed;

	/**
	 * Sets the loaded flag that indicates that input stream is loaded and parsed.
	 * Throws an exception if stream already loaded.
	 */
	protected void setParsed() throws IOException {
		if (parsed == true) {
			throw new IOException("Multi-part request already parsed");
		}
		parsed = true;
	}

	/**
	 * Returns true if multi-part request is already loaded.
	 */
	public boolean isParsed() {
		return parsed;
	}

	// ---------------------------------------------------------------- load and extract

	protected void putFile(String name, FileUpload value) {
		if (requestFiles == null) {
			requestFiles = new HashMap();
		}

		FileUpload[] fileUploads = requestFiles.get(name);

		if (fileUploads != null) {
			fileUploads = ArraysUtil.append(fileUploads, value);
		} else {
			fileUploads = new FileUpload[] {value};
		}

		requestFiles.put(name, fileUploads);
	}

	protected void putParameters(String name, String[] values) {
		if (requestParameters == null) {
			requestParameters = new HashMap();
		}
		requestParameters.put(name, values);
	}

	protected void putParameter(String name, String value) {
		if (requestParameters == null) {
			requestParameters = new HashMap();
		}
		
		String[] params = requestParameters.get(name);

		if (params != null) {
			params = ArraysUtil.append(params, value);
		} else {
			params = new String[] {value};
		}

		requestParameters.put(name, params);
	}

	/**
	 * Extracts uploaded files and parameters from the request data.
	 */
	public void parseRequestStream(InputStream inputStream, String encoding) throws IOException {
		setParsed();

		MultipartRequestInputStream input = new MultipartRequestInputStream(inputStream);
		input.readBoundary();
		while (true) {
			FileUploadHeader header = input.readDataHeader(encoding);
			if (header == null) {
				break;
			}

			if (header.isFile == true) {
				String fileName = header.fileName;
				if (fileName.length() > 0) {
					if (header.contentType.indexOf("application/x-macbinary") > 0) {
						input.skipBytes(128);
					}
				}
				FileUpload newFile = fileUploadFactory.create(input);
				newFile.processStream();
				if (fileName.length() == 0) {
					// file was specified, but no name was provided, therefore it was not uploaded
					if (newFile.getSize() == 0) {
						newFile.size = -1;
					}
				}
				putFile(header.formFieldName, newFile);
			} else {
				// no file, therefore it is regular form parameter.
				FastByteArrayOutputStream fbos = new FastByteArrayOutputStream();
				input.copyAll(fbos);
				String value = encoding != null ? new String(fbos.toByteArray(), encoding) : new String(fbos.toByteArray());
				putParameter(header.formFieldName, value);
			}

			input.skipBytes(1);
			input.mark(1);

			// read byte, but may be end of stream
			int nextByte = input.read();
			if (nextByte == -1 || nextByte == '-') {
				input.reset();
				break;
			}
			input.reset();
		}
	}

	// ---------------------------------------------------------------- parameters


	/**
	 * Returns single value of a parameter. If parameter name is used for
	 * more then one parameter, only the first one will be returned.
	 *
	 * @return parameter value, or null if not found
	 */
	public String getParameter(String paramName) {
		if (requestParameters == null) {
			return null;
		}
		String[] values = requestParameters.get(paramName);
		if ((values != null) && (values.length > 0)) {
			return values[0];
		}
		return null;
	}

	/**
	 * Returns the names of the parameters contained in this request.
	 */
	public Set getParameterNames() {
		if (requestParameters == null) {
			return Collections.emptySet();
		}
		return requestParameters.keySet();
	}

	/**
	 * Returns all values all of the values the given request parameter has.
	 */
	public String[] getParameterValues(String paramName) {
		if (requestParameters == null) {
			return null;
		}
		return requestParameters.get(paramName);
	}


	/**
	 * Returns uploaded file.
	 * @param paramName parameter name of the uploaded file
	 * @return uploaded file or null if parameter name not found
	 */
	public FileUpload getFile(String paramName) {
		if (requestFiles == null) {
			return null;
		}
		FileUpload[] values = requestFiles.get(paramName);
		if ((values != null) && (values.length > 0)) {
			return values[0];
		}
		return null;
	}


	/**
	 * Returns all uploaded files the given request parameter has.
	 */
	public FileUpload[] getFiles(String paramName) {
		if (requestFiles == null) {
			return null;
		}
		return requestFiles.get(paramName);
	}

	/**
	 * Returns parameter names of all uploaded files.
	 */
	public Set getFileParameterNames() {
		if (requestFiles == null) {
			return Collections.emptySet();
		}
		return requestFiles.keySet();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy