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

com.jdroid.java.http.apache.post.ApacheFormPostHttpService Maven / Gradle / Ivy

The newest version!
package com.jdroid.java.http.apache.post;

import com.jdroid.java.collections.Lists;
import com.jdroid.java.exception.UnexpectedException;
import com.jdroid.java.http.HttpServiceProcessor;
import com.jdroid.java.http.Server;
import com.jdroid.java.http.apache.HttpClientFactory;
import com.jdroid.java.utils.EncodingUtils;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.message.BasicNameValuePair;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map.Entry;

public class ApacheFormPostHttpService extends ApachePostHttpService {
	
	public ApacheFormPostHttpService(HttpClientFactory httpClientFactory, Server server, List urlSegments,
									 List httpServiceProcessors) {
		super(httpClientFactory, server, urlSegments, httpServiceProcessors);
	}
	
	/**
	 * @see ApachePostHttpService#addEntity(org.apache.http.client.methods.HttpEntityEnclosingRequestBase)
	 */
	@Override
	protected void addEntity(HttpEntityEnclosingRequestBase httpEntityEnclosingRequestBase) {
		try {
			List nameValuePairs = Lists.newArrayList();
			for (Entry entry : getQueryParameters().entrySet()) {
				nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
			httpEntityEnclosingRequestBase.setEntity(new UrlEncodedFormEntity(nameValuePairs, EncodingUtils.UTF8));
		} catch (UnsupportedEncodingException e) {
			throw new UnexpectedException(e);
		}
	}
}