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.http.response;
018
019import static org.apache.juneau.http.response.BadRequest.*;
020
021import java.text.*;
022import java.util.*;
023
024import org.apache.http.*;
025import org.apache.http.Header;
026import org.apache.juneau.annotation.*;
027import org.apache.juneau.http.*;
028import org.apache.juneau.http.annotation.*;
029import org.apache.juneau.http.header.*;
030
031/**
032 * Exception representing an HTTP 400 (Bad Request).
033 *
034 * <p>
035 * The server cannot or will not process the request due to an apparent client error (e.g., malformed request syntax, size too large, invalid request message framing, or deceptive request routing).
036 *
037 * <h5 class='section'>See Also:</h5><ul>
038 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a>
039 * </ul>
040 *
041 * @serial exclude
042 */
043@Response
044@StatusCode(STATUS_CODE)
045@Schema(description = REASON_PHRASE)
046public class BadRequest extends BasicHttpException {
047   private static final long serialVersionUID = 1L;
048
049   /** HTTP status code */
050   public static final int STATUS_CODE = 400;
051
052   /** Reason phrase */
053   public static final String REASON_PHRASE = "Bad Request";
054
055   /** Default status line */
056   private static final BasicStatusLine STATUS_LINE = BasicStatusLine.create(STATUS_CODE, REASON_PHRASE);
057
058   /** Reusable unmodifiable instance */
059   public static final BadRequest INSTANCE = new BadRequest().setUnmodifiable();
060
061   /**
062    * Constructor.
063    */
064   public BadRequest() {
065      this((Throwable)null, REASON_PHRASE);
066   }
067
068   /**
069    * Constructor.
070    *
071    * <p>
072    * This is the constructor used when parsing an HTTP response.
073    *
074    * @param response The HTTP response to copy from.  Must not be <jk>null</jk>.
075    * @throws AssertionError If HTTP response status code does not match what was expected.
076    */
077   public BadRequest(HttpResponse response) {
078      super(response);
079      assertStatusCode(response);
080   }
081
082   /**
083    * Constructor.
084    *
085    * @param msg The message.  Can be <jk>null</jk>.
086    * @param args Optional {@link MessageFormat}-style arguments in the message.
087    */
088   public BadRequest(String msg, Object...args) {
089      this((Throwable)null, msg, args);
090   }
091
092   /**
093    * Constructor.
094    *
095    * @param cause The cause.  Can be <jk>null</jk>.
096    */
097   public BadRequest(Throwable cause) {
098      this(cause, cause == null ? REASON_PHRASE : cause.getMessage());
099   }
100
101   /**
102    * Constructor.
103    *
104    * @param cause The caused-by exception.  Can be <jk>null</jk>.
105    * @param msg The message.  Can be <jk>null</jk>.
106    * @param args The message arguments.
107    */
108   public BadRequest(Throwable cause, String msg, Object...args) {
109      super(STATUS_CODE, cause, msg, args);
110      setStatusLine(STATUS_LINE.copy());
111   }
112
113   /**
114    * Copy constructor.
115    *
116    * @param copyFrom The bean to copy.
117    */
118   protected BadRequest(BadRequest copyFrom) {
119      super(copyFrom);
120   }
121
122   /**
123    * Creates a modifiable copy of this bean.
124    *
125    * @return A new modifiable bean.
126    */
127   public BadRequest copy() {
128      return new BadRequest(this);
129   }
130
131   @Override /* Overridden from BasicHttpException */
132   public BadRequest setContent(HttpEntity value) {
133      super.setContent(value);
134      return this;
135   }
136
137   @Override /* Overridden from BasicHttpException */
138   public BadRequest setContent(String value) {
139      super.setContent(value);
140      return this;
141   }
142
143   @Override /* Overridden from BasicHttpException */
144   public BadRequest setHeader2(String name, Object value) {
145      super.setHeader2(name, value);
146      return this;
147   }
148
149   @Override /* Overridden from BasicHttpException */
150   public BadRequest setHeaders(HeaderList value) {
151      super.setHeaders(value);
152      return this;
153   }
154
155   @Override /* Overridden from BasicHttpException */
156   public BadRequest setHeaders(List<Header> values) {
157      super.setHeaders(values);
158      return this;
159   }
160
161   @Override /* Overridden from BasicHttpException */
162   public BadRequest setHeaders2(Header...values) {
163      super.setHeaders2(values);
164      return this;
165   }
166
167   @Override /* Overridden from BasicHttpException */
168   public BadRequest setLocale2(Locale value) {
169      super.setLocale2(value);
170      return this;
171   }
172
173   @Override /* Overridden from BasicRuntimeException */
174   public BadRequest setMessage(String message, Object...args) {
175      super.setMessage(message, args);
176      return this;
177   }
178
179   @Override /* Overridden from BasicHttpException */
180   public BadRequest setProtocolVersion(ProtocolVersion value) {
181      super.setProtocolVersion(value);
182      return this;
183   }
184
185   @Override /* Overridden from BasicHttpException */
186   public BadRequest setReasonPhrase2(String value) {
187      super.setReasonPhrase2(value);
188      return this;
189   }
190
191   @Override /* Overridden from BasicHttpException */
192   public BadRequest setReasonPhraseCatalog(ReasonPhraseCatalog value) {
193      super.setReasonPhraseCatalog(value);
194      return this;
195   }
196
197   @Override /* Overridden from BasicHttpException */
198   public BadRequest setStatusCode2(int code) throws IllegalStateException {
199      super.setStatusCode2(code);
200      return this;
201   }
202
203   @Override /* Overridden from BasicHttpException */
204   public BadRequest setStatusLine(BasicStatusLine value) {
205      super.setStatusLine(value);
206      return this;
207   }
208
209   @Override /* Overridden from BasicRuntimeException */
210   public BadRequest setUnmodifiable() {
211      super.setUnmodifiable();
212      return this;
213   }
214}