
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

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

import uws.UWSException;
import uws.UWSToolBox;

import uws.job.JobList;

import uws.service.UserIdentifier;

import uws.service.controller.ExecutionDurationController;
import uws.service.controller.DestructionTimeController;
import uws.service.controller.DestructionTimeController.DateField;

/*
 * Upper-case strings are types of UWS or Job to change for your own servlet. 
 * They are: UWS_TYPE and JOB_TYPE.
 * 
 * A TODO comment with the content "CUSTOMIZE" is put at the end of
 * each line or before a block in which you must change some variables in function of your own service.
 * 
 * Commented codes are optional customizations.
 */
public class UWSServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected UWS_TYPE<JOB_TYPE> uws = null;		/* TODO [CUSTOMIZE] UWS_TYPE and JOB_TYPE */
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		
		// Create your UWS:
		uws = new UWS_TYPE<JOB_TYPE>();								/* TODO [CUSTOMIZE] UWS_TYPE and JOB_TYPE (and constructor parameters, if any) */
		
		// Create your jobs list(s):
		uws.addJobList(new JobList<JOB_TYPE>("JOB_LIST_NAME"));		/* TODO [CUSTOMIZE] JOB_TYPE and JOB_LIST_NAME */
		
		// Set its description:
//		uws.setDescription("THE DESCRIPTION OF YOUR UWS.");				/* TODO [CUSTOMIZE] the UWS description */
		
		// Set the way the UWS must identify a user:
		/* TODO  [CUSTOMIZE] add a user identification */
/*		uws.setUserIdentifier(new UserIdentifier() {
 *			private static final long serialVersionUID = 1L;
 *
 *			@Override
 *			public String extractUserId(UWSUrl urlInterpreter, HttpServletRequest request) throws UWSException {
 *				String userId = null;
 *				// TODO Do what is needed to extract the user ID !
 *				return userId;
 *			}
 *		});
 */
		
		// Set the destruction time for all jobs:
		/* TODO [CUSTOMIZE] the maximum and the default destruction time and whether this field can be modified */
//		DestructionTimeController destructionController = uws.getDestructionTimeController();
//		destructionController.setMaxDestructionInterval(1, DateField.YEAR);
//		destructionController.setDefaultDestructionInterval(1, DateField.MONTH);
//		destructionController.allowModification(false);
		
		// Set the execution duration for all jobs:
		/* TODO [CUSTOMIZE] the maximum and the default execution duration and whether this field can be modified */
//		ExecutionDurationController durationController = uws.getExecutionDurationController();
//		durationController.setMaxExecutionDuration(3600);
//		durationController.setDefaultExecutionDuration(3600);
//		durationController.allowModification(false);
		
		// Set the XSLT URL:
//		uws.setXsltURL(XSLT_PATH);				/* TODO [CUSTOMIZE] XSLT_PATH */
		
		// Set the UWS home page:
//		uws.setHomePage(UWSToolBox.getServerResource("UWS_HOME_PAGE.HTML", req), true);			/* TODO [CUSTOMIZE] the UWS home page */
	}

	@Override
	public void destroy() {
		/* TODO [CUSTOMIZE] Save your UWS ? Destroy something else ? */
		super.destroy();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try{
			// Forward the request to the UWS:
			boolean done = uws.executeRequest(req, resp);
			
			// Display some information about the execution of the request:
			System.out.println("### UWS INFO: REQUEST \""+uws.getExecutedAction()+"\" ? "+(done?"OK":"NOT DONE")+" ###");
				
		}catch(UWSException uwsEx){
			// Display properly the caught UWSException:
			resp.sendError(uwsEx.getHttpErrorCode(), uwsEx.getMessage());
		}
	}
	
}
