2
package org.crosswire.net;
4
import java.io.InputStreamReader;
6
import java.net.HttpURLConnection;
8
import java.net.URLConnection;
9
import java.util.Enumeration;
10
import java.util.Vector;
13
// import java.a*t.event.*;
14
// import javax.sw*ng.*;
15
// import javax.sw*ng.text.*;
16
// import javax.sw*ng.text.html.*;
17
// import sunw.hotjava.bean.*;
18
import org.crosswire.util.Logger;
19
import org.crosswire.util.StringUtil;
22
* Represents a single web page.
28
* Create a URLData object from a URL string
29
* @param browser The browser that created us, from which we get headers
30
* @param url_str The URL to visit
32
public WebPage(WebBrowser browser, String url_str)
36
this.url = new URL(url_str);
37
this.browser = browser;
39
// Create a connection
40
URLConnection cnx = url.openConnection();
41
cnx.setDoOutput(true);
42
cnx.setUseCaches(false);
43
cnx.setAllowUserInteraction(false);
46
for (Enumeration en=browser.headers(); en.hasMoreElements(); )
48
Header header = (Header) en.nextElement();
49
cnx.setRequestProperty(header.getName(), header.getValue());
52
// They all should be http connections ...
53
if (cnx instanceof HttpURLConnection)
55
HttpURLConnection hcnx = (HttpURLConnection) cnx;
57
code = hcnx.getResponseCode();
63
Header header = new Header(cnx.getHeaderFieldKey(i), cnx.getHeaderField(i));
64
if (!header.isValid()) break;
66
// Add new cookies to the browser
67
if (header.isCookie())
69
Cookie cookie = header.getCookie();
70
browser.addCookie(cookie);
71
cookies.addElement(cookie);
74
headers.addElement(header);
79
// Read the actual page data
80
Reader in = new InputStreamReader(cnx.getInputStream());
81
data = StringUtil.read(in);
86
if (ex instanceof ThreadDeath) throw (ThreadDeath) ex;
92
* Check that the given string exists in the data
94
public boolean contains(String test)
96
return data.indexOf(test) != -1;
102
public void printHeaders()
104
for (Enumeration en=headers.elements(); en.hasMoreElements(); )
106
System.out.println(""+en.nextElement());
113
public void printContent()
115
System.out.println(data);
121
public void displayContent()
127
HTMLDocument html = new HTMLDocument();
128
HTMLEditorKit editor = new HTMLEditorKit();
132
// Shove the page data into a stream
133
// And the read it into a Document
134
editor.read(new StringReader(data), html, 0);
138
if (ex instanceof ThreadDeath) throw (ThreadDeath) ex;
139
// The HTML component regularly throws up without any good reason
140
// ignore him and he will go away.
143
// Create the GUI components
144
JFrame frame = new JFrame(url.toString());
145
JTextPane text = new JTextPane(html);
146
JScrollPane scroll = new JScrollPane(text);
149
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
150
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
152
frame.getContentPane().setLayout(new BorderLayout());
153
frame.getContentPane().add("Center", scroll);
154
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
162
if (ex instanceof ThreadDeath) throw (ThreadDeath) ex;
163
// The HTML component regularly throws up without any good reason
164
// ignore him and he will go away.
167
frame.setVisible(true);
172
HotJavaBrowserBean hjb = new HotJavaBrowserBean();
173
StringReader sin = new StringReader(data);
174
hjb.setDocumentSource(sin);
176
final Frame frame = new Frame(url.toString());
179
frame.setLayout(new BorderLayout());
180
frame.add("Center", hjb);
182
frame.addWindowListener(new WindowAdapter() {
183
public void windowClosing(WindowEvent ev)
185
frame.setVisible(false);
191
frame.setVisible(true);
197
if (ex instanceof ThreadDeath) throw (ThreadDeath) ex;
198
// System.out.println(Level.INFO, "Failure", ex);
206
public void printNewCookies()
208
for (Enumeration en=cookies.elements(); en.hasMoreElements(); )
210
System.out.println(en.nextElement().toString());
215
* Works out the reply number 404 or something
216
* @return The reply status
218
public int getStatus()
224
* Gets the page contents
225
* @return The page contents
227
public String getPageContents()
232
/** How are the pages displayed */
233
public static int mode = 1;
235
/** The page headers */
236
private Vector headers = new Vector();
238
/** The new cookies delivered with this page */
239
private Vector cookies = new Vector();
241
/** The page source */
244
/** The response code */
245
private int code = -1;
247
/** The URL we went after in the first place */
250
/** The Web browser we were grabbed from */
251
private WebBrowser browser;
253
/** The log stream */
254
protected static Logger log = Logger.getLogger(WebPage.class);