X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Futils%2Frocker%2FRockerServlet.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Futils%2Frocker%2FRockerServlet.java;h=601c3bf7782cdfd2667f0ae25a5d5cb0eb1e70c5;hb=f64a9fc5fcbdf51d0677424579a76b6b30803b1f;hp=0000000000000000000000000000000000000000;hpb=eecca317b8dc18c7fe13fa3a86978621901be460;p=demoscenemusic.git diff --git a/src/main/java/net/pterodactylus/utils/rocker/RockerServlet.java b/src/main/java/net/pterodactylus/utils/rocker/RockerServlet.java new file mode 100644 index 0000000..601c3bf --- /dev/null +++ b/src/main/java/net/pterodactylus/utils/rocker/RockerServlet.java @@ -0,0 +1,131 @@ +/* + * DemosceneMusic - RockerServlet.java - Copyright © 2012 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.utils.rocker; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import net.pterodactylus.util.collection.IterableWrapper; +import net.pterodactylus.util.collection.IteratorWrapper; +import net.pterodactylus.util.collection.filter.Filter; +import net.pterodactylus.util.web.Method; + +/** + * TODO + * + * @author David ‘Bombe’ Roden + */ +public class RockerServlet extends HttpServlet { + + private final Map> rocklets = new HashMap>(); + + /** + * {@inheritDoc} + */ + @Override + @SuppressWarnings("synthetic-access") + public void init() throws ServletException { + for (String name : IterableWrapper.wrap(new InitParameters()).filter(new Filter() { + + @Override + public boolean filterObject(String object) { + return object.startsWith("rocklet."); + } + + })) { + String rockletClassName = getInitParameter(name); + try { + Class rockletClass = Class.forName(rockletClassName); + for (java.lang.reflect.Method rockletMethod : rockletClass.getMethods()) { + Rocklet rockletAnnotation = rockletMethod.getAnnotation(Rocklet.class); + String path = rockletAnnotation.path(); + if (!rocklets.containsKey(path)) { + rocklets.put(path, new HashMap()); + } + for (Method method : rockletAnnotation.methods()) { + if (rocklets.get(path).containsKey(method)) { + throw new RuntimeException("Duplicate method " + method + " for " + path + "!"); + } + rocklets.get(path).put(method, rockletMethod); + } + } + } catch (ClassNotFoundException cnfe1) { + } + } + } + + // + // HTTPSERVLET METHODS + // + + /** + * {@inheritDoc} + */ + @Override + protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { + httpServletRequest.setCharacterEncoding("UTF-8"); + super.service(httpServletRequest, httpServletResponse); + } + + /** + * {@inheritDoc} + */ + @Override + protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { + } + + /** + * {@inheritDoc} + */ + @Override + protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { + } + + // + // PRIVATE METHODS + // + + private String getPath(HttpServletRequest httpServletRequest) { + String path = httpServletRequest.getPathInfo(); + if (path.startsWith("/")) { + return path.substring(1); + } + return path; + } + + private class InitParameters implements Iterable { + + /** + * {@inheritDoc} + */ + @Override + @SuppressWarnings("unchecked") + public Iterator iterator() { + return IteratorWrapper.wrap(getInitParameterNames()); + } + + } + +}