3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.step;
23 import java.io.IOException;
24 import java.util.Optional;
25 import org.sonar.api.utils.MessageException;
26 import org.sonar.api.utils.TempFolder;
27 import org.sonar.api.utils.ZipUtils;
28 import org.sonar.ce.queue.CeTask;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.ce.CeTaskDataDao;
32 import org.sonar.server.computation.task.projectanalysis.batch.MutableBatchReportDirectoryHolder;
33 import org.sonar.server.computation.task.step.ComputationStep;
36 * Extracts the content zip file of the {@link CeTask} to a temp directory and adds a {@link File}
37 * representing that temp directory to the {@link MutableBatchReportDirectoryHolder}.
39 public class ExtractReportStep implements ComputationStep {
41 private final DbClient dbClient;
42 private final CeTask task;
43 private final TempFolder tempFolder;
44 private final MutableBatchReportDirectoryHolder reportDirectoryHolder;
46 public ExtractReportStep(DbClient dbClient, CeTask task, TempFolder tempFolder,
47 MutableBatchReportDirectoryHolder reportDirectoryHolder) {
48 this.dbClient = dbClient;
50 this.tempFolder = tempFolder;
51 this.reportDirectoryHolder = reportDirectoryHolder;
55 public void execute() {
56 try (DbSession dbSession = dbClient.openSession(false)) {
57 Optional<CeTaskDataDao.DataStream> opt = dbClient.ceTaskDataDao().selectData(dbSession, task.getUuid());
58 if (opt.isPresent()) {
59 File unzippedDir = tempFolder.newDir();
60 try (CeTaskDataDao.DataStream reportStream = opt.get()) {
61 ZipUtils.unzip(reportStream.getInputStream(), unzippedDir);
62 } catch (IOException e) {
63 throw new IllegalStateException("Fail to extract report " + task.getUuid() + " from database", e);
65 reportDirectoryHolder.setDirectory(unzippedDir);
67 throw MessageException.of("Analysis report " + task.getUuid() + " is missing in database");
73 public String getDescription() {
74 return "Extract report";