View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.nio.integration;
29  
30  import java.io.IOException;
31  import java.util.Queue;
32  
33  import org.apache.http.HttpEntity;
34  import org.apache.http.HttpRequest;
35  import org.apache.http.HttpResponse;
36  import org.apache.http.nio.entity.BufferingNHttpEntity;
37  import org.apache.http.nio.entity.ConsumingNHttpEntity;
38  import org.apache.http.nio.protocol.HttpRequestExecutionHandler;
39  import org.apache.http.nio.protocol.NHttpRequestExecutionHandler;
40  import org.apache.http.nio.util.HeapByteBufferAllocator;
41  import org.apache.http.protocol.HttpContext;
42  import org.apache.http.util.EntityUtils;
43  
44  @Deprecated
45  abstract class RequestExecutionHandler
46      implements NHttpRequestExecutionHandler, HttpRequestExecutionHandler {
47  
48      public void initalizeContext(final HttpContext context, final Object attachment) {
49          context.setAttribute("queue", attachment);
50      }
51  
52      protected abstract HttpRequest generateRequest(Job testjob);
53  
54      public HttpRequest submitRequest(final HttpContext context) {
55  
56          @SuppressWarnings("unchecked")
57          Queue<Job> queue = (Queue<Job>) context.getAttribute("queue");
58          if (queue == null) {
59              throw new IllegalStateException("Queue is null");
60          }
61  
62          Job testjob = queue.poll();
63          context.setAttribute("job", testjob);
64  
65          if (testjob != null) {
66              return generateRequest(testjob);
67          } else {
68              return null;
69          }
70      }
71  
72      public ConsumingNHttpEntity responseEntity(
73              final HttpResponse response,
74              final HttpContext context) throws IOException {
75          return new BufferingNHttpEntity(response.getEntity(),
76                  new HeapByteBufferAllocator());
77      }
78  
79      public void handleResponse(final HttpResponse response, final HttpContext context) {
80          Job testjob = (Job) context.removeAttribute("job");
81          if (testjob == null) {
82              throw new IllegalStateException("TestJob is null");
83          }
84  
85          int statusCode = response.getStatusLine().getStatusCode();
86          String content = null;
87  
88          HttpEntity entity = response.getEntity();
89          if (entity != null) {
90              try {
91                  content = EntityUtils.toString(entity);
92              } catch (IOException ex) {
93                  content = "I/O exception: " + ex.getMessage();
94              }
95          }
96          testjob.setResult(statusCode, content);
97      }
98  
99      public void finalizeContext(final HttpContext context) {
100         Job testjob = (Job) context.removeAttribute("job");
101         if (testjob != null) {
102             testjob.fail("Request failed");
103         }
104     }
105 
106 }