diff --git a/scm-webapp/src/main/java/sonia/scm/update/MigrationWizardServlet.java b/scm-webapp/src/main/java/sonia/scm/update/MigrationWizardServlet.java index 7eb11cff8a..e451e60120 100644 --- a/scm-webapp/src/main/java/sonia/scm/update/MigrationWizardServlet.java +++ b/scm-webapp/src/main/java/sonia/scm/update/MigrationWizardServlet.java @@ -3,6 +3,8 @@ package sonia.scm.update; import com.github.mustachejava.DefaultMustacheFactory; import com.github.mustachejava.Mustache; import com.github.mustachejava.MustacheFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import sonia.scm.boot.RestartEvent; import sonia.scm.event.ScmEventBus; import sonia.scm.update.repository.MigrationStrategy; @@ -15,9 +17,11 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.io.PrintWriter; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Map; import static java.util.Arrays.stream; import static java.util.stream.Collectors.toList; @@ -25,6 +29,8 @@ import static java.util.stream.Collectors.toList; @Singleton class MigrationWizardServlet extends HttpServlet { + private static final Logger LOG = LoggerFactory.getLogger(MigrationWizardServlet.class); + private final XmlRepositoryV1UpdateStep repositoryV1UpdateStep; private final MigrationStrategyDao migrationStrategyDao; @@ -42,8 +48,6 @@ class MigrationWizardServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { List missingMigrationStrategies = repositoryV1UpdateStep.missingMigrationStrategies(); - resp.setStatus(200); - HashMap model = new HashMap<>(); model.put("contextPath", req.getContextPath()); @@ -52,8 +56,8 @@ class MigrationWizardServlet extends HttpServlet { model.put("strategies", getMigrationStrategies()); MustacheFactory mf = new DefaultMustacheFactory(); - Mustache mustache = mf.compile("templates/repository-migration.mustache"); - mustache.execute(resp.getWriter(), model).flush(); + Mustache template = mf.compile("templates/repository-migration.mustache"); + respondWithTemplate(resp, model, template); } private List getMigrationStrategies() { @@ -61,7 +65,7 @@ class MigrationWizardServlet extends HttpServlet { } @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + protected void doPost(HttpServletRequest req, HttpServletResponse resp) { resp.setStatus(200); req.getParameterMap().forEach( @@ -69,9 +73,25 @@ class MigrationWizardServlet extends HttpServlet { ); MustacheFactory mf = new DefaultMustacheFactory(); - Mustache mustache = mf.compile("templates/repository-migration-restart.mustache"); - mustache.execute(resp.getWriter(), Collections.singletonMap("contextPath", req.getContextPath())).flush(); + Mustache template = mf.compile("templates/repository-migration-restart.mustache"); + Map model = Collections.singletonMap("contextPath", req.getContextPath()); + + respondWithTemplate(resp, model, template); ScmEventBus.getInstance().post(new RestartEvent(MigrationWizardServlet.class, "wrote migration data")); } + + private void respondWithTemplate(HttpServletResponse resp, Map model, Mustache template) { + PrintWriter writer; + try { + writer = resp.getWriter(); + } catch (IOException e) { + LOG.error("could not create writer for response", e); + resp.setStatus(500); + return; + } + template.execute(writer, model); + writer.flush(); + resp.setStatus(200); + } }