Search in sources :

Example 1 with ResourceType

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);
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) ResourceType(io.github.microcks.domain.ResourceType) Logger(org.slf4j.Logger) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) StringWriter(java.io.StringWriter) LoggerFactory(org.slf4j.LoggerFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) HttpStatus(org.springframework.http.HttpStatus) HttpServletRequest(javax.servlet.http.HttpServletRequest) Stream(java.util.stream.Stream) ResponseEntity(org.springframework.http.ResponseEntity) BufferedReader(java.io.BufferedReader) InputStream(java.io.InputStream) HttpHeaders(org.springframework.http.HttpHeaders) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource) ResponseEntity(org.springframework.http.ResponseEntity) StringWriter(java.io.StringWriter) BufferedReader(java.io.BufferedReader) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ResourceType (io.github.microcks.domain.ResourceType)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 StringWriter (java.io.StringWriter)1 Stream (java.util.stream.Stream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpStatus (org.springframework.http.HttpStatus)1 MediaType (org.springframework.http.MediaType)1 ResponseEntity (org.springframework.http.ResponseEntity)1 PathVariable (org.springframework.web.bind.annotation.PathVariable)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)1