use of io.github.microcks.domain.ResourceType in project microcks by microcks.
the class DocumentationController method execute.
@RequestMapping(value = "/documentation/{name}/{resourceType}", method = RequestMethod.GET)
public ResponseEntity<?> execute(@PathVariable("name") String name, @PathVariable("resourceType") String resourceType, HttpServletRequest request) {
log.info("Requesting {} documentation for resource {}", resourceType, name);
// Prepare HttpHeaders.
InputStream stream = null;
HttpHeaders headers = new HttpHeaders();
org.springframework.core.io.Resource template = null;
// Get the correct template depending on resource type.
if (ResourceType.OPEN_API_SPEC.toString().equals(resourceType) || ResourceType.SWAGGER.toString().equals(resourceType)) {
template = new ClassPathResource("templates/redoc.html");
headers.setContentType(MediaType.TEXT_HTML);
} else if (ResourceType.ASYNC_API_SPEC.toString().equals(resourceType)) {
template = new ClassPathResource("templates/asyncapi.html");
headers.setContentType(MediaType.TEXT_HTML);
}
if (template != null) {
try {
stream = template.getInputStream();
} catch (IOException e) {
log.error("IOException while reading template " + template.getDescription(), e);
}
// Now process the stream, replacing patterns by value.
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringWriter writer = new StringWriter();
try (Stream<String> lines = reader.lines()) {
lines.map(line -> replaceInLine(line, name)).forEach(line -> writer.write(line + "\n"));
}
return new ResponseEntity<>(writer.toString().getBytes(), headers, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Aggregations