Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to Laurent Broudoux (the "Author") under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Author licenses this
* file to you 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.*;
import io.github.microcks.repository.ResponseRepository;
import io.github.microcks.repository.ServiceRepository;
import io.github.microcks.util.*;
import io.github.microcks.util.dispatcher.JsonEvaluationSpecification;
import io.github.microcks.util.dispatcher.JsonExpressionEvaluator;
import io.github.microcks.util.dispatcher.JsonMappingException;
import io.github.microcks.util.soapui.SoapUIScriptEngineBinder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriUtils;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.*;
/**
* A controller for mocking Rest responses.
* @author laurent
*/
@org.springframework.web.bind.annotation.RestController
@RequestMapping("/rest")
public class RestController {
/** A simple logger for diagnostic messages. */
private static Logger log = LoggerFactory.getLogger(RestController.class);
@Autowired
private ServiceRepository serviceRepository;
@Autowired
private ResponseRepository responseRepository;
@Autowired
private ApplicationContext applicationContext;
@Value("${mocks.rest.enable-cors-policy}")
private final Boolean enableCorsPolicy = null;
@RequestMapping(value = "/{service}/{version}/**", method = { RequestMethod.HEAD, RequestMethod.OPTIONS,
RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.PATCH, RequestMethod.DELETE })
public ResponseEntity> execute(
@PathVariable("service") String serviceName,
@PathVariable("version") String version,
@RequestParam(value="delay", required=false) Long delay,
@RequestBody(required=false) String body,
HttpServletRequest request
) {
log.info("Servicing mock response for service [{}, {}] on uri {} with verb {}",
serviceName, version, request.getRequestURI(), request.getMethod());
log.debug("Request body: {}", body);
long startTime = System.currentTimeMillis();
// Extract resourcePath for matching with correct operation.
String requestURI = request.getRequestURI();
String serviceAndVersion = null;
String resourcePath = null;
// Build the encoded URI fragment to retrieve simple resourcePath.
serviceAndVersion = "/" + UriUtils.encodeFragment(serviceName, "UTF-8") + "/" + version;
resourcePath = requestURI.substring(requestURI.indexOf(serviceAndVersion) + serviceAndVersion.length());
resourcePath = UriUtils.decode(resourcePath, "UTF-8");
log.debug("Found resourcePath: {}", resourcePath);
// If serviceName was encoded with '+' instead of '%20', replace them.
if (serviceName.contains("+")) {
serviceName = serviceName.replace('+', ' ');
}
// If resourcePath was encoded with '+' instead of '%20', replace them.
if (resourcePath.contains("+")) {
resourcePath = resourcePath.replace('+', ' ');
}
Service service = serviceRepository.findByNameAndVersion(serviceName, version);
Operation rOperation = null;
for (Operation operation : service.getOperations()){
// Select operation based onto Http verb (GET, POST, PUT, etc ...)
if (operation.getMethod().equals(request.getMethod().toUpperCase())){
// ... then check is we have a matching resource path.
if (operation.getResourcePaths() != null && operation.getResourcePaths().contains(resourcePath)){
rOperation = operation;
break;
}
}
}
if (rOperation != null){
log.debug("Found a valid operation {} with rules: {}", rOperation.getName(), rOperation.getDispatcherRules());
String violationMsg = validateParameterConstraintsIfAny(rOperation, request);
if (violationMsg != null) {
return new ResponseEntity