1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package cz.hobrasoft.pdfmu.operation;
18
19 import cz.hobrasoft.pdfmu.PdfmuUtils;
20 import cz.hobrasoft.pdfmu.WritingMapper;
21 import cz.hobrasoft.pdfmu.error.ErrorType;
22 import cz.hobrasoft.pdfmu.jackson.RpcError;
23 import cz.hobrasoft.pdfmu.jackson.RpcError.Data;
24 import cz.hobrasoft.pdfmu.jackson.RpcResponse;
25 import java.io.IOException;
26 import java.util.Map;
27 import java.util.logging.Logger;
28 import org.apache.commons.lang3.text.StrSubstitutor;
29
30
31
32
33
34
35
36
37 public class OperationException extends Exception {
38
39 private static final Logger logger = Logger.getLogger(OperationException.class.getName());
40
41
42 private static final int defaultErrorCode = -1;
43
44 private ErrorType errorType = null;
45 private Map<String, Object> messageArguments = null;
46
47 public OperationException(ErrorType errorType) {
48 super(errorType.toString());
49 init(errorType, null);
50 }
51
52 public OperationException(ErrorType errorType, Map.Entry<String, Object>... entries) {
53 super(errorType.toString());
54 init(errorType, PdfmuUtils.sortedMap(entries));
55 }
56
57 public OperationException(ErrorType errorType, Map<String, Object> messageArguments) {
58 super(errorType.toString());
59 init(errorType, messageArguments);
60 }
61
62 public OperationException(ErrorType errorType, Throwable cause) {
63 super(errorType.toString(), cause);
64 init(errorType, null);
65 }
66
67 public OperationException(ErrorType errorType, Throwable cause, Map.Entry<String, Object>... entries) {
68 super(errorType.toString(), cause);
69 init(errorType, PdfmuUtils.sortedMap(entries));
70 }
71
72
73
74
75
76
77
78
79
80 public OperationException(ErrorType errorType, Throwable cause, Map<String, Object> messageArguments) {
81 super(errorType.toString(), cause);
82 init(errorType, messageArguments);
83 }
84
85 private void init(ErrorType errorType, Map<String, Object> messageArguments) {
86 assert errorType != null;
87 this.errorType = errorType;
88 this.messageArguments = messageArguments;
89 }
90
91
92
93
94
95
96
97
98
99 public int getCode() {
100 if (errorType != null) {
101 return errorType.getCode();
102 } else {
103 return defaultErrorCode;
104 }
105 }
106
107 @Override
108 public String getLocalizedMessage() {
109 if (errorType != null) {
110 String pattern = errorType.getMessagePattern();
111 if (pattern != null) {
112 StrSubstitutor sub = new StrSubstitutor(messageArguments);
113 return sub.replace(pattern);
114 } else {
115 return super.getLocalizedMessage();
116 }
117 } else {
118 return super.getLocalizedMessage();
119 }
120 }
121
122 private RpcError getRpcError() {
123 RpcError re = new RpcError(getCode(), getLocalizedMessage());
124 Throwable cause = getCause();
125 if (cause != null || messageArguments != null) {
126 re.data = new Data();
127 if (cause != null) {
128 re.data.causeClass = cause.getClass();
129 re.data.causeMessage = cause.getLocalizedMessage();
130 }
131 re.data.arguments = messageArguments;
132 }
133 return re;
134 }
135
136 public void writeInWritingMapper(WritingMapper wm) {
137 RpcResponse response = new RpcResponse(getRpcError());
138
139 try {
140 wm.writeValue(response);
141 } catch (IOException ex) {
142 logger.severe("Could not write JSON output.");
143 }
144 }
145 }