XAPIErrorInfo(HttpStatus,HttpServletRequest,Exception)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
1
/**
2
 * Copyright 2014 Unicon (R) Licensed under the
3
 * Educational Community License, Version 2.0 (the "License"); you may
4
 * not use this file except in compliance with the License. You may
5
 * obtain a copy of the License at
6
 *
7
 * http://www.osedu.org/licenses/ECL-2.0
8
9
 * Unless required by applicable law or agreed to in writing,
10
 * software distributed under the License is distributed on an "AS IS"
11
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12
 * or implied. See the License for the specific language governing
13
 * permissions and limitations under the License.
14
 *
15
 */
16
package unicon.matthews.xapi.endpoint;
17
18
import java.util.ArrayList;
19
import java.util.List;
20
import java.util.Map;
21
22
import javax.servlet.http.HttpServletRequest;
23
24
import org.apache.commons.lang3.builder.ToStringBuilder;
25
import org.apache.commons.lang3.exception.ExceptionUtils;
26
import org.springframework.http.HttpStatus;
27
28
import com.fasterxml.jackson.annotation.JsonProperty;
29
30
/**
31
 * Class used to hold info for a XAPI-call-related error.  This object will be JSONified and returned as the XAPI call response.
32
 * @author Gary Roybal, [email protected]
33
 */
34
public class XAPIErrorInfo {
35
36
    @JsonProperty private int status;
37
    @JsonProperty private String reason;
38
    @JsonProperty private String method;
39
    @JsonProperty private String path;
40
    @JsonProperty private Map<String, String[]> parameters;
41
    @JsonProperty private List<String> messages = new ArrayList<String>();
42
43
    public XAPIErrorInfo(final HttpStatus status, final HttpServletRequest request) {
44
        this.getDataFromHttpStatus(status);
45
        this.getDataFromRequest(request);
46
    }
47
48
    public XAPIErrorInfo(final HttpStatus status, final HttpServletRequest request, final Exception resultingException) {
49
        this(status, request);
50
        this.messages.add(ExceptionUtils.getRootCauseMessage(resultingException));
51
    }
52
53
    public XAPIErrorInfo(final HttpStatus status, final HttpServletRequest request, final String errorMessage) {
54
        this(status, request);
55
        if (errorMessage != null) {
56
            this.messages.add(errorMessage);
57
        }
58
    }
59
60
    public XAPIErrorInfo(final HttpStatus status, final HttpServletRequest request, final List<String> errorMessages) {
61
        this(status, request);
62
        if (errorMessages != null) {
63
            this.messages.addAll(errorMessages);
64
        }
65
    }
66
67
    public void overrideReason(final String reason) {
68
        this.reason = reason;
69
    }
70
71
    @Override
72
    public String toString() {
73
        return ToStringBuilder.reflectionToString(this);
74
    }
75
76
    private void getDataFromHttpStatus(final HttpStatus status) {
77
        this.status = status.value();
78
        this.reason = status.getReasonPhrase();
79
    }
80
81
    private void getDataFromRequest(final HttpServletRequest request) {
82
        this.path = request.getRequestURI();
83
        this.method = request.getMethod();
84
        this.parameters = request.getParameterMap();
85
    }
86
87
}
88