Java Code Examples for play.mvc.Http.Request#current()

The following examples show how to use play.mvc.Http.Request#current() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlayGrizzlyAdapter.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public void serve404(GrizzlyRequest request, GrizzlyResponse response, NotFound e) {
    Logger.warn("404 -> %s %s (%s)", request.getMethod(), request.getRequestURI(), e.getMessage());
    response.setStatus(404);
    response.setContentType("text/html");
    Map<String, Object> binding = new HashMap<String, Object>();
    binding.put("result", e);
    binding.put("session", Scope.Session.current());
    binding.put("request", Http.Request.current());
    binding.put("flash", Scope.Flash.current());
    binding.put("params", Scope.Params.current());
    binding.put("play", new Play());
    try {
        binding.put("errors", Validation.errors());
    } catch (Exception ex) {
        //
    }
    String format = Request.current().format;
    response.setStatus(404);
    // Do we have an ajax request? If we have then we want to display some text even if it is html that is requested
    if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With")) && (format == null || format.equals("html"))) {
        format = "txt";
    }
    if (format == null) {
        format = "txt";
    }
    response.setContentType(MimeTypes.getContentType("404." + format, "text/plain"));
    String errorHtml = TemplateLoader.load("errors/404." + format).render(binding);
    try {
        response.getOutputStream().write(errorHtml.getBytes("utf-8"));
    } catch (Exception fex) {
        Logger.error(fex, "(utf-8 ?)");
    }
}
 
Example 2
Source File: Controller.java    From restcommander with Apache License 2.0 5 votes vote down vote up
/**
 * Work out the default template to load for the invoked action.
 * E.g. "controllers.Pages.index" returns "views/Pages/index.html".
 */
protected static String template() {
    final Request theRequest = Request.current();
    final String format = theRequest.format;
    String templateName = theRequest.action.replace(".", "/") + "." + (format == null ? "html" : format);
    if (templateName.startsWith("@")) {
        templateName = templateName.substring(1);
        if (!templateName.contains(".")) {
            templateName = theRequest.controller + "." + templateName;
        }
        templateName = templateName.replace(".", "/") + "." + (format == null ? "html" : format);
    }
    return templateName;
}
 
Example 3
Source File: Controller.java    From restcommander with Apache License 2.0 5 votes vote down vote up
/**
 * Work out the default template to load for the action.
 * E.g. "controllers.Pages.index" returns "views/Pages/index.html".
 */
protected static String template(String templateName) {
    final Request theRequest = Request.current();
    final String format = theRequest.format;
    if (templateName.startsWith("@")) {
        templateName = templateName.substring(1);
        if (!templateName.contains(".")) {
            templateName = theRequest.controller + "." + templateName;
        }
        templateName = templateName.replace(".", "/") + "." + (format == null ? "html" : format);
    }
    return templateName;
}
 
Example 4
Source File: Controller.java    From restcommander with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected static <T> T await(Future<T> future) {

    if(future != null) {
        Request.current().args.put(ActionInvoker.F, future);
    } else if(Request.current().args.containsKey(ActionInvoker.F)) {
        // Since the continuation will restart in this code that isn't intstrumented by javaflow,
        // we need to reset the state manually.
        StackRecorder.get().isCapturing = false;
        StackRecorder.get().isRestoring = false;
        StackRecorder.get().value = null;
        future = (Future<T>)Request.current().args.get(ActionInvoker.F);

        // Now reset the Controller invocation context
        ControllerInstrumentation.stopActionCall();
        storeOrRestoreDataStateForContinuations( true );
    } else {
        throw new UnexpectedException("Lost promise for " + Http.Request.current() + "!");
    }
    
    if(future.isDone()) {
        try {
            return future.get();
        } catch(Exception e) {
            throw new UnexpectedException(e);
        }
    } else {
        Request.current().isNew = false;
        verifyContinuationsEnhancement();
        storeOrRestoreDataStateForContinuations( false );
        Continuation.suspend(future);
        return null;
    }
}
 
Example 5
Source File: Controller.java    From restcommander with Apache License 2.0 5 votes vote down vote up
protected static <T> void await(Future<T> future, F.Action<T> callback) {
    Request.current().isNew = false;
    Request.current().args.put(ActionInvoker.F, future);
    Request.current().args.put(ActionInvoker.A, callback);
    Request.current().args.put(ActionInvoker.CONTINUATIONS_STORE_RENDER_ARGS, Scope.RenderArgs.current());
    throw new Suspend(future);
}
 
Example 6
Source File: ServletWrapper.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public void serve404(HttpServletRequest servletRequest, HttpServletResponse servletResponse, NotFound e) {
    Logger.warn("404 -> %s %s (%s)", servletRequest.getMethod(), servletRequest.getRequestURI(), e.getMessage());
    servletResponse.setStatus(404);
    servletResponse.setContentType("text/html");
    Map<String, Object> binding = new HashMap<String, Object>();
    binding.put("result", e);
    binding.put("session", Scope.Session.current());
    binding.put("request", Http.Request.current());
    binding.put("flash", Scope.Flash.current());
    binding.put("params", Scope.Params.current());
    binding.put("play", new Play());
    try {
        binding.put("errors", Validation.errors());
    } catch (Exception ex) {
        //
    }
    String format = Request.current().format;
    servletResponse.setStatus(404);
    // Do we have an ajax request? If we have then we want to display some text even if it is html that is requested
    if ("XMLHttpRequest".equals(servletRequest.getHeader("X-Requested-With")) && (format == null || format.equals("html"))) {
        format = "txt";
    }
    if (format == null) {
        format = "txt";
    }
    servletResponse.setContentType(MimeTypes.getContentType("404." + format, "text/plain"));
    String errorHtml = TemplateLoader.load("errors/404." + format).render(binding);
    try {
        servletResponse.getOutputStream().write(errorHtml.getBytes(Response.current().encoding));
    } catch (Exception fex) {
        Logger.error(fex, "(encoding ?)");
    }
}
 
Example 7
Source File: PlayHandler.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public static void serve404(NotFound e, ChannelHandlerContext ctx, Request request, HttpRequest nettyRequest) {
    if (Logger.isTraceEnabled()) {
        Logger.trace("serve404: begin");
    }
    HttpResponse nettyResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
    nettyResponse.setHeader(SERVER, signature);

    nettyResponse.setHeader(CONTENT_TYPE, "text/html");
    Map<String, Object> binding = getBindingForErrors(e, false);

    String format = Request.current().format;
    if (format == null) {
        format = "txt";
    }
    nettyResponse.setHeader(CONTENT_TYPE, (MimeTypes.getContentType("404." + format, "text/plain")));


    String errorHtml = TemplateLoader.load("errors/404." + format).render(binding);
    try {
        byte[] bytes = errorHtml.getBytes(Response.current().encoding);
        ChannelBuffer buf = ChannelBuffers.copiedBuffer(bytes);
        setContentLength(nettyResponse, bytes.length);
        nettyResponse.setContent(buf);
        ChannelFuture writeFuture = ctx.getChannel().write(nettyResponse);
        writeFuture.addListener(ChannelFutureListener.CLOSE);
    } catch (UnsupportedEncodingException fex) {
        Logger.error(fex, "(encoding ?)");
    }
    if (Logger.isTraceEnabled()) {
        Logger.trace("serve404: end");
    }
}
 
Example 8
Source File: Controller.java    From restcommander with Apache License 2.0 4 votes vote down vote up
protected static void await(int millis) {
    Request.current().isNew = false;
    verifyContinuationsEnhancement();
    storeOrRestoreDataStateForContinuations(null);
    Continuation.suspend(millis);
}
 
Example 9
Source File: Controller.java    From restcommander with Apache License 2.0 4 votes vote down vote up
protected static void await(int millis, F.Action0 callback) {
    Request.current().isNew = false;
    Request.current().args.put(ActionInvoker.A, callback);
    Request.current().args.put(ActionInvoker.CONTINUATIONS_STORE_RENDER_ARGS, Scope.RenderArgs.current());
    throw new Suspend(millis);
}
 
Example 10
Source File: OpenID.java    From restcommander with Apache License 2.0 4 votes vote down vote up
private OpenID(String id) {
    this.id = id;
    this.returnAction = this.realmAction = Request.current().action;
}
 
Example 11
Source File: Controller.java    From restcommander with Apache License 2.0 2 votes vote down vote up
/**
 * Suspend the current request for a specified amount of time (in milliseconds).
 *
 * <p><b>Important:</b> The method will not resume on the line after you call this. The method will
 * be called again as if there was a new HTTP request.
 *
 * @param millis Number of milliseconds to wait until trying again.
 */
@Deprecated
protected static void suspend(int millis) {
    Request.current().isNew = false;
    throw new Suspend(millis);
}
 
Example 12
Source File: Controller.java    From restcommander with Apache License 2.0 2 votes vote down vote up
/**
 * Suspend this request and wait for the task completion
 *
 * <p><b>Important:</b> The method will not resume on the line after you call this. The method will
 * be called again as if there was a new HTTP request.
 *
 * @param tasks
 */
@Deprecated
protected static void waitFor(Future<?> task) {
    Request.current().isNew = false;
    throw new Suspend(task);
}