use of org.mockserver.log.model.LogEntryMessages.RECEIVED_REQUEST_MESSAGE_FORMAT in project mockserver by mock-server.
the class HttpState method handle.
public boolean handle(HttpRequest request, ResponseWriter responseWriter, boolean warDeployment) {
request.withLogCorrelationId(UUIDService.getUUID());
setPort(request);
if (MockServerLogger.isEnabled(Level.TRACE)) {
mockServerLogger.logEvent(new LogEntry().setLogLevel(Level.TRACE).setHttpRequest(request).setMessageFormat(RECEIVED_REQUEST_MESSAGE_FORMAT).setArguments(request));
}
if (request.matches("PUT")) {
CompletableFuture<Boolean> canHandle = new CompletableFuture<>();
if (request.matches("PUT", PATH_PREFIX + "/expectation", "/expectation")) {
if (controlPlaneRequestAuthenticated(request, responseWriter)) {
List<Expectation> upsertedExpectations = new ArrayList<>();
for (Expectation expectation : getExpectationSerializer().deserializeArray(request.getBodyAsJsonOrXmlString(), false)) {
if (!warDeployment || validateSupportedFeatures(expectation, request, responseWriter)) {
upsertedExpectations.addAll(add(expectation));
}
}
responseWriter.writeResponse(request, response().withStatusCode(CREATED.code()).withBody(getExpectationSerializer().serialize(upsertedExpectations), MediaType.JSON_UTF_8), true);
}
canHandle.complete(true);
} else if (request.matches("PUT", PATH_PREFIX + "/openapi", "/openapi")) {
if (controlPlaneRequestAuthenticated(request, responseWriter)) {
try {
List<Expectation> upsertedExpectations = new ArrayList<>();
for (OpenAPIExpectation openAPIExpectation : getOpenAPIExpectationSerializer().deserializeArray(request.getBodyAsJsonOrXmlString(), false)) {
upsertedExpectations.addAll(add(openAPIExpectation));
}
responseWriter.writeResponse(request, response().withStatusCode(CREATED.code()).withBody(getExpectationSerializer().serialize(upsertedExpectations), MediaType.JSON_UTF_8), true);
} catch (IllegalArgumentException iae) {
mockServerLogger.logEvent(new LogEntry().setLogLevel(Level.ERROR).setMessageFormat("exception handling request for open api expectation:{}error:{}").setArguments(request, iae.getMessage()).setThrowable(iae));
responseWriter.writeResponse(request, BAD_REQUEST, (!iae.getMessage().startsWith(OPEN_API_LOAD_ERROR) ? OPEN_API_LOAD_ERROR + (isNotBlank(iae.getMessage()) ? ", " : "") : "") + iae.getMessage(), MediaType.create("text", "plain").toString());
}
}
canHandle.complete(true);
} else if (request.matches("PUT", PATH_PREFIX + "/clear", "/clear")) {
if (controlPlaneRequestAuthenticated(request, responseWriter)) {
clear(request);
responseWriter.writeResponse(request, OK);
}
canHandle.complete(true);
} else if (request.matches("PUT", PATH_PREFIX + "/reset", "/reset")) {
if (controlPlaneRequestAuthenticated(request, responseWriter)) {
reset();
responseWriter.writeResponse(request, OK);
}
canHandle.complete(true);
} else if (request.matches("PUT", PATH_PREFIX + "/retrieve", "/retrieve")) {
if (controlPlaneRequestAuthenticated(request, responseWriter)) {
responseWriter.writeResponse(request, retrieve(request), true);
}
canHandle.complete(true);
} else if (request.matches("PUT", PATH_PREFIX + "/verify", "/verify")) {
if (controlPlaneRequestAuthenticated(request, responseWriter)) {
verify(getVerificationSerializer().deserialize(request.getBodyAsJsonOrXmlString()), result -> {
if (isEmpty(result)) {
responseWriter.writeResponse(request, ACCEPTED);
} else {
responseWriter.writeResponse(request, NOT_ACCEPTABLE, result, MediaType.create("text", "plain").toString());
}
canHandle.complete(true);
});
} else {
canHandle.complete(true);
}
} else if (request.matches("PUT", PATH_PREFIX + "/verifySequence", "/verifySequence")) {
if (controlPlaneRequestAuthenticated(request, responseWriter)) {
verify(getVerificationSequenceSerializer().deserialize(request.getBodyAsJsonOrXmlString()), result -> {
if (isEmpty(result)) {
responseWriter.writeResponse(request, ACCEPTED);
} else {
responseWriter.writeResponse(request, NOT_ACCEPTABLE, result, MediaType.create("text", "plain").toString());
}
canHandle.complete(true);
});
} else {
canHandle.complete(true);
}
} else {
canHandle.complete(false);
}
try {
return canHandle.get(configuration.maxFutureTimeoutInMillis(), MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
mockServerLogger.logEvent(new LogEntry().setLogLevel(Level.ERROR).setMessageFormat("exception handling request:{}error:{}").setArguments(request, ex.getMessage()).setThrowable(ex));
return false;
}
} else {
return false;
}
}
Aggregations