package org.osgl.mvc.result;

import org.osgl.http.H;
import org.osgl.http.Http;
import org.osgl.http.util.Path;
import org.osgl.util.E;
import org.osgl.util.IO;
import org.osgl.util.S;

public class Redirect extends Result {
    String url;
    public Redirect(String url) {
        super(Http.Status.FOUND);
        E.illegalArgumentIf(S.blank(url));
        this.url = url;
    }

    public Redirect(String url, Object... args) {
        this(S.fmt(url, args));
    }

    public Redirect(boolean permanent, String url) {
        super(permanent ? Http.Status.MOVED_PERMANENTLY : H.Status.FOUND);
        this.url = url;
    }

    public Redirect(boolean permanent, String url, Object... args) {
        this(permanent, S.fmt(url, args));
    }

    @Override
    public void apply(H.Request req, H.Response resp) {
        String url = Path.fullUrl(this.url, req);
        if (req.isAjax()) {
            resp.status(H.Status.FOUND_AJAX);
        } else {
            applyStatus(resp);
        }
        resp.header("Location", url);
        applyBeforeCommitHandler(req, resp);
        IO.close(resp.outputStream());
        applyAfterCommitHandler(req, resp);
    }
}

@@ -219,20 +219,31 @@
 esult %7B%0A
+%0A
     
+protected 
 String u
@@ -242,24 +242,25 @@
 String url;%0A
+%0A
     public R
@@ -851,21 +851,16 @@
 l = 
-Path.
 fullUrl(
 this
@@ -859,18 +859,8 @@
 Url(
-this.url, 
 req)
@@ -1164,11 +1164,116 @@
 );%0A    %7D
+%0A%0A    protected String fullUrl(H.Request request) %7B%0A        return Path.fullUrl(this.url, request);%0A    %7D
 %0A%7D%0A

allow it able to extend Redirect