/*******************************************************************************
 * Copyright 2016 Darren Blaber
 *
 * 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.
 *
 * 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.
 *******************************************************************************/
package org.da4.urlminimizer.web.servlets;


import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.da4.urlminimizer.UrlMinimizer;

/**
 * Servlet implementation class Redirector
 */
@WebServlet("/")
public class Redirector extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger logger = LogManager.getLogger(Redirector.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Redirector() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UrlMinimizer minimizer =  (UrlMinimizer) request.getServletContext().getAttribute("minimizer");
		logger.debug("Alias Recieved: " + request.getServletPath().substring(1));
		String url = minimizer.maximize(request.getServletPath().substring(1));
		logger.debug("Maximimized URL: " + url);
		response.sendRedirect(url);		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
	}

}

@@ -2327,16 +2327,23 @@
 ption %7B%0A
+%09%09try%7B%0A
 %09%09UrlMin
@@ -2588,80 +2588,245 @@
 ;%0A%09%09
-logger.debug(%22Maximimized URL: %22 + url);%0A%09%09response.sendRedirect(url);%09%09
+if(url == null %7C%7C url.trim().isEmpty())%0A%09%09%7B%0A%09%09%09response.sendRedirect(%22/%22);%0A%09%09%09return;%0A%09%09%7D%0A%09%09logger.debug(%22Maximimized URL: %22 + url);%0A%09%09response.sendRedirect(url);%09%09%0A%09%09%7D%0A%09%09catch (Exception e)%7B%0A%09%09%09response.sendRedirect(%22/%22);%0A%09%09%09return;%0A%09%09%7D
 %0A%09%7D%0A

Fix redirection when error