2 * DemosceneMusic - RockerServlet.java - Copyright © 2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.utils.rocker;
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.Iterator;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import net.pterodactylus.util.collection.IterableWrapper;
31 import net.pterodactylus.util.collection.IteratorWrapper;
32 import net.pterodactylus.util.collection.filter.Filter;
33 import net.pterodactylus.util.web.Method;
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class RockerServlet extends HttpServlet {
42 private final Map<String, Map<Method, java.lang.reflect.Method>> rocklets = new HashMap<String, Map<Method, java.lang.reflect.Method>>();
48 @SuppressWarnings("synthetic-access")
49 public void init() throws ServletException {
50 for (String name : IterableWrapper.wrap(new InitParameters()).filter(new Filter<String>() {
53 public boolean filterObject(String object) {
54 return object.startsWith("rocklet.");
58 String rockletClassName = getInitParameter(name);
60 Class<?> rockletClass = Class.forName(rockletClassName);
61 for (java.lang.reflect.Method rockletMethod : rockletClass.getMethods()) {
62 Rocklet rockletAnnotation = rockletMethod.getAnnotation(Rocklet.class);
63 String path = rockletAnnotation.path();
64 if (!rocklets.containsKey(path)) {
65 rocklets.put(path, new HashMap<Method, java.lang.reflect.Method>());
67 for (Method method : rockletAnnotation.methods()) {
68 if (rocklets.get(path).containsKey(method)) {
69 throw new RuntimeException("Duplicate method " + method + " for " + path + "!");
71 rocklets.get(path).put(method, rockletMethod);
74 } catch (ClassNotFoundException cnfe1) {
80 // HTTPSERVLET METHODS
87 protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
88 httpServletRequest.setCharacterEncoding("UTF-8");
89 super.service(httpServletRequest, httpServletResponse);
96 protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
103 protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
110 private String getPath(HttpServletRequest httpServletRequest) {
111 String path = httpServletRequest.getPathInfo();
112 if (path.startsWith("/")) {
113 return path.substring(1);
118 private class InitParameters implements Iterable<String> {
124 @SuppressWarnings("unchecked")
125 public Iterator<String> iterator() {
126 return IteratorWrapper.wrap(getInitParameterNames());