annotate src/main/java/de/intevation/lada/rest/Response.java @ 79:bb22b5063a94

Handle warnings and errors in the response class.
author Torsten Irländer <torsten.irlaender@intevation.de>
date Fri, 07 Jun 2013 14:13:35 +0200
parents 98fe21a9fbe5
children af8eb04a8100
rev   line source
75
b43b76fc0525 Added Response class to wrap the returned data into a response object which
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
1 package de.intevation.lada.rest;
b43b76fc0525 Added Response class to wrap the returned data into a response object which
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
2
79
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
3 import java.util.HashMap;
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
4 import java.util.Map;
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
5 import java.util.logging.Logger;
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
6
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
7 import javax.inject.Inject;
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
8
78
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
9 /**
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
10 * This class is nice!.
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
11 *
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
12 * @author <a href="mailto:torsten@intevation.de">Torsten Irländer</a>
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
13 */
75
b43b76fc0525 Added Response class to wrap the returned data into a response object which
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
14 @SuppressWarnings("serial")
b43b76fc0525 Added Response class to wrap the returned data into a response object which
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
15 public class Response implements java.io.Serializable {
78
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
16
79
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
17
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
18 /**
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
19 * The logger for this class.
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
20 */
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
21 @Inject
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
22 private Logger log;
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
23
78
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
24 private Boolean success;
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
25 private String message;
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
26 private Object data;
79
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
27 private Map<String, String> errors;
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
28 private Map<String, String> warnings;
78
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
29
79
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
30 public Response(boolean success, int code, Object data) {
78
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
31 this.success = success;
79
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
32 this.message = Integer.toString(code);
78
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
33 this.data = data;
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
34 }
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
35
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
36 public Boolean getSuccess() {
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
37 return success;
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
38 }
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
39
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
40 public void setSuccess(Boolean success) {
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
41 this.success = success;
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
42 }
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
43
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
44 public String getMessage() {
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
45 return message;
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
46 }
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
47
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
48 public void setMessage(String message) {
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
49 this.message = message;
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
50 }
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
51
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
52 public Object getData() {
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
53 return data;
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
54 }
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
55
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
56 public void setData(Object data) {
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
57 this.data = data;
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
58 }
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
59
79
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
60 public Map<String, String> getErrors() {
78
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
61 return errors;
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
62 }
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
63
79
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
64 public void setErrors(Map<String, Integer> errors) {
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
65 this.errors = this.convertCodes(errors);
78
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
66 }
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
67
79
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
68 public Map<String, String> getWarnings() {
78
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
69 return warnings;
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
70 }
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
71
79
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
72 public void setWarnings(Map<String, Integer> warnings) {
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
73 this.warnings = this.convertCodes(warnings);
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
74 }
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
75
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
76 private HashMap<String, String> convertCodes(Map<String, Integer> codes) {
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
77 HashMap<String, String> converted = new HashMap<String, String>();
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
78 if (codes == null || codes.isEmpty()) {
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
79 return converted;
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
80 }
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
81 for (Map.Entry<String, Integer> entry: codes.entrySet()) {
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
82 converted.put(entry.getKey(), Integer.toString(entry.getValue()));
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
83 }
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
84 return converted;
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
85 }
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
86
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
87 /* Currently unused but might be helpfull later */
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
88 private String codes2string(Map<String, Integer> codes) {
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
89 String response = "{";
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
90 if (codes == null || codes.isEmpty()) {
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
91 response += "}";
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
92 return response;
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
93 }
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
94 boolean first = true;
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
95 for (Map.Entry<String, Integer> entry: codes.entrySet()) {
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
96 if (!first) {
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
97 response +=",";
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
98 }
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
99 response += entry.getKey() + ":" + "\"" + entry.getValue() + "\"";
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
100 first = false;
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
101 }
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
102 response += "}";
bb22b5063a94 Handle warnings and errors in the response class.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 78
diff changeset
103 return response;
78
98fe21a9fbe5 Formatting. Make the data attribute of type Object to make it more general.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
104 }
75
b43b76fc0525 Added Response class to wrap the returned data into a response object which
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
105 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)