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.exception;
014
015import static org.apache.juneau.http.exception.Unauthorized.*;
016
017import java.text.*;
018
019import org.apache.juneau.http.annotation.*;
020
021/**
022 * Exception representing an HTTP 401 (Unauthorized).
023 *
024 * <p>
025 * Similar to <c>403 Forbidden</c>, but specifically for use when authentication is required and has failed or has not yet been provided.
026 * <br>The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource.
027 * <br>401 semantically means "unauthenticated",i.e. the user does not have the necessary credentials.
028 * <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.
029 */
030@Response(code=CODE, description=MESSAGE)
031public class Unauthorized extends HttpException {
032   private static final long serialVersionUID = 1L;
033
034   /** HTTP status code */
035   public static final int CODE = 401;
036
037   /** Default message */
038   public static final String MESSAGE = "Unauthorized";
039
040   /**
041    * Constructor.
042    *
043    * @param cause The cause.  Can be <jk>null</jk>.
044    * @param msg The message.  Can be <jk>null</jk>.
045    * @param args Optional {@link MessageFormat}-style arguments in the message.
046    */
047   public Unauthorized(Throwable cause, String msg, Object...args) {
048      super(cause, CODE, msg, args);
049   }
050
051   /**
052    * Constructor.
053    *
054    * @param msg The message.  Can be <jk>null</jk>.
055    */
056   public Unauthorized(String msg) {
057      this((Throwable)null, msg);
058   }
059
060   /**
061    * Constructor.
062    */
063   public Unauthorized() {
064      this((Throwable)null, MESSAGE);
065   }
066
067   /**
068    * Constructor.
069    *
070    * @param msg The message.  Can be <jk>null</jk>.
071    * @param args Optional {@link MessageFormat}-style arguments in the message.
072    */
073   public Unauthorized(String msg, Object...args) {
074      this(null, msg, args);
075   }
076
077   /**
078    * Constructor.
079    *
080    * @param cause The cause.  Can be <jk>null</jk>.
081    */
082   public Unauthorized(Throwable cause) {
083      this(cause, null);
084   }
085
086   //------------------------------------------------------------------------------------------------------------------
087   // Fluent setters.
088   //------------------------------------------------------------------------------------------------------------------
089
090   // <FluentSetters>
091
092   @Override /* GENERATED - HttpException */
093   public Unauthorized header(String name, Object val) {
094      super.header(name, val);
095      return this;
096   }
097
098   // </FluentSetters>
099}