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

io.github.albertus82.net.httpserver.RequestParameterExtractor Maven / Gradle / Ivy

Go to download

Java SWT/JFace Utility Library including a Preferences Framework, Lightweight HTTP Server and macOS support.

There is a newer version: 20.1.0
Show newest version
package io.github.albertus82.net.httpserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.net.httpserver.HttpExchange;

import io.github.albertus82.util.logging.LoggerFactory;

@SuppressWarnings("restriction")
public class RequestParameterExtractor {

	private static final Logger log = LoggerFactory.getLogger(RequestParameterExtractor.class);

	public static final String PREFERRED_CHARSET = "UTF-8";

	private final Map parameterMap = new HashMap<>();

	public RequestParameterExtractor(final HttpExchange exchange, final Charset charset) throws IOException {
		final Map> params = new HashMap<>();
		params.putAll(parseQueryParameters(exchange, charset));
		if (HttpMethod.POST.equalsIgnoreCase(exchange.getRequestMethod()) || HttpMethod.PUT.equalsIgnoreCase(exchange.getRequestMethod()) || HttpMethod.DELETE.equalsIgnoreCase(exchange.getRequestMethod()) || HttpMethod.PATCH.equalsIgnoreCase(exchange.getRequestMethod())) {
			params.putAll(parseBodyParameters(exchange, charset));
		}
		for (final Entry> entry : params.entrySet()) {
			parameterMap.put(entry.getKey(), entry.getValue().toArray(new String[entry.getValue().size()]));
		}
	}

	public RequestParameterExtractor(final HttpExchange exchange, final String charsetName) throws IOException {
		this(exchange, Charset.forName(charsetName));
	}

	public RequestParameterExtractor(final HttpExchange exchange) throws IOException {
		this(exchange, getDefaultCharset());
	}

	private static Charset getDefaultCharset() {
		final String charsetName = PREFERRED_CHARSET;
		try {
			return Charset.forName(charsetName);
		}
		catch (final RuntimeException e) {
			final Charset defaultCharset = Charset.defaultCharset();
			log.log(Level.WARNING, "Cannot determine charset for name \"" + charsetName + "\", falling back to " + defaultCharset + ':', e);
			return defaultCharset;
		}
	}

	public Map getParameterMap() {
		return Collections.unmodifiableMap(parameterMap);
	}

	public String[] getParameterValues(final String name) {
		return parameterMap.get(name);
	}

	public String getParameter(final String name) {
		return parameterMap.containsKey(name) ? parameterMap.get(name)[0] : null;
	}

	public Enumeration getParameterNames() {
		return Collections.enumeration(getParameterMap().keySet());
	}

	private Map> parseQueryParameters(final HttpExchange exchange, final Charset charset) throws UnsupportedEncodingException {
		final URI requestedUri = exchange.getRequestURI();
		final String query = requestedUri.getRawQuery();
		return parseQuery(query, charset);
	}

	private Map> parseBodyParameters(final HttpExchange exchange, final Charset charset) throws IOException {
		final InputStreamReader isr = new InputStreamReader(exchange.getRequestBody(), charset);
		final BufferedReader br = new BufferedReader(isr);
		final String query = br.readLine();
		return parseQuery(query, charset);
	}

	private Map> parseQuery(final String query, final Charset charset) throws UnsupportedEncodingException {
		final Map> map = new HashMap<>();
		if (query != null) {
			final String[] pairs = query.split("&");

			for (final String pair : pairs) {
				final String[] param = pair.split("=");

				String key = null;
				String value = null;
				if (param.length > 0) {
					key = URLDecoder.decode(param[0], charset.name());
				}

				if (param.length > 1) {
					value = URLDecoder.decode(param[1], charset.name());
				}

				if (!map.containsKey(key)) {
					map.put(key, new ArrayList(1));
				}
				map.get(key).add(value);
			}
		}
		return map;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy