001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF 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 007 * the 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, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.bean.jsonapi; 018 019import static org.apache.juneau.commons.utils.CollectionUtils.*; 020import static org.apache.juneau.commons.utils.Shorts.*; 021 022import java.util.*; 023 024import org.apache.juneau.commons.bean.*; 025import org.apache.juneau.marshall.*; 026import org.apache.juneau.marshall.marshaller.*; 027 028/** 029 * Represents a JSON:API Error Object as defined by 030 * <a href="https://jsonapi.org/format/#error-objects">JSON:API v1.1 § Error Objects</a>. 031 * 032 * <h5 class='section'>See Also:</h5><ul> 033 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanJsonApi">juneau-bean-jsonapi</a> 034 * </ul> 035 */ 036@Marshalled 037public class JsonApiError { 038 039 private String id; 040 private Map<String,Object> links; 041 private String status; 042 private String code; 043 private String title; 044 private String detail; 045 private JsonApiErrorSource source; 046 private Map<String,Object> meta; 047 048 /** 049 * Default constructor. 050 */ 051 public JsonApiError() { /* intentionally empty */ } 052 053 /** 054 * Bean property getter: <property>id</property>. 055 * 056 * @return The value of the <property>id</property> property, or <jk>null</jk> if it is not set. 057 */ 058 public String getId() { return id; } 059 060 /** 061 * Bean property setter: <property>id</property>. 062 * 063 * @param value The new value for this property. Can be <jk>null</jk> to unset the property. 064 * @return This object. 065 */ 066 public JsonApiError setId(String value) { 067 id = value; 068 return this; 069 } 070 071 /** 072 * Bean property getter: <property>links</property>. 073 * 074 * <p> 075 * Per spec, each {@code links} value can be either a JSON string URL or a {@link JsonApiLink} object. 076 * 077 * @return The value of the <property>links</property> property, or <jk>null</jk> if it is not set. 078 */ 079 @BeanProp(value="links") 080 @Swap(JsonApiLinkOrStringSwap.class) 081 public Map<String,Object> getLinks() { return u(links); } 082 083 /** 084 * Bean property setter: <property>links</property>. 085 * 086 * @param value The new value for this property. Can be <jk>null</jk> to unset the property. 087 * @return This object. 088 */ 089 @BeanProp(value="links") 090 @Swap(JsonApiLinkOrStringSwap.class) 091 public JsonApiError setLinks(Map<String,Object> value) { 092 links = value; 093 return this; 094 } 095 096 /** 097 * Convenience method to add a single link. 098 * 099 * @param name The link name. Can be <jk>null</jk> ({@link LinkedHashMap} tolerates a <jk>null</jk> key). 100 * @param value The link value (a {@link String} URL or a {@link JsonApiLink} object). Can be <jk>null</jk> (stored as <jk>null</jk>). 101 * @return This object. 102 */ 103 public JsonApiError putLink(String name, Object value) { 104 if (links == null) 105 links = map(); 106 links.put(name, value); 107 return this; 108 } 109 110 /** 111 * Bean property getter: <property>status</property>. 112 * 113 * <p> 114 * Per spec, the HTTP status code as a string. 115 * 116 * @return The value of the <property>status</property> property, or <jk>null</jk> if it is not set. 117 */ 118 public String getStatus() { return status; } 119 120 /** 121 * Bean property setter: <property>status</property>. 122 * 123 * @param value The new value for this property. Can be <jk>null</jk> to unset the property. 124 * @return This object. 125 */ 126 public JsonApiError setStatus(String value) { 127 status = value; 128 return this; 129 } 130 131 /** 132 * Bean property getter: <property>code</property>. 133 * 134 * <p> 135 * An application-specific error code. 136 * 137 * @return The value of the <property>code</property> property, or <jk>null</jk> if it is not set. 138 */ 139 public String getCode() { return code; } 140 141 /** 142 * Bean property setter: <property>code</property>. 143 * 144 * @param value The new value for this property. Can be <jk>null</jk> to unset the property. 145 * @return This object. 146 */ 147 public JsonApiError setCode(String value) { 148 code = value; 149 return this; 150 } 151 152 /** 153 * Bean property getter: <property>title</property>. 154 * 155 * @return The value of the <property>title</property> property, or <jk>null</jk> if it is not set. 156 */ 157 public String getTitle() { return title; } 158 159 /** 160 * Bean property setter: <property>title</property>. 161 * 162 * @param value The new value for this property. Can be <jk>null</jk> to unset the property. 163 * @return This object. 164 */ 165 public JsonApiError setTitle(String value) { 166 title = value; 167 return this; 168 } 169 170 /** 171 * Bean property getter: <property>detail</property>. 172 * 173 * @return The value of the <property>detail</property> property, or <jk>null</jk> if it is not set. 174 */ 175 public String getDetail() { return detail; } 176 177 /** 178 * Bean property setter: <property>detail</property>. 179 * 180 * @param value The new value for this property. Can be <jk>null</jk> to unset the property. 181 * @return This object. 182 */ 183 public JsonApiError setDetail(String value) { 184 detail = value; 185 return this; 186 } 187 188 /** 189 * Bean property getter: <property>source</property>. 190 * 191 * @return The value of the <property>source</property> property, or <jk>null</jk> if it is not set. 192 */ 193 public JsonApiErrorSource getSource() { return source; } 194 195 /** 196 * Bean property setter: <property>source</property>. 197 * 198 * @param value The new value for this property. Can be <jk>null</jk> to unset the property. 199 * @return This object. 200 */ 201 public JsonApiError setSource(JsonApiErrorSource value) { 202 source = value; 203 return this; 204 } 205 206 /** 207 * Bean property getter: <property>meta</property>. 208 * 209 * @return The value of the <property>meta</property> property, or <jk>null</jk> if it is not set. 210 */ 211 public Map<String,Object> getMeta() { return u(meta); } 212 213 /** 214 * Bean property setter: <property>meta</property>. 215 * 216 * @param value The new value for this property. Can be <jk>null</jk> to unset the property. 217 * @return This object. 218 */ 219 public JsonApiError setMeta(Map<String,Object> value) { 220 meta = value; 221 return this; 222 } 223 224 /** 225 * Convenience method to add a single meta entry. 226 * 227 * @param name The meta key. Can be <jk>null</jk> ({@link LinkedHashMap} tolerates a <jk>null</jk> key). 228 * @param value The meta value. Can be <jk>null</jk> (stored as <jk>null</jk>). 229 * @return This object. 230 */ 231 public JsonApiError putMeta(String name, Object value) { 232 if (meta == null) 233 meta = map(); 234 meta.put(name, value); 235 return this; 236 } 237 238 @Override /* Overridden from Object */ 239 public String toString() { 240 return Json.of(this); 241 } 242}