package com.nitorcreations.willow.common;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map.Entry;
import java.util.Properties;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.nitorcreations.willow.utils.MergeableProperties;

public class PropertyServlet implements Servlet {
	ServletConfig config;
	@Override
	public void init(ServletConfig config) throws ServletException {
		this.config = config;
	}
	@Override
	public ServletConfig getServletConfig() {
		return config;
	}
	@Override
	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
		if (!((HttpServletRequest)req).getMethod().equals("GET")) {
			((HttpServletResponse)res).sendError(405, "Only GET allowed");
			return;
		}
		String rootProperties = ((HttpServletRequest)req).getPathInfo().substring(1);
		MergeableProperties mrg = null;
		if (config.getInitParameter("property.roots") != null) {
			String roots = config.getInitParameter("property.roots");
			if (!roots.isEmpty()) {
				mrg = new MergeableProperties(roots.split("\\|"));
			} else {
				mrg = new MergeableProperties();
			}
		} else {
			mrg = new MergeableProperties();
		}
		Properties seed = new Properties();
		for (Entry<String, String[]> next : ((HttpServletRequest)req).getParameterMap().entrySet()) {
			seed.setProperty(next.getKey(), StringUtils.join(next.getValue(), ","));
		}
		res.setContentType("text/plain;charset=utf-8");
		((HttpServletResponse)res).setStatus(200);
		Properties result = mrg.merge(seed, rootProperties);
		try (OutputStream out = res.getOutputStream()){
			result.store(out, null);
			out.flush();
		}
	}
	@Override
	public String getServletInfo() {
		return "PropertiesServlet";
	}
	@Override
	public void destroy() {
	}
}
@@ -93,16 +93,46 @@
 Stream;%0A
+import java.util.Enumeration;%0A
 import j
@@ -455,16 +455,85 @@
 ponse;%0A%0A
+import org.apache.commons.collections.iterators.EnumerationIterator;%0A
 import o
@@ -1774,16 +1774,265 @@
 ));%0A%09%09%7D%0A
+%09%09Enumeration%3CString%3E it = ((HttpServletRequest)req).getHeaderNames();%0A%09%09while (it.hasMoreElements()) %7B%0A%09%09%09String key = it.nextElement();%0A%09%09%09String value = ((HttpServletRequest)req).getHeader(key);%0A%09%09%09seed.setProperty(key.toLowerCase(), value);%0A%09%09%7D%0A
 %09%09res.se

Add headers to seed