Skip to content

Commit

Permalink
Adding support for api redirect on defined redirect url in applicatio…
Browse files Browse the repository at this point in the history
…ns yml
  • Loading branch information
povel62 committed Jun 3, 2020
1 parent 2208daf commit 74dec74
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
69 changes: 50 additions & 19 deletions src/main/java/com/smockin/admin/service/HttpClientServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.smockin.admin.service;

import com.smockin.admin.dto.HttpClientCallDTO;
import com.smockin.admin.dto.response.HttpClientResponseDTO;
import com.smockin.admin.exception.ValidationException;
import com.smockin.mockserver.dto.MockServerState;
import com.smockin.mockserver.exception.MockServerException;
import com.smockin.utils.HttpClientUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
Expand All @@ -14,12 +13,16 @@
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.http.HttpStatus;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import com.smockin.admin.dto.HttpClientCallDTO;
import com.smockin.admin.dto.response.HttpClientResponseDTO;
import com.smockin.admin.exception.ValidationException;
import com.smockin.mockserver.dto.MockServerState;
import com.smockin.mockserver.exception.MockServerException;
import com.smockin.utils.HttpClientUtils;

/**
* Created by mgallina.
Expand All @@ -29,6 +32,9 @@ public class HttpClientServiceImpl implements HttpClientService {

private final Logger logger = LoggerFactory.getLogger(HttpClientServiceImpl.class);

@Value("${smockin.redirect.url}")
private String redirectUrl;

@Autowired
private MockedServerEngineService mockedServerEngineService;

Expand All @@ -43,24 +49,49 @@ public HttpClientResponseDTO handleCall(final HttpClientCallDTO dto) throws Vali
try {

final MockServerState state = mockedServerEngineService.getRestServerState();

HttpClientResponseDTO httpClientResponseDTO;

if (!state.isRunning()) {
return new HttpClientResponseDTO(HttpStatus.NOT_FOUND.value());
}

dto.setUrl("http://localhost:" + state.getPort() + dto.getUrl());

String url = dto.getUrl();
dto.setUrl(redirectUrl + url);
switch (dto.getMethod()) {
case GET:
return get(dto);
httpClientResponseDTO = get(dto);
if(httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = get(dto);
}
return httpClientResponseDTO;
case POST:
return post(dto);
httpClientResponseDTO = post(dto);
if(httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = post(dto);
}
return httpClientResponseDTO;
case PUT:
return put(dto);
httpClientResponseDTO = put(dto);
if(httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = put(dto);
}
return httpClientResponseDTO;
case DELETE:
return delete(dto);
httpClientResponseDTO = delete(dto);
if(httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = delete(dto);
}
return httpClientResponseDTO;
case PATCH:
return patch(dto);
httpClientResponseDTO = patch(dto);
if(httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = patch(dto);
}
return httpClientResponseDTO;
default:
throw new ValidationException("Invalid / Unsupported method: " + dto.getMethod());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ datasource:
smockin:
ftp.root.dir: "${user.home}/.smockin/ftp/"
auth.exclusions: '{"*.html":["GET"],"*.js":["GET"],"*.css":["GET"],"*.woff2":["GET"],"*.png":["GET"],"*.gif":["GET"],"/auth":["POST"],"/user/mode":["GET"],"/mockedserver/rest/status":["GET"],"/mockedserver/jms/status":["GET"],"/mockedserver/ftp/status":["GET"],"/password/reset/token/*":["GET","POST"]}'

redirect.url: "https://genchi.pointclickcare.com:443/genchi/api/"
---
# NOTE
# The 'start.sh' script overrides the below DB related params using values defined
Expand Down

0 comments on commit 74dec74

Please sign in to comment.