Search in sources :

Example 6 with ActResponse

use of act.ActResponse in project actframework by actframework.

the class FileGetter method handle.

@Override
public void handle(ActionContext context) {
    if (null != delegate) {
        delegate.handle(context);
        return;
    }
    context.handler(this);
    File file = base;
    H.Format fmt;
    if (base.isDirectory()) {
        String path = context.paramVal(ParamNames.PATH);
        if (S.blank(path)) {
            AlwaysForbidden.INSTANCE.handle(context);
            return;
        }
        file = new File(base, path);
        if (!file.exists()) {
            AlwaysNotFound.INSTANCE.handle(context);
            return;
        }
        if (file.isDirectory() || !file.canRead()) {
            AlwaysForbidden.INSTANCE.handle(context);
            return;
        }
    }
    ActResponse resp = context.prepareRespForWrite();
    fmt = contentType(file.getPath());
    resp.contentType(fmt);
    context.applyCorsSpec().applyContentSecurityPolicy().applyContentType();
    InputStream is = new BufferedInputStream(IO.is(file));
    IO.copy(is, resp.outputStream());
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) H(org.osgl.http.H) ActResponse(act.ActResponse) File(java.io.File)

Aggregations

ActResponse (act.ActResponse)6 H (org.osgl.http.H)3 ActionContext (act.app.ActionContext)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ByteBuffer (java.nio.ByteBuffer)1 org.osgl.$ (org.osgl.$)1 Format (org.osgl.http.H.Format)1