package org.opentosca.csarrepo.servlet;

import java.io.IOException;
import java.util.Map;

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.opentosca.csarrepo.model.Csar;
import org.opentosca.csarrepo.model.OpenToscaServer;
import org.opentosca.csarrepo.model.User;
import org.opentosca.csarrepo.service.ShowCsarService;
import org.opentosca.csarrepo.service.ShowOpenToscaServerService;

import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * Servlet implementation class HelloWorldServlet
 */
@SuppressWarnings("serial")
@WebServlet(OpenToscaServerDetailsServlet.PATH)
public class OpenToscaServerDetailsServlet extends AbstractServlet {

	private static final String TEMPLATE_NAME = "OpenToscaServerDetailsServlet.ftl";
	public static final String PATH = "/opentoscaserver/*";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OpenToscaServerDetailsServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User user = checkUserAuthentication(request, response);

		// FIXME: replace me with try catch once the checkUserAuthentication
		// method throws an exception
		if (null == user) {
			return;
		}

		try {
			Map<String, Object> root = getRoot(request);
			Template template = getTemplate(this.getServletContext(),
					TEMPLATE_NAME);

			// TODO: length-check
			String[] pathInfo = request.getPathInfo().split("/");
			// TODO: handle exception
			long openToscaServerId = Long.parseLong(pathInfo[1]); // {id}

			ShowOpenToscaServerService showService = new ShowOpenToscaServerService(
					user.getId(), openToscaServerId);
			if (showService.hasErrors()) {
				this.addErrors(request, showService.getErrors());
				this.redirect(
						request,
						response,
						OpenToscaServerDetailsServlet.PATH.replace("*",
								String.valueOf(openToscaServerId)));
				return;
			}

			OpenToscaServer result = showService.getResult();
			root.put("openToscaServer", result);
			root.put("cloudInstances", result.getCloudInstances());
			root.put("title",
					String.format("%s: %s", result.getId(), result.getName()));
			template.process(root, response.getWriter());
		} catch (TemplateException e) {
			response.getWriter().print(e.getMessage());
		}
	}

}

@@ -335,18 +335,41 @@
 epo.
-model.Csar
+exception.AuthenticationException
 ;%0Aim
@@ -465,63 +465,8 @@
 er;%0A
-import org.opentosca.csarrepo.service.ShowCsarService;%0A
 impo
@@ -1222,20 +1222,17 @@
 request,
-%0A%09%09%09
+ 
 HttpServ
@@ -1292,16 +1292,26 @@
 ption %7B%0A
+%0A%09%09try %7B%0A%09
 %09%09User u
@@ -1363,158 +1363,8 @@
 se);
-%0A%0A%09%09// FIXME: replace me with try catch once the checkUserAuthentication%0A%09%09// method throws an exception%0A%09%09if (null == user) %7B%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09try %7B
 %0A%09%09%09
@@ -1468,22 +1468,17 @@
 ntext(),
-%0A%09%09%09%09%09
+ 
 TEMPLATE
@@ -1738,22 +1738,16 @@
 Service(
-%0A%09%09%09%09%09
 user.get
@@ -1882,23 +1882,16 @@
 ect(
-%0A%09%09%09%09%09%09
 request,
 %0A%09%09%09
@@ -1886,23 +1886,17 @@
 request,
-%0A%09%09%09%09%09%09
+ 
 response
@@ -1954,17 +1954,9 @@
 %22*%22,
-%0A%09%09%09%09%09%09%09%09
+ 
 Stri
@@ -2178,22 +2178,17 @@
 %22title%22,
-%0A%09%09%09%09%09
+ 
 String.f
@@ -2288,16 +2288,67 @@
 ter());%0A
+%09%09%7D catch (AuthenticationException e) %7B%0A%09%09%09return;%0A
 %09%09%7D catc

fix bug caused by multiple pullrequests