001/*
002 * Copyright 2010-2014 Ning, Inc.
003 * Copyright 2014-2015 The Billing Project, LLC
004 *
005 * The Billing Project licenses this file to you under the Apache License, version 2.0
006 * (the "License"); you may not use this file except in compliance with the
007 * License.  You may obtain a copy of the License at:
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
014 * License for the specific language governing permissions and limitations
015 * under the License.
016 */
017
018package com.ning.billing.recurly.model;
019
020import javax.xml.bind.annotation.XmlElement;
021import javax.xml.bind.annotation.XmlRootElement;
022import javax.xml.bind.annotation.XmlTransient;
023
024import com.fasterxml.jackson.dataformat.xml.XmlMapper;
025import com.google.common.base.Objects;
026import com.ning.http.client.Response;
027
028import java.io.IOException;
029
030@XmlRootElement(name = "error")
031public class RecurlyAPIError extends RecurlyObject {
032
033    @XmlTransient
034    private ResponseMetadata responseMetadata;
035
036    @XmlElement(name = "description")
037    private String description;
038
039    @XmlElement(name = "symbol")
040    private String symbol;
041
042    @XmlElement(name = "details")
043    private String details;
044
045    private int httpStatusCode;
046
047    public static RecurlyAPIError buildFromResponse(final Response response) {
048        final RecurlyAPIError recurlyAPIError = new RecurlyAPIError();
049        recurlyAPIError.setResponse(response);
050        return recurlyAPIError;
051    }
052
053    public static RecurlyAPIError buildFromXml(final XmlMapper xmlMapper, final String payload, final Response response) throws IOException {
054        final RecurlyAPIError recurlyAPIError = xmlMapper.readValue(payload, RecurlyAPIError.class);
055        recurlyAPIError.setResponse(response);
056        return recurlyAPIError;
057    }
058
059    public String getDescription() {
060        return description;
061    }
062
063    public void setDescription(final String description) {
064        this.description = description;
065    }
066
067    public String getSymbol() {
068        return symbol;
069    }
070
071    public void setSymbol(final String symbol) {
072        this.symbol = symbol;
073    }
074
075    public String getDetails() {
076        return details;
077    }
078
079    public void setDetails(final String details) {
080        this.details = details;
081    }
082
083    public void setHttpStatusCode(final int httpStatusCode) {
084      this.httpStatusCode = httpStatusCode;
085    }
086
087    public int getHttpStatusCode() { return this.httpStatusCode; }
088
089    public void setResponseMetadata(final ResponseMetadata meta) {
090        this.responseMetadata = meta;
091    }
092
093    public ResponseMetadata getResponseMetadata() {
094        return this.responseMetadata;
095    }
096
097    protected void setResponse(final Response response) {
098        this.setHttpStatusCode(response.getStatusCode());
099        this.setResponseMetadata(new ResponseMetadata(response));
100    }
101
102    @Override
103    public String toString() {
104        final StringBuilder sb = new StringBuilder("RecurlyAPIError{");
105        sb.append("description='").append(description).append('\'');
106        sb.append(", symbol='").append(symbol).append('\'');
107        sb.append(", details='").append(details).append('\'');
108        sb.append(", httpStatusCode='").append(httpStatusCode).append('\'');
109        sb.append(", responseMetadata='").append(responseMetadata).append('\'');
110        sb.append('}');
111        return sb.toString();
112    }
113
114    @Override
115    public boolean equals(final Object o) {
116        if (this == o) return true;
117        if (o == null || getClass() != o.getClass()) return false;
118
119        final RecurlyAPIError that = (RecurlyAPIError) o;
120
121        if (description != null ? !description.equals(that.description) : that.description != null) {
122            return false;
123        }
124        if (details != null ? !details.equals(that.details) : that.details != null) {
125            return false;
126        }
127        if (symbol != null ? !symbol.equals(that.symbol) : that.symbol != null) {
128
129            return false;
130        }
131        if (responseMetadata != null ? !responseMetadata.equals(that.responseMetadata) : that.responseMetadata != null) {
132            return false;
133        }
134
135        return true;
136    }
137
138    @Override
139    public int hashCode() {
140        return Objects.hashCode(
141                description,
142                symbol,
143                details,
144                httpStatusCode,
145                responseMetadata
146        );
147    }
148}