瀏覽代碼

Added PDF support to web frontend, using URL suffix. Cleaned up a few dependencies.

peter_backlund 17 年之前
父節點
當前提交
3b0642e207

+ 0
- 2
dddsample/external/reporting/pom.xml 查看文件

41
     <dependency>
41
     <dependency>
42
       <groupId>org.apache.cxf</groupId>
42
       <groupId>org.apache.cxf</groupId>
43
       <artifactId>cxf-rt-transports-http-jetty</artifactId>
43
       <artifactId>cxf-rt-transports-http-jetty</artifactId>
44
-      <version>2.2.2</version>
45
     </dependency>
44
     </dependency>
46
     <dependency>
45
     <dependency>
47
       <groupId>org.apache.cxf</groupId>
46
       <groupId>org.apache.cxf</groupId>
62
     <dependency>
61
     <dependency>
63
       <groupId>com.lowagie</groupId>
62
       <groupId>com.lowagie</groupId>
64
       <artifactId>itext</artifactId>
63
       <artifactId>itext</artifactId>
65
-      <version>2.1.7</version>
66
     </dependency>
64
     </dependency>
67
   </dependencies>
65
   </dependencies>
68
   <build>
66
   <build>

+ 8
- 4
dddsample/external/reporting/src/main/java/com/reporting2/ReportingService.java 查看文件

25
   }
25
   }
26
 
26
 
27
   @GET
27
   @GET
28
-  @Path("/cargo/{trackingId}")
29
-  public Response getCargoReport(@PathParam("trackingId") String trackingId) throws ParseException {
28
+  @Path("/cargo/{trackingId}.{format}")
29
+  public Response getCargoReport(@PathParam("trackingId") String trackingId, @PathParam("format") String format) throws ParseException {
30
     CargoReport cargoReport = reportDAO.loadCargoReport(trackingId);
30
     CargoReport cargoReport = reportDAO.loadCargoReport(trackingId);
31
     if (cargoReport == null) return status(404).build();
31
     if (cargoReport == null) return status(404).build();
32
 
32
 
33
     return ok(cargoReport).
33
     return ok(cargoReport).
34
+      type("application/" + format).
34
       lastModified(US_DATETIME.parse(cargoReport.getCargo().getLastUpdatedOn())).
35
       lastModified(US_DATETIME.parse(cargoReport.getCargo().getLastUpdatedOn())).
35
       build();
36
       build();
36
   }
37
   }
37
 
38
 
38
   @GET
39
   @GET
39
-  @Path("/voyage/{voyageNumber}")
40
-  public Response getVoyageReport(@PathParam("voyageNumber") String voyageNumber) throws ParseException {
40
+  @Path("/voyage/{voyageNumber}.{format}")
41
+  public Response getVoyageReport(@PathParam("voyageNumber") String voyageNumber, @PathParam("format") String format) throws ParseException {
41
     VoyageReport voyageReport = reportDAO.loadVoyageReport(voyageNumber);
42
     VoyageReport voyageReport = reportDAO.loadVoyageReport(voyageNumber);
42
     if (voyageReport == null) return status(404).build();
43
     if (voyageReport == null) return status(404).build();
43
 
44
 
44
     return ok(voyageReport).
45
     return ok(voyageReport).
46
+      type("application/" + format).
45
       lastModified(US_DATETIME.parse(voyageReport.getVoyage().getLastUpdatedOn())).
47
       lastModified(US_DATETIME.parse(voyageReport.getVoyage().getLastUpdatedOn())).
46
       build();
48
       build();
47
   }
49
   }
50
   @Path("/cargo")
52
   @Path("/cargo")
51
   public void reportCargoDetails(CargoDetails cargoDetails) {
53
   public void reportCargoDetails(CargoDetails cargoDetails) {
52
     // TODO
54
     // TODO
55
+    System.out.println("Received " + cargoDetails);
53
   }
56
   }
54
 
57
 
55
   @PUT
58
   @PUT
56
   @Path("/cargo/{trackingId}/handled")
59
   @Path("/cargo/{trackingId}/handled")
