1 /*
2 * @(#)ResponseHandler.java 0.3-3 06/05/2001
3 *
4 * This file is part of the HTTPClient package
5 * Copyright (C) 1996-2001 Ronald Tschalär
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307, USA
21 *
22 * For questions, suggestions, bug-reports, enhancement-requests etc.
23 * I may be contacted at:
24 *
25 * ronald@innovation.ch
26 *
27 * The HTTPClient's home page is located at:
28 *
29 * http://www.innovation.ch/java/HTTPClient/
30 *
31 */
32
33 package HTTPClient;
34
35 import java.io.IOException;
36
37 /**
38 * This holds various information about an active response. Used by the
39 * StreamDemultiplexor and RespInputStream.
40 *
41 * @version 0.3-3 06/05/2001
42 * @author Ronald Tschalär
43 * @since V0.2
44 */
45 final class ResponseHandler
46 {
47 /** the response stream */
48 RespInputStream stream;
49
50 /** the response class */
51 Response resp;
52
53 /** the response class */
54 Request request;
55
56 /** signals that the demux has closed the response stream, and that
57 therefore no more data can be read */
58 boolean eof = false;
59
60 /** this is non-null if the stream has an exception pending */
61 IOException exception = null;
62
63
64 /**
65 * Creates a new handler. This also allocates the response input
66 * stream.
67 *
68 * @param resp the reponse
69 * @param request the request
70 * @param demux our stream demultiplexor.
71 */
72 ResponseHandler(Response resp, Request request, StreamDemultiplexor demux)
73 {
74 this.resp = resp;
75 this.request = request;
76 this.stream = new RespInputStream(demux, this);
77
78 Log.write(Log.DEMUX, "Demux: Opening stream " + this.stream.hashCode() +
79 " for demux (" + demux.hashCode() + ")");
80 }
81
82
83 /** holds the string that marks the end of this stream; used for
84 multipart delimited responses. */
85 private byte[] endbndry = null;
86
87 /** holds the compilation of the above string */
88 private int[] end_cmp = null;
89
90 /**
91 * return the boundary string for this response. Set's up the
92 * InputStream buffer if neccessary.
93 *
94 * @param MasterStream the input stream from which the stream demux
95 * is reading.
96 * @return the boundary string.
97 */
98 byte[] getEndBoundary(BufferedInputStream MasterStream)
99 throws IOException, ParseException
100 {
101 if (endbndry == null)
102 setupBoundary(MasterStream);
103
104 return endbndry;
105 }
106
107 /**
108 * return the compilation of the boundary string for this response.
109 * Set's up the InputStream buffer if neccessary.
110 *
111 * @param MasterStream the input stream from which the stream demux
112 * is reading.
113 * @return the compiled boundary string.
114 */
115 int[] getEndCompiled(BufferedInputStream MasterStream)
116 throws IOException, ParseException
117 {
118 if (end_cmp == null)
119 setupBoundary(MasterStream);
120
121 return end_cmp;
122 }
123
124 /**
125 * Gets the boundary string, compiles it for searching, and initializes
126 * the buffered input stream.
127 */
128 void setupBoundary(BufferedInputStream MasterStream)
129 throws IOException, ParseException
130 {
131 String endstr = "--" + Util.getParameter("boundary",
132 resp.getHeader("Content-Type")) +
133 "--\r\n";
134 endbndry = endstr.getBytes("8859_1");
135 end_cmp = Util.compile_search(endbndry);
136 MasterStream.markForSearch();
137 }
138 }