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

IQSS / dataverse / #22301

14 May 2024 01:47PM CUT coverage: 20.57% (-0.009%) from 20.579%
#22301

Pull #10328

github

jp-tosca
Merge remote-tracking branch 'origin/develop' into openapi-definition-endpoint
Pull Request #10328: OpenAPI definition endpoint

0 of 33 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

17215 of 83688 relevant lines covered (20.57%)

0.21 hits per line

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

0.0
/src/main/java/edu/harvard/iq/dataverse/openapi/OpenAPI.java
1
package edu.harvard.iq.dataverse.openapi;
2

3
import java.io.*;
4
import java.net.URL;
5
import java.nio.charset.StandardCharsets;
6
import java.util.*;
7
import java.util.logging.*;
8
import jakarta.servlet.ServletException;
9
import jakarta.servlet.annotation.WebServlet;
10
import jakarta.servlet.http.*;
11
import jakarta.ws.rs.core.*;
12
import org.apache.commons.io.IOUtils;
13
import edu.harvard.iq.dataverse.api.Info;
14
import edu.harvard.iq.dataverse.util.BundleUtil;
15

16
@WebServlet("/openapi")
NEW
17
public class OpenAPI extends HttpServlet {
×
18

NEW
19
    private static final Logger logger = Logger.getLogger(Info.class.getCanonicalName());
×
20

21
    private static final String YAML_FORMAT = "yaml";
22
    private static final String JSON_FORMAT = "json";
23

24

25
    @Override
26
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
27
        
28
        
NEW
29
        String format = req.getParameter("format");
×
NEW
30
        String accept = req.getHeader("Accept");
×
31

32
        /*
33
         * We first check for the headers, if the request accepts application/json 
34
         * have to check for the format parameter, if it is different from json 
35
         * return BAD_REQUEST (400)
36
         */
NEW
37
        if (MediaType.APPLICATION_JSON.equals(accept)){
×
NEW
38
            if (format != null && !JSON_FORMAT.equals(format)){
×
NEW
39
                List<String> args = Arrays.asList(accept, format);
×
NEW
40
                String bundleResponse = BundleUtil.getStringFromBundle("openapi.exception.unaligned", args);
×
NEW
41
                resp.sendError(Response.Status.BAD_REQUEST.getStatusCode(),
×
42
                                bundleResponse);
NEW
43
                return;
×
44
            } else {
NEW
45
                format = JSON_FORMAT;
×
46
            }
47
        }
48

49
        /*
50
         * We currently support only JSON or YAML being the second the default
51
         * if no format is specified, if a different format is specified we return 
52
         * UNSUPPORTED_MEDIA_TYPE (415) specifing that the format is not supported 
53
         */
54

NEW
55
        format = format == null ? YAML_FORMAT : format.toLowerCase();
×
56

NEW
57
        if (JSON_FORMAT.equals(format)) {
×
NEW
58
            resp.setContentType(MediaType.APPLICATION_JSON_TYPE.toString());
×
NEW
59
        } else  if (YAML_FORMAT.equals(format)){
×
NEW
60
            resp.setContentType(MediaType.TEXT_PLAIN_TYPE.toString());
×
61
        } else {
NEW
62
            List<String> args = Arrays.asList(format);
×
NEW
63
            String bundleResponse = BundleUtil.getStringFromBundle("openapi.exception.invalid.format", args);
×
NEW
64
            resp.sendError(Response.Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode(),
×
65
                            bundleResponse);
NEW
66
            return;
×
67
        }
68

69
        try {
NEW
70
            String baseFileName = "/META-INF/openapi." + format;
×
NEW
71
            ClassLoader classLoader = this.getClass().getClassLoader();
×
NEW
72
            URL aliasesResource = classLoader.getResource(baseFileName);
×
NEW
73
            InputStream openapiDefinitionStream  = aliasesResource.openStream();
×
NEW
74
            String content = IOUtils.toString(openapiDefinitionStream, StandardCharsets.UTF_8);
×
NEW
75
            resp.getWriter().write(content);
×
NEW
76
        } catch (Exception e) {
×
NEW
77
            logger.log(Level.SEVERE, "OpenAPI Definition format not found " + format + ":" + e.getMessage(), e);
×
NEW
78
            String bundleResponse = BundleUtil.getStringFromBundle("openapi.exception");
×
NEW
79
            resp.sendError(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
×
80
                            bundleResponse);
NEW
81
        }
×
82

83
        
NEW
84
    }
×
85
    
86
}
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