当前位置: 首页>>代码示例>>Java>>正文


Java Context.clientError方法代码示例

本文整理汇总了Java中ratpack.handling.Context.clientError方法的典型用法代码示例。如果您正苦于以下问题:Java Context.clientError方法的具体用法?Java Context.clientError怎么用?Java Context.clientError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ratpack.handling.Context的用法示例。


在下文中一共展示了Context.clientError方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: handle

import ratpack.handling.Context; //导入方法依赖的package包/类
@Override
public void handle(Context ctx) throws Exception {
  try {
    Iterable<Action<String,String>> actions = new LinkedList<>(Arrays.asList(
      new LongBlockingIOAction("foo", "data"),
      new LongBlockingIOAction("bar", "data"),
      Action.<String,String>of("buzz", "data", (execControl, data) -> execControl
        .promise(fulfiller -> {
          throw new IOException("CONTROLLED EXCEPTION");
        })),
      new LongBlockingIOAction("quzz", "data"),
      new LongBlockingIOAction("foo_1", "data"),
      new LongBlockingIOAction("foo_2", "data"),
      new LongBlockingIOAction("foo_3", "data"),
      new LongBlockingIOAction("foo_4", "data"),
      new LongBlockingIOAction("foo_5", "data"),
      new LongBlockingIOAction("foo_6", "data")
    ));

    Parallel<String,String> pattern = new Parallel<>();
    ctx.render(pattern.apply(ctx, ctx, actions));
  } catch (Exception ex) {
    ctx.clientError(404);
  }
}
 
开发者ID:zedar,项目名称:ratpack-examples,代码行数:26,代码来源:ParallelHandler.java

示例2: handle

import ratpack.handling.Context; //导入方法依赖的package包/类
/**
 * Runs actions with the given {@code pattern}
 *
 * @param ctx the request context
 * @throws Exception any
 */
@Override
public void handle(Context ctx) throws Exception {
  ctx.getResponse().getHeaders()
    .add("Cache-Control", "no-cache, no-store, must-revalidate")
    .add("Pragma", "no-cache")
    .add("Expires", "0");

  String patternName = ctx.getPathTokens().get(DEFAULT_NAME_TOKEN);
  if (patternName == null || "".equals(patternName)) {
    ctx.clientError(404);
    return;
  }

  if (FanOutFanIn.PATTERN_NAME.equals(patternName)) {
    ctx.insert(fanOutFanInHandler);
  } else if (Parallel.PATTERN_NAME.equals(patternName)) {
    ctx.insert(parallelHandler);
  } else if (InvokeWithRetry.PATTERN_NAME.equals(patternName)) {
    ctx.insert(invokeAndRetryHandler);
  } else {
    ctx.next();
  }
}
 
开发者ID:zedar,项目名称:ratpack-examples,代码行数:30,代码来源:ExecHandler.java

示例3: render

import ratpack.handling.Context; //导入方法依赖的package包/类
@Override
public void render(Context context, HealthCheckResults healthCheckResults) throws Exception {
  if (healthCheckResults == null) {
    context.clientError(405);
    return;
  }
  StringBuilder builder = new StringBuilder();
  healthCheckResults.getResults().forEach((name, result) -> {
    builder.append(name).append(" : ").append(result.isHealthy() ? "HEALTHY" : "UNHEALTHY");
    if (!result.isHealthy()) {
      builder.append(" [").append(result.getMessage()).append(" ]");
      if (result.getError() != null) {
        builder.append(" [").append(result.getError().toString()).append(" ]");
      }
    }
    builder.append("\n");
  });
  context.getResponse().getHeaders()
          .add("Cache-Control", "no-cache, no-store, must-revalidate")
          .add("Pragma", "no-cache")
          .add("Expires", 0);
  context.getResponse().send(builder.toString());
}
 
开发者ID:zedar,项目名称:ratpack-examples,代码行数:24,代码来源:HealthCheckResultsRenderer.java

示例4: handleByName

import ratpack.handling.Context; //导入方法依赖的package包/类
/**
 * Run health check with the given name. If it is not registered HTTP 404 is send to the client
 * All exceptions from health check are wrapped in unhealthy result with error=Exception.
 * @param context request context
 * @param name health check name
 * @throws Exception
 */
private void handleByName(Context context, String name) throws Exception {
  if (name == null || "".equals(name) || DEFAULT_NAME_TOKEN.equals(name)) {
    context.clientError(404);
    return;
  }
  SortedMap<String, HealthCheck.Result> hcheckResults = new ConcurrentSkipListMap<>();
  Optional<HealthCheck> hcheck = context.first(TypeToken.of(HealthCheck.class), hc -> hc.getName().equals(name));
  if (!hcheck.isPresent()) {
    context.clientError(404);
    return;
  }
  try {
    Promise<HealthCheck.Result> promise = hcheck.get().check(context.getExecution().getControl());
    promise.onError(throwable -> {
      hcheckResults.put(hcheck.get().getName(), HealthCheck.Result.unhealthy(throwable));
      context.render(new HealthCheckResults(ImmutableSortedMap.<String, HealthCheck.Result>copyOfSorted(hcheckResults)));
    }).then(r -> {
      hcheckResults.put(hcheck.get().getName(), r);
      context.render(new HealthCheckResults(ImmutableSortedMap.<String, HealthCheck.Result>copyOfSorted(hcheckResults)));
    });
  }
  catch (Exception ex) {
    hcheckResults.put(hcheck.get().getName(), HealthCheck.Result.unhealthy(ex));
    context.render(new HealthCheckResults(ImmutableSortedMap.<String, HealthCheck.Result>copyOfSorted(hcheckResults)));
  }
}
 
