示例#1
0
 public void sendResponse() {
   int statusCode = context.getResponse().getStatus().getCode();
   if (statusCode >= 400) {
     context.clientError(statusCode);
   } else {
     context.getResponse().send(MediaType.TEXT_HTML, responseContent);
   }
 }
示例#2
0
  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());
    }
  }
示例#3
0
 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;
 }
示例#4
0
  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());
           });
 }
示例#6
0
 public void handle(Context context) {
   context.redirect(code, location);
 }
示例#7
0
 private URI getAddress() {
   return context.get(PublicAddress.class).get();
 }
示例#8
0
 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);
 }
示例#10
0
 public void sendResponse(RequiresHttpAction action) {
   context.getResponse().status(action.getCode());
   sendResponse();
 }
示例#11
0
 @Override
 public void setResponseHeader(String name, String value) {
   context.getResponse().getHeaders().set(name, value);
 }
示例#12
0
 @Override
 public void setResponseStatus(int code) {
   context.getResponse().status(code);
 }