public void sendResponse() {
   int statusCode = context.getResponse().getStatus().getCode();
   if (statusCode >= 400) {
     context.clientError(statusCode);
   } else {
     context.getResponse().send(MediaType.TEXT_HTML, responseContent);
   }
 }
  public RatpackWebContext(Context ctx, TypedData body, SessionData session) {
    this.context = ctx;
    this.session = session;
    this.request = ctx.getRequest();
    this.response = ctx.getResponse();

    if (isFormAvailable(request, body)) {
      this.form = FormDecoder.parseForm(ctx, body, MultiValueMap.empty());
    } else {
      this.form = new DefaultForm(MultiValueMap.empty(), MultiValueMap.empty());
    }
  }
 public URI getAddress(Context context) {
   URI currentUrl = null;
   if (publicAddress == null) {
     BindAddress bindAddress = context.getBindAddress();
     try {
       currentUrl =
           new URI(
               uriScheme, null, bindAddress.getHost(), bindAddress.getPort(), null, null, null);
     } catch (URISyntaxException e) {
       context.error(e);
     }
   } else {
     currentUrl = publicAddress;
   }
   return currentUrl;
 }
  private String generateRedirectLocation(Context context, Request request, String path) {
    // Rules
    // 1. Given absolute URL use it
    // 2. Given Starting Slash prepend public facing domain:port if provided if not use base URL of
    // request
    // 3. Given relative URL prepend public facing domain:port plus parent path of request URL
    // otherwise full parent path

    PublicAddress publicAddress = context.get(PublicAddress.class);
    String generatedPath;
    URI host = publicAddress.getAddress(context);

    if (ABSOLUTE_PATTERN.matcher(path).matches()) {
      // Rule 1 - Path is absolute
      generatedPath = path;
    } else {
      if (path.charAt(0) == '/') {
        // Rule 2 - Starting Slash
        generatedPath = host.toString() + path;
      } else {
        // Rule 3
        generatedPath = host.toString() + getParentPath(request.getUri()) + path;
      }
    }

    return generatedPath;
  }
 @Override
 public void handle(Context ctx, RPCConfig rpc) {
   ctx.get(HttpClient.class)
       .requestStream(
           rpc.getURI(),
           requestSpec -> {
             requestSpec.post();
             requestSpec.body(body -> body.type(jsonType).text(buildStatusReq()));
             requestSpec.redirects(0);
             if (rpc.getUsername() != null) {
               requestSpec.basicAuth(rpc.getUsername(), rpc.getPassword());
             }
           })
       .then(
           responseStream -> {
             // TODO: Extract from JsonRpcResponse and return more restful JSON format (no RPC
             // wrapper)
             responseStream.forwardTo(ctx.getResponse());
           });
 }
 public void handle(Context context) {
   context.redirect(code, location);
 }
 private URI getAddress() {
   return context.get(PublicAddress.class).get();
 }
 public void redirect(Context context, String location, int code) {
   context.getResponse().status(code);
   String normalizedLocation = generateRedirectLocation(context, context.getRequest(), location);
   context.getResponse().getHeaders().set(HttpHeaderConstants.LOCATION, normalizedLocation);
   context.getResponse().send();
 }
 public void handle(Context context) {
   invoker.invoke(
       new HierarchicalRegistry(context.getRequest(), context),
       GroovyContext.from(context),
       Closure.DELEGATE_FIRST);
 }
 public void sendResponse(RequiresHttpAction action) {
   context.getResponse().status(action.getCode());
   sendResponse();
 }
 @Override
 public void setResponseHeader(String name, String value) {
   context.getResponse().getHeaders().set(name, value);
 }
 @Override
 public void setResponseStatus(int code) {
   context.getResponse().status(code);
 }