Skip to content

Commit

Permalink
Adding server redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
povel62 committed Jun 9, 2020
1 parent b8d4f4c commit aa2aa02
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
public interface HttpClientService {

HttpClientResponseDTO handleCall(final HttpClientCallDTO httpClientCallDTO) throws ValidationException;
HttpClientResponseDTO handleCall(final HttpClientCallDTO httpClientCallDTO, boolean justRedirect) throws ValidationException;

}
114 changes: 59 additions & 55 deletions src/main/java/com/smockin/admin/service/HttpClientServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,67 +39,71 @@ public class HttpClientServiceImpl implements HttpClientService {
private MockedServerEngineService mockedServerEngineService;

@Override
public HttpClientResponseDTO handleCall(final HttpClientCallDTO dto) throws ValidationException {
logger.debug("handleCall called");

debugDTO(dto);

validateRequest(dto);

try {

final MockServerState state = mockedServerEngineService.getRestServerState();
HttpClientResponseDTO httpClientResponseDTO;

if (!state.isRunning()) {
return new HttpClientResponseDTO(HttpStatus.NOT_FOUND.value());
}
String url = dto.getUrl();
dto.setUrl(redirectUrl + url);
switch (dto.getMethod()) {
case GET:
httpClientResponseDTO = get(dto);
if(httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = get(dto);
}
return httpClientResponseDTO;
case POST:
httpClientResponseDTO = post(dto);
if(httpClientResponseDTO.getStatus() == 404) {
public HttpClientResponseDTO handleCall(final HttpClientCallDTO dto, boolean justRedirect) throws ValidationException {
logger.debug("handleCall called");

debugDTO(dto);

validateRequest(dto);

try {

final MockServerState state = mockedServerEngineService.getRestServerState();
HttpClientResponseDTO httpClientResponseDTO;

if (!state.isRunning()) {
return new HttpClientResponseDTO(HttpStatus.NOT_FOUND.value());
}
String url = dto.getUrl();
dto.setUrl(redirectUrl + url);
switch (dto.getMethod()) {
case GET:
httpClientResponseDTO = get(dto);
if(!justRedirect && httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = post(dto);
httpClientResponseDTO = get(dto);
}
return httpClientResponseDTO;
case PUT:
case POST:
httpClientResponseDTO = post(dto);
if(!justRedirect && httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = post(dto);
}
return httpClientResponseDTO;
case PUT:
httpClientResponseDTO = put(dto);
if(!justRedirect && httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = put(dto);
if(httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = put(dto);
}
return httpClientResponseDTO;
case DELETE:
}
return httpClientResponseDTO;
case DELETE:
httpClientResponseDTO = delete(dto);
if(!justRedirect && httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = delete(dto);
if(httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
httpClientResponseDTO = delete(dto);
}
return httpClientResponseDTO;
case PATCH:
}
return httpClientResponseDTO;
case PATCH:
httpClientResponseDTO = patch(dto);
if(!justRedirect && httpClientResponseDTO.getStatus() == 404) {
dto.setUrl("http://localhost:" + state.getPort() + url);
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());
}

} catch (IOException | MockServerException ex) {
return new HttpClientResponseDTO(HttpStatus.NOT_FOUND.value());
}

}
return httpClientResponseDTO;
default:
throw new ValidationException("Invalid / Unsupported method: " + dto.getMethod());
}

} catch (IOException | MockServerException ex) {
return new HttpClientResponseDTO(HttpStatus.NOT_FOUND.value());
}
}

@Override
public HttpClientResponseDTO handleCall(final HttpClientCallDTO dto) throws ValidationException {
return handleCall(dto, false);
}

HttpClientResponseDTO get(final HttpClientCallDTO reqDto) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
package com.smockin.mockserver.engine;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Optional;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smockin.admin.dto.HttpClientCallDTO;
import com.smockin.admin.dto.response.HttpClientResponseDTO;
import com.smockin.admin.exception.ValidationException;
import com.smockin.admin.persistence.dao.RestfulMockDAO;
import com.smockin.admin.persistence.entity.RestfulMock;
import com.smockin.admin.persistence.entity.RestfulMockDefinitionOrder;
import com.smockin.admin.persistence.entity.RestfulMockDefinitionRule;
import com.smockin.admin.persistence.enums.RestMethodEnum;
import com.smockin.admin.persistence.enums.RestMockTypeEnum;
import com.smockin.admin.persistence.enums.SmockinUserRoleEnum;
import com.smockin.admin.service.HttpClientService;
import com.smockin.mockserver.exception.InboundParamMatchException;
import com.smockin.mockserver.service.*;
import com.smockin.mockserver.service.HttpProxyService;
import com.smockin.mockserver.service.InboundParamMatchService;
import com.smockin.mockserver.service.JavaScriptResponseHandler;
import com.smockin.mockserver.service.MockOrderingCounterService;
import com.smockin.mockserver.service.RuleEngine;
import com.smockin.mockserver.service.ServerSideEventService;
import com.smockin.mockserver.service.StatefulService;
import com.smockin.mockserver.service.dto.RestfulResponseDTO;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import spark.Request;
import spark.Response;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Optional;

/**
* Created by mgallina.
Expand Down Expand Up @@ -59,6 +73,8 @@ public class MockedRestServerEngineUtils {
@Autowired
private StatefulService statefulService;

@Autowired
private HttpClientService httpClientService;

public Optional<String> loadMockedResponse(final Request request,
final Response response,
Expand All @@ -67,6 +83,25 @@ public Optional<String> loadMockedResponse(final Request request,

debugInboundRequest(request);

HttpClientCallDTO http = new HttpClientCallDTO();
http.setUrl(request.pathInfo());
http.setMethod(RestMethodEnum.findByName(request.requestMethod()));
http.setHeaders(request.params());
http.setBody(request.body());
HttpClientResponseDTO responseDto = null;
try
{
responseDto = httpClientService.handleCall(http, true);
} catch (ValidationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

if(responseDto != null && responseDto.getStatus() != 404) {
return Optional.of(new ResponseEntity<HttpClientResponseDTO>(responseDto, HttpStatus.OK).getBody().getBody());
}

try {

final RestfulMock mock = (isMultiUserMode)
Expand Down

0 comments on commit aa2aa02

Please sign in to comment.