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