View Javadoc
1   package ca.uhn.fhir.rest.server.exceptions;
2   
3   import ca.uhn.fhir.rest.api.Constants;
4   import ca.uhn.fhir.util.CoverageIgnore;
5   
6   /*
7    * #%L
8    * HAPI FHIR - Core Library
9    * %%
10   * Copyright (C) 2014 - 2018 University Health Network
11   * %%
12   * Licensed under the Apache License, Version 2.0 (the "License");
13   * you may not use this file except in compliance with the License.
14   * You may obtain a copy of the License at
15   * 
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   * 
18   * Unless required by applicable law or agreed to in writing, software
19   * distributed under the License is distributed on an "AS IS" BASIS,
20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   * See the License for the specific language governing permissions and
22   * limitations under the License.
23   * #L%
24   */
25  
26  /**
27   * Represents an <b>HTTP 401 Client Unauthorized</b> response, which 
28   * means that the client needs to provide credentials, or has 
29   * provided invalid credentials.
30   * <p>
31   * For security failures, you should use
32   * {@link AuthenticationException} if you want to indicate that the
33   * user could not be authenticated (e.g. credential failures), also 
34   * known as an <b>authentication</b> failure. 
35   * You should use {@link ForbiddenOperationException} if you want to 
36   * indicate that the authenticated user does not have permission to
37   * perform the requested operation, also known as an <b>authorization</b>
38   * failure.
39   * </p>
40   * <p>
41   * Note that a complete list of RESTful exceptions is available in the <a href="./package-summary.html">Package
42   * Summary</a>.
43   * </p>
44   
45   */
46  @CoverageIgnore
47  public class AuthenticationException extends BaseServerResponseException {
48  
49  	public static final int STATUS_CODE = Constants.STATUS_HTTP_401_CLIENT_UNAUTHORIZED;
50  
51  	private static final long serialVersionUID = 1L;
52  
53  	public AuthenticationException() {
54  		super(STATUS_CODE, "Client unauthorized");
55  	}
56  
57  	public AuthenticationException(String theMessage) {
58  		super(STATUS_CODE, theMessage);
59  	}
60  
61  	public AuthenticationException(String theMessage, Throwable theCause) {
62  		super(STATUS_CODE, theMessage, theCause);
63  	}
64  	
65  	/**
66  	 * Adds a <code>WWW-Authenticate</code> header to the response, of the form:<br/>
67  	 * <code>WWW-Authenticate: Basic realm="theRealm"</code> 
68  	 * 
69  	 * @return Returns a reference to <code>this</code> for easy method chaining
70  	 */
71  	public AuthenticationException addAuthenticateHeaderForRealm(String theRealm) {
72  		addResponseHeader("WWW-Authenticate", "Basic realm=\"" + theRealm + "\"");
73  		return this;
74  	}
75  
76  }