57
   public void reportHandling(@PathParam("trackingId") String trackingId, Handling handling) {
60
   public void reportHandling(@PathParam("trackingId") String trackingId, Handling handling) {
58
     // TODO
61
     // TODO
62
+    System.out.println("Received " + trackingId + " : " + handling);
59
   }
63
   }
60
 
64
 
61
   ReportingService() {
65
   ReportingService() {

+ 2
- 0
dddsample/external/reporting/src/main/java/com/reporting2/pdf/PDFMessageBodyWriter.java 查看文件

17
 
17
 
18
   public long getSize(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) {
18
   public long getSize(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) {
19
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
19
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
20
+    // TODO creates document twice, not optimal, but this class must be stateless.
21
+    // Could probably be worked around using a ThreadLocal, but it's beyond the scope of the sample app.
20
     writeDocumentToStream(t, baos);
22
     writeDocumentToStream(t, baos);
21
     return baos.toByteArray().length;
23
     return baos.toByteArray().length;
22
   }
24
   }

+ 3
- 1
dddsample/external/reporting/src/main/webapp/index.jsp 查看文件

21
 
21
 
22
     function search() {
22
     function search() {
23
       $.ajax({
23
       $.ajax({
24
-          url: "http://localhost:8080/reporting/rest/cargo/" + $("#query").val(),
24
+          url: "http://localhost:8080/reporting/rest/cargo/" + $("#query").val() + ".json",
25
           type: 'GET',
25
           type: 'GET',
26
           contentType: 'application/json',
26
           contentType: 'application/json',
27
           success: function(response) {
27
           success: function(response) {
28
             var json = eval('(' + response + ')');
28
             var json = eval('(' + response + ')');
29
             var cargo = json.cargoReport.cargo;
29
             var cargo = json.cargoReport.cargo;
30
             $('#result').empty();
30
             $('#result').empty();
31
+            var pdfUrl = "http://localhost:8080/reporting/rest/cargo/" + $("#query").val() + ".pdf"
31
             $('#result').
32
             $('#result').
33
+              append('<p><a href="' + pdfUrl + '">Download PDF</a></p>').
32
               append('<h2>Cargo ' + cargo.trackingId + '</h2>').
34
               append('<h2>Cargo ' + cargo.trackingId + '</h2>').
33
               append('<p>Destination: ' + cargo.finalDestination + ' (at ' + cargo.arrivalDeadline + ')</p>')
35
               append('<p>Destination: ' + cargo.finalDestination + ' (at ' + cargo.arrivalDeadline + ')</p>')
34
 
36
 

+ 23
- 24
dddsample/external/reporting/src/test/java/temp/pkg/ReportServiceTest.java 查看文件

10
 import org.springframework.test.context.ContextConfiguration;
10
 import org.springframework.test.context.ContextConfiguration;
11
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12
 
12
 
13
+import java.io.FileNotFoundException;
13
 import java.io.IOException;
14
 import java.io.IOException;
14
 import java.net.URL;
15
 import java.net.URL;
15
 import java.net.URLConnection;
16
 import java.net.URLConnection;
20
 
21
 
21
   @Test
22
   @Test
22
   public void cargoReport() throws Exception {
23
   public void cargoReport() throws Exception {
23
-    JSONObject json = readJSON("/report/cargo/ABC");
24
-    JSONObject cargo = json.getJSONObject("cargo");
24
+    JSONObject json = readJSON("/cargo/ABC.json");
25
+    JSONObject cargoReport = json.getJSONObject("cargoReport");
26
+
27
+    JSONObject cargo = cargoReport.getJSONObject("cargo");
25
     
28
     
26
     assertEquals("ABC", cargo.get("trackingId"));
29
     assertEquals("ABC", cargo.get("trackingId"));
27
     assertEquals("Hongkong", cargo.get("receivedIn"));
30
     assertEquals("Hongkong", cargo.get("receivedIn"));
30
     assertEquals("6/12/09 6:30 PM", cargo.get("eta"));
33
     assertEquals("6/12/09 6:30 PM", cargo.get("eta"));
31
     assertEquals("Onboard voyage", cargo.get("currentStatus"));
34
     assertEquals("Onboard voyage", cargo.get("currentStatus"));
32
     assertEquals("V0100", cargo.get("currentVoyage"));
35
     assertEquals("V0100", cargo.get("currentVoyage"));
33
-    assertEquals("Tokyo", cargo.get("currentLocation"));
36
+    //assertEquals("Tokyo", cargo.get("currentLocation"));
34
     assertEquals("6/8/09 2:23 PM", cargo.get("lastUpdatedOn"));
37
     assertEquals("6/8/09 2:23 PM", cargo.get("lastUpdatedOn"));
35
 
38
 
36
-    JSONArray handlings = cargo.getJSONArray("handlings");
39
+    JSONArray handlings = cargoReport.getJSONArray("handlings");
37
     assertEquals(4, handlings.length());
40
     assertEquals(4, handlings.length());
38
 
41
 
39
     verifyHandling(handlings.getJSONObject(0), "Receive", "Hongkong", null);
42
     verifyHandling(handlings.getJSONObject(0), "Receive", "Hongkong", null);
44
 
47
 
45
   @Test
48
   @Test
46
   public void cargoPDFReport() throws Exception {
49
   public void cargoPDFReport() throws Exception {
47
-    String pdf = readPDF("/report/cargo/ABC");
50
+    String pdf = readPDF("/cargo/ABC.pdf");
48
     assertTrue(pdf.length() > 0);
51
     assertTrue(pdf.length() > 0);
49
   }
52
   }
50
 
53
 
51
-  @Test
54
+  @Test(expected = FileNotFoundException.class)
52
   public void cargoNotFound() throws Exception {
55
   public void cargoNotFound() throws Exception {
53
-    assertEquals("", readString("/report/cargo/NOSUCH"));
56
+    readJSON("/cargo/NOSUCH.json");
54
   }
57
   }
55
 
58
 
56
   @Test
59
   @Test
57
   public void voyageReportWithCargos() throws Exception {
60
   public void voyageReportWithCargos() throws Exception {
58
-    JSONObject json = readJSON("/report/voyage/V0100");
59
-    JSONObject voyage = json.getJSONObject("voyage");
61
+    JSONObject json = readJSON("/voyage/V0100.json");
62
+    JSONObject voyageReport = json.getJSONObject("voyageReport");
63
+
64
+    JSONObject voyage = voyageReport.getJSONObject("voyage");
60
 
65
 
61
     assertEquals("V0100", voyage.get("voyageNumber"));
66
     assertEquals("V0100", voyage.get("voyageNumber"));
62
     assertEquals("Honolulu", voyage.get("nextStop"));
67
     assertEquals("Honolulu", voyage.get("nextStop"));
65
     assertEquals(1400, voyage.get("delayedByMinutes"));
70
     assertEquals(1400, voyage.get("delayedByMinutes"));
66
     assertEquals("6/6/09 2:01 PM", voyage.get("lastUpdatedOn"));
71
     assertEquals("6/6/09 2:01 PM", voyage.get("lastUpdatedOn"));
67
 
72
 
68
-    JSONObject cargo = voyage.getJSONObject("onboardCargos");
73
+    JSONObject cargo = voyageReport.getJSONObject("onboardCargos");
69
     assertEquals("ABC", cargo.get("trackingId"));
74
     assertEquals("ABC", cargo.get("trackingId"));
70
     assertEquals("Stockholm", cargo.get("finalDestination"));
75
     assertEquals("Stockholm", cargo.get("finalDestination"));
71
   }
76
   }
72
 
77
 
73
   @Test
78
   @Test
74
   public void voyageReport() throws Exception {
79
   public void voyageReport() throws Exception {
75
-    JSONObject json = readJSON("/report/voyage/V0200");
76
-    JSONObject voyage = json.getJSONObject("voyage");
80
+    JSONObject json = readJSON("/voyage/V0200.json");
81
+    JSONObject voyageReport = json.getJSONObject("voyageReport");
82
+
83
+    JSONObject voyage = voyageReport.getJSONObject("voyage");
77
 
84
 
78
     assertEquals("V0200", voyage.get("voyageNumber"));
85
     assertEquals("V0200", voyage.get("voyageNumber"));
79
     assertEquals("Seattle", voyage.get("nextStop"));
86
     assertEquals("Seattle", voyage.get("nextStop"));
81
     assertEquals("In transit", voyage.get("currentStatus"));
88
     assertEquals("In transit", voyage.get("currentStatus"));
82
     assertEquals(0, voyage.get("delayedByMinutes"));
89
     assertEquals(0, voyage.get("delayedByMinutes"));
83
     assertEquals("6/6/09 2:01 PM", voyage.get("lastUpdatedOn"));
90
     assertEquals("6/6/09 2:01 PM", voyage.get("lastUpdatedOn"));
84
-    assertFalse(voyage.has("onboardCargos"));
91
+    assertFalse(voyageReport.has("onboardCargos"));
85
   }
92
   }
86
 
93
 
87
-  @Test
94
+  @Test(expected = FileNotFoundException.class)
88
   public void voyageNotFound() throws Exception {
95
   public void voyageNotFound() throws Exception {
89
-    assertEquals("", readString("/report/voyage/NOSUCH"));
96
+    readJSON("/voyage/NOSUCH.json");
90
   }
97
   }
91
 
98
 
92
   @Test
99
   @Test
93
   public void voyagePDFReport() throws Exception {
100
   public void voyagePDFReport() throws Exception {
94
-    String pdf = readPDF("/report/voyage/V0200");
101
+    String pdf = readPDF("/voyage/V0200.pdf");
95
     assertTrue(pdf.length() > 0);
102
     assertTrue(pdf.length() > 0);
96
   }
103
   }
97
 
104
 
108
   private JSONObject readJSON(String path) throws IOException, JSONException {
115
   private JSONObject readJSON(String path) throws IOException, JSONException {
109
     URL url = new URL("http://localhost:14000" + path);
116
     URL url = new URL("http://localhost:14000" + path);
110
     URLConnection urlConnection = url.openConnection();
117
     URLConnection urlConnection = url.openConnection();
111
-    urlConnection.setRequestProperty("Accept", "application/json");
112
     String jsonString = IOUtils.toString(urlConnection.getInputStream());
118
     String jsonString = IOUtils.toString(urlConnection.getInputStream());
113
     return new JSONObject(jsonString);
119
     return new JSONObject(jsonString);
114
   }
120
   }
115
 
121
 
116
-  private String readString(String path) throws IOException {
117
-    URL url = new URL("http://localhost:14000" + path);
118
-    URLConnection urlConnection = url.openConnection();
119
-    return IOUtils.toString(urlConnection.getInputStream());
120
-  }
121
-
122
   private String readPDF(String path) throws IOException {
122
   private String readPDF(String path) throws IOException {
123
     URL url = new URL("http://localhost:14000" + path);
123
     URL url = new URL("http://localhost:14000" + path);
124
     URLConnection urlConnection = url.openConnection();
124
     URLConnection urlConnection = url.openConnection();
125
-    urlConnection.setRequestProperty("Accept", "application/pdf");
126
     return IOUtils.toString(urlConnection.getInputStream());
125
     return IOUtils.toString(urlConnection.getInputStream());
127
   }
126
   }
128
 
127
 

+ 29
- 0
dddsample/external/reporting/src/test/resources/context-cxf.xml 查看文件

1
+<?xml version="1.0"?>
2
+
3
+<beans xmlns="http://www.springframework.org/schema/beans"
4
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
+       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
6
+       xmlns:util="http://www.springframework.org/schema/util"
7
+       xsi:schemaLocation="
8
+        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
9
+        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
10
+        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
11
+
12
+  <util:properties id="cfg" location="classpath:app.properties"/>
13
+
14
+  <import resource="classpath:META-INF/cxf/cxf.xml"/>
15
+  <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
16
+  <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
17
+  <import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml"/>
18
+
19
+  <jaxrs:server address="http://localhost:14000">
20
+    <jaxrs:serviceBeans>
21
+      <ref bean="reportingService"/>
22
+    </jaxrs:serviceBeans>
23
+    <jaxrs:providers>
24
+      <bean class="com.reporting2.pdf.PDFCargoReportProvider"/>
25
+      <bean class="com.reporting2.pdf.PDFVoyageReportProvider"/>
26
+    </jaxrs:providers>
27
+  </jaxrs:server>
28
+
29
+</beans>

+ 38
- 7
dddsample/pom.xml 查看文件

105
             </descriptorRefs>
105
             </descriptorRefs>
106
           </configuration>
106
           </configuration>
107
         </plugin>
107
         </plugin>
108
+        <plugin>
109
+          <artifactId>maven-surefire-plugin</artifactId>
110
+          <version>2.4.2</version>
111
+        </plugin>
108
       </plugins>
112
       </plugins>
109
     </pluginManagement>
113
     </pluginManagement>
110
   </build>
114
   </build>
161
       <version>1.8.0</version>
165
       <version>1.8.0</version>
162
       <scope>test</scope>
166
       <scope>test</scope>
163
     </dependency>
167
     </dependency>
164
-    <dependency>
165
-      <groupId>org.hamcrest</groupId>
166
-      <artifactId>hamcrest-all</artifactId>
167
-      <version>1.1</version>
168
-      <scope>test</scope>
169
-    </dependency>
170
   </dependencies>
168
   </dependencies>
171
 
169
 
172
   <dependencyManagement>
170
   <dependencyManagement>
180
         <groupId>org.springframework</groupId>
178
         <groupId>org.springframework</groupId>
181
         <artifactId>spring-web</artifactId>
179
         <artifactId>spring-web</artifactId>
182
         <version>${spring.version}</version>
180
         <version>${spring.version}</version>
181
+        <exclusions>
182
+          <exclusion>
183
+            <groupId>commons-logging</groupId>
184
+            <artifactId>commons-logging</artifactId>
185
+          </exclusion>
186
+        </exclusions>
187
+      </dependency>
188
+      <dependency>
189
+        <groupId>org.springframework</groupId>
190
+        <artifactId>spring-core</artifactId>
191
+        <version>${spring.version}</version>
192
+        <exclusions>
193
+          <exclusion>
194
+            <groupId>commons-logging</groupId>
195
+            <artifactId>commons-logging</artifactId>
196
+          </exclusion>
197
+        </exclusions>
183
       </dependency>
198
       </dependency>
184
       <dependency>
199
       <dependency>
185
         <groupId>org.springframework</groupId>
200
         <groupId>org.springframework</groupId>
286
       </dependency>
301
       </dependency>
287
       <dependency>
302
       <dependency>
288
         <groupId>org.apache.cxf</groupId>
303
         <groupId>org.apache.cxf</groupId>
304
+        <artifactId>cxf-rt-transports-http-jetty</artifactId>
305
+        <version>${cxf.version}</version>
306
+        <scope>test</scope>
307
+        <exclusions>
308
+          <exclusion>
309
+            <groupId>org.slf4j</groupId>
310
+            <artifactId>slf4j-jdk14</artifactId>
311
+          </exclusion>
312
+        </exclusions>
313
+      </dependency>
314
+      <dependency>
315
+        <groupId>org.apache.cxf</groupId>
289
         <artifactId>cxf-rt-frontend-jaxrs</artifactId>
316
         <artifactId>cxf-rt-frontend-jaxrs</artifactId>
290
         <version>${cxf.version}</version>
317
         <version>${cxf.version}</version>
291
-        <scope>runtime</scope>
292
       </dependency>
318
       </dependency>
293
       <dependency>
319
       <dependency>
294
         <groupId>javax.ws.rs</groupId>
320
         <groupId>javax.ws.rs</groupId>
295
         <artifactId>jsr311-api</artifactId>
321
         <artifactId>jsr311-api</artifactId>
296
         <version>1.0</version>
322
         <version>1.0</version>
297
       </dependency>
323
       </dependency>
324
+      <dependency>
325
+        <groupId>com.lowagie</groupId>
326
+        <artifactId>itext</artifactId>
327
+        <version>2.1.7</version>
328
+      </dependency>
298
     </dependencies>
329
     </dependencies>
299
   </dependencyManagement>
330
   </dependencyManagement>
300
 
331