2019-06-04 16:37:53 +02:00
|
|
|
package sonia.scm.update;
|
|
|
|
|
|
|
|
|
|
import com.github.mustachejava.DefaultMustacheFactory;
|
|
|
|
|
import com.github.mustachejava.Mustache;
|
|
|
|
|
import com.github.mustachejava.MustacheFactory;
|
2019-06-05 15:39:36 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2019-06-04 16:37:53 +02:00
|
|
|
import sonia.scm.boot.RestartEvent;
|
|
|
|
|
import sonia.scm.event.ScmEventBus;
|
|
|
|
|
import sonia.scm.update.repository.MigrationStrategy;
|
|
|
|
|
import sonia.scm.update.repository.MigrationStrategyDao;
|
|
|
|
|
import sonia.scm.update.repository.XmlRepositoryV1UpdateStep;
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
import javax.inject.Singleton;
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.IOException;
|
2019-06-05 15:39:36 +02:00
|
|
|
import java.io.PrintWriter;
|
2019-06-07 13:59:31 +02:00
|
|
|
import java.util.ArrayList;
|
2019-06-07 14:16:23 +02:00
|
|
|
import java.util.Arrays;
|
2019-06-05 11:52:36 +02:00
|
|
|
import java.util.Collections;
|
2019-06-07 13:59:31 +02:00
|
|
|
import java.util.Comparator;
|
2019-06-04 16:37:53 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
2019-06-05 15:39:36 +02:00
|
|
|
import java.util.Map;
|
2019-06-04 16:37:53 +02:00
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
|
class MigrationWizardServlet extends HttpServlet {
|
|
|
|
|
|
2019-06-05 15:39:36 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(MigrationWizardServlet.class);
|
|
|
|
|
|
2019-06-04 16:37:53 +02:00
|
|
|
private final XmlRepositoryV1UpdateStep repositoryV1UpdateStep;
|
|
|
|
|
private final MigrationStrategyDao migrationStrategyDao;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
MigrationWizardServlet(XmlRepositoryV1UpdateStep repositoryV1UpdateStep, MigrationStrategyDao migrationStrategyDao) {
|
|
|
|
|
this.repositoryV1UpdateStep = repositoryV1UpdateStep;
|
|
|
|
|
this.migrationStrategyDao = migrationStrategyDao;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean wizardNecessary() {
|
2019-06-06 12:47:32 +02:00
|
|
|
return !repositoryV1UpdateStep.getRepositoriesWithoutMigrationStrategies().isEmpty();
|
2019-06-04 16:37:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-06-06 12:47:32 +02:00
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
|
|
|
|
|
List<XmlRepositoryV1UpdateStep.V1Repository> repositoriesWithoutMigrationStrategies =
|
2019-06-07 13:59:31 +02:00
|
|
|
new ArrayList<>(repositoryV1UpdateStep.getRepositoriesWithoutMigrationStrategies());
|
|
|
|
|
repositoriesWithoutMigrationStrategies.sort(Comparator.comparing(XmlRepositoryV1UpdateStep.V1Repository::getPath));
|
2019-06-04 16:37:53 +02:00
|
|
|
|
|
|
|
|
HashMap<String, Object> model = new HashMap<>();
|
|
|
|
|
|
2019-06-05 11:52:36 +02:00
|
|
|
model.put("contextPath", req.getContextPath());
|
2019-06-04 16:37:53 +02:00
|
|
|
model.put("submitUrl", req.getRequestURI());
|
2019-06-06 12:47:32 +02:00
|
|
|
model.put("repositories", repositoriesWithoutMigrationStrategies);
|
2019-06-04 16:37:53 +02:00
|
|
|
model.put("strategies", getMigrationStrategies());
|
|
|
|
|
|
|
|
|
|
MustacheFactory mf = new DefaultMustacheFactory();
|
2019-06-05 15:39:36 +02:00
|
|
|
Mustache template = mf.compile("templates/repository-migration.mustache");
|
|
|
|
|
respondWithTemplate(resp, model, template);
|
2019-06-04 16:37:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-06-05 15:39:36 +02:00
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
|
2019-06-04 16:37:53 +02:00
|
|
|
resp.setStatus(200);
|
|
|
|
|
|
2019-06-07 14:16:23 +02:00
|
|
|
Arrays.stream(req.getParameterValues("ids")).forEach(
|
|
|
|
|
id -> {
|
|
|
|
|
String strategy = req.getParameter("strategy-" + id);
|
|
|
|
|
String namespace = req.getParameter("namespace-" + id);
|
|
|
|
|
String name = req.getParameter("name-" + id);
|
|
|
|
|
migrationStrategyDao.set(id, MigrationStrategy.valueOf(strategy), namespace, name);
|
|
|
|
|
}
|
2019-06-04 16:37:53 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
MustacheFactory mf = new DefaultMustacheFactory();
|
2019-06-05 15:39:36 +02:00
|
|
|
Mustache template = mf.compile("templates/repository-migration-restart.mustache");
|
|
|
|
|
Map<String, Object> model = Collections.singletonMap("contextPath", req.getContextPath());
|
|
|
|
|
|
|
|
|
|
respondWithTemplate(resp, model, template);
|
2019-06-04 16:37:53 +02:00
|
|
|
|
|
|
|
|
ScmEventBus.getInstance().post(new RestartEvent(MigrationWizardServlet.class, "wrote migration data"));
|
|
|
|
|
}
|
2019-06-05 15:39:36 +02:00
|
|
|
|
2019-06-06 12:47:32 +02:00
|
|
|
private MigrationStrategy[] getMigrationStrategies() {
|
|
|
|
|
return MigrationStrategy.values();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 15:39:36 +02:00
|
|
|
private void respondWithTemplate(HttpServletResponse resp, Map<String, Object> 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);
|
|
|
|
|
}
|
2019-06-04 16:37:53 +02:00
|
|
|
}
|