io.github.microcks.web.ResourceController Maven / Gradle / Ivy
/*
* Copyright The Microcks Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.microcks.web;
import io.github.microcks.domain.GenericResource;
import io.github.microcks.domain.Operation;
import io.github.microcks.domain.Resource;
import io.github.microcks.domain.Service;
import io.github.microcks.domain.ServiceType;
import io.github.microcks.repository.GenericResourceRepository;
import io.github.microcks.repository.ResourceRepository;
import io.github.microcks.repository.ServiceRepository;
import io.github.microcks.util.ResourceUtil;
import io.github.microcks.util.openapi.OpenAPISchemaBuilder;
import com.fasterxml.jackson.databind.JsonNode;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import static io.github.microcks.web.DynamicMockRestController.ID_FIELD;
/**
* A controller for distributing or generating resources associated to services mocked within Microcks.
* @author laurent
*/
@org.springframework.web.bind.annotation.RestController
@RequestMapping("/api")
public class ResourceController {
/** A simple logger for diagnostic messages. */
private static Logger log = LoggerFactory.getLogger(ResourceController.class);
private static final String SWAGGER_20 = "swagger_20";
private static final String OPENAPI_30 = "openapi_30";
@Autowired
private ResourceRepository resourceRepository;
@Autowired
private ServiceRepository serviceRepository;
@Autowired
private GenericResourceRepository genericResourceRepository;
@RequestMapping(value = "/resources/{name}", method = RequestMethod.GET)
public ResponseEntity> execute(
@PathVariable("name") String name,
HttpServletRequest request
) {
String extension = request.getRequestURI().substring(request.getRequestURI().lastIndexOf('.'));
try {
name = URLDecoder.decode(name, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
log.error("Exception while decoding resource name: {}", e.getMessage());
}
log.info("Requesting resource named " + name);
Resource resource = resourceRepository.findByName(name);
if (resource != null){
HttpHeaders headers = new HttpHeaders();
if (".json".equals(extension)) {
headers.setContentType(MediaType.APPLICATION_JSON);
} else if (".yaml".equals(extension) || ".yml".equals(extension)) {
headers.set("Content-Type", "text/yaml");
headers.setContentDisposition(ContentDisposition.builder("inline").filename(name).build());
} else if (".wsdl".equals(extension) || ".xsd".equals(extension)) {
headers.setContentType(MediaType.TEXT_XML);
} else if (".avsc".equals(extension)) {
headers.setContentType(MediaType.APPLICATION_JSON);
}
return new ResponseEntity © 2015 - 2025 Weber Informatics LLC | Privacy Policy