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

org.s1.web.services.ServiceDispatcherServlet Maven / Gradle / Ivy

There is a newer version: 0.1.3
Show newest version
package org.s1.web.services;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Service dispatcher servlet.
 * 
* Add it to web.xml: *

 * <servlet>
 *  <servlet-name>services</servlet-name>
 *  <servlet-class>org.s1.web.services.ServiceDispatcherServlet</servlet-class>
 *  <load-on-startup>1</load-on-startup>
 * </servlet>
 * <servlet-mapping>
 *  <servlet-name>services</servlet-name>
 *  <url-pattern>/services/*</url-pattern>
 * </servlet-mapping>
 * 
* Add your WebOperations from {@link org.s1.web.ApplicationFilter} ServiceDispatcherServlet.getOperations().add("MyOperation", myWebOperationImpl) * * @author Grigory Pykhov */ @MultipartConfig public class ServiceDispatcherServlet extends HttpServlet { private static final Operations operations = new Operations(); /** * @return Operations */ public static Operations getOperations() { return operations; } /** * Operations holder */ public static class Operations { private Operations() { } private final Map list = new ConcurrentHashMap(); /** * Add operation * * @param name Operation name (operation will be accessible with /services/OperationName) * @param operation WebOperation instance * @return Operations */ public Operations add(String name, WebOperation operation) { list.put(name, operation); return this; } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } @Override protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } @Override protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } @Override protected void doHead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } @Override protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } @Override protected void doTrace(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } /** * Get operation name from request * * @param request Request * @return Web operation name */ protected String getWebOperationNameFromRequest(HttpServletRequest request) { String uri = request.getRequestURI(); String q = uri.substring((request.getContextPath() + request.getServletPath()).length() + 1); return q; } /** * Get operation from holder * * @param name Name * @return Operation */ protected WebOperation getOperationByName(String name) { return operations.list.get(name); } /** * Process request * * @param request Request * @param response Response * @throws ServletException ServletException * @throws IOException IOException */ protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); WebOperation op = getOperationByName(getWebOperationNameFromRequest(request)); if (op == null) { response.setStatus(404); return; } op.request(request, response); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy