View Javadoc
1   /* 
2    * Copyright (C) 2016 Hobrasoft s.r.o.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   * This exception is thrown by {@link Operation#execute(Namespace)} to notify
32   * {@link cz.hobrasoft.pdfmu.Main#main(String[])} that the operation has
33   * encountered a condition it cannot recover from.
34   *
35   * @author <a href="mailto:filip.bartek@hobrasoft.cz">Filip Bartek</a>
36   */
37  public class OperationException extends Exception {
38  
39      private static final Logger logger = Logger.getLogger(OperationException.class.getName());
40  
41      // Configuration
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       * Creates a chained operation exception with error identifier and message
74       * arguments.
75       *
76       * @param errorType the error identifier.
77       * @param cause the original cause.
78       * @param messageArguments the arguments of the message.
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       * Returns the error code associated with this exception
93       *
94       * <p>
95       * The code should uniquely identify the error that caused the exception.
96       *
97       * @return the error code associated with this exception
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 }