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 org.apache.http.HttpResponse;
025
026import com.fasterxml.jackson.dataformat.xml.XmlMapper;
027import com.google.common.base.Objects;
028
029import java.io.IOException;
030
031@XmlRootElement(name = "error")
032public class RecurlyAPIError extends RecurlyObject {
033
034    @XmlTransient
035    private ResponseMetadata responseMetadata;
036
037    @XmlElement(name = "description")
038    private String description;
039
040    @XmlElement(name = "symbol")
041    private String symbol;
042
043    @XmlElement(name = "details")
044    private String details;
045
046    private int httpStatusCode;
047
048    public static RecurlyAPIError buildFromResponse(final HttpResponse response) {
049        final RecurlyAPIError recurlyAPIError = new RecurlyAPIError();
050        recurlyAPIError.setResponse(response);
051        return recurlyAPIError;
052    }
053
054    public static RecurlyAPIError buildFromXml(final XmlMapper xmlMapper, final String payload, final HttpResponse response) throws IOException {
055        final RecurlyAPIError recurlyAPIError = xmlMapper.readValue(payload, RecurlyAPIError.class);
056        recurlyAPIError.setResponse(response);
057        return recurlyAPIError;
058    }
059
060    public String getDescription() {
061        return description;
062    }
063
064    public void setDescription(final String description) {
065        this.description = description;
066    }
067
068    public String getSymbol() {
069        return symbol;
070    }
071
072    public void setSymbol(final String symbol) {
073        this.symbol = symbol;
074    }
075
076    public String getDetails() {
077        return details;
078    }
079
080    public void setDetails(final String details) {
081        this.details = details;
082    }
083
084    public void setHttpStatusCode(final int httpStatusCode) {
085      this.httpStatusCode = httpStatusCode;
086    }
087
088    public int getHttpStatusCode() { return this.httpStatusCode; }
089
090    public void setResponseMetadata(final ResponseMetadata meta) {
091        this.responseMetadata = meta;
092    }
093
094    public ResponseMetadata getResponseMetadata() {
095        return this.responseMetadata;
096    }
097
098    protected void setResponse(final HttpResponse response) {
099        this.setHttpStatusCode(response.getStatusLine().getStatusCode());
100        this.setResponseMetadata(new ResponseMetadata(response));
101    }
102
103    @Override
104    public String toString() {
105        final StringBuilder sb = new StringBuilder("RecurlyAPIError{");
106        sb.append("description='").append(description).append('\'');
107        sb.append(", symbol='").append(symbol).append('\'');
108        sb.append(", details='").append(details).append('\'');
109        sb.append(", httpStatusCode='").append(httpStatusCode).append('\'');
110        sb.append(", responseMetadata='").append(responseMetadata).append('\'');
111        sb.append('}');
112        return sb.toString();
113    }
114
115    @Override
116    public boolean equals(final Object o) {
117        if (this == o) return true;
118        if (o == null || getClass() != o.getClass()) return false;
119
120        final RecurlyAPIError that = (RecurlyAPIError) o;
121
122        if (description != null ? !description.equals(that.description) : that.description != null) {
123            return false;
124        }
125        if (details != null ? !details.equals(that.details) : that.details != null) {
126            return false;
127        }
128        if (symbol != null ? !symbol.equals(that.symbol) : that.symbol != null) {
129
130            return false;
131        }
132        if (responseMetadata != null ? !responseMetadata.equals(that.responseMetadata) : that.responseMetadata != null) {
133            return false;
134        }
135
136        return true;
137    }
138
139    @Override
140    public int hashCode() {
141        return Objects.hashCode(
142                description,
143                symbol,
144                details,
145                httpStatusCode,
146                responseMetadata
147        );
148    }
149}