use of org.mockserver.log.model.LogEntry.LogMessageType.RETRIEVED in project mockserver by mock-server.
the class HttpState method retrieve.
public HttpResponse retrieve(HttpRequest request) {
final String logCorrelationId = UUIDService.getUUID();
CompletableFuture<HttpResponse> httpResponseFuture = new CompletableFuture<>();
HttpResponse response = response().withStatusCode(OK.code());
if (request != null) {
try {
final RequestDefinition requestDefinition = isNotBlank(request.getBodyAsString()) ? getRequestDefinitionSerializer().deserialize(request.getBodyAsJsonOrXmlString()) : request();
requestDefinition.withLogCorrelationId(logCorrelationId);
Format format = Format.valueOf(defaultIfEmpty(request.getFirstQueryStringParameter("format").toUpperCase(), "JSON"));
RetrieveType type = RetrieveType.valueOf(defaultIfEmpty(request.getFirstQueryStringParameter("type").toUpperCase(), "REQUESTS"));
switch(type) {
case LOGS:
{
mockServerLog.retrieveMessageLogEntries(requestDefinition, (List<LogEntry> logEntries) -> {
StringBuilder stringBuffer = new StringBuilder();
for (int i = 0; i < logEntries.size(); i++) {
LogEntry messageLogEntry = logEntries.get(i);
stringBuffer.append(messageLogEntry.getTimestamp()).append(" - ").append(messageLogEntry.getMessage());
if (i < logEntries.size() - 1) {
stringBuffer.append(LOG_SEPARATOR);
}
}
stringBuffer.append(NEW_LINE);
response.withBody(stringBuffer.toString(), MediaType.PLAIN_TEXT_UTF_8);
if (MockServerLogger.isEnabled(Level.INFO)) {
mockServerLogger.logEvent(new LogEntry().setType(RETRIEVED).setLogLevel(Level.INFO).setCorrelationId(logCorrelationId).setHttpRequest(requestDefinition).setMessageFormat("retrieved logs that match:{}").setArguments(requestDefinition));
}
httpResponseFuture.complete(response);
});
break;
}
case REQUESTS:
{
LogEntry logEntry = new LogEntry().setType(RETRIEVED).setLogLevel(Level.INFO).setCorrelationId(logCorrelationId).setHttpRequest(requestDefinition).setMessageFormat("retrieved requests in " + format.name().toLowerCase() + " that match:{}").setArguments(requestDefinition);
switch(format) {
case JAVA:
mockServerLog.retrieveRequests(requestDefinition, requests -> {
response.withBody(getRequestDefinitionSerializer().serialize(requests), MediaType.create("application", "java").withCharset(UTF_8));
mockServerLogger.logEvent(logEntry);
httpResponseFuture.complete(response);
});
break;
case JSON:
mockServerLog.retrieveRequests(requestDefinition, requests -> {
response.withBody(getRequestDefinitionSerializer().serialize(true, requests), MediaType.JSON_UTF_8);
mockServerLogger.logEvent(logEntry);
httpResponseFuture.complete(response);
});
break;
case LOG_ENTRIES:
mockServerLog.retrieveRequestLogEntries(requestDefinition, logEntries -> {
response.withBody(getLogEntrySerializer().serialize(logEntries), MediaType.JSON_UTF_8);
mockServerLogger.logEvent(logEntry);
httpResponseFuture.complete(response);
});
break;
}
break;
}
case REQUEST_RESPONSES:
{
LogEntry logEntry = new LogEntry().setType(RETRIEVED).setLogLevel(Level.INFO).setCorrelationId(logCorrelationId).setHttpRequest(requestDefinition).setMessageFormat("retrieved requests and responses in " + format.name().toLowerCase() + " that match:{}").setArguments(requestDefinition);
switch(format) {
case JAVA:
response.withBody("JAVA not supported for REQUEST_RESPONSES", MediaType.create("text", "plain").withCharset(UTF_8));
mockServerLogger.logEvent(logEntry);
httpResponseFuture.complete(response);
break;
case JSON:
mockServerLog.retrieveRequestResponses(requestDefinition, httpRequestAndHttpResponses -> {
response.withBody(getHttpRequestResponseSerializer().serialize(httpRequestAndHttpResponses), MediaType.JSON_UTF_8);
mockServerLogger.logEvent(logEntry);
httpResponseFuture.complete(response);
});
break;
case LOG_ENTRIES:
mockServerLog.retrieveRequestResponseMessageLogEntries(requestDefinition, logEntries -> {
response.withBody(getLogEntrySerializer().serialize(logEntries), MediaType.JSON_UTF_8);
mockServerLogger.logEvent(logEntry);
httpResponseFuture.complete(response);
});
break;
}
break;
}
case RECORDED_EXPECTATIONS:
{
LogEntry logEntry = new LogEntry().setType(RETRIEVED).setLogLevel(Level.INFO).setCorrelationId(logCorrelationId).setHttpRequest(requestDefinition).setMessageFormat("retrieved recorded expectations in " + format.name().toLowerCase() + " that match:{}").setArguments(requestDefinition);
switch(format) {
case JAVA:
mockServerLog.retrieveRecordedExpectations(requestDefinition, requests -> {
response.withBody(getExpectationToJavaSerializer().serialize(requests), MediaType.create("application", "java").withCharset(UTF_8));
mockServerLogger.logEvent(logEntry);
httpResponseFuture.complete(response);
});
break;
case JSON:
mockServerLog.retrieveRecordedExpectations(requestDefinition, requests -> {
response.withBody(getExpectationSerializerThatSerializesBodyDefault().serialize(requests), MediaType.JSON_UTF_8);
mockServerLogger.logEvent(logEntry);
httpResponseFuture.complete(response);
});
break;
case LOG_ENTRIES:
mockServerLog.retrieveRecordedExpectationLogEntries(requestDefinition, logEntries -> {
response.withBody(getLogEntrySerializer().serialize(logEntries), MediaType.JSON_UTF_8);
mockServerLogger.logEvent(logEntry);
httpResponseFuture.complete(response);
});
break;
}
break;
}
case ACTIVE_EXPECTATIONS:
{
List<Expectation> expectations = requestMatchers.retrieveActiveExpectations(requestDefinition);
switch(format) {
case JAVA:
response.withBody(getExpectationToJavaSerializer().serialize(expectations), MediaType.create("application", "java").withCharset(UTF_8));
break;
case JSON:
response.withBody(getExpectationSerializer().serialize(expectations), MediaType.JSON_UTF_8);
break;
case LOG_ENTRIES:
response.withBody("LOG_ENTRIES not supported for ACTIVE_EXPECTATIONS", MediaType.create("text", "plain").withCharset(UTF_8));
break;
}
if (MockServerLogger.isEnabled(Level.INFO)) {
mockServerLogger.logEvent(new LogEntry().setType(RETRIEVED).setLogLevel(Level.INFO).setCorrelationId(logCorrelationId).setHttpRequest(requestDefinition).setMessageFormat("retrieved " + expectations.size() + " active expectations in " + format.name().toLowerCase() + " that match:{}").setArguments(requestDefinition));
}
httpResponseFuture.complete(response);
break;
}
}
try {
return httpResponseFuture.get(configuration.maxFutureTimeoutInMillis(), MILLISECONDS);
} catch (ExecutionException | InterruptedException | TimeoutException ex) {
mockServerLogger.logEvent(new LogEntry().setLogLevel(Level.ERROR).setCorrelationId(logCorrelationId).setMessageFormat("exception handling request:{}error:{}").setArguments(request, ex.getMessage()).setThrowable(ex));
throw new RuntimeException("Exception retrieving state for " + request, ex);
}
} catch (IllegalArgumentException iae) {
mockServerLogger.logEvent(new LogEntry().setLogLevel(Level.ERROR).setCorrelationId(logCorrelationId).setMessageFormat("exception handling request:{}error:{}").setArguments(request, iae.getMessage()).setThrowable(iae));
if (iae.getMessage().contains(RetrieveType.class.getSimpleName())) {
throw new IllegalArgumentException("\"" + request.getFirstQueryStringParameter("type") + "\" is not a valid value for \"type\" parameter, only the following values are supported " + Arrays.stream(RetrieveType.values()).map(input -> input.name().toLowerCase()).collect(Collectors.toList()));
}
if (iae.getMessage().contains(Format.class.getSimpleName())) {
throw new IllegalArgumentException("\"" + request.getFirstQueryStringParameter("format") + "\" is not a valid value for \"format\" parameter, only the following values are supported " + Arrays.stream(Format.values()).map(input -> input.name().toLowerCase()).collect(Collectors.toList()));
}
throw iae;
}
} else {
return response().withStatusCode(200);
}
}
Aggregations