package com.atlassian.stash.rest.mail;

import com.atlassian.bitbucket.mail.MailHostConfiguration;
import com.atlassian.bitbucket.server.ApplicationPropertiesService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CreateMailServerServlet extends HttpServlet {

    private final ApplicationPropertiesService propertiesService;

    public CreateMailServerServlet(ApplicationPropertiesService propertiesService) {
        this.propertiesService = propertiesService;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        propertiesService.setMailHostConfiguration(new MailHostConfiguration.Builder()
                .hostname(req.getParameter("hostname"))
                .port(Integer.parseInt(req.getParameter("port")))
                .build());
        propertiesService.setServerEmailAddress(req.getParameter("emailAddress"));

        resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
        resp.getOutputStream().close();
    }
}