开发者ID:zedar,项目名称:ratpack-examples,代码行数:34,代码来源:HealthCheckHandler.java

示例5: handle

import ratpack.handling.Context; //导入方法依赖的package包/类
@Override
public void handle(Context ctx) throws Exception {
  try {
    AtomicInteger execCounter = new AtomicInteger(0);
    Action<String,String> action = Action.<String, String>of("foo", "data", (execControl, data) -> execControl
      .promise(fulfiller -> {
        if (execCounter.incrementAndGet() <= 3) {
          throw new IOException("FAILED EXECUTION");
        }
        fulfiller.success(ActionResult.success("BAR"));
      })
    );

    // check if retries have to be executed asynchronously
    boolean asyncRetry = false;
    MultiValueMap<String, String> queryAttrs = ctx.getRequest().getQueryParams();
    System.out.println("QUERY: " + queryAttrs.toString());
    if ("async".equals(queryAttrs.get("retrymode"))) {
      asyncRetry = true;
    }
    //InvokeWithRetry pattern = ctx.get(PATTERN_TYPE_TOKEN);
    PatternsModule.Config patternsConfig = ctx.get(PATTERN_CONFIG_TYPE_TOKEN);
    Objects.requireNonNull(patternsConfig);
    InvokeWithRetry<String,String> pattern = new InvokeWithRetry<>(patternsConfig.getDefaultRetryCount());

    // check if action should be executed asynchronously (in background)
    boolean asyncAction = false;
    if ("async".equals(queryAttrs.get("mode"))) {
      asyncAction = true;
    }

    if (asyncAction) {
      boolean finalAsyncRetry = asyncRetry;
      ctx.exec().start(execution -> pattern.apply(execution, ctx, action, 5, finalAsyncRetry)
        .then(actionResults -> {
          // TODO: log and store results for the future
        }));
      ctx.render(ctx.promiseOf(new ActionResults<>(ImmutableMap.of(action.getName(), ActionResult.success("EXECUTING IN BACKGROUND")))));
    } else {
      ctx.render(pattern.apply(ctx, ctx, action, 5, asyncRetry));
    }
  } catch (Exception ex) {
    ctx.clientError(404);
  }
}
 
开发者ID:zedar,项目名称:ratpack-examples,代码行数:46,代码来源:InvokeWithRetryHandler.java

示例6: handle

import ratpack.handling.Context; //导入方法依赖的package包/类
/**
 * Runs example actions with Fan-out/Fan-in pattern.
 *
 * @param ctx the request context
 * @throws Exception any
 */
@Override
public void handle(Context ctx) throws Exception {
  try {
    Iterable<Action<String,String>> actions = new LinkedList<>(Arrays.asList(
      new LongBlockingIOAction("foo", "data"),
      new LongBlockingIOAction("bar", "data"),
      Action.<String,String>of("buzz", "data", (execControl, data) -> execControl
        .promise(fulfiller -> {
          throw new IOException("CONTROLLED EXCEPTION");
        })),
      new LongBlockingIOAction("quzz", "data"),
      new LongBlockingIOAction("foo_1", "data"),
      new LongBlockingIOAction("foo_2", "data"),
      new LongBlockingIOAction("foo_3", "data"),
      new LongBlockingIOAction("foo_4", "data"),
      new LongBlockingIOAction("foo_5", "data"),
      new LongBlockingIOAction("foo_6", "data")
    ));
    Action<ActionResults<String>, String> mergeResults = Action.of("merge", null, (execControl, actionResults) ->
        execControl.promise(fulfiller -> {
          final int[] counters = {0, 0};
          actionResults.getResults().forEach((name, result) -> {
            if (result.getCode() != null && "0".equals(result.getCode())) {
              counters[0]++;
            } else if (result.getCode() != null && !"0".equals(result.getCode())) {
              counters[1]++;
            }
          });
          StringBuilder strB = new StringBuilder();
          strB.append("Succeeded: ").append(counters[0]).append(" Failed: ").append(counters[1]);
          fulfiller.success(ActionResult.error("0", strB.toString()));
        })
    );

    FanOutFanIn<String,String,String> pattern = new FanOutFanIn<>();
    ctx.render(pattern.apply(ctx, ctx, actions, mergeResults));
  } catch (Exception ex) {
    ctx.clientError(404);
  }
}
 
开发者ID:zedar,项目名称:ratpack-examples,代码行数:47,代码来源:FanOutFanInHandler.java


注:本文中的ratpack.handling.Context.clientError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。