Last active
March 8, 2019 06:50
-
-
Save nickbabcock/e8601c417b6b65b3280b to your computer and use it in GitHub Desktop.
Dropwizard Example using Raw Jetty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import com.fasterxml.jackson.core.JsonFactory; | |
import com.fasterxml.jackson.core.JsonGenerator; | |
import com.google.common.base.Strings; | |
import io.dropwizard.Application; | |
import io.dropwizard.setup.Environment; | |
import org.eclipse.jetty.servlet.ServletHolder; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.util.concurrent.atomic.AtomicLong; | |
public class DropRawApplication extends Application<DropRawConfiguration> { | |
public static void main(final String[] args) throws Exception { | |
new DropRawApplication().run(args); | |
} | |
@Override | |
public String getName() { | |
return "DropRaw"; | |
} | |
@Override | |
public void run(final DropRawConfiguration config, | |
final Environment env) { | |
final DropRawServlet servlet = new DropRawServlet(env.getObjectMapper().getFactory(), | |
config.getTemplate(), config.getDefaultName()); | |
env.getApplicationContext().addServlet(new ServletHolder(servlet), "/perf"); | |
} | |
public static class DropRawServlet extends HttpServlet { | |
private final JsonFactory jsonFactory; | |
private final String template; | |
private final String defaultName; | |
private final AtomicLong counter = new AtomicLong(); | |
public DropRawServlet(JsonFactory jsonFactory, String template, String defaultName) { | |
this.jsonFactory = jsonFactory; | |
this.template = template; | |
this.defaultName = defaultName ; | |
} | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
final String name = Strings.nullToEmpty(req.getParameter("name")); | |
resp.setContentType("application/json"); | |
if (name.length() > 5) { | |
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); | |
try (final JsonGenerator json = jsonFactory.createGenerator(resp.getOutputStream())) { | |
json.writeStartObject(); | |
json.writeStringField("error", "Query parameter name must be shorter than 5 letters"); | |
json.writeEndObject(); | |
return; | |
} | |
} | |
resp.setStatus(HttpServletResponse.SC_OK); | |
try (final JsonGenerator json = jsonFactory.createGenerator(resp.getOutputStream())) { | |
json.writeStartObject(); | |
json.writeNumberField("id", counter.incrementAndGet()); | |
json.writeStringField("content", String.format(template, name.length() != 0 ? name : defaultName)); | |
json.writeEndObject(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.raw.jetty; | |
import com.fasterxml.jackson.core.JsonFactory; | |
import com.fasterxml.jackson.core.JsonGenerator; | |
import com.google.common.base.Strings; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.servlet.ServletHandler; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.util.concurrent.atomic.AtomicLong; | |
/** Replicates the same happy path as the Dropwizard getting started example without | |
* and configuration builtin | |
*/ | |
public class JettyServlet { | |
private static final String defaultName = "Nick" ; | |
private static final String template = "Hello %s!"; | |
private static final JsonFactory jsonFactory = new JsonFactory(); | |
private static final AtomicLong counter = new AtomicLong(); | |
public static void main( String[] args ) throws Exception { | |
Server server = new Server(8095); | |
ServletHandler handler = new ServletHandler(); | |
server.setHandler(handler); | |
handler.addServletWithMapping(DropwizardExampleHandler.class, "/*"); | |
server.start(); | |
server.join(); | |
} | |
public static class DropwizardExampleHandler extends HttpServlet { | |
@Override | |
protected void doGet(HttpServletRequest request, HttpServletResponse response ) | |
throws ServletException, IOException { | |
final String name = Strings.nullToEmpty(request.getParameter("name")); | |
response.setContentType("application/json"); | |
if (name.length() > 5) { | |
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); | |
try (final JsonGenerator json = jsonFactory.createGenerator(response.getOutputStream())) { | |
json.writeStartObject(); | |
json.writeStringField("error", "Query parameter name must be shorter than 5 letters"); | |
json.writeEndObject(); | |
return; | |
} | |
} | |
response.setStatus(HttpServletResponse.SC_OK); | |
try (final JsonGenerator json = jsonFactory.createGenerator(response.getOutputStream())) { | |
json.writeStartObject(); | |
json.writeNumberField("id", counter.incrementAndGet()); | |
json.writeStringField("content", String.format(template, name.length() != 0 ? name : defaultName)); | |
json.writeEndObject(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<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/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>raw-jetty</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<!-- this is required because jetty-server pom | |
put servlet-api into default scope when it should | |
always be provided scope so it doesn't get compiled. | |
without this there are security exceptions because | |
javax.servlet-api jar is not signed --> | |
<groupId>javax.servlet</groupId> | |
<artifactId>javax.servlet-api</artifactId> | |
<version>4.0.0-b01</version> | |
<!--<scope>provided</scope>--> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-server</artifactId> | |
<version>9.3.7.v20160115</version> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-servlet</artifactId> | |
<version>9.3.7.v20160115</version> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-servlets</artifactId> | |
<version>9.3.7.v20160115</version> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-http</artifactId> | |
<version>9.3.7.v20160115</version> | |
</dependency> | |
<dependency> | |
<groupId>com.fasterxml.jackson.core</groupId> | |
<artifactId>jackson-core</artifactId> | |
<version>2.7.1</version> | |
</dependency> | |
<dependency> | |
<groupId>com.fasterxml.jackson.core</groupId> | |
<artifactId>jackson-databind</artifactId> | |
<version>2.7.1-1</version> | |
</dependency> | |
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>19.0</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.3.2</version> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
<optimize>true</optimize> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-shade-plugin</artifactId> | |
<version>1.6</version> | |
<configuration> | |
<createDependencyReducedPom>true</createDependencyReducedPom> | |
<filters> | |
<filter> | |
<artifact>*:*</artifact> | |
<excludes> | |
<exclude>META-INF/*.SF</exclude> | |
<exclude>META-INF/*.DSA</exclude> | |
<exclude>META-INF/*.RSA</exclude> | |
</excludes> | |
</filter> | |
</filters> | |
</configuration> | |
<executions> | |
<execution> | |
<phase>package</phase> | |
<goals> | |
<goal>shade</goal> | |
</goals> | |
<configuration> | |
<transformers> | |
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /> | |
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | |
<mainClass>com.example.raw.jetty.RawJetty</mainClass> | |
</transformer> | |
</transformers> | |
</configuration> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.raw.jetty; | |
import com.fasterxml.jackson.core.JsonFactory; | |
import com.fasterxml.jackson.core.JsonGenerator; | |
import com.google.common.base.Strings; | |
import org.eclipse.jetty.http.HttpField; | |
import org.eclipse.jetty.http.HttpHeader; | |
import org.eclipse.jetty.http.PreEncodedHttpField; | |
import org.eclipse.jetty.server.Request; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.server.handler.AbstractHandler; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.util.concurrent.atomic.AtomicLong; | |
/** Replicates the same happy path as the Dropwizard getting started example without | |
* and configuration builtin | |
*/ | |
public class RawJetty { | |
private static final String defaultName = "Nick" ; | |
private static final String template = "Hello %s!"; | |
private static final JsonFactory jsonFactory = new JsonFactory(); | |
private static final AtomicLong counter = new AtomicLong(); | |
public static void main( String[] args ) throws Exception { | |
Server server = new Server(8095); | |
server.setHandler(new DropwizardExampleHandler()); | |
server.start(); | |
server.join(); | |
} | |
public static class DropwizardExampleHandler extends AbstractHandler { | |
private static final HttpField contentType = new PreEncodedHttpField(HttpHeader.CONTENT_TYPE, "application/json"); | |
@Override | |
public void handle(String s, Request baseRequest, HttpServletRequest request, HttpServletResponse response) | |
throws IOException, ServletException { | |
baseRequest.setHandled(true); | |
final String name = Strings.nullToEmpty(request.getParameter("name")); | |
baseRequest.getResponse().getHttpFields().add(contentType); | |
if (name.length() > 5) { | |
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); | |
try (final JsonGenerator json = jsonFactory.createGenerator(response.getOutputStream())) { | |
json.writeStartObject(); | |
json.writeStringField("error", "Query parameter name must be shorter than 5 letters"); | |
json.writeEndObject(); | |
return; | |
} | |
} | |
response.setStatus(HttpServletResponse.SC_OK); | |
try (final JsonGenerator json = jsonFactory.createGenerator(response.getOutputStream())) { | |
json.writeStartObject(); | |
json.writeNumberField("id", counter.incrementAndGet()); | |
json.writeStringField("content", String.format(template, name.length() != 0 ? name : defaultName)); | |
json.writeEndObject(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment