3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info 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;
22 import java.io.BufferedInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Optional;
27 import org.sonar.api.utils.MessageException;
28 import org.sonar.api.utils.TempFolder;
29 import org.sonar.api.utils.ZipUtils;
30 import org.sonar.ce.queue.CeTask;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.ce.CeTaskInputDao;
34 import org.sonar.server.computation.task.projectanalysis.batch.MutableBatchReportDirectoryHolder;
35 import org.sonar.server.computation.task.step.ComputationStep;
38 * Extracts the content zip file of the {@link CeTask} to a temp directory and adds a {@link File}
39 * representing that temp directory to the {@link MutableBatchReportDirectoryHolder}.
41 public class ExtractReportStep implements ComputationStep {
43 private final DbClient dbClient;
44 private final CeTask task;
45 private final TempFolder tempFolder;
46 private final MutableBatchReportDirectoryHolder reportDirectoryHolder;
48 public ExtractReportStep(DbClient dbClient, CeTask task, TempFolder tempFolder,
49 MutableBatchReportDirectoryHolder reportDirectoryHolder) {
50 this.dbClient = dbClient;
52 this.tempFolder = tempFolder;
53 this.reportDirectoryHolder = reportDirectoryHolder;
57 public void execute() {
58 try (DbSession dbSession = dbClient.openSession(false)) {
59 Optional<CeTaskInputDao.DataStream> opt = dbClient.ceTaskInputDao().selectData(dbSession, task.getUuid());
60 if (opt.isPresent()) {
61 File unzippedDir = tempFolder.newDir();
62 try (CeTaskInputDao.DataStream reportStream = opt.get();
63 InputStream zipStream = new BufferedInputStream(reportStream.getInputStream())) {
64 ZipUtils.unzip(zipStream, unzippedDir);
65 } catch (IOException e) {
66 throw new IllegalStateException("Fail to extract report " + task.getUuid() + " from database", e);
68 reportDirectoryHolder.setDirectory(unzippedDir);
70 throw MessageException.of("Analysis report " + task.getUuid() + " is missing in database");
76 public String getDescription() {
77 return "Extract report";