Skip to content

Commit bc510de

Browse files
committed
Implemented solution for Video 3.1.
1 parent a63088e commit bc510de

File tree

6 files changed

+169
-3
lines changed

6 files changed

+169
-3
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
In this video we are going to talk about Content-Types and Content Negotiation.
88

9-
| Method | URI | Status | Description |
10-
|--------|-----|--------|-------------|
11-
| GET | /api/version | 200 | |
9+
| Method | URI | Status | Description |
10+
|--------|--------------|--------|-------------|
11+
| GET | /api/version | 200 | Get version string based on header |
12+
| GET | /api/documents/magic.gif | 200 | Get and display GIF |
13+
| GET | /api/documents/me.jpg | 200 | Get and download JPEG |
14+
1215

1316
### Video 3.2: Easy Data Binding using JSON-B
1417

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.packtpub.javaee8;
2+
3+
import javax.enterprise.context.ApplicationScoped;
4+
import javax.servlet.ServletContext;
5+
import javax.ws.rs.GET;
6+
import javax.ws.rs.Path;
7+
import javax.ws.rs.Produces;
8+
import javax.ws.rs.core.Context;
9+
import javax.ws.rs.core.Response;
10+
import java.io.File;
11+
12+
@ApplicationScoped
13+
@Path("documents")
14+
public class DocumentsResource {
15+
16+
@Context
17+
private ServletContext context;
18+
19+
@GET
20+
@Path("/me.jpg")
21+
@Produces("image/jpeg")
22+
public Response jpg() {
23+
String path = context.getRealPath("/me.jpg");
24+
return Response.ok(new File(path))
25+
.header("Content-Disposition", "attachment; filename=me.jpg")
26+
.build();
27+
}
28+
29+
@GET
30+
@Path("/magic.gif")
31+
@Produces("image/gif")
32+
public Response gif() {
33+
String path = context.getRealPath("/magic.gif");
34+
return Response.ok(new File(path)).build();
35+
}
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.packtpub.javaee8;
2+
3+
import javax.enterprise.context.RequestScoped;
4+
import javax.ws.rs.GET;
5+
import javax.ws.rs.Path;
6+
import javax.ws.rs.Produces;
7+
import javax.ws.rs.core.MediaType;
8+
import javax.ws.rs.core.Response;
9+
import java.util.Collections;
10+
import java.util.Map;
11+
12+
@RequestScoped
13+
@Path("version")
14+
public class VersionResource {
15+
16+
/**
17+
* MediaType implementation for the version resource in v1.
18+
*/
19+
public static final MediaType V1 = new MediaType("application", "vnd.version.v1+json");
20+
21+
/**
22+
* MediaType implementation for the version resource in v2.
23+
*/
24+
public static final MediaType V2 = new MediaType("application", "vnd.version.v2+json");
25+
26+
@GET
27+
@Produces("application/vnd.version.v2+json")
28+
public Response v2() {
29+
Map<String, String> version = Collections.singletonMap("version", "v2");
30+
return Response.ok(version).build();
31+
}
32+
33+
@GET
34+
@Produces({"application/json; qs=0.75", "application/vnd.version.v1+json; qs=1.0"})
35+
public Response v1() {
36+
Map<String, String> version = Collections.singletonMap("version", "v1");
37+
return Response.ok(version).build();
38+
}
39+
40+
}

src/main/webapp/magic.gif

1.39 MB
Loading

src/main/webapp/me.jpg

368 KB
Loading
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.packtpub.javaee8;
2+
3+
import org.glassfish.jersey.jsonb.JsonBindingFeature;
4+
import org.junit.After;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import javax.ws.rs.client.Client;
9+
import javax.ws.rs.client.ClientBuilder;
10+
import javax.ws.rs.client.WebTarget;
11+
import javax.ws.rs.core.GenericType;
12+
import javax.ws.rs.core.MediaType;
13+
import javax.ws.rs.core.Response;
14+
import java.util.Map;
15+
import java.util.concurrent.TimeUnit;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
19+
public class VersionResourceIntegrationTest {
20+
21+
private Client client;
22+
private WebTarget webTarget;
23+
24+
@Before
25+
public void setUp() throws Exception {
26+
client = ClientBuilder.newBuilder()
27+
.connectTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS)
28+
.register(JsonBindingFeature.class)
29+
.build();
30+
31+
webTarget = client.target("http://localhost:8080").path("/content-service/api").path("/version");
32+
}
33+
34+
@After
35+
public void tearDown() throws Exception {
36+
client.close();
37+
}
38+
39+
@Test
40+
public void v1UsingMediaType() {
41+
Response response = webTarget.request().accept(VersionResource.V1).get();
42+
43+
MediaType mediaType = response.getMediaType();
44+
assertThat(mediaType).isEqualTo(VersionResource.V1);
45+
46+
Map<String, String> body = response.readEntity(genericMap());
47+
assertThat(body).containsEntry("version", "v1");
48+
}
49+
50+
@Test
51+
public void v1UsingApplicationJson() {
52+
Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
53+
54+
MediaType mediaType = response.getMediaType();
55+
assertThat(mediaType.toString()).isEqualTo(MediaType.APPLICATION_JSON);
56+
57+
Map<String, String> body = response.readEntity(genericMap());
58+
assertThat(body).containsEntry("version", "v1");
59+
}
60+
61+
@Test
62+
public void usingAnyMediaType() {
63+
Response response = webTarget.request(MediaType.WILDCARD).get();
64+
65+
MediaType mediaType = response.getMediaType();
66+
assertThat(mediaType).isEqualTo(VersionResource.V1);
67+
68+
Map<String, String> body = response.readEntity(genericMap());
69+
assertThat(body).containsEntry("version", "v1");
70+
}
71+
72+
@Test
73+
public void v2UsingMediaType() {
74+
Response response = webTarget.request().accept(VersionResource.V2).get();
75+
76+
MediaType mediaType = response.getMediaType();
77+
assertThat(mediaType).isEqualTo(VersionResource.V2);
78+
79+
Map<String, String> body = response.readEntity(genericMap());
80+
assertThat(body).containsEntry("version", "v2");
81+
}
82+
83+
private GenericType<Map<String, String>> genericMap() {
84+
return new GenericType<Map<String, String>>() {
85+
};
86+
}
87+
}

0 commit comments

Comments
 (0)