Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
jerry-http - Common Java Functionality Copyright (c) 2012-2014, Sandeep Gupta http://sangupta.com/projects/jerry-http Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package com.sangupta.jerry.http;
 
 import java.util.List;
 import java.util.Map;
 
 
Builder class to help add form parameters to a WebRequest object.

Author(s):
sangupta
 
 public class WebForm {

Internal list of all params that have been added
 
 	private final List<NameValuePairparams;

Private constructor
 
 	private WebForm() {
 		this. = new ArrayList<NameValuePair>();
 	}

Utility function to create a new instance.

Returns:
a new instance of WebForm
 
 	public static WebForm newForm() {
 		return new WebForm();
 	}

Return the values of the parameter with the given name.

Parameters:
name the param name
Returns:
the list containing all values of the parameter, or empty list if the parameter is not present
 
 	public List<StringgetParam(final String name) {
 		List<Stringvalues = new ArrayList<String>();
 		
 		for(int index = 0; index < this..size(); index++) {
 			NameValuePair pair = this..get(index);
 			if(name.equals(pair.getName())) {
 				values.add(pair.getValue());
 			}
 		}
 		
 		return values;
 	}

Add a new parameter and value to this form. Any existing duplicates will be replaced.

Parameters:
name the param name
value the param value
Returns:
this very WebForm
 
 	public WebForm addParam(final String namefinal String value) {
		return this.addParam(namevaluefalse);
	}

Add a new parameter and value to this form.

Parameters:
name the param name
value the param value
keepDuplicates whether to preserve duplicates or not
Returns:
this very WebForm
	public WebForm addParam(final String namefinal String valuefinal boolean keepDuplicates) {
		if(AssertUtils.isEmpty(name)) {
			throw new IllegalArgumentException("Parameter name cannot be null/empty");
		}
		NameValuePair newPair = new BasicNameValuePair(namevalue);
		if(!keepDuplicates) {
			if(this..isEmpty()) {
				this..add(newPair);
				return this;
			}
			for(int index = 0; index < this..size(); index++) {
				NameValuePair pair = this..get(index);
				if(name.equals(pair.getName())) {
					// we need to replace this
					this..remove(index);
					this..add(indexnewPair);
					return this;
				}
			}
		}
		this..add(newPair);
		return this;
	}

Add all map entries to this form. This method is safe against null being passed as input parameter object. The parameters will replace any existing parameter.

Parameters:
params the params to add
Returns:
this very WebForm
	public WebForm addParams(Map<StringStringparams) {
		return this.addParams(paramsfalse);
	}

Add all map entries to this form. This method is safe against null being passed as input parameter object.

Parameters:
params the params to add
keepDuplicates whether to preserve duplicates or replace them
Returns:
this very WebForm
	public WebForm addParams(Map<StringStringparamsfinal boolean keepDuplicates) {
		if(params == null) {
			return this;
		}
		for(Entry<StringStringentry : params.entrySet()) {
			this.addParam(entry.getKey(), entry.getValue(), keepDuplicates);
		}
		return this;
	}

Check if the current web form contains any parameters or not.

Returns:
true if the form has no params added, false otherwise.
	public boolean isEmpty() {
		return this..isEmpty();
	}

Removes all of the elements from this operation. The form will be empty after this call returns.
	public void clear() {
		this..clear();
	}

Return all the params back.

Returns:
a list of all params as org.apache.http.NameValuePair
	public List<NameValuePairbuild() {
		return Collections.unmodifiableList(this.);
	}
New to GrepCode? Check out our FAQ X