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.classic.response; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020import static org.apache.juneau.http.classic.response.Unauthorized.*; 021 022import java.util.*; 023 024import org.apache.http.*; 025import org.apache.http.Header; 026import org.apache.juneau.commons.*; 027import org.apache.juneau.http.*; 028import org.apache.juneau.http.classic.*; 029import org.apache.juneau.http.classic.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/JuneauRestCommon">juneau-rest-common</a> 042 * </ul> 043 * 044 * @serial exclude 045 */ 046@Response 047@StatusCode(STATUS_CODE) 048@Schema(description = REASON_PHRASE) 049@SuppressWarnings({ 050 "java:S110" // Inheritance depth acceptable for HTTP exception hierarchy 051}) 052public class Unauthorized extends BasicHttpException { 053 private static final long serialVersionUID = 1L; 054 055 /** HTTP status code */ 056 public static final int STATUS_CODE = 401; 057 058 /** Reason phrase */ 059 public static final String REASON_PHRASE = "Unauthorized"; 060 061 /** Default status line */ 062 private static final BasicStatusLine STATUS_LINE = BasicStatusLine.create(STATUS_CODE, REASON_PHRASE); 063 064 /** Reusable unmodifiable instance */ 065 public static final Unauthorized INSTANCE = new Unauthorized().unmodifiable(); 066 067 /** 068 * Constructor. 069 */ 070 public Unauthorized() { 071 this((Throwable)null, REASON_PHRASE); 072 } 073 074 /** 075 * Constructor. 076 * 077 * <p> 078 * This is the constructor used when parsing an HTTP response. 079 * 080 * @param response The HTTP response to copy from. Must not be <jk>null</jk>. 081 * @throws IllegalArgumentException If HTTP response status code does not match what was expected. 082 */ 083 public Unauthorized(HttpResponse response) { 084 super(requireStatus(response, STATUS_CODE)); 085 } 086 087 /** 088 * Constructor. 089 * 090 * @param msg The message. Can be <jk>null</jk>, in which case the reason phrase is used as the message. 091 * @param args Optional {@link String#format(String, Object...) String.format}-style arguments in the message. 092 */ 093 public Unauthorized(String msg, Object...args) { 094 this((Throwable)null, msg, args); 095 } 096 097 /** 098 * Constructor. 099 * 100 * @param cause The cause. Can be <jk>null</jk>, in which case the reason phrase is used as the message and no cause is chained. 101 */ 102 public Unauthorized(Throwable cause) { 103 this(cause, cause == null ? REASON_PHRASE : cause.getMessage()); 104 } 105 106 /** 107 * Constructor. 108 * 109 * @param cause The caused-by exception. Can be <jk>null</jk> (no cause is chained). 110 * @param msg The message. Can be <jk>null</jk>, in which case the cause's message is used, or the reason phrase if the cause is also <jk>null</jk> or has no message. 111 * @param args The message arguments. 112 */ 113 public Unauthorized(Throwable cause, String msg, Object...args) { 114 super(STATUS_CODE, cause, msg, args); 115 setStatusLine(STATUS_LINE.copy()); 116 } 117 118 /** 119 * Copy constructor. 120 * 121 * @param copyFrom The bean to copy. Must not be <jk>null</jk>. 122 */ 123 protected Unauthorized(Unauthorized copyFrom) { 124 super(copyFrom); 125 } 126 127 /** 128 * Creates a modifiable copy of this bean. 129 * 130 * @return A new modifiable bean. 131 */ 132 public Unauthorized copy() { 133 return new Unauthorized(this); 134 } 135 136 @Override /* Overridden from BasicHttpException */ 137 public Unauthorized setContent(HttpEntity value) { 138 super.setContent(value); 139 return this; 140 } 141 142 @Override /* Overridden from BasicHttpException */ 143 public Unauthorized setContent(String value) { 144 super.setContent(value); 145 return this; 146 } 147 148 @Override /* Overridden from BasicHttpException */ 149 public Unauthorized setHeader2(String name, Object value) { 150 super.setHeader2(name, value); 151 return this; 152 } 153 154 @Override /* Overridden from BasicHttpException */ 155 public Unauthorized setHeaders(HeaderList value) { 156 super.setHeaders(value); 157 return this; 158 } 159 160 @Override /* Overridden from BasicHttpException */ 161 public Unauthorized setHeaders(List<Header> values) { 162 super.setHeaders(values); 163 return this; 164 } 165 166 @Override /* Overridden from BasicHttpException */ 167 public Unauthorized setHeaders2(Header...values) { 168 super.setHeaders2(values); 169 return this; 170 } 171 172 @Override /* Overridden from BasicHttpException */ 173 public Unauthorized setLocale2(Locale value) { 174 super.setLocale2(value); 175 return this; 176 } 177 178 @Override /* Overridden from BasicRuntimeException */ 179 public Unauthorized setMessage(String message, Object...args) { 180 super.setMessage(message, args); 181 return this; 182 } 183 184 @Override /* Overridden from BasicHttpException */ 185 public Unauthorized setProtocolVersion(ProtocolVersion value) { 186 super.setProtocolVersion(value); 187 return this; 188 } 189 190 @Override /* Overridden from BasicHttpException */ 191 public Unauthorized setReasonPhrase2(String value) { 192 super.setReasonPhrase2(value); 193 return this; 194 } 195 196 @Override /* Overridden from BasicHttpException */ 197 public Unauthorized setReasonPhraseCatalog(ReasonPhraseCatalog value) { 198 super.setReasonPhraseCatalog(value); 199 return this; 200 } 201 202 @Override /* Overridden from BasicHttpException */ 203 public Unauthorized setStatusCode2(int code) throws IllegalStateException { 204 super.setStatusCode2(code); 205 return this; 206 } 207 208 @Override /* Overridden from BasicHttpException */ 209 public Unauthorized setStatusLine(BasicStatusLine value) { 210 super.setStatusLine(value); 211 return this; 212 } 213 214 @Override /* Overridden from BasicHttpException */ 215 public Unauthorized unmodifiable() { 216 return this instanceof UnmodifiableBean ? this : new Unmodifiable(this); 217 } 218 219 /** 220 * Unmodifiable point-in-time snapshot of the enclosing {@link Unauthorized} exception. 221 * 222 * <p> 223 * Its only behavioral override is {@link #modify(Runnable)}, which throws — because all mutation is funneled through 224 * {@code modify(...)}, this single override freezes the entire mutation surface. 225 */ 226 public static class Unmodifiable extends Unauthorized implements UnmodifiableBean { 227 228 private static final long serialVersionUID = 1L; 229 230 /** 231 * Constructor. 232 * 233 * @param copyFrom The exception to snapshot. Must not be <jk>null</jk>. 234 */ 235 @SuppressWarnings({ 236 "java:S1699" // Paradigm intentionally calls the overridable freeze() from the ctor to deep-freeze sub-beans. 237 }) 238 protected Unmodifiable(Unauthorized copyFrom) { 239 super(copyFrom); 240 freeze(); 241 } 242 243 @Override /* Overridden from BasicHttpException */ 244 protected BasicHttpException modify(Runnable mutation) { 245 throw uoex("Bean is unmodifiable."); 246 } 247 } 248}