Skip to content

Commit c9e1549

Browse files
committed
Migrate to spring 4 and implement convert format json and a test.
1 parent 2eee37c commit c9e1549

4 files changed

Lines changed: 452 additions & 211 deletions

File tree

pom.xml

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.springframework.boot</groupId>
99
<artifactId>spring-boot-starter-parent</artifactId>
10-
<version>3.5.11</version>
10+
<version>4.0.3</version>
1111
<relativePath/>
1212
</parent>
1313

@@ -35,31 +35,22 @@
3535
</dependency>
3636
<dependency>
3737
<groupId>org.springframework.boot</groupId>
38-
<artifactId>spring-boot-starter</artifactId>
38+
<artifactId>spring-boot-starter-log4j2</artifactId>
3939
<exclusions>
4040
<exclusion>
41-
<groupId>org.springframework.boot</groupId>
42-
<artifactId>spring-boot-starter-logging</artifactId>
41+
<groupId>org.apache.logging.log4j</groupId>
42+
<artifactId>log4j-to-slf4j</artifactId>
43+
</exclusion>
44+
<exclusion>
45+
<groupId>org.apache.logging.log4j</groupId>
46+
<artifactId>log4j-slf4j2-impl</artifactId>
4347
</exclusion>
4448
</exclusions>
4549
</dependency>
46-
<dependency>
47-
<groupId>org.springframework.boot</groupId>
48-
<artifactId>spring-boot-starter-log4j2</artifactId>
49-
</dependency>
50-
51-
<dependency>
52-
<groupId>org.apache.logging.log4j</groupId>
53-
<artifactId>log4j-api</artifactId>
54-
</dependency>
55-
<dependency>
56-
<groupId>org.apache.logging.log4j</groupId>
57-
<artifactId>log4j-core</artifactId>
58-
</dependency>
5950
<dependency>
6051
<groupId>org.springdoc</groupId>
6152
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
62-
<version>2.8.16</version>
53+
<version>3.0.2</version>
6354
</dependency>
6455
<dependency>
6556
<groupId>org.openapitools</groupId>
@@ -102,13 +93,8 @@
10293
<scope>test</scope>
10394
</dependency>
10495
<dependency>
105-
<groupId>org.junit.jupiter</groupId>
106-
<artifactId>junit-jupiter</artifactId>
107-
<scope>test</scope>
108-
</dependency>
109-
<dependency>
110-
<groupId>org.testcontainers</groupId>
111-
<artifactId>testcontainers</artifactId>
96+
<groupId>org.springframework.boot</groupId>
97+
<artifactId>spring-boot-starter-webflux</artifactId>
11298
<scope>test</scope>
11399
</dependency>
114100
<dependency>
@@ -132,13 +118,6 @@
132118
<type>pom</type>
133119
<scope>import</scope>
134120
</dependency>
135-
<dependency>
136-
<groupId>org.apache.logging.log4j</groupId>
137-
<artifactId>log4j-bom</artifactId>
138-
<version>2.25.3</version>
139-
<type>pom</type>
140-
<scope>import</scope>
141-
</dependency>
142121
<dependency>
143122
<groupId>org.testcontainers</groupId>
144123
<artifactId>testcontainers-bom</artifactId>
@@ -167,9 +146,6 @@
167146
<groupId>org.apache.maven.plugins</groupId>
168147
<artifactId>maven-surefire-plugin</artifactId>
169148
<version>3.5.4</version>
170-
<configuration>
171-
<argLine>@{argLine} -javaagent:${org.mockito:mockito-core:jar}</argLine>
172-
</configuration>
173149
</plugin>
174150
<plugin>
175151
<groupId>org.apache.maven.plugins</groupId>
Lines changed: 61 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package massbank_export_api.api;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import massbank.Record;
45
import massbank.RecordParser;
6+
import massbank.export.RecordToJson;
57
import massbank.export.RecordToNIST_MSP;
68
import massbank.export.RecordToRIKEN_MSP;
79
import massbank_export_api.api.db.DbRecord;
@@ -11,7 +13,6 @@
1113
import org.springframework.beans.factory.annotation.Autowired;
1214
import org.springframework.context.annotation.Primary;
1315
import org.springframework.core.io.ByteArrayResource;
14-
import org.springframework.core.io.InputStreamResource;
1516
import org.springframework.core.io.Resource;
1617
import org.springframework.http.HttpHeaders;
1718
import org.springframework.http.MediaType;
@@ -20,8 +21,6 @@
2021

