View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.events.impl;
17  
18  import org.apache.http.client.methods.CloseableHttpResponse;
19  import org.esigate.events.Event;
20  import org.esigate.http.IncomingRequest;
21  import org.esigate.http.OutgoingRequest;
22  import org.esigate.http.OutgoingRequestContext;
23  
24  /**
25   * Fragment event : when a fragment (a complete or partial page) is required for rendering.
26   * 
27   * <p>
28   * This event may trigger a fetch event in case of a cache miss. If the cache already contains a non stale version of
29   * the response, the fragment is retrieved from the cache.
30   * 
31   * <p>
32   * In proxy mode, the first fragment is a complete page, and following fragments are complete and/or partial page
33   * according to ESI directives.
34   * 
35   * @author Nicolas Richeton
36   * 
37   */
38  public class FragmentEvent extends Event {
39  
40      /**
41       * The response data.
42       * <p>
43       * May be null if the request has not been executed yet. If this case, setting a response cancels the HTTP call and
44       * use the given object instead.
45       * 
46       */
47      private CloseableHttpResponse httpResponse;
48      /**
49       * The request context.
50       */
51      private final OutgoingRequestContext httpContext;
52      /**
53       * The new HTTP call details.
54       * <p>
55       * This object can been updated during pre-event processing and the HTTP call will use the updated object.
56       */
57      private final OutgoingRequest httpRequest;
58  
59      /**
60       * The request which was received by ESIgate.
61       * <p>
62       * It is ready only and not intended to be altered.
63       */
64      private final IncomingRequest originalRequest;
65  
66      public FragmentEvent(IncomingRequest originalRequest, OutgoingRequest httpRequest,
67              OutgoingRequestContext httpContext) {
68          this.originalRequest = originalRequest;
69          this.httpRequest = httpRequest;
70          this.httpContext = httpContext;
71      }
72  
73      public CloseableHttpResponse getHttpResponse() {
74          return httpResponse;
75      }
76  
77      public void setHttpResponse(CloseableHttpResponse httpResponse) {
78          this.httpResponse = httpResponse;
79      }
80  
81      public OutgoingRequestContext getHttpContext() {
82          return httpContext;
83      }
84  
85      public OutgoingRequest getHttpRequest() {
86          return httpRequest;
87      }
88  
89      public IncomingRequest getOriginalRequest() {
90          return originalRequest;
91      }
92  
93      @Override
94      public String toString() {
95          String result = httpRequest.toString();
96          if (httpResponse != null)
97              result = result + " -> " + httpResponse;
98          return result;
99      }
100 }