001: /*
002: * $Header$
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: import java.awt.BorderLayout;
033: import java.awt.FlowLayout;
034: import java.awt.event.ActionEvent;
035: import java.awt.event.ActionListener;
036: import java.awt.event.WindowAdapter;
037: import java.awt.event.WindowEvent;
038: import java.io.ByteArrayInputStream;
039: import java.io.IOException;
040:
041: import javax.swing.JButton;
042: import javax.swing.JComboBox;
043: import javax.swing.JEditorPane;
044: import javax.swing.JFrame;
045: import javax.swing.JLabel;
046: import javax.swing.JPanel;
047: import javax.swing.JScrollPane;
048: import javax.swing.JSplitPane;
049: import javax.swing.JTextArea;
050: import javax.swing.SwingUtilities;
051: import javax.swing.text.BadLocationException;
052: import javax.swing.text.html.HTMLDocument;
053:
054: import org.apache.commons.httpclient.HttpClient;
055: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
056: import org.apache.commons.httpclient.methods.GetMethod;
057:
058: /**
059: * A simple Swing application that demonstrates how to use the Jakarta
060: * HttpClient API. This application loads HTML from servers and displays the
061: * content as text and as rendered HTML.
062: *
063: * @author Sean C. Sullivan
064: * @author Ortwin Glück
065: * @author Michael Becke
066: */
067: public class ClientApp {
068:
069: public static void main(String[] args) {
070: HttpClientFrame f = new HttpClientFrame();
071: f.setTitle("HttpClient demo application");
072: f.setSize(700, 500);
073: f.addWindowListener(new WindowAdapter() {
074: public void windowClosing(WindowEvent e) {
075: System.exit(0);
076: }
077: });
078: f.setVisible(true);
079: }
080:
081: public static class HttpClientFrame extends JFrame {
082:
083: private JComboBox cmbURL;
084: private JTextArea taTextResponse;
085: private JEditorPane htmlPane;
086:
087: private HttpClient client;
088:
089: public HttpClientFrame() {
090: client = new HttpClient(
091: new MultiThreadedHttpConnectionManager());
092: client.getHttpConnectionManager().getParams()
093: .setConnectionTimeout(30000);
094:
095: JPanel panInput = new JPanel(new FlowLayout());
096:
097: String[] aURLs = { "http://www.apache.org/",
098: "http://www.google.com/",
099: "http://www.opensource.org/",
100: "http://www.anybrowser.org/",
101: "http://jakarta.apache.org/", "http://www.w3.org/" };
102:
103: final JButton btnGET = new JButton("GET");
104: btnGET.addActionListener(new ActionListener() {
105: public void actionPerformed(ActionEvent ae) {
106: String url = (String) cmbURL.getSelectedItem();
107: if (url != null && url.length() > 0) {
108: loadPage(url);
109: }
110: }
111: });
112:
113: cmbURL = new JComboBox(aURLs);
114: cmbURL.setToolTipText("Enter a URL");
115: cmbURL.setEditable(true);
116: cmbURL.setSelectedIndex(0);
117:
118: JLabel lblURL = new JLabel("URL:");
119:
120: panInput.add(lblURL);
121: panInput.add(cmbURL);
122: panInput.add(btnGET);
123:
124: taTextResponse = new JTextArea();
125: taTextResponse.setEditable(false);
126: taTextResponse.setCaretPosition(0);
127:
128: htmlPane = new JEditorPane();
129: htmlPane.setContentType("text/html");
130: htmlPane.setEditable(false);
131:
132: JSplitPane splitResponsePane = new JSplitPane(
133: JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(
134: taTextResponse), new JScrollPane(htmlPane));
135: splitResponsePane.setOneTouchExpandable(false);
136: splitResponsePane.setDividerLocation(350);
137: // it would be better to set resizeWeight, but this method does
138: // not exist in JRE 1.2.2
139: // splitResponsePane.setResizeWeight(0.5);
140:
141: this .getContentPane().setLayout(new BorderLayout());
142: this .getContentPane().add(panInput, BorderLayout.NORTH);
143: this .getContentPane().add(splitResponsePane,
144: BorderLayout.CENTER);
145: }
146:
147: /**
148: * Sets the HTML content to be displayed.
149: *
150: * @param content an HTML document
151: */
152: private void setDocumentContent(String content) {
153:
154: HTMLDocument doc = new HTMLDocument();
155: try {
156: doc.remove(0, doc.getLength());
157: } catch (BadLocationException e) {
158: e.printStackTrace();
159: }
160: doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
161:
162: try {
163: htmlPane.read(new ByteArrayInputStream(content
164: .getBytes()), doc);
165: } catch (IOException e) {
166: e.printStackTrace();
167: }
168:
169: htmlPane.setDocument(doc);
170: htmlPane.setCaretPosition(0);
171:
172: taTextResponse.setText(content);
173: taTextResponse.setCaretPosition(0);
174: taTextResponse.requestFocus();
175: }
176:
177: /**
178: * Loads the page at the given URL from a separate thread.
179: * @param url
180: */
181: private void loadPage(final String url) {
182: // create a new thread to load the URL from
183: new Thread() {
184: public void run() {
185: GetMethod get = new GetMethod(url);
186: get.setFollowRedirects(true);
187:
188: try {
189: int iGetResultCode = client.executeMethod(get);
190: final String strGetResponseBody = get
191: .getResponseBodyAsString();
192:
193: if (strGetResponseBody != null) {
194: // set the HTML on the UI thread
195: SwingUtilities.invokeLater(new Runnable() {
196: public void run() {
197: setDocumentContent(strGetResponseBody);
198: }
199: });
200: }
201: } catch (Exception ex) {
202: ex.printStackTrace();
203: } finally {
204: get.releaseConnection();
205: }
206: }
207: }.start();
208: }
209:
210: }
211:
212: }
|