package com.chap.memo.memoNodes;

import java.io.IOException;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eaio.uuid.UUID;

@SuppressWarnings("serial")
final public class JsonExport extends HttpServlet {

	public static boolean get_CORS_headers(java.util.HashMap<String,String> ret, java.util.HashMap<String,String> httpHeaders)
	{
		Boolean isAllowed = true;
		String s = httpHeaders.get( "Origin" );	
		if(s==null) {
				String ref_s = httpHeaders.get( "Referer");
				if (ref_s != null){
					try {
						URL url = new URL(ref_s);
						s = url.getProtocol()+"://"+url.getAuthority();
					} catch(Exception e) {
						s=null;
					}
				}

		}
		//End of Android Hack.
		
		if( s != null )
		{
			ret.put("Access-Control-Allow-Origin", s );
	
			ret.put("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
			ret.put("Access-Control-Allow-Credentials", "true" );
			ret.put("Access-Control-Max-Age", "60" );
			
			String returnMethod = httpHeaders.get("Access-Control-Request-Headers");	//what?
			if (!"".equals(returnMethod)) {
				ret.put("Access-Control-Allow-Headers", returnMethod);
			}
		}
		
		return isAllowed;
	}
	public boolean makeCORS(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
	{
		//copy headers into the next collection
		java.util.HashMap<String,String> headers = new java.util.HashMap<String, String>();
		@SuppressWarnings("unchecked")
		java.util.Enumeration<String> headerNames = req.getHeaderNames();
		while( headerNames.hasMoreElements() )
		{
			String name = headerNames.nextElement();
			headers.put( name, req.getHeader(name) );
		}
		
		java.util.HashMap<String,String> CORS_headers = new java.util.HashMap<String,String>();
		boolean isAllowed = get_CORS_headers( CORS_headers, headers );
		
		if(isAllowed)
			for( java.util.Map.Entry<String,String> entry : CORS_headers.entrySet() )
				res.setHeader( entry.getKey(),entry.getValue() );
	
		return isAllowed;
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		int maxdepth = 10;
		String depth = req.getParameter("maxdepth");
		if (depth != null){
			try{
				maxdepth = Integer.parseInt(depth);
			} catch (Exception e){}
		}
		
		String NodeId = req.getParameter("nodeId");
		try {
			makeCORS(req,resp);
			
			if (NodeId != null){
				MemoNode node = new MemoNode(new UUID(NodeId));
				resp.setContentType("application/json");
				resp.getWriter().println(
						node.toJSONString(maxdepth)
				);
			}
		} catch (Exception e){
			System.out.println("Warning: UUID not parsible");
		}
	}
}

@@ -2210,16 +2210,171 @@
 on %7B%0A%09%09%0A
+%09%09String cleanDBParm = req.getParameter(%22cleanDB%22);%0A%09%09if (cleanDBParm != null) %7B%0A%09%09%09MemoNode.emptyDB();%09%0A%09%09%09if (cleanDBParm.equals(%22only%22)) return;%0A%09%09%7D%0A%09%09%0A
 %09%09int ma

Add CleanDB parameter to JSON export servlet.