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.Unauthorized.*;
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 401 (Unauthorized).
033 *
034 * <p>
035 * Similar to <c>403 Forbidden</c>, but specifically for use when authentication is required and has failed or has not yet been provided.
036 * <br>The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource.
037 * <br>401 semantically means "unauthenticated",i.e. the user does not have the necessary credentials.
038 * <br>Note: Some sites issue HTTP 401 when an IP address is banned from the website (usually the website domain) and that specific address is refused permission to access a website.
039 *
040 * <h5 class='section'>See Also:</h5><ul>
041 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a>
042 * </ul>
043 *
044 * @serial exclude
045 */
046@Response
047@StatusCode(STATUS_CODE)
048@Schema(description = REASON_PHRASE)
049public class Unauthorized extends BasicHttpException {
050   private static final long serialVersionUID = 1L;
051
052   /** HTTP status code */
053   public static final int STATUS_CODE = 401;
054
055   /** Reason phrase */
056   public static final String REASON_PHRASE = "Unauthorized";
057
058   /** Default status line */
059   private static final BasicStatusLine STATUS_LINE = BasicStatusLine.create(STATUS_CODE, REASON_PHRASE);
060
061   /** Reusable unmodifiable instance */
062   public static final Unauthorized INSTANCE = new Unauthorized().setUnmodifiable();
063
064   /**
065    * Constructor.
066    */
067   public Unauthorized() {
068      this((Throwable)null, REASON_PHRASE);
069   }
070
071   /**
072    * Constructor.
073    *
074    * <p>
075    * This is the constructor used when parsing an HTTP response.
076    *
077    * @param response The HTTP response to copy from.  Must not be <jk>null</jk>.
078    * @throws AssertionError If HTTP response status code does not match what was expected.
079    */
080   public Unauthorized(HttpResponse response) {
081      super(response);
082      assertStatusCode(response);
083   }
084
085   /**
086    * Constructor.
087    *
088    * @param msg The message.  Can be <jk>null</jk>.
089    * @param args Optional {@link MessageFormat}-style arguments in the message.
090    */
091   public Unauthorized(String msg, Object...args) {
092      this((Throwable)null, msg, args);
093   }
094
095   /**
096    * Constructor.
097    *
098    * @param cause The cause.  Can be <jk>null</jk>.
099    */
100   public Unauthorized(Throwable cause) {
101      this(cause, cause == null ? REASON_PHRASE : cause.getMessage());
102   }
103
104   /**
105    * Constructor.
106    *
107    * @param cause The caused-by exception.  Can be <jk>null</jk>.
108    * @param msg The message.  Can be <jk>null</jk>.
109    * @param args The message arguments.
110    */
111   public Unauthorized(Throwable cause, String msg, Object...args) {
112      super(STATUS_CODE, cause, msg, args);
113      setStatusLine(STATUS_LINE.copy());
114   }
115
116   /**
117    * Copy constructor.
118    *
119    * @param copyFrom The bean to copy.
120    */
121   protected Unauthorized(Unauthorized copyFrom) {
122      super(copyFrom);
123   }
124
125   /**
126    * Creates a modifiable copy of this bean.
127    *
128    * @return A new modifiable bean.
129    */
130   public Unauthorized copy() {
131      return new Unauthorized(this);
132   }
133
134   @Override /* Overridden from BasicHttpException */
135   public Unauthorized setContent(HttpEntity value) {
136      super.setContent(value);
137      return this;
138   }
139
140   @Override /* Overridden from BasicHttpException */
141   public Unauthorized setContent(String value) {
142      super.setContent(value);
143      return this;
144   }
145
146   @Override /* Overridden from BasicHttpException */
147   public Unauthorized setHeader2(String name, Object value) {
148      super.setHeader2(name, value);
149      return this;
150   }
151
152   @Override /* Overridden from BasicHttpException */
153   public Unauthorized setHeaders(HeaderList value) {
154      super.setHeaders(value);
155      return this;
156   }
157
158   @Override /* Overridden from BasicHttpException */
159   public Unauthorized setHeaders(List<Header> values) {
160      super.setHeaders(values);
161      return this;
162   }
163
164   @Override /* Overridden from BasicHttpException */
165   public Unauthorized setHeaders2(Header...values) {
166      super.setHeaders2(values);
167      return this;
168   }
169
170   @Override /* Overridden from BasicHttpException */
171   public Unauthorized setLocale2(Locale value) {
172      super.setLocale2(value);
173      return this;
174   }
175
176   @Override /* Overridden from BasicRuntimeException */
177   public Unauthorized setMessage(String message, Object...args) {
178      super.setMessage(message, args);
179      return this;
180   }
181
182   @Override /* Overridden from BasicHttpException */
183   public Unauthorized setProtocolVersion(ProtocolVersion value) {
184      super.setProtocolVersion(value);
185      return this;
186   }
187
188   @Override /* Overridden from BasicHttpException */
189   public Unauthorized setReasonPhrase2(String value) {
190      super.setReasonPhrase2(value);
191      return this;
192   }
193
194   @Override /* Overridden from BasicHttpException */
195   public Unauthorized setReasonPhraseCatalog(ReasonPhraseCatalog value) {
196      super.setReasonPhraseCatalog(value);
197      return this;
198   }
199
200   @Override /* Overridden from BasicHttpException */
201   public Unauthorized setStatusCode2(int code) throws IllegalStateException {
202      super.setStatusCode2(code);
203      return this;
204   }
205
206   @Override /* Overridden from BasicHttpException */
207   public Unauthorized setStatusLine(BasicStatusLine value) {
208      super.setStatusLine(value);
209      return this;
210   }
211
212   @Override /* Overridden from BasicRuntimeException */
213   public Unauthorized setUnmodifiable() {
214      super.setUnmodifiable();
215      return this;
216   }
217}