copper-server/src/main/java/com/pogeyan/cmis/browser/shared/POSTHttpServletRequestWrapper.java
/**
	 * Parses a form data request
	 * 
	 * @param request the request
	 * @return {@code true} if the body contained data, {@code false} otherwise
	 */
Parses a form data request

@param request the request
@return  true if the body contained data,  false otherwise
Parses a form data request @param request the request @return true if the body contained data false otherwise
protected boolean parseFormUrlEncodedData(HttpServletRequest request) throws IOException {
		byte[] data = new byte[BUFFER_SIZE];
		int dataPos = 0;
		InputStream stream = request.getInputStream();
		int b;
		byte[] buffer = new byte[BUFFER_SIZE];
		while ((b = stream.read(buffer)) != -1) {
			if (dataPos + b > MAX_CONTENT_BYTES) {
				throw new CmisInvalidArgumentException("Limit exceeded!");
			}
			if (data.length - dataPos < b) {
				int newSize = ((data.length + b) * 2 < MAX_CONTENT_BYTES ? (data.length + b * 2) : MAX_CONTENT_BYTES);
				byte[] newbuf = new byte[newSize];
				System.arraycopy(data, 0, newbuf, 0, dataPos);
				data = newbuf;
			}
			System.arraycopy(buffer, 0, data, dataPos, b);
			dataPos += b;
		}
		if (dataPos == 0) {
			return false;
		}
		boolean parseName = true;
		boolean parseCharset = false;
		int startPos = 0;
		List<String[]> rawParameters = new ArrayList<String[]>();
		String rawName = null;
		String rawValue = null;
		String charset = null;
		for (int i = 0; i < dataPos; i++) {
			switch (data[i]) {
			case '=':
				if (startPos < i) {
					rawName = new String(data, startPos, i - startPos, IOUtils.ISO_8859_1);
					if (CHARSET_FIELD.equalsIgnoreCase(rawName)) {
						parseCharset = true;
					}
				}
				parseName = false;
				startPos = i + 1;
				break;
			case '&':
				if (parseName) {
					if (startPos < i) {
						rawName = new String(data, startPos, i - startPos, IOUtils.ISO_8859_1);
						rawParameters.add(new String[] { rawName, null });
					}
				} else {
					if (rawName != null) {
						rawValue = new String(data, startPos, i - startPos, IOUtils.ISO_8859_1);
						rawParameters.add(new String[] { rawName, rawValue });
						if (parseCharset) {
							charset = rawValue;
						}
					}
				}
				rawName = null;
				rawValue = null;
				parseName = true;
				parseCharset = false;
				startPos = i + 1;
				break;
			default:
				break;
			}
		}
		if (startPos < dataPos) {
			if (parseName) {
				rawName = new String(data, startPos, dataPos - startPos, IOUtils.ISO_8859_1);
				rawParameters.add(new String[] { rawName, null });
			} else {
				if (rawName != null) {
					rawValue = new String(data, startPos, dataPos - startPos, IOUtils.ISO_8859_1);
					rawParameters.add(new String[] { rawName, rawValue });
					if (parseCharset) {
						charset = rawValue;
					}
				}
			}
		} else if (!parseName) {
			rawParameters.add(new String[] { rawName, "" });
		}
		data = null;
		if (charset == null) {
			String contentType = request.getContentType();
			if (contentType != null) {
				String[] parts = contentType.split(";");
				for (int i = 1; i < parts.length; i++) {
					String part = parts[i].trim().toLowerCase(Locale.ENGLISH);
					if (part.startsWith("charset")) {
						int x = part.indexOf('=');
						charset = part.substring(x + 1).trim();
						break;
					}
				}
			}
		}
		if (charset == null) {
			charset = IOUtils.UTF8;
		}
		for (String[] rawParameter : rawParameters) {
			String name = URLDecoder.decode(rawParameter[0], charset);
			String value = null;
			if (rawParameter[1] != null) {
				value = URLDecoder.decode(rawParameter[1], charset);
			}
			addParameter(name, value);
		}
		return true;
	}
