Exemple #1
0
  @Override
  public void redirect(String url, String alt) {
    // location
    if ("back".equals(url)) {
      url = getHeader("Referrer");
      if (url == null) {
        url = alt;
      }
      if (url == null) {
        url = "/";
      }
    }

    setHeader("Location", url);

    // status
    if (!REDIRECT_STATUSES.contains(getStatus())) {
      setStatus(FOUND);
    }

    // html
    if (ctx.accepts("html") != null) {
      url = HTTPEncode.encodeURIComponent(url);
      setType("text/html; charset=utf-8");
      end("Redirecting to <a href=\"" + url + "\">" + url + "</a>.");
      return;
    }

    // text
    setType("text/plain; charset=utf-8");
    end("Redirecting to " + url + ".");
  }
Exemple #2
0
  protected void triggerHeadersHandlers() {
    if (!headersHandlerTriggered) {
      headersHandlerTriggered = true;
      // if there are handlers call them
      if (headersHandler != null) {
        Handler<Void> handler;
        while ((handler = headersHandler.pollFirst()) != null) {
          handler.handle();
        }
      }

      // convert the cookies
      for (HttpCookie cookie : getCookies()) {
        appendHeader(Headers.SET_COOKIE, ServerCookieEncoder.encode(cookie));
      }

      //      // if there is a filter then putAt the right get
      //      if (filter != null) {
      //        // verify if the filter can filter this content
      //        if (filter.canFilter(getResponse.getHeaders().get("content-type"))) {
      //          getResponse.putHeader("content-encoding", filter.encoding());
      //        } else {
      //          // disable the filter
      //          filter = null;
      //        }
      //      }

      // if there is no content and method is not HEAD delete content-type, content-encoding
      if (!hasBody() && ctx.getRequest().getMethod() != Method.HEAD) {
        removeHeader("Content-Encoding");
        removeHeader("Content-Type");
      }
    }
  }
Exemple #3
0
  @Override
  public void render(@NotNull final String template) {
    int sep = template.lastIndexOf('.');
    if (sep != -1) {
      String extension = template.substring(sep);

      final Engine renderEngine = ENGINE.get(extension);

      if (renderEngine == null) {
        ctx.fail("No engine registered for extension: " + extension);
      } else {
        renderEngine.render(template, ctx);
      }
    } else {
      // when no extension is provided but only 1 engine is available assume that one
      if (DEFAULT_ENGINE != null) {
        final Engine renderEngine = ENGINE.get(DEFAULT_ENGINE);
        renderEngine.render(template, ctx);
      } else {
        ctx.fail("Cannot determine the extension of the template");
      }
    }
  }
Exemple #4
0
 @Override
 public void json(Object bean) {
   if (bean == null) {
     end();
   } else {
     try {
       final String encoded = JSON.encode(bean);
       setType("application/json; charset=utf-8");
       end(encoded);
     } catch (JsonProcessingException | RuntimeException e) {
       ctx.fail(INTERNAL_SERVER_ERROR, e);
     }
   }
 }