// Copyright (c) Keith D Gregory, all rights reserved
package com.kdgregory.example.cognito.servlets;

import java.io.IOException;

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


import com.amazonaws.services.cognitoidp.model.*;
import net.sf.kdgcommons.lang.StringUtil;
import net.sf.kdgcommons.lang.ThreadUtil;


/**
 *  This servlet initiates the signup process for a new user.
 */
public class SignUp extends AbstractCognitoServlet
{
    private static final long serialVersionUID = 1L;


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        String emailAddress = request.getParameter(Constants.RequestParameters.EMAIL);
        if (StringUtil.isBlank(emailAddress))
        {
            reportResult(response, Constants.ResponseMessages.INVALID_REQUEST);
            return;
        }

        logger.debug("creating user {}", emailAddress);

        try
        {
           AttributeType emailAttribute = new AttributeType()
                                           .withName("email")
                                           .withValue(emailAddress);

            AdminCreateUserRequest cognitoRequest = new AdminCreateUserRequest()
                    .withUserPoolId(cognitoPoolId())
                    .withUsername(emailAddress)
                    .withUserAttributes(emailAttribute)
                    .withDesiredDeliveryMediums(DeliveryMediumType.EMAIL)
                    .withForceAliasCreation(Boolean.FALSE);

            cognitoClient.adminCreateUser(cognitoRequest);
            reportResult(response, Constants.ResponseMessages.USER_CREATED);
        }
        catch (UsernameExistsException ex)
        {
            logger.debug("user already exists: {}", emailAddress);
            reportResult(response, Constants.ResponseMessages.USER_ALREADY_EXISTS);
        }
        catch (TooManyRequestsException ex)
        {
            logger.warn("caught TooManyRequestsException, delaying then retrying");
            ThreadUtil.sleepQuietly(250);
            doPost(request, response);
        }
    }


    @Override
    public String getServletInfo()
    {
        return "Handles user signup";
    }

}

@@ -1066,82 +1066,131 @@
     
-AttributeType emailAttribute = new AttributeType()%0A                       
+ AdminCreateUserRequest cognitoRequest = new AdminCreateUserRequest()%0A                    .withUserPoolId(cognitoPoolId())%0A
     
@@ -1214,20 +1214,29 @@
 with
-N
+Usern
 ame(
-%22
 email
-%22
+Address
 )%0A  
@@ -1236,32 +1236,110 @@
 ess)%0A           
+         .withUserAttributes(%0A                            new AttributeType()%0A
                 
@@ -1363,164 +1363,135 @@
 with
-Valu
+Nam
 e(
+%22
 email
-Address);%0A%0A            AdminCreateUserRequest cognitoRequest = new AdminCreateUserRequest()%0A                    .withUserPoolId(cognitoPoolId())%0A
+%22)%0A                                .withValue(emailAddress),%0A                            new AttributeType()%0A       
     
@@ -1510,36 +1510,53 @@
     
-.withUsern
+     .withN
 ame(
+%22
 email
-Address)%0A
+_verified%22)%0A            
     
@@ -1580,37 +1580,21 @@
 with
-UserAttributes(emailAttribute
+Value(%22true%22)
 )%0A  

set "email_verified" attribute when creating user (documented, but doesn't actually work)