Skip to content

Commit b11b320

Browse files
committed
start with dropwizard sample project
1 parent 580e67a commit b11b320

File tree

7 files changed

+274
-0
lines changed

7 files changed

+274
-0
lines changed

example.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
template: Hello, %s!
2+
defaultName: Stranger

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@
9494
</executions>
9595
</plugin>
9696

97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-jar-plugin</artifactId>
100+
<configuration>
101+
<archive>
102+
<manifest>
103+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
104+
</manifest>
105+
</archive>
106+
</configuration>
107+
</plugin>
108+
97109
</plugins>
98110
</build>
99111

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2015 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.example.helloworld;
21+
22+
import io.dropwizard.Application;
23+
import io.dropwizard.setup.Bootstrap;
24+
import io.dropwizard.setup.Environment;
25+
26+
import com.example.helloworld.health.TemplateHealthCheck;
27+
import com.example.helloworld.resources.HelloWorldResource;
28+
29+
public class HelloWorldApplication extends Application<HelloWorldConfiguration> {
30+
31+
public static void main(String[] args) throws Exception {
32+
new HelloWorldApplication().run(args);
33+
}
34+
35+
@Override
36+
public String getName() {
37+
return "hello-world";
38+
}
39+
40+
@Override
41+
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
42+
// nothing to do yet
43+
}
44+
45+
@Override
46+
public void run(HelloWorldConfiguration configuration,
47+
Environment environment) {
48+
final HelloWorldResource resource = new HelloWorldResource(
49+
configuration.getTemplate(),
50+
configuration.getDefaultName()
51+
);
52+
final TemplateHealthCheck healthCheck =
53+
new TemplateHealthCheck(configuration.getTemplate());
54+
environment.healthChecks().register("template", healthCheck);
55+
environment.jersey().register(resource);
56+
}
57+
58+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2015 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.example.helloworld;
21+
22+
import io.dropwizard.Configuration;
23+
24+
import org.hibernate.validator.constraints.NotEmpty;
25+
26+
import com.fasterxml.jackson.annotation.JsonProperty;
27+
28+
public class HelloWorldConfiguration extends Configuration {
29+
30+
@NotEmpty
31+
private String template;
32+
33+
@NotEmpty
34+
private String defaultName = "Stranger";
35+
36+
@JsonProperty
37+
public String getTemplate() {
38+
return template;
39+
}
40+
41+
@JsonProperty
42+
public void setTemplate(String template) {
43+
this.template = template;
44+
}
45+
46+
@JsonProperty
47+
public String getDefaultName() {
48+
return defaultName;
49+
}
50+
51+
@JsonProperty
52+
public void setDefaultName(String name) {
53+
this.defaultName = name;
54+
}
55+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2015 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.example.helloworld.core;
21+
22+
import org.hibernate.validator.constraints.Length;
23+
24+
import com.fasterxml.jackson.annotation.JsonProperty;
25+
26+
public class Saying {
27+
28+
private long id;
29+
30+
@Length(max = 3)
31+
private String content;
32+
33+
public Saying() {
34+
// Jackson deserialization
35+
}
36+
37+
public Saying(long id, String content) {
38+
this.id = id;
39+
this.content = content;
40+
}
41+
42+
@JsonProperty
43+
public long getId() {
44+
return id;
45+
}
46+
47+
@JsonProperty
48+
public String getContent() {
49+
return content;
50+
}
51+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2015 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.example.helloworld.health;
21+
22+
import com.codahale.metrics.health.HealthCheck;
23+
24+
public class TemplateHealthCheck extends HealthCheck {
25+
26+
private final String template;
27+
28+
public TemplateHealthCheck(String template) {
29+
this.template = template;
30+
}
31+
32+
@Override
33+
protected Result check() throws Exception {
34+
final String saying = String.format(template, "TEST");
35+
if (!saying.contains("TEST")) {
36+
return Result.unhealthy("template doesn't include a name");
37+
}
38+
return Result.healthy();
39+
}
40+
41+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2015 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.example.helloworld.resources;
21+
22+
import java.util.concurrent.atomic.AtomicLong;
23+
24+
import javax.ws.rs.GET;
25+
import javax.ws.rs.Path;
26+
import javax.ws.rs.Produces;
27+
import javax.ws.rs.QueryParam;
28+
import javax.ws.rs.core.MediaType;
29+
30+
import com.codahale.metrics.annotation.Timed;
31+
import com.example.helloworld.core.Saying;
32+
import com.google.common.base.Optional;
33+
34+
@Path("/hello-world")
35+
@Produces(MediaType.APPLICATION_JSON)
36+
public class HelloWorldResource {
37+
38+
private final String template;
39+
private final String defaultName;
40+
private final AtomicLong counter;
41+
42+
public HelloWorldResource(String template, String defaultName) {
43+
this.template = template;
44+
this.defaultName = defaultName;
45+
this.counter = new AtomicLong();
46+
}
47+
48+
@GET
49+
@Timed
50+
public Saying sayHello(@QueryParam("name") Optional<String> name) {
51+
final String value = String.format(template, name.or(defaultName));
52+
return new Saying(counter.incrementAndGet(), value);
53+
}
54+
55+
}

0 commit comments

Comments
 (0)