init project..
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import com.jayway.jsonpath.DocumentContext;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
|
||||
@Controller
|
||||
public class ApiController {
|
||||
@Autowired
|
||||
private ApiServiceKeyService apiKeyService;
|
||||
|
||||
@Autowired
|
||||
private ApiService apiService;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@RequestMapping(value = "/mock-api/**")
|
||||
public ResponseEntity<String> mockApi(HttpEntity<String> httpEntity, HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
System.out.println("==================================================");
|
||||
System.out.println("==================================================");
|
||||
System.out.println(request.getServletPath());
|
||||
System.out.println("--------------------------------------------------");
|
||||
System.out.println("httpEntity.getHeaders().toString()=" + httpEntity.getHeaders().toString());
|
||||
System.out.println("--------------------------------------------------");
|
||||
System.out.println("httpEntity.getBody()=" + httpEntity.getBody());
|
||||
System.out.println("--------------------------------------------------");
|
||||
|
||||
Map<String, String[]> paramMap = request.getParameterMap();
|
||||
for (String key : paramMap.keySet()) {
|
||||
String[] values = paramMap.get(key);
|
||||
for (String value : values) {
|
||||
System.out.println(String.format("%s=%s", key, value));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("==================================================");
|
||||
System.out.println("==================================================");
|
||||
|
||||
String url = request.getServletPath();
|
||||
String serviceValue = assignServiceValue(httpEntity, request, url);
|
||||
ApiInfo api = apiService.findByUrlAndServiceValue(url, serviceValue);
|
||||
if (api == null) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
|
||||
String.format("Not found API(URL=%s, ServiceValue=%s", url, serviceValue));
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(api.getMethod())) {
|
||||
if (!api.getMethod().equalsIgnoreCase(request.getMethod())) {
|
||||
throw new ResponseStatusException(HttpStatus.METHOD_NOT_ALLOWED,
|
||||
String.format("Method(%s) not allowed", request.getMethod()));
|
||||
}
|
||||
}
|
||||
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
if (StringUtils.isNotBlank(api.getResponseContentType())) {
|
||||
responseHeaders.setContentType(MediaType.parseMediaType(api.getResponseContentType()));
|
||||
} else {
|
||||
responseHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
}
|
||||
|
||||
assignResponseHeaders(responseHeaders, api.getResponseHeaders());
|
||||
|
||||
return new ResponseEntity<String>(assignResponseBody(api), responseHeaders, HttpStatus.OK);
|
||||
}
|
||||
|
||||
private String assignServiceValue(HttpEntity<String> httpEntity, HttpServletRequest request, String url) {
|
||||
ApiServiceKey apiServiceKey = apiKeyService.findByUrl(url);
|
||||
if (apiServiceKey == null || StringUtils.isBlank(apiServiceKey.getServiceType())) {
|
||||
return "";
|
||||
}
|
||||
|
||||
String serviceType = apiServiceKey.getServiceType();
|
||||
String serviceKey = apiServiceKey.getServiceKey();
|
||||
if (StringUtils.isBlank(serviceKey)) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
|
||||
String.format("ServiceKey not found(URL=%s, ServiceType=%s)", url, serviceType));
|
||||
}
|
||||
|
||||
if (serviceType.equalsIgnoreCase("header")) {
|
||||
String serviceValue = request.getHeader(serviceKey);
|
||||
if (StringUtils.isBlank(serviceValue)) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
|
||||
String.format("Header not found(URL=%s, ServiceKey=%s)", url, serviceKey));
|
||||
}
|
||||
return serviceValue;
|
||||
} else if (serviceType.equalsIgnoreCase("parameter")) {
|
||||
String serviceValue = request.getParameter(serviceKey);
|
||||
if (StringUtils.isBlank(serviceValue)) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
|
||||
String.format("Parameter not found(URL=%s, ServiceKey=%s)", url, serviceKey));
|
||||
}
|
||||
return serviceValue;
|
||||
} else if (serviceType.equalsIgnoreCase("json-body")) {
|
||||
// ex) $.dataHeader.serviceId
|
||||
DocumentContext jsonContext = JsonPath.parse(httpEntity.getBody());
|
||||
String serviceValue = jsonContext.read(serviceKey);
|
||||
if (StringUtils.isBlank(serviceValue)) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
|
||||
String.format("json-key not found(URL=%s, ServiceKey=%s)", url, serviceKey));
|
||||
}
|
||||
return serviceValue;
|
||||
} else if (serviceType.equalsIgnoreCase("flat-data")) {
|
||||
String msg = httpEntity.getBody();
|
||||
String[] args = org.springframework.util.StringUtils.tokenizeToStringArray(serviceKey, ",");
|
||||
int startIdx = NumberUtils.toInt(args[0]);
|
||||
int length = NumberUtils.toInt(args[1]);
|
||||
String serviceValue = msg.substring(startIdx, startIdx + length);
|
||||
System.out.println("--------------------------------------------------");
|
||||
System.out.println("serviceCode=" + serviceValue);
|
||||
System.out.println("==================================================");
|
||||
System.out.println("==================================================");
|
||||
if (StringUtils.isBlank(serviceValue)) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
|
||||
String.format("sbi-std-servlet not found(URL=%s, ServiceKey=%s)", url, serviceKey));
|
||||
}
|
||||
return serviceValue;
|
||||
} else {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
|
||||
String.format("Unsuported ServiceType(ServiceType=%s, ServiceKey)", serviceType, serviceKey));
|
||||
}
|
||||
}
|
||||
|
||||
private String assignResponseBody(ApiInfo api) {
|
||||
if (api == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(api.getResponseBody2())) {
|
||||
return api.getResponseBody1();
|
||||
} else {
|
||||
Random random = new Random();
|
||||
if (random.nextInt(2) == 0) {
|
||||
return api.getResponseBody2();
|
||||
} else {
|
||||
return api.getResponseBody1();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void assignResponseHeaders(HttpHeaders httpHeaders, String headerStr) {
|
||||
if (StringUtils.isBlank(headerStr)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// service_code=1234,user_id=james
|
||||
String[] headerPairs = org.springframework.util.StringUtils.tokenizeToStringArray(headerStr, ",");
|
||||
for (String headerPair : headerPairs) {
|
||||
String[] keyValue = org.springframework.util.StringUtils.tokenizeToStringArray(headerPair, "=");
|
||||
if (keyValue.length == 2 && StringUtils.isNotBlank(keyValue[0]) && StringUtils.isNotBlank(keyValue[1])) {
|
||||
httpHeaders.add(keyValue[0], keyValue[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ApiDao extends JpaRepository<ApiInfo, Long> {
|
||||
ApiInfo findByUrlAndServiceValue(String url, String serviceValue);
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Index;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.UniqueConstraint;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@Entity
|
||||
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "url", "serviceValue" }) }, indexes = {
|
||||
@Index(name = "idxApiInfo01", columnList = "apiGroupName") })
|
||||
public class ApiInfo implements Serializable {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@NotBlank
|
||||
@Column(length = 100, nullable = false)
|
||||
private String apiGroupName;
|
||||
|
||||
@NotBlank
|
||||
@Column(length = 100, nullable = false)
|
||||
private String apiName;
|
||||
|
||||
@NotBlank
|
||||
@Column(length = 200, nullable = false)
|
||||
private String url;
|
||||
|
||||
@Column(length = 50)
|
||||
private String serviceValue;
|
||||
|
||||
@NotBlank
|
||||
@Column(length = 10)
|
||||
private String method;
|
||||
|
||||
@Column(length = 50)
|
||||
private String responseContentType;
|
||||
|
||||
@Column(length = 5000)
|
||||
private String responseHeaders;
|
||||
|
||||
@Column(length = 5000)
|
||||
private String responseBody1;
|
||||
|
||||
@Column(length = 5000)
|
||||
private String responseBody2;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getApiGroupName() {
|
||||
return apiGroupName;
|
||||
}
|
||||
|
||||
public void setApiGroupName(String apiGroupName) {
|
||||
this.apiGroupName = apiGroupName;
|
||||
}
|
||||
|
||||
public String getApiName() {
|
||||
return apiName;
|
||||
}
|
||||
|
||||
public void setApiName(String apiName) {
|
||||
this.apiName = apiName;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getServiceValue() {
|
||||
return serviceValue;
|
||||
}
|
||||
|
||||
public void setServiceValue(String serviceValue) {
|
||||
this.serviceValue = serviceValue;
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public String getResponseContentType() {
|
||||
return responseContentType;
|
||||
}
|
||||
|
||||
public void setResponseContentType(String responseContentType) {
|
||||
this.responseContentType = responseContentType;
|
||||
}
|
||||
|
||||
public String getResponseHeaders() {
|
||||
return responseHeaders;
|
||||
}
|
||||
|
||||
public void setResponseHeaders(String responseHeaders) {
|
||||
this.responseHeaders = responseHeaders;
|
||||
}
|
||||
|
||||
public String getResponseBody1() {
|
||||
return responseBody1;
|
||||
}
|
||||
|
||||
public void setResponseBody1(String responseBody1) {
|
||||
this.responseBody1 = responseBody1;
|
||||
}
|
||||
|
||||
public String getResponseBody2() {
|
||||
return responseBody2;
|
||||
}
|
||||
|
||||
public void setResponseBody2(String responseBody2) {
|
||||
this.responseBody2 = responseBody2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ApiInfo [id=" + id + ", apiGroupName=" + apiGroupName + ", apiName=" + apiName + ", url=" + url
|
||||
+ ", serviceValue=" + serviceValue + ", method=" + method + ", responseContentType="
|
||||
+ responseContentType + ", responseHeaders=" + responseHeaders + ", responseBody1=" + responseBody1
|
||||
+ ", responseBody2=" + responseBody2 + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
public class ApiManageController {
|
||||
@Autowired
|
||||
private ApiService apiService;
|
||||
|
||||
@Autowired
|
||||
private ApiServiceKeyService apiKeyService;
|
||||
|
||||
@GetMapping("/manage/findAllApis")
|
||||
public String getAllApis(Pageable pageable, Model model) {
|
||||
model.addAttribute("apiInfoList", apiService.findAllApis(pageable));
|
||||
return "page/list-api";
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@GetMapping(path = { "/manage/getApiForm", "/manage/getApiForm/{id}" })
|
||||
public String getApiForm(Model model, @PathVariable Optional<Long> id) {
|
||||
if (id.isPresent()) {
|
||||
Optional<ApiInfo> optional = apiService.findById(id.get());
|
||||
if (optional.isPresent()) {
|
||||
model.addAttribute("apiInfo", optional.get());
|
||||
} else {
|
||||
throw new IllegalArgumentException("No data, id: " + id.get());
|
||||
}
|
||||
} else {
|
||||
ApiInfo apiInfo = new ApiInfo();
|
||||
apiInfo.setMethod(HttpMethod.POST.name());
|
||||
apiInfo.setResponseContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||
model.addAttribute("apiInfo", apiInfo);
|
||||
}
|
||||
|
||||
return "page/add-edit-api";
|
||||
}
|
||||
|
||||
@PostMapping("/manage/saveApi")
|
||||
public String saveApi(@Valid ApiInfo apiInfo, BindingResult result, Model model) {
|
||||
if (result.hasErrors()) {
|
||||
return "page/add-edit-api";
|
||||
}
|
||||
|
||||
if (apiInfo.getId() == null) {
|
||||
apiService.save(apiInfo);
|
||||
} else {
|
||||
apiService.update(apiInfo);
|
||||
}
|
||||
|
||||
return "redirect:/manage/findAllApis";
|
||||
}
|
||||
|
||||
@RequestMapping("/manage/deleteApi/{id}")
|
||||
public String deleteApi(@PathVariable Long id) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("No data, id: " + id);
|
||||
}
|
||||
|
||||
apiService.delete(id);
|
||||
|
||||
return "redirect:/manage/findAllApis";
|
||||
}
|
||||
|
||||
@GetMapping("/manage/findAllApiKeys")
|
||||
public String getAllApiServiceKeys(Pageable pageable, Model model) {
|
||||
model.addAttribute("apiServiceKeyList", apiKeyService.findAllApiServiceKeys(pageable));
|
||||
return "page/list-api-key";
|
||||
}
|
||||
|
||||
@GetMapping(path = { "/manage/getApiKeyForm", "/manage/getApiKeyForm/{id}" })
|
||||
public String getApiKeyForm(Model model, @PathVariable Optional<Long> id) {
|
||||
if (id.isPresent()) {
|
||||
Optional<ApiServiceKey> optional = apiKeyService.findById(id.get());
|
||||
if (optional.isPresent()) {
|
||||
model.addAttribute("apiServiceKey", optional.get());
|
||||
} else {
|
||||
throw new IllegalArgumentException("No data, id: " + id.get());
|
||||
}
|
||||
} else {
|
||||
model.addAttribute("apiServiceKey", new ApiServiceKey());
|
||||
}
|
||||
|
||||
return "page/add-edit-api-key";
|
||||
}
|
||||
|
||||
@PostMapping("/manage/saveApiKey")
|
||||
public String saveApiKey(@Valid ApiServiceKey apiServiceKey, BindingResult result, Model model) {
|
||||
if (result.hasErrors()) {
|
||||
return "page/add-edit-api-key";
|
||||
}
|
||||
|
||||
if (apiServiceKey.getId() == null) {
|
||||
apiKeyService.save(apiServiceKey);
|
||||
} else {
|
||||
apiKeyService.update(apiServiceKey);
|
||||
}
|
||||
|
||||
return "redirect:/manage/findAllApiKeys";
|
||||
}
|
||||
|
||||
@RequestMapping("/manage/deleteApiKey/{id}")
|
||||
public String deleteApiKey(@PathVariable Long id) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("No data, id: " + id);
|
||||
}
|
||||
|
||||
apiKeyService.delete(id);
|
||||
|
||||
return "redirect:/manage/findAllApiKeys";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.ExampleMatcher;
|
||||
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatcher;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.data.domain.Sort.Order;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import com.eactive.httpmockserver.datatables.Column;
|
||||
import com.eactive.httpmockserver.datatables.DataTablesRequest;
|
||||
import com.eactive.httpmockserver.datatables.DataTablesResponse;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/manage")
|
||||
public class ApiManageRestController {
|
||||
@Autowired
|
||||
private ApiService apiService;
|
||||
|
||||
@Autowired
|
||||
private ApiServiceKeyService apiKeyService;
|
||||
|
||||
@PostMapping("/rest/saveApi")
|
||||
public ApiInfo saveApi(@RequestBody ApiInfo apiInfo) {
|
||||
return apiService.save(apiInfo);
|
||||
}
|
||||
|
||||
@PostMapping("/rest/updateApi")
|
||||
public ApiInfo updateApi(@RequestBody ApiInfo apiInfo) {
|
||||
if (apiInfo.getId() == null) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "id is null");
|
||||
}
|
||||
|
||||
return apiService.update(apiInfo);
|
||||
}
|
||||
|
||||
@PostMapping("/rest/deleteApi/{id}")
|
||||
public ResponseEntity<?> deleteApi(@PathVariable Long id) {
|
||||
if (id == null) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "id is null");
|
||||
}
|
||||
|
||||
apiService.delete(id);
|
||||
|
||||
return ResponseEntity.ok("Entity deleted");
|
||||
}
|
||||
|
||||
@GetMapping("/rest/findAllApis")
|
||||
public List<ApiInfo> getAllApis(Pageable pageable) {
|
||||
return apiService.findAllApis(pageable);
|
||||
}
|
||||
|
||||
@PostMapping("/rest/apis")
|
||||
public DataTablesResponse<ApiInfo> getAllApisForDatatables(@RequestBody DataTablesRequest req) {
|
||||
Pageable pageable = assignJpaPageable(req);
|
||||
|
||||
// search option
|
||||
Example<ApiInfo> example = null;
|
||||
if (StringUtils.isNotBlank(req.getSearch().getValue())) {
|
||||
ApiInfo apiInfo = new ApiInfo();
|
||||
apiInfo.setApiGroupName(req.getSearch().getValue());
|
||||
apiInfo.setApiName(req.getSearch().getValue());
|
||||
apiInfo.setServiceValue(req.getSearch().getValue());
|
||||
|
||||
ExampleMatcher matcher = ExampleMatcher.matchingAny()
|
||||
.withMatcher("apiGroupName", new GenericPropertyMatcher().contains().ignoreCase())
|
||||
.withMatcher("apiName", new GenericPropertyMatcher().contains().ignoreCase())
|
||||
.withMatcher("serviceValue", new GenericPropertyMatcher().startsWith().ignoreCase());
|
||||
example = Example.of(apiInfo, matcher);
|
||||
}
|
||||
|
||||
DataTablesResponse<ApiInfo> res = new DataTablesResponse<ApiInfo>(apiService.findApis(example, pageable));
|
||||
res.setRecordsFiltered(apiService.getRecordsCount(example));
|
||||
res.setRecordsTotal(apiService.getRecordsCount(null));
|
||||
res.setDraw(req.getDraw());
|
||||
return res;
|
||||
}
|
||||
|
||||
public static Pageable assignJpaPageable(DataTablesRequest req) {
|
||||
int pageSize = req.getLength();
|
||||
if (pageSize == 0) {
|
||||
pageSize = 10;
|
||||
}
|
||||
|
||||
int pageNo = req.getStart() / pageSize;
|
||||
|
||||
if (req.getOrder() == null || req.getOrder().size() == 0) {
|
||||
return PageRequest.of(pageNo, pageSize);
|
||||
}
|
||||
|
||||
List<Sort.Order> orderList = new ArrayList<Sort.Order>();
|
||||
for (com.eactive.httpmockserver.datatables.Order order : req.getOrder()) {
|
||||
Column column = req.getColumns().get(order.getColumn());
|
||||
Direction direction = null;
|
||||
if (order.getDir().equals(com.eactive.httpmockserver.datatables.Direction.desc)) {
|
||||
direction = Direction.DESC;
|
||||
} else {
|
||||
direction = Direction.ASC;
|
||||
}
|
||||
orderList.add(new Order(direction, column.getData()));
|
||||
}
|
||||
|
||||
return PageRequest.of(pageNo, pageSize, Sort.by(orderList));
|
||||
}
|
||||
|
||||
@PostMapping("/rest/saveApiServiceKey")
|
||||
public ApiServiceKey saveApiServiceKey(@RequestBody ApiServiceKey apiServiceKey) {
|
||||
return apiKeyService.save(apiServiceKey);
|
||||
}
|
||||
|
||||
@PostMapping("/rest/updateApiServiceKey")
|
||||
public ApiServiceKey updateApiServiceKey(@RequestBody ApiServiceKey apiServiceKey) {
|
||||
if (apiServiceKey.getId() == null) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "id is null");
|
||||
}
|
||||
|
||||
return apiKeyService.update(apiServiceKey);
|
||||
}
|
||||
|
||||
@PostMapping("/rest/deleteApiServiceKey/{id}")
|
||||
public ResponseEntity<?> deleteApiServiceKey(@PathVariable Long id) {
|
||||
if (id == null) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "id is null");
|
||||
}
|
||||
|
||||
apiKeyService.delete(id);
|
||||
|
||||
return ResponseEntity.ok("Entity deleted");
|
||||
}
|
||||
|
||||
@GetMapping("/rest/findAllApiServiceKeys")
|
||||
public List<ApiServiceKey> getAllApiServiceKeys(Pageable pageable) {
|
||||
return apiKeyService.findAllApiServiceKeys(pageable);
|
||||
}
|
||||
|
||||
@PostMapping("/rest/apiKeys")
|
||||
public DataTablesResponse<ApiServiceKey> getAllApiKeysForDatatables(@RequestBody DataTablesRequest req) {
|
||||
Pageable pageable = assignJpaPageable(req);
|
||||
|
||||
// search option
|
||||
Example<ApiServiceKey> example = null;
|
||||
if (StringUtils.isNotBlank(req.getSearch().getValue())) {
|
||||
ApiServiceKey key = new ApiServiceKey();
|
||||
key.setUrl(req.getSearch().getValue());
|
||||
key.setServiceType(req.getSearch().getValue());
|
||||
key.setServiceKey(req.getSearch().getValue());
|
||||
|
||||
ExampleMatcher matcher = ExampleMatcher.matchingAny()
|
||||
.withMatcher("url", new GenericPropertyMatcher().contains().ignoreCase())
|
||||
.withMatcher("serviceType", new GenericPropertyMatcher().startsWith().ignoreCase())
|
||||
.withMatcher("serviceKey", new GenericPropertyMatcher().startsWith().ignoreCase());
|
||||
example = Example.of(key, matcher);
|
||||
}
|
||||
|
||||
DataTablesResponse<ApiServiceKey> res = new DataTablesResponse<ApiServiceKey>(
|
||||
apiKeyService.findApiKeys(example, pageable));
|
||||
res.setRecordsFiltered(apiKeyService.getRecordsCount(example));
|
||||
res.setRecordsTotal(apiKeyService.getRecordsCount(null));
|
||||
res.setDraw(req.getDraw());
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
public interface ApiService {
|
||||
public Optional<ApiInfo> findById(Long id);
|
||||
|
||||
public ApiInfo save(ApiInfo apiInfo);
|
||||
|
||||
public List<ApiInfo> findAllApis(Pageable pageable);
|
||||
|
||||
public List<ApiInfo> findApis(Example<ApiInfo> example, Pageable pageable);
|
||||
|
||||
public ApiInfo update(ApiInfo apiInfo);
|
||||
|
||||
public ApiInfo findByUrlAndServiceValue(String url, String serviceValue);
|
||||
|
||||
public void delete(Long id);
|
||||
|
||||
public long getRecordsCount(Example<ApiInfo> example);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional(propagation = Propagation.SUPPORTS)
|
||||
public class ApiServiceImpl implements ApiService {
|
||||
@Autowired
|
||||
private ApiDao apiDao;
|
||||
|
||||
@Override
|
||||
public Optional<ApiInfo> findById(Long id) {
|
||||
return apiDao.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
|
||||
public ApiInfo save(ApiInfo apiInfo) {
|
||||
return apiDao.save(apiInfo);
|
||||
}
|
||||
|
||||
public List<ApiInfo> findAllApis(Pageable pageable) {
|
||||
Page<ApiInfo> pagedResult = apiDao.findAll(pageable);
|
||||
if (pagedResult.hasContent()) {
|
||||
return pagedResult.getContent();
|
||||
} else {
|
||||
return new ArrayList<ApiInfo>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ApiInfo> findApis(Example<ApiInfo> example, Pageable pageable) {
|
||||
if (example == null) {
|
||||
return findAllApis(pageable);
|
||||
}
|
||||
|
||||
Page<ApiInfo> pagedResult = apiDao.findAll(example, pageable);
|
||||
if (pagedResult.hasContent()) {
|
||||
return pagedResult.getContent();
|
||||
} else {
|
||||
return new ArrayList<ApiInfo>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
|
||||
public ApiInfo update(ApiInfo apiInfo) {
|
||||
return apiDao.save(apiInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiInfo findByUrlAndServiceValue(String url, String serviceValue) {
|
||||
return apiDao.findByUrlAndServiceValue(url, serviceValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
apiDao.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getRecordsCount(Example<ApiInfo> example) {
|
||||
if (example == null) {
|
||||
return apiDao.count();
|
||||
} else {
|
||||
return apiDao.count(example);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.UniqueConstraint;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@Entity
|
||||
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "url" }) })
|
||||
public class ApiServiceKey implements Serializable {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@NotBlank
|
||||
@Column(length = 200, nullable = false)
|
||||
private String url;
|
||||
|
||||
@NotBlank
|
||||
@Column(length = 20, nullable = false)
|
||||
private String serviceType;
|
||||
|
||||
@NotBlank
|
||||
@Column(length = 50)
|
||||
private String serviceKey;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getServiceType() {
|
||||
return serviceType;
|
||||
}
|
||||
|
||||
public void setServiceType(String serviceType) {
|
||||
this.serviceType = serviceType;
|
||||
}
|
||||
|
||||
public String getServiceKey() {
|
||||
return serviceKey;
|
||||
}
|
||||
|
||||
public void setServiceKey(String serviceKey) {
|
||||
this.serviceKey = serviceKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ApiServiceKey [id=" + id + ", url=" + url + ", serviceType=" + serviceType + ", serviceKey="
|
||||
+ serviceKey + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ApiServiceKeyDao extends JpaRepository<ApiServiceKey, Long> {
|
||||
ApiServiceKey findByUrl(String url);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
public interface ApiServiceKeyService {
|
||||
public ApiServiceKey save(ApiServiceKey apiServiceKey);
|
||||
|
||||
public ApiServiceKey update(ApiServiceKey apiServiceKey);
|
||||
|
||||
public List<ApiServiceKey> findAllApiServiceKeys(Pageable pageable);
|
||||
|
||||
public ApiServiceKey findByUrl(String url);
|
||||
|
||||
public void delete(Long id);
|
||||
|
||||
public Optional<ApiServiceKey> findById(Long id);
|
||||
|
||||
public List<ApiServiceKey> findApiKeys(Example<ApiServiceKey> example, Pageable pageable);
|
||||
|
||||
public long getRecordsCount(Example<ApiServiceKey> example);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional(propagation = Propagation.SUPPORTS)
|
||||
public class ApiServiceKeyServiceImpl implements ApiServiceKeyService {
|
||||
@Autowired
|
||||
private ApiServiceKeyDao dao;
|
||||
|
||||
@Override
|
||||
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
|
||||
public ApiServiceKey save(ApiServiceKey apiServiceKey) {
|
||||
return dao.save(apiServiceKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
|
||||
public ApiServiceKey update(ApiServiceKey apiServiceKey) {
|
||||
return dao.save(apiServiceKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ApiServiceKey> findAllApiServiceKeys(Pageable pageable) {
|
||||
Page<ApiServiceKey> pagedResult = dao.findAll(pageable);
|
||||
|
||||
if (pagedResult.hasContent()) {
|
||||
return pagedResult.getContent();
|
||||
} else {
|
||||
return new ArrayList<ApiServiceKey>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ApiServiceKey> findApiKeys(Example<ApiServiceKey> example, Pageable pageable) {
|
||||
if (example == null) {
|
||||
return findAllApiServiceKeys(pageable);
|
||||
}
|
||||
|
||||
Page<ApiServiceKey> pagedResult = dao.findAll(example, pageable);
|
||||
if (pagedResult.hasContent()) {
|
||||
return pagedResult.getContent();
|
||||
} else {
|
||||
return new ArrayList<ApiServiceKey>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiServiceKey findByUrl(String url) {
|
||||
return dao.findByUrl(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
dao.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ApiServiceKey> findById(Long id) {
|
||||
return dao.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getRecordsCount(Example<ApiServiceKey> example) {
|
||||
if (example == null) {
|
||||
return dao.count();
|
||||
} else {
|
||||
return dao.count(example);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.eactive.httpmockserver;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class HttpmockserverApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(HttpmockserverApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.eactive.httpmockserver.config;
|
||||
|
||||
import org.apache.catalina.connector.Connector;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
||||
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ServletConfig {
|
||||
@Value("${server.port.http}")
|
||||
private int serverPortHttp;
|
||||
|
||||
@Bean
|
||||
public ServletWebServerFactory serverFactory() {
|
||||
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
|
||||
tomcat.addAdditionalTomcatConnectors(createStandardConnector());
|
||||
return tomcat;
|
||||
}
|
||||
|
||||
private Connector createStandardConnector() {
|
||||
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
|
||||
connector.setPort(serverPortHttp);
|
||||
return connector;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.eactive.httpmockserver.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
//registry.addViewController("/").setViewName("forward:/manage/findAllApis");
|
||||
registry.addViewController("/").setViewName("redirect:/manage/findAllApis");
|
||||
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
||||
WebMvcConfigurer.super.addViewControllers(registry);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.eactive.httpmockserver.datatables;
|
||||
|
||||
public class Column {
|
||||
private String data;
|
||||
private String name;
|
||||
private Boolean searchable;
|
||||
private Boolean orderable;
|
||||
private Search search;
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Boolean getSearchable() {
|
||||
return searchable;
|
||||
}
|
||||
|
||||
public void setSearchable(Boolean searchable) {
|
||||
this.searchable = searchable;
|
||||
}
|
||||
|
||||
public Boolean getOrderable() {
|
||||
return orderable;
|
||||
}
|
||||
|
||||
public void setOrderable(Boolean orderable) {
|
||||
this.orderable = orderable;
|
||||
}
|
||||
|
||||
public Search getSearch() {
|
||||
return search;
|
||||
}
|
||||
|
||||
public void setSearch(Search search) {
|
||||
this.search = search;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Column [data=" + data + ", name=" + name + ", searchable=" + searchable + ", orderable=" + orderable
|
||||
+ ", search=" + search + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.eactive.httpmockserver.datatables;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DataTablesRequest {
|
||||
private int start;
|
||||
private int length;
|
||||
private int draw;
|
||||
private List<Order> order;
|
||||
private List<Column> columns;
|
||||
private Search search;
|
||||
|
||||
public int getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public void setStart(int start) {
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public void setLength(int length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public int getDraw() {
|
||||
return draw;
|
||||
}
|
||||
|
||||
public void setDraw(int draw) {
|
||||
this.draw = draw;
|
||||
}
|
||||
|
||||
public List<Order> getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setOrder(List<Order> order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
public List<Column> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public void setColumns(List<Column> columns) {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
public Search getSearch() {
|
||||
return search;
|
||||
}
|
||||
|
||||
public void setSearch(Search search) {
|
||||
this.search = search;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DataTablesRequest [start=" + start + ", length=" + length + ", draw=" + draw + ", order=" + order
|
||||
+ ", columns=" + columns + ", search=" + search + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.eactive.httpmockserver.datatables;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DataTablesResponse<T> {
|
||||
public DataTablesResponse(List<T> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
private List<T> data;
|
||||
private long recordsFiltered;
|
||||
private long recordsTotal;
|
||||
private int draw;
|
||||
private String error;
|
||||
|
||||
public List<T> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(List<T> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public long getRecordsFiltered() {
|
||||
return recordsFiltered;
|
||||
}
|
||||
|
||||
public void setRecordsFiltered(long recordsFiltered) {
|
||||
this.recordsFiltered = recordsFiltered;
|
||||
}
|
||||
|
||||
public long getRecordsTotal() {
|
||||
return recordsTotal;
|
||||
}
|
||||
|
||||
public void setRecordsTotal(long recordsTotal) {
|
||||
this.recordsTotal = recordsTotal;
|
||||
}
|
||||
|
||||
public int getDraw() {
|
||||
return draw;
|
||||
}
|
||||
|
||||
public void setDraw(int draw) {
|
||||
this.draw = draw;
|
||||
}
|
||||
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
public void setError(String error) {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DataTablesResponse [data=" + data + ", recordsFiltered=" + recordsFiltered + ", recordsTotal="
|
||||
+ recordsTotal + ", draw=" + draw + ", error=" + error + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.eactive.httpmockserver.datatables;
|
||||
|
||||
public enum Direction {
|
||||
asc, desc;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.eactive.httpmockserver.datatables;
|
||||
|
||||
public class Order {
|
||||
private Integer column;
|
||||
private Direction dir;
|
||||
|
||||
public Integer getColumn() {
|
||||
return column;
|
||||
}
|
||||
|
||||
public void setColumn(Integer column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public Direction getDir() {
|
||||
return dir;
|
||||
}
|
||||
|
||||
public void setDir(Direction dir) {
|
||||
this.dir = dir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Order [column=" + column + ", dir=" + dir + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.eactive.httpmockserver.datatables;
|
||||
|
||||
public class Search {
|
||||
private String value;
|
||||
private String regexp;
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getRegexp() {
|
||||
return regexp;
|
||||
}
|
||||
|
||||
public void setRegexp(String regexp) {
|
||||
this.regexp = regexp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Search [value=" + value + ", regexp=" + regexp + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user