Explorar el Código

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

peter_backlund hace 17 años
padre
commit
3b0642e207

+ 0
- 2
dddsample/external/reporting/pom.xml Ver fichero

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

+ 8
- 4
dddsample/external/reporting/src/main/java/com/reporting2/ReportingService.java Ver fichero

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

+ 2
- 0
dddsample/external/reporting/src/main/java/com/reporting2/pdf/PDFMessageBodyWriter.java Ver fichero

@@ -17,6 +17,8 @@ public abstract class PDFMessageBodyWriter<T> implements MessageBodyWriter<T> {
17 17
 
18 18
   public long getSize(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) {
19 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 22
     writeDocumentToStream(t, baos);
21 23
     return baos.toByteArray().length;
22 24
   }

+ 3
- 1
dddsample/external/reporting/src/main/webapp/index.jsp Ver fichero

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

+ 23
- 24
dddsample/external/reporting/src/test/java/temp/pkg/ReportServiceTest.java Ver fichero

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

+ 29
- 0
dddsample/external/reporting/src/test/resources/context-cxf.xml Ver fichero

@@ -0,0 +1,29 @@
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 Ver fichero

@@ -105,6 +105,10 @@
105 105
             </descriptorRefs>
106 106
           </configuration>
107 107
         </plugin>
108
+        <plugin>
109
+          <artifactId>maven-surefire-plugin</artifactId>
110
+          <version>2.4.2</version>
111
+        </plugin>
108 112
       </plugins>
109 113
     </pluginManagement>
110 114
   </build>
@@ -161,12 +165,6 @@
161 165
       <version>1.8.0</version>
162 166
       <scope>test</scope>
163 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 168
   </dependencies>
171 169
 
172 170
   <dependencyManagement>
@@ -180,6 +178,23 @@
180 178
         <groupId>org.springframework</groupId>
181 179
         <artifactId>spring-web</artifactId>
182 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 198
       </dependency>
184 199
       <dependency>
185 200
         <groupId>org.springframework</groupId>
@@ -286,15 +301,31 @@
286 301
       </dependency>
287 302
       <dependency>
288 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 316
         <artifactId>cxf-rt-frontend-jaxrs</artifactId>
290 317
         <version>${cxf.version}</version>
291
-        <scope>runtime</scope>
292 318
       </dependency>
293 319
       <dependency>
294 320
         <groupId>javax.ws.rs</groupId>
295 321
         <artifactId>jsr311-api</artifactId>
296 322
         <version>1.0</version>
297 323
       </dependency>
324
+      <dependency>
325
+        <groupId>com.lowagie</groupId>
326
+        <artifactId>itext</artifactId>
327
+        <version>2.1.7</version>
328
+      </dependency>
298 329
     </dependencies>
299 330
   </dependencyManagement>
300 331