All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.muserver.ContextHandler Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
package io.muserver;

import java.net.URI;
import java.util.List;

import static io.muserver.Mutils.urlEncode;

public class ContextHandler implements MuHandler {

    private final String contextPath;
    private final List muHandlers;
    private final String slashContextSlash;
    private final String slashContext;

    public ContextHandler(String contextPath, List muHandlers) {
        this.contextPath = urlEncode(Mutils.trim(contextPath, "/"));
        this.muHandlers = muHandlers;
        this.slashContextSlash = "/" + this.contextPath + "/";
        this.slashContext = "/" + this.contextPath;
    }

    @Override
    public boolean handle(MuRequest request, MuResponse response) throws Exception {
        String rp = request.relativePath();
        if (rp.equals(slashContext)) {
            URI cur = request.uri();
            URI newUri = new URI(cur.getScheme(), cur.getUserInfo(), cur.getHost(), cur.getPort(), cur.getPath() + "/", cur.getQuery(), cur.getFragment());
            response.redirect(newUri);
            return true;
        }
        if (rp.startsWith(slashContextSlash)) {
            ((NettyRequestAdapter) request).addContext(contextPath);
            for (MuHandler muHandler : muHandlers) {
                if (muHandler.handle(request, response)) {
                    return true;
                }
            }
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy