@Override protected KnotContext processError(KnotContext knotContext, Throwable error) { ClientResponse errorResponse = new ClientResponse() .setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code()); return new KnotContext() .setClientRequest(knotContext.getClientRequest()) .setClientResponse(errorResponse); }
private KnotContext createSuccessResponse(KnotContext inputContext) { return new KnotContext() .setClientRequest(inputContext.getClientRequest()) .setClientResponse(inputContext.getClientResponse()) .setFragments( Optional.ofNullable(inputContext.getFragments()).orElse(Collections.emptyList())) .setTransition(DEFAULT_TRANSITION); }
public static void toJson(KnotContext obj, JsonObject json) { if (obj.getClientRequest() != null) { json.put("clientRequest", obj.getClientRequest().toJson()); } if (obj.getClientResponse() != null) { json.put("clientResponse", obj.getClientResponse().toJson()); } if (obj.getFragments() != null) { JsonArray array = new JsonArray(); obj.getFragments().forEach(item -> array.add(item.toJson())); json.put("fragments", array); } if (obj.getTransition() != null) { json.put("transition", obj.getTransition()); } } }
public static KnotContext create(List<Pair<List<String>, String>> fragments) { return new KnotContext() .setFragments( fragments != null ? fragments.stream().map(data -> Fragment.snippet(data.getKey(), data.getValue())).collect(Collectors.toList()) : null) .setClientRequest(new ClientRequest()) .setClientResponse( new ClientResponse() .setHeaders(MultiMap.caseInsensitiveMultiMap())); }
@Override protected KnotContext processError(KnotContext knotContext, Throwable error) { HttpResponseStatus statusCode; if (error instanceof NoSuchElementException) { statusCode = HttpResponseStatus.NOT_FOUND; } else { statusCode = HttpResponseStatus.INTERNAL_SERVER_ERROR; } knotContext.getClientResponse().setStatusCode(statusCode.code()); return knotContext; }
public static KnotContext empty(String template) { return new KnotContext() .setClientResponse( new ClientResponse() .setBody(StringUtils.isEmpty(template) ? Buffer.buffer() : Buffer.buffer(template)) .setStatusCode(HttpResponseStatus.OK.code()) .setHeaders(MultiMap.caseInsensitiveMultiMap())) .setClientRequest(new ClientRequest()); }
public static void fromJson(JsonObject json, KnotContext obj) { if (json.getValue("clientRequest") instanceof JsonObject) { obj.setClientRequest(new io.knotx.dataobjects.ClientRequest((JsonObject)json.getValue("clientRequest"))); } if (json.getValue("clientResponse") instanceof JsonObject) { obj.setClientResponse(new io.knotx.dataobjects.ClientResponse((JsonObject)json.getValue("clientResponse"))); } if (json.getValue("fragments") instanceof JsonArray) { java.util.ArrayList<io.knotx.dataobjects.Fragment> list = new java.util.ArrayList<>(); json.getJsonArray("fragments").forEach( item -> { if (item instanceof JsonObject) list.add(new io.knotx.dataobjects.Fragment((JsonObject)item)); }); obj.setFragments(list); } if (json.getValue("transition") instanceof String) { obj.setTransition((String)json.getValue("transition")); } }
@Override public void handle(RoutingContext context) { context.put(KnotContext.KEY, new KnotContext().setClientRequest(new ClientRequest(context.request()))); context.next(); }
private void createSimpleKnot(Vertx vertx, final String address, final String addToBody, final String transition) { MockKnotProxy.register(vertx.getDelegate(), address, knotContext -> { knotContext.getClientResponse().setBody( knotContext.getClientResponse().getBody().appendString(addToBody) ); knotContext.setTransition(transition); }); }
private boolean hasFragments(KnotContext knotContext) { return knotContext.getFragments() != null && !knotContext.getFragments().isEmpty(); }
@Override protected Single<KnotContext> processRequest(KnotContext knotContext) { try { knotContext .setFragments(splitter.split(knotContext.getClientResponse().getBody().toString())); knotContext.getClientResponse().setStatusCode(HttpResponseStatus.OK.code()).clearBody(); return Single.just(knotContext); } catch (Exception ex) { LOGGER.error("Exception happened during HTML splitting.", ex); return Single.just(processError(knotContext, ex)); } }
private static Optional<String> getFormIdentifierFromRequest(KnotContext knotContext, String formIdAttrName) { return Optional.ofNullable( knotContext.getClientRequest().getFormAttributes().get(formIdAttrName)); }
@Test @KnotxApplyConfiguration("io/knotx/splitter/knotx-fragment-splitter-test.json") public void callSplitterWithEmptyBody_expectNotFoundResponse( VertxTestContext context, Vertx vertx) { callFragmentSplitterWithAssertions(context, vertx, "", knotContext -> { assertEquals(knotContext.getClientResponse().getStatusCode(), HttpResponseStatus.NOT_FOUND.code()); assertNull(knotContext.getFragments()); }); }
@Test public void handleRepositoryResponse_whenResponseIsSuccessAndShouldBeProcessed_expectContextContinuation() { when(clientResponse.getStatusCode()).thenReturn(200); when(repositoryEntry.isDoProcessing()).thenReturn(true); tested.handleRepositoryResponse(clientResponse, routingContext, repositoryEntry, knotContext); verify(knotContext, times(1)).setClientResponse(clientResponse); verify(routingContext, times(1)).put(KnotContext.KEY, knotContext); verify(routingContext, times(1)).next(); }
private void createSimpleGatewayKnot(Vertx vertx, final String address, final String transition) { MockKnotProxy.register(vertx.getDelegate(), address, knotContext -> { ClientResponse clientResponse = new ClientResponse(); clientResponse.setStatusCode(200); knotContext.setClientResponse(clientResponse); knotContext.setTransition(transition); }); }
@Override protected Single<KnotContext> processRequest(KnotContext knotContext) { return Single.create(observer -> { try { knotContext.setTransition(DEFAULT_TRANSITION); Optional.ofNullable(knotContext.getFragments()).ifPresent(fragments -> fragments.stream() .filter(this::shouldProcess) .forEach(fragment -> fragment.content(evaluateWithFallback(fragment))) ); observer.onSuccess(knotContext); } catch (Exception e) { observer.onError(e); } }); }
private KnotContext handleGetMethod(List<FormEntity> forms, KnotContext knotContext) { LOGGER.debug("Pass-through {} request", knotContext.getClientRequest().getMethod()); knotContext.setTransition(DEFAULT_TRANSITION); forms.forEach(form -> form.fragment() .content(simplifier.transform(form.fragment().content(), options.getFormIdentifierName(), form.identifier()))); return knotContext; }
@Override public void process(KnotContext knotContext, Handler<AsyncResult<KnotContext>> result) { if (closed) { result.handle(Future.failedFuture(new IllegalStateException("Proxy is closed"))); return; } JsonObject _json = new JsonObject(); _json.put("knotContext", knotContext == null ? null : knotContext.toJson()); DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options) : new DeliveryOptions(); _deliveryOptions.addHeader("action", "process"); _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> { if (res.failed()) { result.handle(Future.failedFuture(res.cause())); } else { result.handle(Future.succeededFuture(res.result().body() == null ? null : new KnotContext(res.result().body()))); } }); }
private KnotContext redirectKnotResponse(KnotContext knotContext, FormEntity form, ClientResponse clientResponse, String redirectLocation) { LOGGER.debug("Request redirected to [{}]", redirectLocation); knotContext.getClientResponse() .setStatusCode(HttpResponseStatus.MOVED_PERMANENTLY.code()); MultiMap headers = MultiMap.caseInsensitiveMultiMap(); headers.addAll(getFilteredHeaders(clientResponse.getHeaders(), form.adapter().getAllowedResponseHeadersPatterns())); headers.add(HttpHeaders.LOCATION.toString(), redirectLocation); knotContext.getClientResponse().setHeaders(headers); knotContext.clearFragments(); return knotContext; }
public static KnotContext empty(List<Fragment> fragments) { return empty("").setFragments(fragments); }