package de.taimos.springcxfdaemon;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.RedirectionException;
import javax.ws.rs.core.Response.Status;

import org.apache.cxf.jaxrs.ext.MessageContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import com.google.common.base.Charsets;

import de.taimos.restutils.RESTAssert;
import de.taimos.springcxfdaemon.monitoring.InvocationInstance;

public class AbstractAPI implements IContextAware {
	
	protected MessageContext context;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	
	@Value("${server.url:http://localhost:${jaxrs.bindport:${svc.port:8080}}}")
	private String serverURL;
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	
	@Override
	public void setMessageContext(MessageContext context) {
		this.context = context;
	}
	
	@Override
	public void setHttpServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	@Override
	public void setHttpServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	protected final String getFirstHeader(String name) {
		return this.request.getHeader(name);
	}
	
	protected final void redirectPath(String path) {
		this.redirect(this.getServerURL() + path);
	}
	
	protected String getServerURL() {
		return this.serverURL;
	}
	
	protected final void redirect(String uriString) {
		throw new RedirectionException(Status.SEE_OTHER, URI.create(uriString));
	}
	
	protected final UUID requestId() {
		final InvocationInstance ii = this.context.getContent(InvocationInstance.class);
		RESTAssert.assertNotNull(ii, Status.INTERNAL_SERVER_ERROR);
		return ii.getMessageId();
	}
	
	protected String getCurrentURIEncoded() {
		try {
			return URLEncoder.encode(this.getCurrentURI(), Charsets.UTF_8.name());
		} catch (UnsupportedEncodingException e) {
			throw new InternalServerErrorException(e);
		}
	}
	
	protected String getCurrentURI() {
		final String path = this.context.getHttpServletRequest().getRequestURI();
		final String query = this.context.getHttpServletRequest().getQueryString();
		if (query != null) {
			return this.getServerURL() + path + "?" + query;
		}
		return this.getServerURL() + path;
	}
	
}
@@ -281,32 +281,75 @@
 ErrorException;%0A
+import javax.ws.rs.NotAuthorizedException;%0A
 import javax.ws.
@@ -409,15 +409,90 @@
 onse
-.Status
+;%0Aimport javax.ws.rs.core.Response.Status;%0Aimport javax.ws.rs.core.SecurityContext
 ;%0A%0Ai
@@ -1470,24 +1470,754 @@
 ponse;%0A%09%7D%0A%09%0A
+%09protected final SecurityContext getSC() %7B%0A%09%09SecurityContext sc = this.context.getSecurityContext();%0A%09%09return sc;%0A%09%7D%0A%09%0A%09protected final void assertSC() %7B%0A%09%09if ((this.getSC() == null) %7C%7C (this.getSC().getUserPrincipal() == null)) %7B%0A%09%09%09throw new NotAuthorizedException(Response.status(Status.UNAUTHORIZED).header(%22X-Error%22, %22Invalid apikey or session%22).build());%0A%09%09%7D%0A%09%7D%0A%09%0A%09protected final String getUser() %7B%0A%09%09SecurityContext sc = this.getSC();%0A%09%09if ((sc != null) && (sc.getUserPrincipal() != null)) %7B%0A%09%09%09return sc.getUserPrincipal().getName();%0A%09%09%7D%0A%09%09return null;%0A%09%7D%0A%09%0A%09protected final boolean hasRole(String role) %7B%0A%09%09SecurityContext sc = this.getSC();%0A%09%09if (sc != null) %7B%0A%09%09%09return sc.isUserInRole(role);%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%09%0A
 %09protected f

add auth helper methods