package com.burgmeier.jerseyoauth2.testsuite.ui;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

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

import com.burgmeier.jerseyoauth2.api.client.ClientServiceException;
import com.burgmeier.jerseyoauth2.api.client.IClientService;
import com.burgmeier.jerseyoauth2.api.client.IRegisteredClientApp;
import com.burgmeier.jerseyoauth2.api.user.IUser;
import com.burgmeier.jerseyoauth2.api.user.IUserService;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class AllowServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final IClientService clientService;
	private final IUserService userService;
	
	@Inject
	public AllowServlet(final IClientService clientService, final IUserService userService) {
		super();
		this.clientService = clientService;
		this.userService = userService;
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		IUser user = userService.getCurrentUser(request);
		
		String clientId = request.getParameter("client_id");
		IRegisteredClientApp clientApp = clientService.getRegisteredClient(clientId);
		
		String scopes = request.getParameter("scope");
		Set<String> allowedScopes = new HashSet<String>(Arrays.asList(scopes.split(" ")));
		
		try {
			clientService.authorizeClient(user, clientApp, allowedScopes);
			
			PrintWriter out = response.getWriter();
			out.println("<html><body><h1>Authorized</h1></body></html>");
		} catch (ClientServiceException e) {
			throw new ServletException(e);
		}
	}

}

@@ -78,37 +78,8 @@
 n;%0D%0A
-import java.io.PrintWriter;%0D%0A
 impo
@@ -340,64 +340,342 @@
 ort 
-com.burgmeier.jerseyoauth2.api.client.ClientServiceExcep
+org.apache.amber.oauth2.common.exception.OAuthSystemException;%0D%0A%0D%0Aimport com.burgmeier.jerseyoauth2.api.client.ClientServiceException;%0D%0Aimport com.burgmeier.jerseyoauth2.api.client.IAuthorizationService;%0D%0Aimport com.burgmeier.jerseyoauth2.api.client.IAuthorizedClientApp;%0D%0Aimport com.burgmeier.jerseyoauth2.api.client.IClientAuthoriza
 tion
@@ -1209,16 +1209,76 @@
 rvice;%0D%0A
+%09private final IAuthorizationService authorizationService;%0D%0A
 %09%0D%0A%09@Inj
@@ -1369,16 +1369,66 @@
 rService
+, final IAuthorizationService authorizationService
 ) %7B%0D%0A%09%09s
@@ -1510,16 +1510,69 @@
 rvice;%0D%0A
+%09%09this.authorizationService = authorizationService;%0D%0A
 %09%7D%0D%0A%0D%0A%09@
@@ -2060,16 +2060,56 @@
 y %7B%0D%0A%09%09%09
+IAuthorizedClientApp authorizedClient = 
 clientSe
@@ -2176,139 +2176,292 @@
 %0A%09%09%09
-PrintWriter o
+IClientAuthorization clientA
 ut
+h
  = 
-response.getWriter(
+clientService.createClientAuthorization(authorizedClient
 );%0D%0A%09%09%09
-out.println(%22%3Chtml%3E%3Cbody%3E%3Ch1%3EAuthorized%3C/h1%3E%3C/body%3E%3C/html%3E%22);%0D%0A%09%09%7D catch (ClientService
+authorizationService.sendAuthorizationReponse(request, response, clientAuth, clientApp);%0D%0A%09%09%7D catch (ClientServiceException e) %7B%0D%0A%09%09%09throw new ServletException(e);%0D%0A%09%09%7D catch (OAuthSystem
 Exce

return auth response after approving client