protected boolean parseFormUrlEncodedData ( HttpServletRequest request ) throws IOException { byte [ ] data = new byte [ BUFFER_SIZE ] ; int dataPos = 0 ; InputStream stream = request . getInputStream ( ) ; int b ; byte [ ] buffer = new byte [ BUFFER_SIZE ] ; while ( ( b = stream . read ( buffer ) ) != - 1 ) { if ( dataPos + b > MAX_CONTENT_BYTES ) { throw new CmisInvalidArgumentException ( "Limit exceeded!" ) ; } if ( data . length - dataPos < b ) { int newSize = ( ( data . length + b ) * 2 < MAX_CONTENT_BYTES ? ( data . length + b * 2 ) : MAX_CONTENT_BYTES ) ; byte [ ] newbuf = new byte [ newSize ] ; System . arraycopy ( data , 0 , newbuf , 0 , dataPos ) ; data = newbuf ; } System . arraycopy ( buffer , 0 , data , dataPos , b ) ; dataPos += b ; } if ( dataPos == 0 ) { return false ; } boolean parseName = true ; boolean parseCharset = false ; int startPos = 0 ; List < String [ ] > rawParameters = new ArrayList < String [ ] > ( ) ; String rawName = null ; String rawValue = null ; String charset = null ; for ( int i = 0 ; i < dataPos ; i ++ ) { switch ( data [ i ] ) { case '=' : if ( startPos < i ) { rawName = new String ( data , startPos , i - startPos , IOUtils . ISO_8859_1 ) ; if ( CHARSET_FIELD . equalsIgnoreCase ( rawName ) ) { parseCharset = true ; } } parseName = false ; startPos = i + 1 ; break ; case '&' : if ( parseName ) { if ( startPos < i ) { rawName = new String ( data , startPos , i - startPos , IOUtils . ISO_8859_1 ) ; rawParameters . add ( new String [ ] { rawName , null } ) ; } } else { if ( rawName != null ) { rawValue = new String ( data , startPos , i - startPos , IOUtils . ISO_8859_1 ) ; rawParameters . add ( new String [ ] { rawName , rawValue } ) ; if ( parseCharset ) { charset = rawValue ; } } } rawName = null ; rawValue = null ; parseName = true ; parseCharset = false ; startPos = i + 1 ; break ; default : break ; } } if ( startPos < dataPos ) { if ( parseName ) { rawName = new String ( data , startPos , dataPos - startPos , IOUtils . ISO_8859_1 ) ; rawParameters . add ( new String [ ] { rawName , null } ) ; } else { if ( rawName != null ) { rawValue = new String ( data , startPos , dataPos - startPos , IOUtils . ISO_8859_1 ) ; rawParameters . add ( new String [ ] { rawName , rawValue } ) ; if ( parseCharset ) { charset = rawValue ; } } } } else if ( ! parseName ) { rawParameters . add ( new String [ ] { rawName , "" } ) ; } data = null ; if ( charset == null ) { String contentType = request . getContentType ( ) ; if ( contentType != null ) { String [ ] parts = contentType . split ( ";" ) ; for ( int i = 1 ; i < parts . length ; i ++ ) { String part = parts [ i ] . trim ( ) . toLowerCase ( Locale . ENGLISH ) ; if ( part . startsWith ( "charset" ) ) { int x = part . indexOf ( '=' ) ; charset = part . substring ( x + 1 ) . trim ( ) ; break ; } } } } if ( charset == null ) { charset = IOUtils . UTF8 ; } for ( String [ ] rawParameter : rawParameters ) { String name = URLDecoder . decode ( rawParameter [ 0 ] , charset ) ; String value = null ; if ( rawParameter [ 1 ] != null ) { value = URLDecoder . decode ( rawParameter [ 1 ] , charset ) ; } addParameter ( name , value ) ; } return true ; }
Parses a form data request
Parses a form data request
// read stream // expand buffer // empty stream // parse parameters // there is a final parameter after the last '&' // the stream ended with '=' // find charset // check charset in content type // set default charset // decode parameters