KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ClientApp


1 /*
2  * $Header$
3  * $Revision: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
5  * ====================================================================
6  *
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements. See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.FlowLayout JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.event.WindowAdapter JavaDoc;
37 import java.awt.event.WindowEvent JavaDoc;
38 import java.io.ByteArrayInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40
41 import javax.swing.JButton JavaDoc;
42 import javax.swing.JComboBox JavaDoc;
43 import javax.swing.JEditorPane JavaDoc;
44 import javax.swing.JFrame JavaDoc;
45 import javax.swing.JLabel JavaDoc;
46 import javax.swing.JPanel JavaDoc;
47 import javax.swing.JScrollPane JavaDoc;
48 import javax.swing.JSplitPane JavaDoc;
49 import javax.swing.JTextArea JavaDoc;
50 import javax.swing.SwingUtilities JavaDoc;
51 import javax.swing.text.BadLocationException JavaDoc;
52 import javax.swing.text.html.HTMLDocument JavaDoc;
53
54 import org.apache.commons.httpclient.HttpClient;
55 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
56 import org.apache.commons.httpclient.methods.GetMethod;
57
58 /**
59  * A simple Swing application that demonstrates how to use the Jakarta
60  * HttpClient API. This application loads HTML from servers and displays the
61  * content as text and as rendered HTML.
62  *
63  * @author Sean C. Sullivan
64  * @author Ortwin Glück
65  * @author Michael Becke
66  */

67 public class ClientApp {
68
69     public static void main(String JavaDoc[] args) {
70         HttpClientFrame f = new HttpClientFrame();
71         f.setTitle("HttpClient demo application");
72         f.setSize(700, 500);
73         f.addWindowListener(
74             new WindowAdapter JavaDoc() {
75                 public void windowClosing(WindowEvent JavaDoc e) {
76                     System.exit(0);
77                 }
78             }
79         );
80         f.setVisible(true);
81     }
82
83     public static class HttpClientFrame extends JFrame JavaDoc {
84
85         private JComboBox JavaDoc cmbURL;
86         private JTextArea JavaDoc taTextResponse;
87         private JEditorPane JavaDoc htmlPane;
88         
89         private HttpClient client;
90         
91         public HttpClientFrame() {
92             client = new HttpClient(new MultiThreadedHttpConnectionManager());
93             client.getHttpConnectionManager().
94                 getParams().setConnectionTimeout(30000);
95
96             JPanel JavaDoc panInput = new JPanel JavaDoc(new FlowLayout JavaDoc());
97
98             String JavaDoc[] aURLs = {
99                 "http://www.apache.org/",
100                 "http://www.google.com/",
101                 "http://www.opensource.org/",
102                 "http://www.anybrowser.org/",
103                 "http://jakarta.apache.org/",
104                 "http://www.w3.org/"
105             };
106
107             final JButton JavaDoc btnGET = new JButton JavaDoc("GET");
108             btnGET.addActionListener(
109                 new ActionListener JavaDoc() {
110                     public void actionPerformed(ActionEvent JavaDoc ae) {
111                         String JavaDoc url = (String JavaDoc) cmbURL.getSelectedItem();
112                         if (url != null && url.length() > 0) {
113                             loadPage(url);
114                         }
115                     }
116                 }
117             );
118             
119             cmbURL = new JComboBox JavaDoc(aURLs);
120             cmbURL.setToolTipText("Enter a URL");
121             cmbURL.setEditable(true);
122             cmbURL.setSelectedIndex(0);
123
124             JLabel JavaDoc lblURL = new JLabel JavaDoc("URL:");
125
126             panInput.add(lblURL);
127             panInput.add(cmbURL);
128             panInput.add(btnGET);
129
130             taTextResponse = new JTextArea JavaDoc();
131             taTextResponse.setEditable(false);
132             taTextResponse.setCaretPosition(0);
133
134             htmlPane = new JEditorPane JavaDoc();
135             htmlPane.setContentType("text/html");
136             htmlPane.setEditable(false);
137
138             JSplitPane JavaDoc splitResponsePane = new JSplitPane JavaDoc(
139                 JSplitPane.HORIZONTAL_SPLIT,
140                 new JScrollPane JavaDoc(taTextResponse),
141                 new JScrollPane JavaDoc(htmlPane)
142             );
143             splitResponsePane.setOneTouchExpandable(false);
144             splitResponsePane.setDividerLocation(350);
145             // it would be better to set resizeWeight, but this method does
146
// not exist in JRE 1.2.2
147
// splitResponsePane.setResizeWeight(0.5);
148

149             this.getContentPane().setLayout(new BorderLayout JavaDoc());
150             this.getContentPane().add(panInput, BorderLayout.NORTH);
151             this.getContentPane().add(splitResponsePane, BorderLayout.CENTER);
152         }
153
154         /**
155          * Sets the HTML content to be displayed.
156          *
157          * @param content an HTML document
158          */

159         private void setDocumentContent(String JavaDoc content) {
160         
161             HTMLDocument JavaDoc doc = new HTMLDocument JavaDoc();
162             try {
163                 doc.remove(0, doc.getLength());
164             } catch (BadLocationException JavaDoc e) {
165                 e.printStackTrace();
166             }
167             doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
168         
169             try {
170                 htmlPane.read(new ByteArrayInputStream JavaDoc(content.getBytes()), doc);
171             } catch (IOException JavaDoc e) {
172                 e.printStackTrace();
173             }
174         
175             htmlPane.setDocument(doc);
176             htmlPane.setCaretPosition(0);
177
178             taTextResponse.setText(content);
179             taTextResponse.setCaretPosition(0);
180             taTextResponse.requestFocus();
181         }
182         
183         /**
184          * Loads the page at the given URL from a separate thread.
185          * @param url
186          */

187         private void loadPage(final String JavaDoc url) {
188             // create a new thread to load the URL from
189
new Thread JavaDoc() {
190                 public void run() {
191                     GetMethod get = new GetMethod(url);
192                     get.setFollowRedirects(true);
193                     
194                     try {
195                         int iGetResultCode = client.executeMethod(get);
196                         final String JavaDoc strGetResponseBody = get.getResponseBodyAsString();
197
198                         if (strGetResponseBody != null) {
199                             // set the HTML on the UI thread
200
SwingUtilities.invokeLater(
201                                 new Runnable JavaDoc() {
202                                     public void run() {
203                                         setDocumentContent(strGetResponseBody);
204                                     }
205                                 }
206                             );
207                         }
208                     } catch (Exception JavaDoc ex) {
209                         ex.printStackTrace();
210                     } finally {
211                         get.releaseConnection();
212                     }
213                 }
214             }.start();
215         }
216
217     }
218         
219 }
220
Popular Tags