@@ -0,0 +1,3032 @@
+package com.itranswarp.jxrest;%0A%0Aimport java.io.IOException;%0Aimport java.util.stream.Stream;%0A%0Aimport javax.servlet.GenericServlet;%0Aimport javax.servlet.ServletException;%0Aimport javax.servlet.ServletRequest;%0Aimport javax.servlet.ServletResponse;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A%0A/**%0A * A REST API filter to handle REST API calls.%0A * %0A * @author Michael Liao%0A */%0Apublic class RestApiServlet extends GenericServlet %7B%0A%0A    final Log log = LogFactory.getLog(getClass());%0A    String urlPrefix = %22%22;%0A    RestApiHandler handler = null;%0A%0A    /**%0A     * Set RestApiHandler to handle REST API.%0A     * %0A     * @param handler Instance of RestApiHandler.%0A     */%0A    public void setRestApiHandler(RestApiHandler handler) %7B%0A        this.handler = handler;%0A    %7D%0A%0A    /**%0A     * Set URL prefix. e.g. %22/api/v1%22.%0A     * %0A     * @param prefix Prefix string, or null if no prefix.%0A     */%0A    public void setUrlPrefix(String prefix) %7B%0A        prefix = (prefix == null) ? %22%22 : prefix.trim();%0A        if (prefix.endsWith(%22/%22)) %7B%0A            prefix = prefix.substring(0, prefix.length() - 1);%0A        %7D%0A        if (!prefix.isEmpty() && !prefix.startsWith(%22/%22)) %7B%0A            log.error(%22Invalid urlPrefix: must start with /, but actual is: %22 + prefix);%0A            throw new IllegalArgumentException(%22Invalid urlPrefix parameter: %22 + prefix);%0A        %7D%0A        log.info(%22Set urlPrefix of RestApiFilter to: %22 + prefix);%0A        this.urlPrefix = prefix;%0A    %7D%0A%0A    public void init() throws ServletException %7B%0A        log.info(%22Init RestApiServlet...%22);%0A        if (this.handler != null) %7B%0A            this.handler = new RestApiHandler();%0A        %7D%0A        setUrlPrefix(getInitParameter(%22urlPrefix%22));%0A        String handlers = getInitParameter(%22handlers%22);%0A        if (handlers != null) %7B%0A            Stream.of(handlers.split(%22%5C%5C,%22)).map((s) -%3E %7B%0A                return s.trim();%0A            %7D).filter((s) -%3E %7B%0A                return !s.isEmpty();%0A            %7D).forEach((s) -%3E %7B%0A                this.handler.findHandlers(s);%0A            %7D);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException %7B%0A        HttpServletRequest req = (HttpServletRequest) request;%0A        HttpServletResponse resp = (HttpServletResponse) response;%0A        String path = req.getRequestURI();%0A        if (path.startsWith(this.urlPrefix)) %7B%0A            String apiUrl = path.substring(this.urlPrefix.length());%0A            if (apiUrl.startsWith(%22/%22)) %7B%0A                log.info(%22Process API request: %22 + apiUrl);%0A                this.handler.processApi(req, resp, req.getMethod(), apiUrl);%0A                return;%0A            %7D%0A        %7D%0A        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, %22Method Not Allowed%22);%0A    %7D%0A%0A    public void destroy() %7B%0A        log.info(%22Destroy RestApiServlet...%22);%0A        this.handler = null;%0A    %7D%0A%0A%7D%0A

add RestApiServlet