Permalink
Browse files

Initial commit

  • Loading branch information...
1 parent ac87b96 commit 65a18d8c4e77ed6690a95f714b9897491f05b15c @lgadi committed May 22, 2012
Showing with 865 additions and 0 deletions.
  1. BIN .DS_Store
  2. BIN SimpleTriLog/.DS_Store
  3. +7 −0 SimpleTriLog/.classpath
  4. +39 −0 SimpleTriLog/.project
  5. +4 −0 SimpleTriLog/.settings/com.google.appengine.eclipse.core.prefs
  6. +3 −0 SimpleTriLog/.settings/com.google.gdt.eclipse.core.prefs
  7. +7 −0 SimpleTriLog/.settings/org.eclipse.jdt.core.prefs
  8. +4 −0 SimpleTriLog/.settings/org.eclipse.wst.common.project.facet.core.xml
  9. +15 −0 SimpleTriLog/src/META-INF/jdoconfig.xml
  10. +44 −0 SimpleTriLog/src/com/gl/routing/AbstractServlet.java
  11. +17 −0 SimpleTriLog/src/com/gl/routing/IHandler.java
  12. +30 −0 SimpleTriLog/src/com/gl/routing/JspServlet.java
  13. +174 −0 SimpleTriLog/src/com/gl/routing/RoutingServlet.java
  14. +255 −0 SimpleTriLog/src/com/gl/routing/WriteableParametersHttpServletRequestWrapper.java
  15. +45 −0 SimpleTriLog/src/com/gl/simpletrilog/ListAllActivities.java
  16. +43 −0 SimpleTriLog/src/com/gl/simpletrilog/LogSingleActivity.java
  17. +32 −0 SimpleTriLog/src/com/gl/simpletrilog/SimpleTriLogServlet.java
  18. +24 −0 SimpleTriLog/src/log4j.properties
  19. BIN SimpleTriLog/war/.DS_Store
  20. BIN SimpleTriLog/war/WEB-INF/.DS_Store
  21. +31 −0 SimpleTriLog/war/WEB-INF/appengine-web.xml
  22. BIN SimpleTriLog/war/WEB-INF/lib/appengine-api-1.0-sdk-1.6.5.jar
  23. BIN SimpleTriLog/war/WEB-INF/lib/appengine-api-labs-1.6.5.jar
  24. BIN SimpleTriLog/war/WEB-INF/lib/appengine-jsr107cache-1.6.5.jar
  25. BIN SimpleTriLog/war/WEB-INF/lib/datanucleus-appengine-1.0.10.final.jar
  26. BIN SimpleTriLog/war/WEB-INF/lib/datanucleus-core-1.1.5.jar
  27. BIN SimpleTriLog/war/WEB-INF/lib/datanucleus-jpa-1.1.5.jar
  28. BIN SimpleTriLog/war/WEB-INF/lib/geronimo-jpa_3.0_spec-1.1.1.jar
  29. BIN SimpleTriLog/war/WEB-INF/lib/geronimo-jta_1.1_spec-1.1.1.jar
  30. BIN SimpleTriLog/war/WEB-INF/lib/jdo2-api-2.3-eb.jar
  31. BIN SimpleTriLog/war/WEB-INF/lib/jsr107cache-1.1.jar
  32. +13 −0 SimpleTriLog/war/WEB-INF/logging.properties
  33. +19 −0 SimpleTriLog/war/WEB-INF/web.xml
  34. BIN SimpleTriLog/war/favicon.ico
  35. +27 −0 SimpleTriLog/war/index.html
  36. +32 −0 SimpleTriLog/war/simpletrilog.jsp
