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

io.yawp.servlet.EndpointServlet Maven / Gradle / Ivy

There is a newer version: 2.08alpha
Show newest version
package io.yawp.servlet;

import io.yawp.commons.http.HttpException;
import io.yawp.commons.http.HttpResponse;
import io.yawp.commons.http.HttpVerb;
import io.yawp.commons.http.JsonResponse;
import io.yawp.commons.http.StatusObject;
import io.yawp.commons.utils.JsonUtils;
import io.yawp.driver.api.DriverFactory;
import io.yawp.repository.EndpointScanner;
import io.yawp.repository.Repository;
import io.yawp.repository.RepositoryFeatures;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EndpointServlet extends HttpServlet {

	private static final long serialVersionUID = 8155293897299089610L;

	private RepositoryFeatures features;

	private boolean enableHooks = true;
	private boolean enableCrossDomain = false;

	public EndpointServlet() {
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		setWithHooks(config.getInitParameter("enableHooks"));
		setCrossDomain(config.getInitParameter("enableCrossDomain"));
		scanEndpoints(config.getInitParameter("packagePrefix"));
	}

	private void setWithHooks(String enableHooksParameter) {
		boolean enableHooks = enableHooksParameter == null || Boolean.valueOf(enableHooksParameter);
		setWithHooks(enableHooks);
	}

	private void setCrossDomain(String enableCrossDomainParameter) {
		if (enableCrossDomainParameter != null) {
			this.enableCrossDomain = Boolean.valueOf(enableCrossDomainParameter);
		} else {
			this.enableCrossDomain = !DriverFactory.getDriver().environment().isProduction();
		}
	}

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

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

	private void scanEndpoints(String packagePrefix) {
		features = new EndpointScanner(packagePrefix).enableHooks(enableHooks).scan();
	}

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

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpResponse httpResponse;
		try {
			httpResponse = execute(req.getMethod(), getUri(req), JsonUtils.readJson(req.getReader()), makeParams(req));
		} catch (HttpException e) {
			httpResponse = e.createErrorResponse();
		}

		if (enableCrossDomain) {
			resp.setHeader("Access-Control-Allow-Origin", "*");
			resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
			resp.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, DELETE");
		}
		response(resp, httpResponse);
	}

	@SuppressWarnings("unchecked")
	protected Map makeParams(HttpServletRequest req) {
		Map map = new HashMap();

		Enumeration e = req.getParameterNames();
		while (e.hasMoreElements()) {
			String name = e.nextElement();
			map.put(name, req.getParameter(name));
		}

		return map;
	}

	private String getUri(HttpServletRequest req) {
		return req.getRequestURI().substring(req.getServletPath().length());
	}

	public HttpResponse execute(String method, String uri, String requestJson, Map params) {
		Repository r = getRepository(params);

		EndpointRouter router = EndpointRouter.parse(r, HttpVerb.fromString(method), uri, requestJson, params);

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

		return router.executeRestAction(enableHooks);
	}

	protected Repository getRepository(Map params) {
		return Repository.r().setFeatures(features);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy