/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.afbb.bibo.servlet.server.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
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.google.gson.Gson;

import de.afbb.bibo.servlet.server.SessionContainer;
import de.afbb.bibo.servlet.server.Utils;

/**
 *
 * @author philipp
 */
@MultipartConfig
public class MainServlet extends HttpServlet {

	private static final long serialVersionUID = -4080327750481043213L;
	private final Gson gson = new Gson();
	private static final Logger log = LoggerFactory.getLogger(MainServlet.class);

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 *
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws NumberFormatException
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 * @throws java.lang.InterruptedException
	 * @throws org.apache.commons.fileupload.FileUploadException
	 */
	protected void processRequest(final HttpServletRequest request, final HttpServletResponse response)
			throws NumberFormatException, IOException {
		boolean valid = true;
		final String requestRoot = Utils.getRequestPart(request, 0);
		log.debug("entering MAIN Servlet...");

		if (!requestRoot.equals("/login")) {
			valid = SessionContainer.getInstance().validate(request.getHeader("sessionId"));
		}
		log.info("new request: " + request.getRequestURI());
		if (!valid) {
			response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
		} else {

			response.setContentType("application/json");

			try {
				switch (requestRoot) {
				case "/login":
					new LoginServlet(request, response).processRequest();
					break;
				case "/user":
					new UserServlet(request, response).processRequest();
					break;
				case "/stock":
					new StockServlet(request, response).processRequest();
					break;
				case "/borrow":
					new BorrowServlet(request, response).processRequest();
					break;
				default:
					Utils.returnErrorMessage(MainServlet.class, request, response);
					break;
				}
			} catch (final SQLException e) {
				response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			} catch (final IOException e) {
				response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			}
		}
	}

	@Override
	protected void doGet(final HttpServletRequest request, final HttpServletResponse response) {
		try {
			processRequest(request, response);
		} catch (final Exception ex) {
			java.util.logging.Logger.getLogger(MainServlet.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

}

@@ -2974,16 +2974,357 @@
  (final 
+NumberFormatException %7C IOException ex) %7B%0A%09%09%09java.util.logging.Logger.getLogger(MainServlet.class.getName()).log(Level.SEVERE, null, ex);%0A%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09protected void doPost(final HttpServletRequest request, final HttpServletResponse response) %7B%0A%09%09try %7B%0A%09%09%09processRequest(request, response);%0A%09%09%7D catch (final NumberFormatException %7C IO
 Exceptio

enable post in servlet