View
BIN .DS_Store
Binary file not shown.
View
Binary file not shown.
View
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="com.google.appengine.eclipse.core.GAE_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="output" path="war/WEB-INF/classes"/>
+</classpath>
View
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>SimpleTriLog</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.wst.common.project.facet.core.builder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.google.gdt.eclipse.core.webAppProjectValidator</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.google.appengine.eclipse.core.projectValidator</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.google.appengine.eclipse.core.enhancerbuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ <nature>com.google.appengine.eclipse.core.gaeNature</nature>
+ <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
+ </natures>
+</projectDescription>
@@ -0,0 +1,4 @@
+eclipse.preferences.version=1
+filesCopiedToWebInfLib=appengine-api-1.0-sdk-1.6.5.jar|appengine-api-labs-1.6.5.jar|appengine-jsr107cache-1.6.5.jar|jsr107cache-1.1.jar|datanucleus-appengine-1.0.10.final.jar|datanucleus-core-1.1.5.jar|datanucleus-jpa-1.1.5.jar|geronimo-jpa_3.0_spec-1.1.1.jar|geronimo-jta_1.1_spec-1.1.1.jar|jdo2-api-2.3-eb.jar
+gaeHrdEnabled=true
+gaeIsEclipseDefaultInstPath=true
@@ -0,0 +1,3 @@
+eclipse.preferences.version=1
+warSrcDir=war
+warSrcDirIsOutput=true
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<faceted-project>
+ <installed facet="java" version="1.6"/>
+</faceted-project>
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
+
+ <persistence-manager-factory name="transactions-optional">
+ <property name="javax.jdo.PersistenceManagerFactoryClass"
+ value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"/>
+ <property name="javax.jdo.option.ConnectionURL" value="appengine"/>
+ <property name="javax.jdo.option.NontransactionalRead" value="true"/>
+ <property name="javax.jdo.option.NontransactionalWrite" value="true"/>
+ <property name="javax.jdo.option.RetainValues" value="true"/>
+ <property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
+ </persistence-manager-factory>
+</jdoconfig>
@@ -0,0 +1,44 @@
+package com.gl.routing;
+
+
+import java.io.IOException;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public abstract class AbstractServlet implements IHandler {
+
+
+
+ private ServletContext servletContext;
+
+ @Override
+ public void setServletContext(ServletContext servletContext) {
+ this.servletContext = servletContext;
+ }
+
+ @Override
+ public ServletContext getServletContext() {
+ return servletContext;
+ }
+
+ @Override
+ public void handle(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+
+}
@@ -0,0 +1,17 @@
+package com.gl.routing;
+
+
+import java.io.IOException;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public interface IHandler {
+ String getUrlPattern();
+ void setServletContext(ServletContext servletContext);
+ ServletContext getServletContext();
+ void handle(HttpServletRequest req, HttpServletResponse resp) throws IOException;
+ void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException;
+ void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException;
+}
@@ -0,0 +1,30 @@
+package com.gl.routing;
+
+
+import java.io.IOException;
+import java.util.logging.Logger;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public abstract class JspServlet extends AbstractServlet {
+
+
+ private static final long serialVersionUID = 1L;
+ private String jspName;
+
+ public JspServlet(String jspName) {
+ this.jspName = jspName;
+ }
+
+ @Override
+ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ try {
+ getServletContext().getRequestDispatcher(jspName).forward(req, resp);
+ } catch (ServletException e) {
+ Logger.getLogger("JspServlet for "+jspName).severe(e.getMessage());
+ }
+ }
+
+}
@@ -0,0 +1,174 @@
+package com.gl.routing;
+
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.logging.Logger;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import com.gl.simpletrilog.ListAllActivities;
+import com.gl.simpletrilog.LogSingleActivity;
+import com.gl.simpletrilog.SimpleTriLogServlet;
+
+@SuppressWarnings("serial")
+public class RoutingServlet extends HttpServlet {
+
+ private static Method GET_METHOD = null;
+ private static Method POST_METHOD = null;
+ private static final String TOKEN_PLACEHOLDER = "TOKEN_PLACEHOLDER";
+ private static final String REST_PLACEHOLDER = "REST_PLACEHOLDER";
+ static {
+ try {
+ GET_METHOD = IHandler.class.getDeclaredMethod("doGet", HttpServletRequest.class, HttpServletResponse.class);
+ POST_METHOD = IHandler.class.getDeclaredMethod("doPost", HttpServletRequest.class, HttpServletResponse.class);
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ private static final Logger log = Logger.getLogger(RoutingServlet.class.getName());
+ private static final String BASE_PATH = "/polls";
+ private Map<String, IHandler> handlers = new HashMap<String, IHandler>();
+ private Map<String[], IHandler> complexHandlers = new HashMap<String[], IHandler>();
+
+ @Override
+ public void init() throws ServletException {
+ super.init();
+ log.warning("RoutingServlet init");
+ initHandlers();
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ log.warning("doPost(" + req.getRequestURI() + ")");
+ processUri(req.getRequestURI(), req, resp, POST_METHOD);
+ }
+
+ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ log.warning("doGet(" + req.getRequestURI() + ")");
+
+ processUri(req.getRequestURI(), req, resp, GET_METHOD);
+ }
+
+ private String[] urlToTokens(String url, boolean replacePlaceHolders) {
+ StringTokenizer strtok = new StringTokenizer(url, "/");
+
+ List<String> tokensList = new ArrayList<String>();
+ while (strtok.hasMoreTokens()) {
+ String token = strtok.nextToken();
+ if (replacePlaceHolders && token.startsWith("{")) {
+ token = TOKEN_PLACEHOLDER;
+ }
+ if (replacePlaceHolders && !strtok.hasMoreTokens() && token.equals("*")) {
+ token = REST_PLACEHOLDER;
+ }
+ tokensList.add(token);
+ }
+ return tokensList.toArray(new String[tokensList.size()]);
+ }
+
+ private String[] urlToTokens(String url) {
+ return urlToTokens(url, true);
+ }
+
+ public void addHandler(IHandler handler) {
+ String[] pattern = urlToTokens(BASE_PATH + handler.getUrlPattern());
+ handlers.put(handler.getUrlPattern(), handler);
+ complexHandlers.put(pattern, handler);
+
+ }
+
+ public IHandler getHandlerForUri(String uri) {
+ IHandler handler = handlers.get(uri);
+ if (handler == null) {
+ String[] targetPattern = urlToTokens(uri);
+ for (String[] pattern : complexHandlers.keySet()) {
+ if (patternsMatch(pattern, targetPattern)) {
+ handler = complexHandlers.get(pattern);
+ break;
+ }
+ }
+ }
+ return handler;
+ }
+
+ public Map<String, String> getValuesForPlaceHolders(IHandler handler, String url) {
+ String[] pattern = urlToTokens(BASE_PATH+handler.getUrlPattern());
+ String[] originalPattern = urlToTokens(BASE_PATH+handler.getUrlPattern(), false);
+ String[] targetPattern = urlToTokens(url, false);
+ Map<String, String> result = new HashMap<String, String>();
+
+ for (int i = 0; i < pattern.length; i++) {
+ if (pattern[i].equals(TOKEN_PLACEHOLDER)) {
+ result.put(originalPattern[i].substring(1, originalPattern[i].length() - 1), targetPattern[i]);
+ }
+ }
+ return result;
+ }
+
+ private boolean patternsMatch(String[] pattern, String[] targetPattern) {
+ for (int i = 0; i < pattern.length; i++) {
+ if (i >= targetPattern.length)
+ return false;
+ if (pattern[i].equals(REST_PLACEHOLDER))
+ return true;
+ if (!pattern[i].equals(TOKEN_PLACEHOLDER) && !pattern[i].equals(targetPattern[i])) {
+ return false;
+ }
+ }
+ if (pattern.length != targetPattern.length)
+ return false; // should have been handled with the REST_PLACEHOLDER
+ return true;
+ }
+
+ private void initHandlers() {
+
+// addHandler(new CreatePollServlet("/pollcreate.jsp"));
+// addHandler(new ListPollsServlet());
+// addHandler(new ImageGenerator());
+// addHandler(new ProcessPollServlet());
+// addHandler(new ViewPollResultsServlet());
+// addHandler(new ProcessPollServlet());
+// addHandler(new UploadImageServlet("/uploadimage.jsp"));
+// addHandler(new FileUpload());
+ // IHandler handler = new UsersHandler();
+ // handlers.put(handler.getUrlPattern(), handler);
+ // handler = new ListHandler();
+ // handlers.put(handler.getUrlPattern(), handler);
+ addHandler(new LogSingleActivity());
+ addHandler(new ListAllActivities());
+
+ }
+
+ private void processUri(String requestURI, HttpServletRequest req, HttpServletResponse resp, Method handlingMethod)
+ throws IOException {
+ //requestURI = requestURI.substring(BASE_PATH.length());
+ req = new WriteableParametersHttpServletRequestWrapper(req);
+ IHandler handler = getHandlerForUri(requestURI);
+ if (handler == null) {
+ throw new IOException("No handler found for "+requestURI);
+ }
+ for (Map.Entry<String, String> entry : getValuesForPlaceHolders(handler, requestURI).entrySet()) {
+ req.getParameterMap().put(entry.getKey(), entry.getValue());
+ }
+ try {
+ handler.setServletContext(getServletContext());
+ log.warning("about to execute on: "+handler);
+ handlingMethod.invoke(handler, req, resp);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ handler.setServletContext(null);
+ }
+
+ }
+}
Oops, something went wrong.

0 comments on commit 65a18d8

Please sign in to comment.