• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / pit-service / #103

pending completion
#103

Pull #77

github-actions

web-flow
Merge 9c891e410 into f6202a4b9
Pull Request #77: Add simpler JSON format for the API

54 of 54 new or added lines in 6 files covered. (100.0%)

690 of 1376 relevant lines covered (50.15%)

0.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.0
/src/main/java/edu/kit/datamanager/pit/web/converter/SimplePidRecordConverter.java
1
package edu.kit.datamanager.pit.web.converter;
2

3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStreamReader;
6
import java.nio.charset.StandardCharsets;
7
import java.util.Arrays;
8
import java.util.List;
9
import java.util.stream.Collectors;
10

11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13
import org.springframework.http.HttpInputMessage;
14
import org.springframework.http.HttpOutputMessage;
15
import org.springframework.http.MediaType;
16
import org.springframework.http.converter.HttpMessageConverter;
17
import org.springframework.http.converter.HttpMessageNotReadableException;
18
import org.springframework.http.converter.HttpMessageNotWritableException;
19

20
import edu.kit.datamanager.pit.Application;
21
import edu.kit.datamanager.pit.domain.PIDRecord;
22
import edu.kit.datamanager.pit.domain.SimplePidRecord;
23

24
/**
25
 * Converts to-and-from PIDRecord format when SimplePidRecord format is actually
26
 * expected.
27
 * 
28
 * Do not use explicitly. Spring will use it, as explained below.
29
 * 
30
 * The idea is that all handlers in the REST API simply expect a serialized
31
 * (marshalled) version of a PID record. This is not always true, though. The
32
 * PIDRecord representation is pretty complex. To offer a simpler format,
33
 * `SimplePidRecord` was introduced. To avoid larger modifications within the
34
 * code, and to not break the API, the simple format comes into play only when
35
 * its content type is being used.
36
 * 
37
 * If the client wants to send in the simple format, it needs to set the
38
 * content-type header accordingly. Spring will then use this converter to
39
 * convert it directly into a PIDRecord instance, as the handler expects. This
40
 * way only one handler must be used for multiple formats.
41
 * 
42
 * For accepting formats, it is the same. With the accept header, a client may
43
 * control which format it would like to receive. If it prefers to receive the
44
 * simple format and sets the header accordingly, instead of directly
45
 * serializing the PIDRecord, this class will be used. It first converts the
46
 * record into the simple class representation before serializing into JSON.
47
 */
48
public class SimplePidRecordConverter implements HttpMessageConverter<PIDRecord> {
1✔
49

50
    private static final Logger LOGGER = LoggerFactory.getLogger(SimplePidRecordConverter.class);
1✔
51

52
    private boolean isValidMediaType(MediaType arg1) {
53
        return arg1.toString().contains(SimplePidRecord.CONTENT_TYPE_PURE);
1✔
54
    }
55

56
    @Override
57
    public boolean canRead(Class<?> arg0, MediaType arg1) {
58
        if (arg0 == null || arg1 == null) {
1✔
59
            return false;
×
60
        }
61
        LOGGER.trace("canRead: Checking applicability for class {} and mediatype {}.", arg0, arg1);
1✔
62
        return PIDRecord.class.equals(arg0) && isValidMediaType(arg1);
1✔
63
    }
64

65
    @Override
66
    public boolean canWrite(Class<?> arg0, MediaType arg1) {
67
        LOGGER.trace("canWrite: Checking applicability for class {} and mediatype {}.", arg0, arg1);
1✔
68
        return PIDRecord.class.equals(arg0) && isValidMediaType(arg1);
1✔
69
    }
70

71
    @Override
72
    public List<MediaType> getSupportedMediaTypes() {
73
        return Arrays.asList(
×
74
                MediaType.valueOf(SimplePidRecord.CONTENT_TYPE)
×
75
        );
76
    }
77

78
    @Override
79
    public PIDRecord read(Class<? extends PIDRecord> arg0, HttpInputMessage arg1)
80
            throws IOException, HttpMessageNotReadableException {
81
        LOGGER.trace("Read simple message from client and convert to PIDRecord.");
1✔
82
        try (InputStreamReader reader = new InputStreamReader(arg1.getBody(), StandardCharsets.UTF_8)) {
1✔
83
            String data = new BufferedReader(reader).lines().collect(Collectors.joining("\n"));
1✔
84
            return new PIDRecord(Application.jsonObjectMapper().readValue(data, SimplePidRecord.class));
1✔
85
        }
86
    }
87

88
    @Override
89
    public void write(PIDRecord arg0, MediaType arg1, HttpOutputMessage arg2)
90
            throws IOException, HttpMessageNotWritableException {
91
        LOGGER.trace("Write PIDRecord to simple format for client.");
1✔
92
        SimplePidRecord sim = new SimplePidRecord(arg0);
1✔
93
        byte[] simSerialized = Application.jsonObjectMapper().writeValueAsBytes(sim);
1✔
94
        arg2.getBody().write(simSerialized);
1✔
95
    }
1✔
96
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc