Current state of Rocker.
[demoscenemusic.git] / src / main / java / net / pterodactylus / utils / rocker / RockerServlet.java
1 /*
2  * DemosceneMusic - RockerServlet.java - Copyright © 2012 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.utils.rocker;
19
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
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;
34
35 /**
36  * TODO
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class RockerServlet extends HttpServlet {
41
42         private final Map<String, Map<Method, java.lang.reflect.Method>> rocklets = new HashMap<String, Map<Method, java.lang.reflect.Method>>();
43
44         /**
45          * {@inheritDoc}
46          */
47         @Override
48         @SuppressWarnings("synthetic-access")
49         public void init() throws ServletException {
50                 for (String name : IterableWrapper.wrap(new InitParameters()).filter(new Filter<String>() {
51
52                         @Override
53                         public boolean filterObject(String object) {
54                                 return object.startsWith("rocklet.");
55                         }
56
57                 })) {
58                         String rockletClassName = getInitParameter(name);
59                         try {
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>());
66                                         }
67                                         for (Method method : rockletAnnotation.methods()) {
68                                                 if (rocklets.get(path).containsKey(method)) {
69                                                         throw new RuntimeException("Duplicate method " + method + " for " + path + "!");
70                                                 }
71                                                 rocklets.get(path).put(method, rockletMethod);
72                                         }
73                                 }
74                         } catch (ClassNotFoundException cnfe1) {
75                         }
76                 }
77         }
78
79         //
80         // HTTPSERVLET METHODS
81         //
82
83         /**
84          * {@inheritDoc}
85          */
86         @Override
87         protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
88                 httpServletRequest.setCharacterEncoding("UTF-8");
89                 super.service(httpServletRequest, httpServletResponse);
90         }
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
96         protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
97         }
98
99         /**
100          * {@inheritDoc}
101          */
102         @Override
103         protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
104         }
105
106         //
107         // PRIVATE METHODS
108         //
109
110         private String getPath(HttpServletRequest httpServletRequest) {
111                 String path = httpServletRequest.getPathInfo();
112                 if (path.startsWith("/")) {
113                         return path.substring(1);
114                 }
115                 return path;
116         }
117
118         private class InitParameters implements Iterable<String> {
119
120                 /**
121                  * {@inheritDoc}
122                  */
123                 @Override
124                 @SuppressWarnings("unchecked")
125                 public Iterator<String> iterator() {
126                         return IteratorWrapper.wrap(getInitParameterNames());
127                 }
128
129         }
130
131 }