2122
import java.io.ByteArrayOutputStream;
2223
import java.io.IOException;
23-
import java.io.PipedInputStream;
24-
import java.io.PipedOutputStream;
2524
import java.nio.charset.StandardCharsets;
2625
import java.util.HashSet;
2726
import java.util.Objects;
@@ -50,63 +49,48 @@ public ConvertApiDelegateImpl(RecordServiceImplementation recordServiceImplement
5049
@Override
5150
public ResponseEntity<Resource> convertPost(Conversion conversion) {
5251
String formatValue = conversion.getFormat() != null ? conversion.getFormat().getValue() : "";
53-
Resource resource = null;
54-
String filename = null;
55-
MediaType mediaType = null;
56-
5752
final RecordParser recordparser = new RecordParser(new HashSet<>());
58-
5953
if (conversion.getRecordList() == null || conversion.getRecordList().isEmpty()) {
6054
conversion.setRecordList(recordServiceImplementation.getAllAccessions());
6155
}
6256

57+
Resource resource;
58+
String filename;
59+
MediaType mediaType;
60+
6361
switch (formatValue) {
6462
case "nist_msp":
63+
case "riken_msp": {
64+
boolean isNist = formatValue.equals("nist_msp");
6565
mediaType = MediaType.TEXT_PLAIN;
6666
filename = "records.msp";
67-
resource = new ByteArrayResource(
68-
conversion.getRecordList().parallelStream()
69-
.map(recordServiceImplementation::findByAccession)
70-
.filter(Objects::nonNull)
71-
.map(DbRecord::getContent)
72-
.map(recordparser::parse)
73-
.filter(Result::isSuccess)
74-
.map(Result::get)
75-
.map(record -> (massbank.Record) record)
76-
.map(RecordToNIST_MSP::convert)
77-
.collect(Collectors.joining(System.lineSeparator(), "", System.lineSeparator()))
78-
.getBytes(StandardCharsets.UTF_8));
67+
String content = conversion.getRecordList().parallelStream()
68+
.map(recordServiceImplementation::findByAccession)
69+
.filter(Objects::nonNull)
70+
.map(DbRecord::getContent)
71+
.map(recordparser::parse)
72+
.filter(Result::isSuccess)
73+
.map(Result::get)
74+
.map(r -> (massbank.Record) r)
75+
.map(isNist ? RecordToNIST_MSP::convert : RecordToRIKEN_MSP::convert)
76+
.collect(Collectors.joining(System.lineSeparator(), "", System.lineSeparator()));
77+
resource = new ByteArrayResource(content.getBytes(StandardCharsets.UTF_8));
7978
break;
80-
case "riken_msp":
81-
mediaType = MediaType.TEXT_PLAIN;
82-
filename = "records.msp";
83-
resource = new ByteArrayResource(
84-
conversion.getRecordList().parallelStream()
85-
.map(recordServiceImplementation::findByAccession)
86-
.filter(Objects::nonNull)
87-
.map(DbRecord::getContent)
88-
.map(recordparser::parse)
89-
.filter(Result::isSuccess)
90-
.map(Result::get)
91-
.map(record -> (massbank.Record) record)
92-
.map(RecordToRIKEN_MSP::convert)
93-
.collect(Collectors.joining(System.lineSeparator(), "", System.lineSeparator()))
94-
.getBytes(StandardCharsets.UTF_8));
95-
break;
96-
case "massbank":
79+
}
80+
case "massbank": {
9781
mediaType = MediaType.parseMediaType("application/zip");
9882
filename = "records.zip";
99-
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
100-
final ZipOutputStream zos = new ZipOutputStream(baos, StandardCharsets.UTF_8)) {
83+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
84+
ZipOutputStream zos = new ZipOutputStream(baos, StandardCharsets.UTF_8)) {
10185
conversion.getRecordList().parallelStream()
10286
.map(recordServiceImplementation::findByAccession)
10387
.filter(Objects::nonNull)
10488
.map(DbRecord::getContent)
10589
.forEach(record -> {
106-
final String accession = record.substring(record.indexOf("ACCESSION:") + 10,
90+
String accession = record.substring(record.indexOf("ACCESSION:") + 10,
10791
record.indexOf("\n", record.indexOf("ACCESSION:"))).trim();
10892
try {
109-
final ZipEntry entry = new ZipEntry(accession + ".txt");
93+
ZipEntry entry = new ZipEntry(accession + ".txt");
11094
synchronized (zos) {
11195
zos.putNextEntry(entry);
11296
zos.write(record.getBytes(StandardCharsets.UTF_8));
@@ -122,66 +106,53 @@ public ResponseEntity<Resource> convertPost(Conversion conversion) {
122106
throw new RuntimeException("Error creating zip file", e);
123107
}
124108
break;
125-
case "json":
126-
mediaType = MediaType.parseMediaType("application/jsonl");
127-
try {
128-
PipedOutputStream pos = new PipedOutputStream();
129-
PipedInputStream pis = new PipedInputStream(pos);
130-
new Thread(() -> {
131-
try {
132-
ObjectMapper mapper = new ObjectMapper();
133-
boolean first = true;
134-
for (String accession : conversion.getRecordList()) {
135-
DbRecord dbRecord = recordServiceImplementation.findByAccession(accession);
136-
if (dbRecord == null) continue;
137-
Result parseResult = recordparser.parse(dbRecord.getContent());
138-
if (!parseResult.isSuccess()) continue;
139-
massbank.Record record = (massbank.Record) parseResult.get();
140-
String json = mapper.writeValueAsString(massbank.export.RecordToJson.convert(record));
141-
if (!first) {
142-
pos.write('\n');
143-
} else {
144-
first = false;
145-
}
146-
pos.write(json.getBytes(StandardCharsets.UTF_8));
147-
}
148-
pos.close();
149-
} catch (IOException e) {
150-
throw new RuntimeException("Error streaming JSONL", e);
151-
}
152-
}).start();
153-
resource = new InputStreamResource(pis);
109+
}
110+
case "json": {
111+
mediaType = MediaType.APPLICATION_JSON;
112+
filename = "records.json";
113+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
114+
ObjectMapper mapper = new ObjectMapper();
115+
java.util.List<Record> recordList = conversion.getRecordList().parallelStream()
116+
.map(recordServiceImplementation::findByAccession)
117+
.filter(Objects::nonNull)
118+
.map(DbRecord::getContent)
119+
.map(recordparser::parse)
120+
.filter(Result::isSuccess)
121+
.map(Result::get)
122+
.map(r -> (massbank.Record) r)
123+
.toList();
124+
String json = RecordToJson.convertRecords(recordList);
125+
baos.write(json.getBytes(StandardCharsets.UTF_8));
126+
resource = new ByteArrayResource(baos.toByteArray());
154127
} catch (IOException e) {
155-
throw new RuntimeException("Error creating stream for JSONL", e);
128+
throw new RuntimeException("Error creating JSON file", e);
156129
}
157-
filename = null;
158130
break;
159-
default:
131+
}
132+
default: {
160133
String message = "Missing or unsupported format value.";
161134
resource = new ByteArrayResource(message.getBytes(StandardCharsets.UTF_8));
135+
mediaType = MediaType.TEXT_PLAIN;
136+
filename = null;
162137
return ResponseEntity.badRequest()
163-
.contentType(MediaType.TEXT_PLAIN)
138+
.contentType(mediaType)
164139
.body(resource);
140+
}
165141
}
166142

167-
if ("json".equals(formatValue)) {
168-
// Kein Download, sondern Stream
169-
return ResponseEntity.ok()
170-
.contentType(mediaType)
171-
.body(resource);
172-
} else {
173-
HttpHeaders headers = new HttpHeaders();
143+
HttpHeaders headers = new HttpHeaders();
144+
if (filename != null) {
174145
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
175-
long contentLength = -1;
176-
try {
177-
contentLength = resource.contentLength();
178-
} catch (IOException ignored) {}
179-
return ResponseEntity.ok()
180-
.headers(headers)
181-
.contentLength(contentLength)
182-
.contentType(mediaType)
183-
.body(resource);
184146
}
147+
long contentLength = -1;
148+
try {
149+
contentLength = resource.contentLength();
150+
} catch (IOException ignored) {}
151+
return ResponseEntity.ok()
152+
.headers(headers)
153+
.contentLength(contentLength)
154+
.contentType(mediaType)
155+
.body(resource);
185156
}
186157

187158
}

src/main/java/massbank_export_api/api/DataReader.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.springframework.beans.factory.annotation.Autowired;
1212
import org.springframework.beans.factory.annotation.Value;
1313
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
14-
import org.springframework.boot.autoconfigure.domain.EntityScan;
1514
import org.springframework.boot.context.event.ApplicationReadyEvent;
1615
import org.springframework.context.event.EventListener;
1716
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@@ -27,7 +26,6 @@
2726

2827
@Component
2928
@EnableJpaRepositories(basePackages = "massbank_export_api.api.db")
30-
@EntityScan(basePackages = "massbank_export_api.api.db")
3129
@EnableAutoConfiguration
3230
public class DataReader {
3331

0 commit comments

Comments
 (0)