
io.knotx.mocks.knot.MockKnotHandler Maven / Gradle / Ivy
/*
* Copyright (C) 2016 Cognifide Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.knotx.mocks.knot;
import io.knotx.dataobjects.ClientResponse;
import io.knotx.dataobjects.KnotContext;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.reactivex.Observable;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.RoutingContext;
import io.vertx.reactivex.core.eventbus.Message;
import io.vertx.reactivex.core.file.FileSystem;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
public class MockKnotHandler implements Handler> {
private static final Logger LOGGER = LoggerFactory.getLogger(RoutingContext.class);
private static final KnotContext ERROR_RESPONSE = new KnotContext()
.setClientResponse(
new ClientResponse().setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code()));
private static final KnotContext NOT_FOUND = new KnotContext()
.setClientResponse(new ClientResponse().setStatusCode(HttpResponseStatus.NOT_FOUND.code()));
private final FileSystem fileSystem;
private final JsonObject handlerConfig;
public MockKnotHandler(JsonObject handlerConfig, FileSystem fileSystem) {
this.handlerConfig = handlerConfig;
this.fileSystem = fileSystem;
}
@Override
public void handle(Message message) {
final Observable result = Observable.just(message)
.map(Message::body)
.filter(this::findConfiguration)
.doOnNext(this::logProcessedInfo)
.flatMap(this::prepareHandlerResponse)
.defaultIfEmpty(NOT_FOUND);
result.subscribe(
message::reply,
error -> {
LOGGER.error("Unable to return response!", error.getMessage());
message.reply(ERROR_RESPONSE);
}
);
}
private Boolean findConfiguration(KnotContext context) {
return handlerConfig.containsKey(context.getClientRequest().getPath());
}
private void logProcessedInfo(KnotContext context) {
LOGGER.info("Processing `{}`", context.getClientRequest().getPath());
}
private Observable prepareHandlerResponse(KnotContext context) {
final JsonObject responseConfig = handlerConfig
.getJsonObject(context.getClientRequest().getPath());
return Observable.fromArray(KnotContextKeys.values())
.flatMap(key -> key.valueOrDefault(fileSystem, responseConfig, context))
.filter(value -> value.getRight().isPresent())
.reduce(new JsonObject(), this::mergeResponseValues)
.map(val -> new KnotContext().setClientRequest(null))
.toObservable();
}
private JsonObject mergeResponseValues(JsonObject result, Pair> value) {
return new JsonObject().put(value.getLeft(), value.getRight().orElse(StringUtils.EMPTY));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy