package io.yawp.servlet;

import io.yawp.commons.http.HttpException;
import io.yawp.commons.http.HttpResponse;
import io.yawp.commons.http.JsonResponse;
import io.yawp.commons.http.RequestContext;
import io.yawp.driver.api.DriverFactory;
import io.yawp.repository.Repository;
import io.yawp.repository.Yawp;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Logger;

public class EndpointServlet extends HttpServlet {

    private static final long serialVersionUID = 8155293897299089610L;

    private final static Logger logger = Logger.getLogger(EndpointServlet.class.getName());

    private boolean enableHooks = true;

    private boolean enableCrossDomain = false;

    private CrossDomainManager crossDomainManager = new CrossDomainManager();

    public EndpointServlet() {
    }

    protected EndpointServlet(String packagePrefix) {
        initYawp(packagePrefix);
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        setWithHooks(config.getInitParameter("enableHooks"));

        initYawp(config.getInitParameter("packagePrefix"));

        boolean enableCrossDomain = false;
        if (config.getInitParameter("enableCrossDomain") != null) {
            enableCrossDomain = Boolean.valueOf(config.getInitParameter("enableCrossDomain"));
        } else {
            enableCrossDomain = !DriverFactory.getDriver().environment().isProduction();
        }

        setCrossDomain(this.crossDomainManager.isCrossDomainEnabled(config), config);
    }

    @Override
    public void destroy() {
        super.destroy();
        Yawp.destroyFeatures();
    }

    private void setWithHooks(String enableHooksParameter) {
        if (!enableHooks) {
            return;
        }

        boolean enableHooks = enableHooksParameter == null || Boolean.valueOf(enableHooksParameter);
        setWithHooks(enableHooks);
    }

    private void setCrossDomain(boolean enableCrossDomainParameter,
                                ServletConfig servletConfig) {

        this.enableCrossDomain = enableCrossDomainParameter;

        if (this.enableCrossDomain && this.crossDomainManager.hasAnyValueSet(servletConfig)) {
            this.crossDomainManager.setOrigins(crossDomainManager.getOrigins(servletConfig));
            this.crossDomainManager.setMethods(crossDomainManager.getMethods(servletConfig));
            this.crossDomainManager.setHeaders(crossDomainManager.getHeaders(servletConfig));
        }
    }

    protected void setWithHooks(boolean enableHooks) {
        this.enableHooks = enableHooks;
    }

    /**
     * @deprecated in 2.0, yawp will be configured only by yawp.yml
     */
    @Deprecated
    private void initYawp(String packagePrefix) {
        if (packagePrefix == null) {
            return;
        }
        Yawp.init(packagePrefix);
    }

    protected void response(HttpServletResponse resp, HttpResponse httpResponse) throws IOException {
        if (httpResponse == null) {
            new JsonResponse().execute(resp);
        } else {
            httpResponse.execute(resp);
        }
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.finer("begin");

        HttpResponse httpResponse;
        try {
            httpResponse = execute(new RequestContext(req, resp));
        } catch (HttpException e) {
            httpResponse = e.createResponse();
        }

        this.crossDomainManager.setResponseHeaders(resp);
        response(resp, httpResponse);

        logger.finer("done");
    }

    public HttpResponse execute(RequestContext ctx) {
        try {
            Repository r = getRepository(ctx);
            EndpointRouter router = EndpointRouter.parse(r, ctx);

            if (!router.isValid()) {
                throw new HttpException(400, "Invalid route. Please check uri, json format, object ids and parent structure, etc.");
            }

            return router.executeRestAction(enableHooks);

        } finally {
            Yawp.dispose();
        }
    }

    protected Repository getRepository(RequestContext ctx) {
        return Yawp.yawp().setRequestContext(ctx);
    }

}

@@ -1397,331 +1397,8 @@
 );%0A%0A
-        boolean enableCrossDomain = false;%0A        if (config.getInitParameter(%22enableCrossDomain%22) != null) %7B%0A            enableCrossDomain = Boolean.valueOf(config.getInitParameter(%22enableCrossDomain%22));%0A        %7D else %7B%0A            enableCrossDomain = !DriverFactory.getDriver().environment().isProduction();%0A        %7D%0A%0A
     

Remove redundant cross domain check