From: David ‘Bombe’ Roden Date: Thu, 2 Aug 2012 15:13:12 +0000 (+0200) Subject: Current state of Rocker. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=refs%2Fheads%2Frocker;p=demoscenemusic.git Current state of Rocker. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/rocker/ArtistRocker.java b/src/main/java/net/pterodactylus/demoscenemusic/rocker/ArtistRocker.java new file mode 100644 index 0000000..739bc0f --- /dev/null +++ b/src/main/java/net/pterodactylus/demoscenemusic/rocker/ArtistRocker.java @@ -0,0 +1,41 @@ +/* + * DemosceneMusic - ArtistRocker.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.demoscenemusic.rocker; + +import net.pterodactylus.util.web.Method; +import net.pterodactylus.utils.rocker.Parameter; +import net.pterodactylus.utils.rocker.Rocklet; + +/** + * TODO + * + * @author David ‘Bombe’ Roden + */ +public class ArtistRocker { + + @Rocklet(path = "addArtist", methods = Method.POST) + public void addArtist(@Parameter("name") String name) { + + } + + @Rocklet(path = "editArtist", methods = Method.POST) + public void editArtist(@Parameter("artist") String id, @Parameter("name") String name) { + + } + +} diff --git a/src/main/java/net/pterodactylus/utils/rocker/Parameter.java b/src/main/java/net/pterodactylus/utils/rocker/Parameter.java new file mode 100644 index 0000000..0682fc7 --- /dev/null +++ b/src/main/java/net/pterodactylus/utils/rocker/Parameter.java @@ -0,0 +1,36 @@ +/* + * DemosceneMusic - Parameter.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.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * TODO + * + * @author David ‘Bombe’ Roden + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.PARAMETER) +public @interface Parameter { + + String value(); + +} 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()); + } + + } + +} diff --git a/src/main/java/net/pterodactylus/utils/rocker/Rocklet.java b/src/main/java/net/pterodactylus/utils/rocker/Rocklet.java new file mode 100644 index 0000000..b17f815 --- /dev/null +++ b/src/main/java/net/pterodactylus/utils/rocker/Rocklet.java @@ -0,0 +1,40 @@ +/* + * DemosceneMusic - Rocklet.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.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import net.pterodactylus.util.web.Method; + +/** + * TODO + * + * @author David ‘Bombe’ Roden + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface Rocklet { + + String path(); + + Method[] methods(); + +}