1 | /** |
---|
2 | * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. |
---|
3 | * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior |
---|
4 | * |
---|
5 | * This program is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU Affero General Public License as |
---|
7 | * published by the Free Software Foundation, either version 3 of the |
---|
8 | * License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU Affero General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Affero General Public License |
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | |
---|
19 | /******************************************************************************\ |
---|
20 | * |
---|
21 | * This product was developed by |
---|
22 | * |
---|
23 | * SERVIÃO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), |
---|
24 | * |
---|
25 | * a government company established under Brazilian law (5.615/70), |
---|
26 | * at Department of Development of Porto Alegre. |
---|
27 | * |
---|
28 | \******************************************************************************/ |
---|
29 | |
---|
30 | package serpro.mailarchiver.service.web; |
---|
31 | |
---|
32 | import java.util.HashMap; |
---|
33 | import java.util.Map; |
---|
34 | |
---|
35 | import javax.xml.bind.annotation.XmlTransient; |
---|
36 | import javax.xml.soap.SOAPException; |
---|
37 | import javax.xml.soap.SOAPFactory; |
---|
38 | import javax.xml.soap.SOAPFault; |
---|
39 | import javax.xml.ws.WebFault; |
---|
40 | import javax.xml.ws.soap.SOAPFaultException; |
---|
41 | |
---|
42 | import org.apache.commons.lang3.exception.ExceptionUtils; |
---|
43 | |
---|
44 | import static com.google.common.base.Strings.*; |
---|
45 | |
---|
46 | import serpro.mailarchiver.service.dto.TFault; |
---|
47 | |
---|
48 | @WebFault(name="Fault") |
---|
49 | public class ServiceFault extends Exception { |
---|
50 | |
---|
51 | private TFault fault; |
---|
52 | |
---|
53 | @XmlTransient |
---|
54 | @Override |
---|
55 | public StackTraceElement[] getStackTrace() { |
---|
56 | return super.getStackTrace(); |
---|
57 | } |
---|
58 | |
---|
59 | @XmlTransient |
---|
60 | @Override |
---|
61 | public Throwable getCause() { |
---|
62 | return super.getCause(); |
---|
63 | } |
---|
64 | |
---|
65 | public TFault getFaultInfo() { |
---|
66 | return fault; |
---|
67 | } |
---|
68 | |
---|
69 | public ServiceFault(String message) { |
---|
70 | super(message); |
---|
71 | } |
---|
72 | |
---|
73 | public ServiceFault(String message, TFault fault) { |
---|
74 | super(message); |
---|
75 | this.fault = fault; |
---|
76 | } |
---|
77 | |
---|
78 | public ServiceFault(String message, TFault fault, Throwable cause) { |
---|
79 | super(message, cause); |
---|
80 | this.fault = fault; |
---|
81 | } |
---|
82 | |
---|
83 | //-------------------------------------------------------------------------- |
---|
84 | public static Config invalidFolderName() { |
---|
85 | return new Config("Client", "INVALID_FOLDER_NAME"); |
---|
86 | } |
---|
87 | |
---|
88 | public static Config folderNameAlreadyExists() { |
---|
89 | return new Config("Client", "FOLDER_NAME_ALREADY_EXISTS"); |
---|
90 | } |
---|
91 | |
---|
92 | public static Config invalidFolderPath() { |
---|
93 | return new Config("Client", "INVALID_FOLDER_PATH"); |
---|
94 | } |
---|
95 | |
---|
96 | public static Config invalidFolderId() { |
---|
97 | return new Config("Client", "INVALID_FOLDER_ID"); |
---|
98 | } |
---|
99 | |
---|
100 | public static Config invalidMessageId() { |
---|
101 | return new Config("Client", "INVALID_MESSAGE_ID"); |
---|
102 | } |
---|
103 | |
---|
104 | public static Config invalidBodyId() { |
---|
105 | return new Config("Client", "INVALID_BODY_ID"); |
---|
106 | } |
---|
107 | |
---|
108 | public static Config invalidQueryConfig() { |
---|
109 | return new Config("Client", "INVALID_QUERY_CONFIG"); |
---|
110 | } |
---|
111 | |
---|
112 | public static Config invalidTagConfig() { |
---|
113 | return new Config("Client", "INVALID_TAG_CONFIG"); |
---|
114 | } |
---|
115 | |
---|
116 | public static Config invalidZipConfig() { |
---|
117 | return new Config("Client", "INVALID_ZIP_CONFIG"); |
---|
118 | } |
---|
119 | |
---|
120 | public static Config unsupportedArchiveFormat() { |
---|
121 | return new Config("Client", "UNSUPPORTED_ARCHIVE_FORMAT"); |
---|
122 | } |
---|
123 | |
---|
124 | public static Config folderNotFound() { |
---|
125 | return new Config("Client", "FOLDER_NOT_FOUND"); |
---|
126 | } |
---|
127 | |
---|
128 | public static Config folderNotEmpty() { |
---|
129 | return new Config("Client", "FOLDER_NOT_EMPTY"); |
---|
130 | } |
---|
131 | |
---|
132 | public static Config fileSystemFolderNotEmpty() { |
---|
133 | return new Config("Server", "FILE_SYSTEM_FOLDER_NOT_EMPTY"); |
---|
134 | } |
---|
135 | |
---|
136 | public static Config messageNotFound() { |
---|
137 | return new Config("Client", "MESSAGE_NOT_FOUND"); |
---|
138 | } |
---|
139 | |
---|
140 | public static Config binaryBodyNotFound() { |
---|
141 | return new Config("Client", "BINARY_BODY_NOT_FOUND"); |
---|
142 | } |
---|
143 | |
---|
144 | public static Config moreThanOneFolderFound() { |
---|
145 | return new Config("Server", "MORE_THAN_ONE_FOLDER_FOUND"); |
---|
146 | } |
---|
147 | |
---|
148 | public static Config expungedMessage() { |
---|
149 | return new Config("Server", "EXPUNGED_MESSAGE"); |
---|
150 | } |
---|
151 | |
---|
152 | public static Config archiveFailure() { |
---|
153 | return new Config("Server", "ARCHIVE_FAILURE"); |
---|
154 | } |
---|
155 | |
---|
156 | public static Config fileSystemFailure() { |
---|
157 | return new Config("Server", "FILE_SYSTEM_FAILURE"); |
---|
158 | } |
---|
159 | |
---|
160 | public static Config luceneFailure() { |
---|
161 | return new Config("Server", "LUCENE_FAILURE"); |
---|
162 | } |
---|
163 | |
---|
164 | public static Config loginFailure() { |
---|
165 | return new Config("Server", "LOGIN_FAILURE"); |
---|
166 | } |
---|
167 | |
---|
168 | public static Config runtimeException() { |
---|
169 | return new Config("Server", "RUNTIME_EXCEPTION"); |
---|
170 | } |
---|
171 | |
---|
172 | public static Config fakeException() { |
---|
173 | return new Config("Server", "FAKE_EXCEPTION"); |
---|
174 | } |
---|
175 | |
---|
176 | public static class Config { |
---|
177 | |
---|
178 | private final String code; |
---|
179 | private final String subCode; |
---|
180 | |
---|
181 | private String message; |
---|
182 | private String actor; |
---|
183 | private Throwable cause; |
---|
184 | |
---|
185 | private Map<String, String> context = new HashMap<String, String>(); |
---|
186 | |
---|
187 | private Config(String code, String subCode) { |
---|
188 | this.code = code; |
---|
189 | this.subCode = subCode; |
---|
190 | } |
---|
191 | |
---|
192 | public Config setMessage(String message) { |
---|
193 | this.message = message; |
---|
194 | return this; |
---|
195 | } |
---|
196 | |
---|
197 | public Config setActor(String actor) { |
---|
198 | this.actor = actor; |
---|
199 | return this; |
---|
200 | } |
---|
201 | |
---|
202 | public Config setCause(Throwable cause) { |
---|
203 | this.cause = cause; |
---|
204 | return this; |
---|
205 | } |
---|
206 | |
---|
207 | public Config addValue(String key, Object value) { |
---|
208 | context.put(key, value.toString()); |
---|
209 | return this; |
---|
210 | } |
---|
211 | |
---|
212 | public void raise() throws ServiceFault { |
---|
213 | throw create(); |
---|
214 | } |
---|
215 | |
---|
216 | public ServiceFault create() { |
---|
217 | |
---|
218 | TFault fault = new TFault(); |
---|
219 | |
---|
220 | if(code != null) { |
---|
221 | fault.setSoapFaultCode(code); |
---|
222 | } |
---|
223 | if(subCode != null) { |
---|
224 | fault.setSoapFaultSubCode(subCode); |
---|
225 | } |
---|
226 | if(message != null) { |
---|
227 | fault.setSoapFaultString(message); |
---|
228 | } |
---|
229 | if(actor != null) { |
---|
230 | fault.setSoapFaultActor(actor); |
---|
231 | } |
---|
232 | |
---|
233 | fault.setContext(context); |
---|
234 | |
---|
235 | if(cause != null) { |
---|
236 | fault.setCauseClass(cause.getClass().getName()); |
---|
237 | fault.setCauseMessage(cause.getLocalizedMessage()); |
---|
238 | fault.setCauseStackTrace(ExceptionUtils.getStackTrace(cause)); |
---|
239 | } |
---|
240 | |
---|
241 | SOAPFault soapFault = null; |
---|
242 | try { |
---|
243 | soapFault = SOAPFactory.newInstance().createFault(); |
---|
244 | if(code != null) { |
---|
245 | soapFault.setFaultCode(code + "." + subCode); |
---|
246 | } |
---|
247 | if(message != null) { |
---|
248 | soapFault.setFaultString(message); |
---|
249 | } |
---|
250 | else { |
---|
251 | soapFault.setFaultString(subCode); |
---|
252 | } |
---|
253 | if(actor != null) { |
---|
254 | soapFault.setFaultActor(actor); |
---|
255 | } |
---|
256 | } |
---|
257 | catch(SOAPException ex) { |
---|
258 | ex.printStackTrace(); |
---|
259 | } |
---|
260 | |
---|
261 | ServiceFault e = new ServiceFault(nullToEmpty(message), fault, new SOAPFaultException(soapFault)); |
---|
262 | |
---|
263 | fault.setStackTrace(ExceptionUtils.getStackTrace(e)); |
---|
264 | |
---|
265 | return e; |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|