]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/net/http/HttpRequest.java
LP1827055 Remove Python libs, install bits, and docs
[OpenSRF.git] / src / java / org / opensrf / net / http / HttpRequest.java
1 package org.opensrf.net.http;
2 import org.opensrf.*;
3 import org.opensrf.util.*;
4
5 import java.util.List;
6 import java.util.LinkedList;
7 import java.net.HttpURLConnection;
8
9 public abstract class HttpRequest {
10
11     /** OpenSRF service. */
12     protected String service;
13     /** OpenSRF method. */
14     protected Method method;
15     /** Connection to server. */
16     protected HttpURLConnection urlConn;
17     /** Connection manager. */
18     protected HttpConnection httpConn;
19
20     /** 
21      * List of responses.
22      *
23      * Responses are not kept after being passed to onResponse .
24      */
25     protected List<Object> responseList;
26
27     /** True if all responses data has been read and delivered */
28     protected boolean complete;
29
30     // use a no-op handler by default
31     protected HttpRequestHandler handler = new HttpRequestHandler() {};
32
33     public HttpRequest() {
34         complete = false;
35         handler = null;
36         urlConn = null;
37     }
38
39     /**
40      * Constructor.
41      *
42      * @param conn Connection 
43      * @param service The OpenSRF service.
44      * @param method The method to send to the server.
45      */
46     public HttpRequest(HttpConnection conn, String service, Method method) {
47         this();
48         this.httpConn = conn;
49         this.service = service;
50         this.method = method;
51     }
52
53     /**
54      * Send a request asynchronously (via threads).
55      *
56      * @param handler Manages responses
57      */
58     public void sendAsync(final HttpRequestHandler handler) {
59         if (handler != null) 
60             this.handler = handler;
61         httpConn.manageAsyncRequest(this);
62     }
63
64     protected void pushResponse(Object response) {
65         if (responseList == null)
66             responseList = new LinkedList<Object>();
67         responseList.add(response);
68     }
69
70     protected List responses() {
71         return responseList;
72     }
73     
74     protected Object nextResponse() {
75         if (complete || responseList == null) 
76             return null;
77         if (responseList.size() > 0)
78             return responseList.remove(0);
79         return null;
80     }
81
82     /**
83      * Send a request to the server.
84      */
85     public abstract GatewayRequest send() throws java.io.IOException;
86
87     /**
88      * Receive one response from the server.
89      *
90      * @return The response object
91      */
92     public abstract Object recv() throws java.io.IOException;
93 }
94
95