View Javadoc
1   /*
2    * Copyright 2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.xds.core;
17  
18  import org.apache.commons.lang3.Validate;
19  import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorCode;
20  import org.openehealth.ipf.commons.ihe.xds.core.responses.Severity;
21  
22  /**
23   * @author Dmytro Rud
24   */
25  public class XdsRuntimeException extends RuntimeException {
26      private static final long serialVersionUID = -5969918509312648991L;
27  
28      private final ErrorCode errorCode;
29      private final String codeContext;
30      private final Severity severity;
31      private final String location;
32  
33      public XdsRuntimeException(
34              ErrorCode errorCode,
35              String codeContext,
36              Severity severity,
37              String location)
38      {
39          super();
40          this.errorCode   = Validate.notNull(errorCode);
41          this.codeContext = Validate.notEmpty(codeContext);
42          this.severity    = Validate.notNull(severity);
43          this.location    = location;
44      }
45  
46      public ErrorCode getErrorCode() {
47          return errorCode;
48      }
49  
50      public String getCodeContext() {
51          return codeContext;
52      }
53  
54      public Severity getSeverity() {
55          return severity;
56      }
57  
58      public String getLocation() {
59          return location;
60      }
61  
62      @Override
63      public String getMessage() {
64          StringBuilder sb = new StringBuilder()
65                  .append((severity == Severity.ERROR) ? "Error " : "Warning ")
66                  .append(errorCode.getOpcode())
67                  .append(": ")
68                  .append(codeContext);
69          if (location != null) {
70              sb.append(". Location: ").append(location);
71          }
72          return sb.toString();
73      }
74  }