001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http.response;
014
015import static org.apache.juneau.http.response.TooManyRequests.*;
016
017import java.text.*;
018import java.util.*;
019
020import org.apache.http.*;
021import org.apache.http.Header;
022import org.apache.juneau.annotation.*;
023import org.apache.juneau.http.*;
024import org.apache.juneau.http.annotation.*;
025import org.apache.juneau.http.header.*;
026import org.apache.juneau.internal.*;
027
028/**
029 * Exception representing an HTTP 429 (Too Many Requests).
030 *
031 * <p>
032 * The user has sent too many requests in a given amount of time.
033 * <br>Intended for use with rate-limiting schemes.
034 *
035 * <h5 class='section'>See Also:</h5><ul>
036 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
037 * </ul>
038 *
039 * @serial exclude
040 */
041@Response
042@StatusCode(STATUS_CODE)
043@Schema(description=REASON_PHRASE)
044@FluentSetters
045public class TooManyRequests extends BasicHttpException {
046   private static final long serialVersionUID = 1L;
047
048   /** HTTP status code */
049   public static final int STATUS_CODE = 429;
050
051   /** Reason phrase */
052   public static final String REASON_PHRASE = "Too Many Requests";
053
054   /** Default status line */
055   private static final BasicStatusLine STATUS_LINE = BasicStatusLine.create(STATUS_CODE, REASON_PHRASE);
056
057   /** Reusable unmodifiable instance */
058   public static final TooManyRequests INSTANCE = new TooManyRequests().setUnmodifiable();
059
060   /**
061    * Constructor.
062    *
063    * @param cause The caused-by exception.  Can be <jk>null</jk>.
064    * @param msg The message.  Can be <jk>null</jk>.
065    * @param args The message arguments.
066    */
067   public TooManyRequests(Throwable cause, String msg, Object...args) {
068      super(STATUS_CODE, cause, msg, args);
069      setStatusLine(STATUS_LINE.copy());
070   }
071
072   /**
073    * Constructor.
074    */
075   public TooManyRequests() {
076      this((Throwable)null, REASON_PHRASE);
077   }
078
079   /**
080    * Constructor.
081    *
082    * @param msg The message.  Can be <jk>null</jk>.
083    * @param args Optional {@link MessageFormat}-style arguments in the message.
084    */
085   public TooManyRequests(String msg, Object...args) {
086      this((Throwable)null, msg, args);
087   }
088
089   /**
090    * Constructor.
091    *
092    * @param cause The cause.  Can be <jk>null</jk>.
093    */
094   public TooManyRequests(Throwable cause) {
095      this(cause, cause == null ? REASON_PHRASE : cause.getMessage());
096   }
097
098   /**
099    * Constructor.
100    *
101    * <p>
102    * This is the constructor used when parsing an HTTP response.
103    *
104    * @param response The HTTP response to copy from.  Must not be <jk>null</jk>.
105    * @throws AssertionError If HTTP response status code does not match what was expected.
106    */
107   public TooManyRequests(HttpResponse response) {
108      super(response);
109      assertStatusCode(response);
110   }
111
112   /**
113    * Copy constructor.
114    *
115    * @param copyFrom The bean to copy.
116    */
117   protected TooManyRequests(TooManyRequests copyFrom) {
118      super(copyFrom);
119   }
120
121   /**
122    * Creates a modifiable copy of this bean.
123    *
124    * @return A new modifiable bean.
125    */
126   public TooManyRequests copy() {
127      return new TooManyRequests(this);
128   }
129
130   // <FluentSetters>
131
132   @Override /* GENERATED - org.apache.juneau.BasicRuntimeException */
133   public TooManyRequests setMessage(String message, Object...args) {
134      super.setMessage(message, args);
135      return this;
136   }
137
138   @Override /* GENERATED - org.apache.juneau.BasicRuntimeException */
139   public TooManyRequests setUnmodifiable() {
140      super.setUnmodifiable();
141      return this;
142   }
143
144   @Override /* GENERATED - org.apache.juneau.http.response.BasicHttpException */
145   public TooManyRequests setHeader2(String name, Object value) {
146      super.setHeader2(name, value);
147      return this;
148   }
149
150   @Override /* GENERATED - org.apache.juneau.http.response.BasicHttpException */
151   public TooManyRequests setHeaders(HeaderList value) {
152      super.setHeaders(value);
153      return this;
154   }
155
156   @Override /* GENERATED - org.apache.juneau.http.response.BasicHttpException */
157   public TooManyRequests setHeaders2(Header...values) {
158      super.setHeaders2(values);
159      return this;
160   }
161
162   @Override /* GENERATED - org.apache.juneau.http.response.BasicHttpException */
163   public TooManyRequests setLocale2(Locale value) {
164      super.setLocale2(value);
165      return this;
166   }
167
168   @Override /* GENERATED - org.apache.juneau.http.response.BasicHttpException */
169   public TooManyRequests setProtocolVersion(ProtocolVersion value) {
170      super.setProtocolVersion(value);
171      return this;
172   }
173
174   @Override /* GENERATED - org.apache.juneau.http.response.BasicHttpException */
175   public TooManyRequests setReasonPhrase2(String value) {
176      super.setReasonPhrase2(value);
177      return this;
178   }
179
180   @Override /* GENERATED - org.apache.juneau.http.response.BasicHttpException */
181   public TooManyRequests setReasonPhraseCatalog(ReasonPhraseCatalog value) {
182      super.setReasonPhraseCatalog(value);
183      return this;
184   }
185
186   @Override /* GENERATED - org.apache.juneau.http.response.BasicHttpException */
187   public TooManyRequests setStatusCode2(int code) throws IllegalStateException{
188      super.setStatusCode2(code);
189      return this;
190   }
191
192   @Override /* GENERATED - org.apache.juneau.http.response.BasicHttpException */
193   public TooManyRequests setStatusLine(BasicStatusLine value) {
194      super.setStatusLine(value);
195      return this;
196   }
197
198   // </FluentSetters>
199}