Skip to content

Commit d02b459

Browse files
committed
refactor and add logging
* move header preparation (export) and file name checks (import) to service layer * calculate and log the duration of imports and exports
1 parent f8203b7 commit d02b459

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

src/main/java/de/rwth/idsg/steve/service/DataImportExportService.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
*/
1919
package de.rwth.idsg.steve.service;
2020

21+
import de.rwth.idsg.steve.SteveException;
2122
import de.rwth.idsg.steve.repository.DataImportExportRepository;
2223
import de.rwth.idsg.steve.web.dto.DataExportForm.ExportType;
2324
import lombok.RequiredArgsConstructor;
2425
import lombok.extern.slf4j.Slf4j;
26+
import org.apache.commons.lang3.StringUtils;
2527
import org.jooq.Named;
2628
import org.jooq.Table;
2729
import org.springframework.stereotype.Service;
30+
import org.springframework.web.multipart.MultipartFile;
2831

32+
import jakarta.servlet.http.HttpServletResponse;
2933
import java.io.FilterInputStream;
3034
import java.io.IOException;
3135
import java.io.InputStream;
@@ -34,6 +38,7 @@
3438
import java.nio.charset.StandardCharsets;
3539
import java.util.Collection;
3640
import java.util.List;
41+
import java.util.concurrent.TimeUnit;
3742
import java.util.stream.Stream;
3843
import java.util.zip.ZipEntry;
3944
import java.util.zip.ZipInputStream;
@@ -115,7 +120,37 @@ public List<String> getMasterDataTableNames() {
115120
return MASTER_DATA_TABLES.stream().map(Named::getName).toList();
116121
}
117122

118-
public void exportZip(OutputStream out, ExportType exportType) throws IOException {
123+
public void exportZip(HttpServletResponse response, ExportType exportType) throws IOException {
124+
String fileName = "data-export_" + System.currentTimeMillis() + ".zip";
125+
String headerKey = "Content-Disposition";
126+
String headerValue = "attachment; filename=\"%s\"".formatted(fileName);
127+
response.setHeader(headerKey, headerValue);
128+
response.setContentType("application/zip");
129+
130+
exportZip(response.getOutputStream(), exportType);
131+
}
132+
133+
public void importZip(MultipartFile file) throws IOException {
134+
if (file.isEmpty()) {
135+
throw new SteveException.BadRequest("File is empty");
136+
}
137+
138+
String fileName = file.getOriginalFilename();
139+
140+
if (StringUtils.isEmpty(fileName)) {
141+
throw new SteveException.BadRequest("File name is empty");
142+
}
143+
144+
if (!fileName.endsWith(".zip")) {
145+
throw new SteveException.BadRequest("File must be a ZIP archive");
146+
}
147+
148+
importZip(file.getInputStream());
149+
}
150+
151+
private void exportZip(OutputStream out, ExportType exportType) throws IOException {
152+
long start = System.currentTimeMillis();
153+
119154
try (ZipOutputStream zipOut = new ZipOutputStream(out);
120155
OutputStreamWriter writer = new OutputStreamWriter(zipOut, StandardCharsets.UTF_8)) {
121156

@@ -136,10 +171,17 @@ public void exportZip(OutputStream out, ExportType exportType) throws IOExceptio
136171
}
137172
}
138173
zipOut.finish();
174+
175+
} finally {
176+
long stop = System.currentTimeMillis();
177+
long durationSeconds = TimeUnit.MILLISECONDS.toSeconds(stop - start);
178+
log.info("Data export finished in {} seconds.", durationSeconds);
139179
}
140180
}
141181

142-
public void importZip(InputStream in) throws IOException {
182+
private void importZip(InputStream in) throws IOException {
183+
long start = System.currentTimeMillis();
184+
143185
dataImportExportRepository.beforeImport();
144186

145187
try (ZipInputStream zipIn = new ZipInputStream(in)) {
@@ -170,6 +212,10 @@ public void close() throws IOException {
170212
}
171213
} finally {
172214
dataImportExportRepository.afterImport();
215+
216+
long stop = System.currentTimeMillis();
217+
long durationSeconds = TimeUnit.MILLISECONDS.toSeconds(stop - start);
218+
log.info("Data import finished in {} seconds.", durationSeconds);
173219
}
174220
}
175221

src/main/java/de/rwth/idsg/steve/web/controller/AboutSettingsController.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package de.rwth.idsg.steve.web.controller;
2020

2121
import de.rwth.idsg.steve.NotificationFeature;
22-
import de.rwth.idsg.steve.SteveException;
2322
import de.rwth.idsg.steve.config.SteveProperties;
2423
import de.rwth.idsg.steve.repository.GenericRepository;
2524
import de.rwth.idsg.steve.repository.SettingsRepository;
@@ -30,7 +29,6 @@
3029
import de.rwth.idsg.steve.web.dto.EndpointInfo;
3130
import de.rwth.idsg.steve.web.dto.SettingsForm;
3231
import lombok.RequiredArgsConstructor;
33-
import org.apache.commons.lang3.StringUtils;
3432
import org.joda.time.DateTime;
3533
import org.joda.time.DateTimeZone;
3634
import org.springframework.http.HttpHeaders;
@@ -134,32 +132,12 @@ public String testMail(@Valid @ModelAttribute("settingsForm") SettingsForm setti
134132
@GetMapping(value = ABOUT_PATH + "/export")
135133
public void exportZip(@ModelAttribute("exportForm") DataExportForm exportForm,
136134
HttpServletResponse response) throws IOException {
137-
String fileName = "data-export_" + System.currentTimeMillis() + ".zip";
138-
String headerKey = "Content-Disposition";
139-
String headerValue = "attachment; filename=\"%s\"".formatted(fileName);
140-
response.setHeader(headerKey, headerValue);
141-
response.setContentType("application/zip");
142-
143-
dataImportExportService.exportZip(response.getOutputStream(), exportForm.getExportType());
135+
dataImportExportService.exportZip(response, exportForm.getExportType());
144136
}
145137

146138
@PostMapping(value = ABOUT_PATH + "/import")
147139
public String importZip(@RequestParam("file") MultipartFile file, Model model) throws IOException {
148-
if (file.isEmpty()) {
149-
throw new SteveException.BadRequest("File is empty");
150-
}
151-
152-
String fileName = file.getOriginalFilename();
153-
154-
if (StringUtils.isEmpty(fileName)) {
155-
throw new SteveException.BadRequest("File name is empty");
156-
}
157-
158-
if (!fileName.endsWith(".zip")) {
159-
throw new SteveException.BadRequest("File must be a ZIP archive");
160-
}
161-
162-
dataImportExportService.importZip(file.getInputStream());
140+
dataImportExportService.importZip(file);
163141
return "redirect:/manager/home";
164142
}
165143
}

0 commit comments

Comments
 (0)