Skip to content

Commit

Permalink
Rework Dingus to use Front Controller
Browse files Browse the repository at this point in the history
* Keep logic out of the JSP templates, as far as possible.
* Fix drop down "Results" menu to be "sticky".
* Based on Heroku's "Containerless web app" application template.

Signed-off-by: Alex Coles <alex@alexbcoles.com>
  • Loading branch information
myabc committed Feb 17, 2013
1 parent a7585d8 commit e6f05fd
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 127 deletions.
6 changes: 5 additions & 1 deletion .gitignore
@@ -1 +1,5 @@
target/
.classpath
.project
.settings
.springBeans
target
1 change: 1 addition & 0 deletions Procfile
@@ -0,0 +1 @@
web: java $JAVA_OPTS -cp target/classes:target/dependency/* org.markdownj.dingus.Main
12 changes: 11 additions & 1 deletion README.md
@@ -1,2 +1,12 @@
markdownj-dingus
================
================

## Running the application locally

First build with:

$ mvn clean install

Then run it with:

$ java -cp target/classes:target/dependency/* org.markdownj.dingus.Main
144 changes: 72 additions & 72 deletions pom.xml
@@ -1,85 +1,85 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.markdownj</groupId>
<artifactId>markdownj</artifactId>
<version>0.4-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<parent>
<groupId>org.markdownj</groupId>
<artifactId>markdownj</artifactId>
<version>0.4-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>www</artifactId>
<packaging>war</packaging>
<name>MarkdownJ Web Dingus</name>
<artifactId>www</artifactId>
<packaging>war</packaging>
<name>MarkdownJ Web Dingus</name>

<dependencies>
<dependencies>
<dependency>
<groupId>${pom.parent.groupId}</groupId>
<artifactId>${pom.parent.artifactId}-core</artifactId>
<version>${pom.parent.version}</version>
</dependency>

<dependency>
<groupId>${pom.parent.groupId}</groupId>
<artifactId>${pom.parent.artifactId}-core</artifactId>
<version>${pom.parent.version}</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.2</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>7.6.0.v20120127</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>7.6.0.v20120127</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.14</version>
<configuration>
<contextPath>/</contextPath>
<scanIntervalSeconds>3</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
<finalName>www</finalName>
</build>
<artifactId>jsp-2.1-glassfish</artifactId>
<version>2.1.v20100127</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
<!--
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.14</version>
<configuration>
<contextPath>/</contextPath>
<scanIntervalSeconds>3</scanIntervalSeconds>
</configuration>
</plugin>
-->
</plugins>
<finalName>www</finalName>
</build>

<properties>
<site.directory>${basedir}/../</site.directory>
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<netbeans.hint.deploy.server>gfv3</netbeans.hint.deploy.server>
</properties>

</project>
57 changes: 57 additions & 0 deletions src/main/java/org/markdownj/dingus/DingusServlet.java
@@ -0,0 +1,57 @@
package org.markdownj.dingus;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.markdownj.MarkdownProcessor;

public class DingusServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if(getCurrentPath(request).equals("/")) {
request.setAttribute("showSource", false);
request.setAttribute("showPreview", false);
request.getRequestDispatcher("dingus.jsp").forward(request, response);
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Page Not Found");
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if(getCurrentPath(request).equals("/")) {
MarkdownProcessor markdown = new MarkdownProcessor();
String markup = request.getParameter("markdown");
String view = request.getParameter("view");
boolean showSource = true;
boolean showPreview = true;

if (view != null) {
if ("source".equals(view)) {
showSource = true;
showPreview = false;
} else if ("preview".equals(view)) {
showSource = false;
showPreview = true;
} // otherwise, default to showing both
}

request.setAttribute("markup", markup);
request.setAttribute("html", markdown.markdown(markup));
request.setAttribute("showSource", showSource);
request.setAttribute("showPreview", showPreview);
request.getRequestDispatcher("dingus.jsp").forward(request, response);
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Page Not Found");
}
}

private String getCurrentPath(HttpServletRequest request) {
return request.getRequestURI().substring(request.getContextPath().length());
}
}
48 changes: 48 additions & 0 deletions src/main/java/org/markdownj/dingus/Main.java
@@ -0,0 +1,48 @@
package org.markdownj.dingus;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

/**
*
* This class launches the web application in an embedded Jetty container.
* This is the entry point to your application. The Java command that is used for
* launching should fire this main method.
*
*/
public class Main {

/**
* @param args
*/
public static void main(String[] args) throws Exception{
String webappDirLocation = "src/main/webapp/";

//The port that we should run on can be set into an environment variable
//Look for that variable and default to 8080 if it isn't there.
String webPort = System.getenv("PORT");
if(webPort == null || webPort.isEmpty()) {
webPort = "8080";
}

Server server = new Server(Integer.valueOf(webPort));
WebAppContext root = new WebAppContext();

root.setContextPath("/");
root.setDescriptor(webappDirLocation+"/WEB-INF/web.xml");
root.setResourceBase(webappDirLocation);

//Parent loader priority is a class loader setting that Jetty accepts.
//By default Jetty will behave like most web containers in that it will
//allow your application to replace non-server libraries that are part of the
//container. Setting parent loader priority to true changes this behavior.
//Read more here: http://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading
root.setParentLoaderPriority(true);

server.setHandler(root);

server.start();
server.join();
}

}
16 changes: 9 additions & 7 deletions src/main/webapp/WEB-INF/web.xml
@@ -1,10 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<description>MarkdownJ Web Dingus</description>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>dingus</servlet-name>
<servlet-class>org.markdownj.dingus.DingusServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dingus</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

0 comments on commit e6f05fd

Please sign in to comment.