View Javadoc
1   package ca.uhn.fhir.rest.server.interceptor;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import ca.uhn.fhir.model.api.Bundle;
27  import ca.uhn.fhir.model.api.IResource;
28  import ca.uhn.fhir.model.api.TagList;
29  import ca.uhn.fhir.rest.method.RequestDetails;
30  import ca.uhn.fhir.rest.server.exceptions.AuthenticationException;
31  
32  /**
33   * Provides methods to intercept requests and responses. Note that implementations of this interface may wish
34   * to use {@link InterceptorAdapter} in order to not need to implement every method.
35   */
36  public interface IServerInterceptor {
37  
38  	/**
39  	 * This method is called before any other processing takes place for each incoming request. It may be used to
40  	 * provide alternate handling for some requests, or to screen requests before they are handled, etc.
41  	 * <p>
42  	 * Note that any exceptions thrown by this method will not be trapped by HAPI (they will be passed up to the server)
43  	 * </p>
44  	 * 
45  	 * @param theRequest
46  	 *            The incoming request
47  	 * @param theResponse
48  	 *            The response. Note that interceptors may choose to provide a response (i.e. by calling
49  	 *            {@link HttpServletResponse#getWriter()}) but in that case it is important to return <code>false</code>
50  	 * @return Return <code>true</code> if processing should continue normally. This is generally the right thing to do.
51  	 *         If your interceptor is providing a response rather than letting HAPI handle the response normally, you
52  	 *         must return <code>false</code>. In this case, no further processing will occur and no further
53  	 *         interceptors will be called.
54  	 */
55  	public boolean incomingRequestPreProcessed(HttpServletRequest theRequest, HttpServletResponse theResponse);
56  
57  	/**
58  	 * This method is called just before the actual implementing server method is invoked.
59  	 * <p>
60  	 * Note about exceptions: 
61  	 * </p>
62  	 * 
63  	 * @param theRequestDetails
64  	 *            A bean containing details about the request that is about to be processed, including
65  	 * @param theRequest
66  	 *            The incoming request
67  	 * @param theResponse
68  	 *            The response. Note that interceptors may choose to provide a response (i.e. by calling
69  	 *            {@link HttpServletResponse#getWriter()}) but in that case it is important to return <code>false</code>
70  	 * @return Return <code>true</code> if processing should continue normally. This is generally the right thing to do.
71  	 *         If your interceptor is providing a response rather than letting HAPI handle the response normally, you
72  	 *         must return <code>false</code>. In this case, no further processing will occur and no further
73  	 *         interceptors will be called.
74  	 * @throws AuthenticationException
75  	 *             This exception may be thrown to indicate that the interceptor has detected an unauthorized access
76  	 *             attempt. If thrown, processing will stop and an HTTP 401 will be returned to the client.
77  	 */
78  	public boolean incomingRequestPostProcessed(RequestDetails theRequestDetails, HttpServletRequest theRequest, HttpServletResponse theResponse) throws AuthenticationException;
79  
80  	/**
81  	 * This method is called after the server implementation method has been called, but before any attempt to stream
82  	 * the response back to the client
83  	 * 
84  	 * @param theRequestDetails
85  	 *            A bean containing details about the request that is about to be processed, including
86  	 * @param theResponseObject
87  	 *            The actual object which is being streamed to the client as a response
88  	 * @param theRequest
89  	 *            The incoming request
90  	 * @param theResponse
91  	 *            The response. Note that interceptors may choose to provide a response (i.e. by calling
92  	 *            {@link HttpServletResponse#getWriter()}) but in that case it is important to return <code>false</code>
93  	 * @return Return <code>true</code> if processing should continue normally. This is generally the right thing to do.
94  	 *         If your interceptor is providing a response rather than letting HAPI handle the response normally, you
95  	 *         must return <code>false</code>. In this case, no further processing will occur and no further
96  	 *         interceptors will be called.
97  	 * @throws AuthenticationException
98  	 *             This exception may be thrown to indicate that the interceptor has detected an unauthorized access
99  	 *             attempt. If thrown, processing will stop and an HTTP 401 will be returned to the client.
100 	 */
101 	public boolean outgoingResponse(RequestDetails theRequestDetails, TagList theResponseObject, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse) throws AuthenticationException;
102 
103 
104 	/**
105 	 * This method is called after the server implementation method has been called, but before any attempt to stream
106 	 * the response back to the client
107 	 * 
108 	 * @param theRequestDetails
109 	 *            A bean containing details about the request that is about to be processed, including
110 	 * @param theResponseObject
111 	 *            The actual object which is being streamed to the client as a response
112 	 * @param theRequest
113 	 *            The incoming request
114 	 * @param theResponse
115 	 *            The response. Note that interceptors may choose to provide a response (i.e. by calling
116 	 *            {@link HttpServletResponse#getWriter()}) but in that case it is important to return <code>false</code>
117 	 * @return Return <code>true</code> if processing should continue normally. This is generally the right thing to do.
118 	 *         If your interceptor is providing a response rather than letting HAPI handle the response normally, you
119 	 *         must return <code>false</code>. In this case, no further processing will occur and no further
120 	 *         interceptors will be called.
121 	 * @throws AuthenticationException
122 	 *             This exception may be thrown to indicate that the interceptor has detected an unauthorized access
123 	 *             attempt. If thrown, processing will stop and an HTTP 401 will be returned to the client.
124 	 */
125 	public boolean outgoingResponse(RequestDetails theRequestDetails, Bundle theResponseObject, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse) throws AuthenticationException;
126 
127 	/**
128 	 * This method is called after the server implementation method has been called, but before any attempt to stream
129 	 * the response back to the client
130 	 * 
131 	 * @param theRequestDetails
132 	 *            A bean containing details about the request that is about to be processed, including
133 	 * @param theResponseObject
134 	 *            The actual object which is being streamed to the client as a response
135 	 * @param theRequest
136 	 *            The incoming request
137 	 * @param theResponse
138 	 *            The response. Note that interceptors may choose to provide a response (i.e. by calling
139 	 *            {@link HttpServletResponse#getWriter()}) but in that case it is important to return <code>false</code>
140 	 * @return Return <code>true</code> if processing should continue normally. This is generally the right thing to do.
141 	 *         If your interceptor is providing a response rather than letting HAPI handle the response normally, you
142 	 *         must return <code>false</code>. In this case, no further processing will occur and no further
143 	 *         interceptors will be called.
144 	 * @throws AuthenticationException
145 	 *             This exception may be thrown to indicate that the interceptor has detected an unauthorized access
146 	 *             attempt. If thrown, processing will stop and an HTTP 401 will be returned to the client.
147 	 */
148 	public boolean outgoingResponse(RequestDetails theRequestDetails, IResource theResponseObject, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse) throws AuthenticationException;
149 
150 	/**
151 	 * This method is called after the server implementation method has been called, but before any attempt to stream
152 	 * the response back to the client
153 	 * 
154 	 * @param theRequestDetails
155 	 *            A bean containing details about the request that is about to be processed, including
156 	 * @param theResponseObject
157 	 *            The actual object which is being streamed to the client as a response
158 	 * @param theRequest
159 	 *            The incoming request
160 	 * @param theResponse
161 	 *            The response. Note that interceptors may choose to provide a response (i.e. by calling
162 	 *            {@link HttpServletResponse#getWriter()}) but in that case it is important to return <code>false</code>
163 	 * @return Return <code>true</code> if processing should continue normally. This is generally the right thing to do.
164 	 *         If your interceptor is providing a response rather than letting HAPI handle the response normally, you
165 	 *         must return <code>false</code>. In this case, no further processing will occur and no further
166 	 *         interceptors will be called.
167 	 * @throws AuthenticationException
168 	 *             This exception may be thrown to indicate that the interceptor has detected an unauthorized access
169 	 *             attempt. If thrown, processing will stop and an HTTP 401 will be returned to the client.
170 	 */
171 	public boolean outgoingResponse(RequestDetails theRequestDetails, HttpServletRequest theServletRequest, HttpServletResponse theServletResponse) throws AuthenticationException;
172 
173 }