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  package org.esigate.test.conn;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.apache.http.HttpRequest;
21  import org.apache.http.HttpResponse;
22  
23  /**
24   * A response handler, which returns HTTP reponses one after another.
25   * <p>
26   * Sends IllegalStateException if no reponse has been added, or if execute is called too many times.
27   * 
28   * @author Nicolas Richeton
29   * 
30   */
31  public class SequenceResponse implements IResponseHandler {
32      private int count = 0;
33      private List<HttpResponse> responses = new ArrayList<>();
34  
35      @Override
36      public HttpResponse execute(HttpRequest request) {
37  
38          if (this.responses.size() <= this.count) {
39              throw new IllegalStateException("Unexpected request");
40          }
41  
42          HttpResponse result = this.responses.get(this.count);
43          this.count++;
44          return result;
45      }
46  
47      /**
48       * Add a Http response.
49       * 
50       * @param response
51       * @return this object
52       */
53      public SequenceResponse response(HttpResponse response) {
54          this.responses.add(response);
55          return this;
56      }
57  
58  }