package com.webbricks.controllers;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import com.webbricks.cmsdata.WBUri;

public class WBUriValidator {
	public final static int MAX_URI_LENGHT = 100;
	public final static int MAX_PAGENAME_LENGHT = 250;
	private HashSet<String> httpOperations;

	public WBUriValidator()
	{
		httpOperations = new HashSet<String>();
		httpOperations.add("PUT");
		httpOperations.add("POST");
		httpOperations.add("GET");
		httpOperations.add("DELETE");
	}
	public Map<String, String> validateUpdate(WBUri wbUri)
	{
		Map<String, String> errors = new HashMap<String, String>();
		if (wbUri.getUri() == null || wbUri.getUri().length() == 0)
		{
			errors.put("uri", WBErrors.ERROR_URI_LENGTH);
		} else
		if (wbUri.getUri().indexOf("/") != 0)
		{
			errors.put("uri", WBErrors.ERROR_URI_START_CHAR);
		} else
		if (wbUri.getUri().length() > MAX_URI_LENGHT)
		{
			errors.put("uri", WBErrors.ERROR_URI_LENGTH);
		} else
		{
			if (! wbUri.getUri().matches("/([0-9a-zA-Z_~.-]*/?[0-9a-zA-Z_~.-]*)*"))
			{
				errors.put("uri", WBErrors.ERROR_URI_BAD_FORMAT);
			}
		}
		
		if (wbUri.getHttpOperation() == null || wbUri.getHttpOperation().length() == 0)
		{
			errors.put("httpOperation", WBErrors.ERROR_INVALID_HTTP_OPERATION);
		} else if (! httpOperations.contains( wbUri.getHttpOperation().toUpperCase()))
		{
			errors.put("httpOperation", WBErrors.ERROR_INVALID_HTTP_OPERATION);
		}
		if (wbUri.getPageName() == null || wbUri.getPageName().length() == 0 || wbUri.getPageName().length()> this.MAX_PAGENAME_LENGHT)
		{
			errors.put("pageName", WBErrors.ERROR_PAGENAME_LENGTH);
		} else
		if (!wbUri.getPageName().matches("[0-9a-zA-Z_.-]*")) 
		{
			errors.put("pageName", WBErrors.ERROR_PAGE_BAD_FORMAT);
		}

		if (wbUri.getControllerClass()!= null && !wbUri.getControllerClass().matches("[0-9a-zA-Z_.-]*"))
		{
			errors.put("controllerClass", WBErrors.ERROR_CONTROLLER_BAD_FORMAT);
		}
		
		if ((wbUri.getKey() == null) || (wbUri.getKey() == 0))
		{
			errors.put("key", WBErrors.ERROR_NO_KEY);
		}
		if (wbUri.getLastModified() != null)
		{
			errors.put("lastModified", WBErrors.ERROR_CANT_SPECIFY_LAST_MODIFIED);
		}
		return errors;
	}
	
	public Map<String, String> validateCreate(WBUri wbUri)
	{
		Map<String, String> errors = new HashMap<String, String>();
		if (wbUri.getUri() == null || wbUri.getUri().length() == 0)
		{
			errors.put("uri", WBErrors.ERROR_URI_LENGTH);
		} else
		if (wbUri.getUri().indexOf("/") != 0)
		{
			errors.put("uri", WBErrors.ERROR_URI_START_CHAR);
		}else
		if (wbUri.getUri().length() > MAX_URI_LENGHT)
		{
			errors.put("uri", WBErrors.ERROR_URI_LENGTH);
		} else
		{
			if (! wbUri.getUri().matches("/([0-9a-zA-Z_~.-]*/?[0-9a-zA-Z_~.-]*)*"))
			{
				errors.put("uri", WBErrors.ERROR_URI_BAD_FORMAT);
			}
		}
		if (wbUri.getHttpOperation() == null || wbUri.getHttpOperation().length() == 0)
		{
			errors.put("httpOperation", WBErrors.ERROR_INVALID_HTTP_OPERATION);
		} else if (! httpOperations.contains( wbUri.getHttpOperation().toUpperCase()))
		{
			errors.put("httpOperation", WBErrors.ERROR_INVALID_HTTP_OPERATION);
		}
		if (wbUri.getPageName() == null || wbUri.getPageName().length() == 0 || wbUri.getPageName().length()> this.MAX_PAGENAME_LENGHT)
		{
			errors.put("pageName", WBErrors.ERROR_PAGENAME_LENGTH);
		}else
		if (!wbUri.getPageName().matches("[0-9a-zA-Z_.-]*")) 
		{
			errors.put("pageName", WBErrors.ERROR_PAGE_BAD_FORMAT);
		}
		if (wbUri.getControllerClass()!= null && !wbUri.getControllerClass().matches("[0-9a-zA-Z_.-]*"))
		{
			errors.put("controllerClass", WBErrors.ERROR_CONTROLLER_BAD_FORMAT);
		}
		if (wbUri.getKey() != null)
		{
			errors.put("key", WBErrors.ERROR_CANT_SPECIFY_KEY);
		}
		if (wbUri.getLastModified() != null)
		{
			errors.put("lastModified", WBErrors.ERROR_CANT_SPECIFY_LAST_MODIFIED);
		}
		return errors;
	}
	
}

@@ -1050,34 +1050,57 @@
 %5B0-9a-zA-Z_~.-%5D*
-/?
+(%5C%5C%7B%5B0-9a-zA-Z_.*-%5D+%5C%5C%7D)*
 %5B0-9a-zA-Z_~.-%5D*
@@ -1091,32 +1091,34 @@
 %5B0-9a-zA-Z_~.-%5D*
+/?
 )*%22))%0D%0A%09%09%09%7B%0D%0A%09%09%09
@@ -2825,18 +2825,41 @@
 -Z_~.-%5D*
-/?
+(%5C%5C%7B%5B0-9a-zA-Z_.*-%5D+%5C%5C%7D)*
 %5B0-9a-zA
@@ -2866,16 +2866,18 @@
 -Z_~.-%5D*
+/?
 )*%22))%0D%0A%09

update uri validator for {*} and {param} cases