package com.khs.sherpa.servlet;

/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import static com.khs.sherpa.util.Util.msg;

import java.io.IOException;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.khs.sherpa.context.GenericApplicationContext;
import com.khs.sherpa.exception.SherpaInvalidUsernamePassword;
import com.khs.sherpa.exception.SherpaRuntimeException;
import com.khs.sherpa.servlet.request.DefaultSherpaRequest;

public class SherpaServlet extends HttpServlet {

	private static final long serialVersionUID = 4345668988238038540L;	
	
	private static final Logger LOGGER = LoggerFactory.getLogger(SherpaServlet.class);

	@Override
	public void init() throws ServletException {
		super.init();
	}

	private void doService(HttpServletRequest request, HttpServletResponse response) throws RuntimeException, IOException {
		try {
			
			DefaultSherpaRequest sherpaRequest = new DefaultSherpaRequest();
			sherpaRequest.setApplicationContext(GenericApplicationContext.getApplicationContext(getServletContext()));

			sherpaRequest.doService(request, response);
			
		} catch (SherpaInvalidUsernamePassword e) {
			response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid Username or Password!");
			LOGGER.info(msg("INFO "+e.getMessage() ));			
		} catch (SherpaRuntimeException e) {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Sherpa Error "+e.getMessage());
			LOGGER.error(msg("ERROR "+e.getMessage() ));
		} catch (Exception e) {
			throw new SherpaRuntimeException(e);
		} finally {
			// do nothing right now
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

	@Override
	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

	@Override
	protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

	@Override
	protected long getLastModified(HttpServletRequest req) {
		return super.getLastModified(req);
	}

	@Override
	protected void doHead(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doHead(req, resp);
	}
	
	@Override
	protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doOptions(req, resp);
	}

	@Override
	protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doTrace(req, resp);
	}
	
}

@@ -1935,39 +1935,39 @@
 %0D%0A%09%09%09response.se
-ndError
+tStatus
 (HttpServletResp
@@ -1990,40 +1990,56 @@
 IZED
-, %22Invalid Username or Password!
+);%0D%0A%09%09%09response.setContentType(%22application/json
 %22);%0D
@@ -2147,15 +2147,15 @@
 e.se
-ndError
+tStatus
 (Htt
@@ -2199,39 +2199,57 @@
 RROR
-,%22Sherpa Error %22+e.getMessage()
+);%0D%0A%09%09%09response.setContentType(%22application/json%22
 );%0D%0A
@@ -2331,42 +2331,120 @@
 %0A%09%09%09
-throw new SherpaRuntimeException(e
+response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);%0D%0A%09%09%09response.setContentType(%22application/json%22
 );%0D%0A

Make the error return as a JSON object