Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 45251 invoked from network); 17 Dec 2002 01:28:40 -0000 Received: from exchange.sun.com (HELO nagoya.betaversion.org) (192.18.33.10) by daedalus.apache.org with SMTP; 17 Dec 2002 01:28:40 -0000 Received: (qmail 19798 invoked by uid 97); 17 Dec 2002 01:29:54 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 19782 invoked by uid 97); 17 Dec 2002 01:29:54 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 19771 invoked by uid 97); 17 Dec 2002 01:29:53 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Date: 17 Dec 2002 01:28:33 -0000 Message-ID: <20021217012833.15881.qmail@icarus.apache.org> From: jsdever@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/httpclient/src/examples ClientApp.java CookieDemoApp.java MultipartFileUploadApp.java PostXML.java TrivialApp.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N jsdever 2002/12/16 17:28:33 Modified: httpclient/src/examples ClientApp.java CookieDemoApp.java MultipartFileUploadApp.java PostXML.java TrivialApp.java Log: Update the examples to remove deprecated method calls. Revision Changes Path 1.8 +44 -68 jakarta-commons/httpclient/src/examples/ClientApp.java Index: ClientApp.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/examples/ClientApp.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- ClientApp.java 8 Sep 2002 05:19:06 -0000 1.7 +++ ClientApp.java 17 Dec 2002 01:28:32 -0000 1.8 @@ -1,5 +1,5 @@ /* - * $Header $ + * $Header$ * $Revision$ * $Date$ * @@ -78,13 +78,11 @@ * how to use the Jakarta HttpClient API. * * @author Sean C. Sullivan - * + * @author Ortwin Gl�ck */ -public class ClientApp -{ +public class ClientApp { - public static void main(String[] args) - { + public static void main(String[] args) { HttpClientMainFrame f = new HttpClientMainFrame(); f.setTitle("HttpClient demo application"); f.setSize(700, 500); @@ -92,41 +90,37 @@ f.setVisible(true); } - public static class HttpClientMainFrame extends javax.swing.JFrame - { + public static class HttpClientMainFrame extends javax.swing.JFrame { private HttpClientPanel m_panel; - public HttpClientMainFrame() - { + public HttpClientMainFrame() { m_panel = new HttpClientPanel(); this.getContentPane().add(m_panel); } } - public static class HttpClientPanel extends JPanel - { + public static class HttpClientPanel extends JPanel { private static final String strTenSpaces = " "; private static final String strFortySpaces = strTenSpaces + strTenSpaces + strTenSpaces + strTenSpaces; private static final String strEightySpaces = strFortySpaces + strFortySpaces; - public HttpClientPanel() - { + public HttpClientPanel() { final JPanel panInput = new JPanel(); panInput.setLayout(new FlowLayout()); final JPanel panDisplay = new JPanel(); panDisplay.setLayout(new BorderLayout()); - String[] aURLs = - { + String[] aURLs = { "http://www.apache.org/", "http://www.google.com/", "http://www.opensource.org/", "http://www.anybrowser.org/", "http://jakarta.apache.org/", - "http://www.w3.org/" }; + "http://www.w3.org/" + }; final JComboBox cmbURL = new JComboBox(aURLs); cmbURL.setToolTipText("Enter a URL"); @@ -142,54 +136,53 @@ final JButton btnGET = new JButton("GET"); - cmbURL.addActionListener(new ActionListener() - { + cmbURL.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { btnGET.doClick(); } }); - btnGET.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent ae) - { + btnGET.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent ae) { final String strRawURL = (String) cmbURL.getSelectedItem(); - if (strRawURL.length() > 0) - { + if (strRawURL.length() > 0) { final URL u; - try - { + try { u = new URL(strRawURL.trim()); - Thread t = new Thread() - { - public void run() - { - HttpClient client = new HttpClient(); + Thread t = new Thread() { + public void run() { + HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager()); GetMethod get = new GetMethod(); HeadMethod head = new HeadMethod(); - client.startSession(u); - client.setTimeout(30000 /* milliseconds */ - ); + HostConfiguration hc = new HostConfiguration(); + try { + hc.setHost(new URI(u)); + } catch(URIException e) { + throw new RuntimeException(e.toString()); + } + client.setHostConfiguration(hc); + client.setConnectionTimeout(30000); int iGetResultCode; int iHeadResultCode; - try - { + try { + //to execute two methods in a row without reading the first method's response + //we *need* the MultiThreadedHttpConnectionManager as set in the HttpClient's + //constructor. iGetResultCode = client.executeMethod(get); iHeadResultCode = client.executeMethod(head); final String strGetResponseBody = get.getResponseBodyAsString(); + get.releaseConnection(); final String strHeadResponseBody = head.getResponseBodyAsString(); + head.releaseConnection(); - if (strGetResponseBody != null) - { - Runnable r = new Runnable() - { - public void run() - { + if (strGetResponseBody != null) { + Runnable r = new Runnable() { + public void run() { panDisplay.removeAll(); taTextResponse.setText( @@ -197,11 +190,9 @@ taTextResponse.setCaretPosition(0); taTextResponse.requestFocus(); - try - { JEditorPane htmlPane = - new JEditorPane( - u.toExternalForm()); + new JEditorPane("text/html", + strGetResponseBody); htmlPane.setEditable(false); final JSplitPane splitResponsePane = @@ -222,40 +213,25 @@ panDisplay.validate(); } - catch (java.io.IOException ex) - { - ex.printStackTrace(); - } - } }; - try - { + try { SwingUtilities.invokeAndWait(r); - } - catch (InvocationTargetException ex) - { + } catch (InvocationTargetException ex) { ex.printStackTrace(); - } - catch (InterruptedException ex) - { + } catch (InterruptedException ex) { ex.printStackTrace(); } } } - catch (HttpException ex) - { + catch (HttpException ex) { ex.printStackTrace(); - } - catch (IOException ex) - { + } catch (IOException ex) { ex.printStackTrace(); } } }; t.start(); - } - catch (MalformedURLException ignored) - { + } catch (MalformedURLException ignored) { // ignore } } 1.5 +20 -25 jakarta-commons/httpclient/src/examples/CookieDemoApp.java Index: CookieDemoApp.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/examples/CookieDemoApp.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- CookieDemoApp.java 29 Sep 2002 15:23:28 -0000 1.4 +++ CookieDemoApp.java 17 Dec 2002 01:28:32 -0000 1.5 @@ -78,8 +78,7 @@ * @author Sean C. Sullivan * */ -public class CookieDemoApp -{ +public class CookieDemoApp { private static final String COOKIE_NAME = "count"; /** @@ -92,11 +91,10 @@ * * */ - public static void main(String[] args) throws Exception - { - if (args.length != 1) - { - System.err.println("missing command line argument"); + public static void main(String[] args) throws Exception { + if (args.length != 1) { + System.err.println("Usage: java CookieDemoApp "); + System.err.println(" The url of a webpage"); System.exit(1); } @@ -112,32 +110,29 @@ initialState.addCookie(ck); - HttpClient hc = new HttpClient(); + HttpClient client = new HttpClient(); + HostConfiguration hc = new HostConfiguration(); + hc.setHost(new URI(u)); + client.setHostConfiguration(hc); - hc.startSession(u); + client.setConnectionTimeout(30000); + client.setState(initialState); - hc.setTimeout(30000 /* milliseconds */); - hc.setState(initialState); - - for (int i = 0; i < 10; i++) - { + for (int i = 0; i < 10; i++) { GetMethod get = new GetMethod("/"); - int iResultCode = hc.executeMethod(get); - HttpState state = hc.getState(); + int iResultCode = client.executeMethod(get); + HttpState state = client.getState(); Cookie[] cookies = state.getCookies(); - for (int k = 0; k < cookies.length; k++) - { + for (int k = 0; k < cookies.length; k++) { Cookie currentCookie = cookies[k]; - if (currentCookie.getName().equals(COOKIE_NAME)) - { + if (currentCookie.getName().equals(COOKIE_NAME)) { Integer iCount = new Integer(currentCookie.getValue()); System.out.println("count value is : " + iCount); int iNewCount = iCount.intValue() + 1; currentCookie.setValue("" + iNewCount); } } + get.releaseConnection(); } - - hc.endSession(); } } 1.2 +25 -47 jakarta-commons/httpclient/src/examples/MultipartFileUploadApp.java Index: MultipartFileUploadApp.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/examples/MultipartFileUploadApp.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MultipartFileUploadApp.java 12 Oct 2002 04:36:32 -0000 1.1 +++ MultipartFileUploadApp.java 17 Dec 2002 01:28:32 -0000 1.2 @@ -1,5 +1,5 @@ /* - * $Header $ + * $Header$ * $Revision$ * $Date$ * @@ -81,11 +81,9 @@ * @author Sean C. Sullivan * */ -public class MultipartFileUploadApp -{ +public class MultipartFileUploadApp { - public static void main(String[] args) - { + public static void main(String[] args) { MultipartFileUploadMainFrame f = new MultipartFileUploadMainFrame(); f.setTitle("HTTP multipart file upload application"); f.setSize(700, 500); @@ -93,19 +91,16 @@ f.setVisible(true); } - public static class MultipartFileUploadMainFrame extends javax.swing.JFrame - { + public static class MultipartFileUploadMainFrame extends javax.swing.JFrame { private MultipartFileUploadPanel m_panel; - public MultipartFileUploadMainFrame() - { + public MultipartFileUploadMainFrame() { m_panel = new MultipartFileUploadPanel(); this.getContentPane().add(m_panel); } } - public static class MultipartFileUploadPanel extends JPanel - { + public static class MultipartFileUploadPanel extends JPanel { private static final String strTenSpaces = " "; private static final String strFortySpaces = strTenSpaces + strTenSpaces + strTenSpaces + strTenSpaces; @@ -114,12 +109,10 @@ private File targetFile = null; - public MultipartFileUploadPanel() - { + public MultipartFileUploadPanel() { final JPanel panInput = new JPanel(); - String[] aURLs = - { + String[] aURLs = { "http://localhost:8080/foo/bar" }; @@ -136,18 +129,15 @@ btnDoUpload.setEnabled(false); final JButton btnSelectFile = new JButton("Select a file..."); - btnSelectFile.addActionListener(new ActionListener() - { - public void actionPerformed(final ActionEvent evt) - { + btnSelectFile.addActionListener(new ActionListener() { + public void actionPerformed(final ActionEvent evt) { final JFileChooser chooser = new JFileChooser(); chooser.setFileHidingEnabled(false); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setMultiSelectionEnabled(false); chooser.setDialogType(JFileChooser.OPEN_DIALOG); chooser.setDialogTitle("Choose a file..."); - if (chooser.showOpenDialog(MultipartFileUploadPanel.this) == JFileChooser.APPROVE_OPTION) - { + if (chooser.showOpenDialog(MultipartFileUploadPanel.this) == JFileChooser.APPROVE_OPTION) { targetFile = chooser.getSelectedFile(); lblTargetFile.setText("Selected file: " + targetFile.toString()); btnDoUpload.setEnabled(true); @@ -162,54 +152,42 @@ final JLabel lblURL = new JLabel("URL:"); - btnDoUpload.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent ae) - { + btnDoUpload.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent ae) { URL targetURL = null; - try - { + try { targetURL = new URL(cmbURL.getSelectedItem().toString()); - } - catch (MalformedURLException ex) - { + } catch (MalformedURLException ex) { // todo - code here ex.printStackTrace(); } MultipartPostMethod filePost = new MultipartPostMethod(); - if (targetURL.getPath() == null) - { + if (targetURL.getPath() == null) { filePost.setPath("/"); - } - else - { + } else { filePost.setPath(targetURL.getPath()); } - try - { + try { filePost.addParameter( targetFile.getName(), targetFile); HttpClient client = new HttpClient(); - client.startSession(targetURL); + HostConfiguration hc = new HostConfiguration(); + hc.setHost(new URI(targetURL)); + client.setHostConfiguration(hc); client.executeMethod(filePost); - } - catch (FileNotFoundException ex) - { + filePost.releaseConnection(); + } catch (FileNotFoundException ex) { // todo - put real code here ex.printStackTrace(); - } - catch (HttpException ex) - { + } catch (HttpException ex) { // todo - put real code here ex.printStackTrace(); - } - catch (java.io.IOException ex) - { + } catch (java.io.IOException ex) { // todo - put real code here ex.printStackTrace(); } 1.4 +18 -17 jakarta-commons/httpclient/src/examples/PostXML.java Index: PostXML.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/examples/PostXML.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- PostXML.java 8 Sep 2002 05:19:06 -0000 1.3 +++ PostXML.java 17 Dec 2002 01:28:32 -0000 1.4 @@ -75,10 +75,9 @@ * to a remote web server using HTTP POST * * @author Sean C. Sullivan - * + * @author Ortwin Gl�ck */ -public class PostXML -{ +public class PostXML { /** * @@ -90,11 +89,14 @@ * Argument 1 is a local filename * */ - public static void main(String[] args) throws Exception - { - if (args.length != 2) - { - System.err.println("missing command line arguments"); + public static void main(String[] args) throws Exception { + if (args.length != 2) { + System.out.println("Usage: java -classpath [-Dorg.apache.commons.logging.simplelog.defaultlog=] PostXML ]"); + System.out.println(" - must contain the commons-httpclient.jar and commons-logging.jar"); + System.out.println(" - one of error, warn, info, debug, trace"); + System.out.println(" - the URL to post the file to"); + System.out.println(" - file to post to the URL"); + System.out.println(); System.exit(1); } @@ -112,18 +114,17 @@ post.setRequestBody(input); - if ( (u.getPath() == null) || (u.getPath().length() == 0) ) - { + if ((u.getPath() == null) || (u.getPath().length() == 0)) { post.setPath("/"); } - else - { + else { post.setPath(u.getPath()); } post.setRequestHeader("Content-type", "text/xml"); HttpClient hc = new HttpClient(); - hc.startSession(u); + HostConfiguration cfg = new HostConfiguration(); + cfg.setHost(new URI(u)); int iResultCode = hc.executeMethod(post); @@ -135,6 +136,6 @@ System.out.println( new String(yaResponse) ); - hc.endSession(); + post.releaseConnection(); } } 1.6 +31 -12 jakarta-commons/httpclient/src/examples/TrivialApp.java Index: TrivialApp.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/examples/TrivialApp.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- TrivialApp.java 26 Nov 2002 10:16:03 -0000 1.5 +++ TrivialApp.java 17 Dec 2002 01:28:32 -0000 1.6 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -61,10 +61,20 @@ * */ -import org.apache.commons.httpclient.*; -import org.apache.commons.httpclient.methods.*; -import java.net.*; -import java.io.*; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import org.apache.commons.httpclient.Credentials; +import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HostConfiguration; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.URI; +import org.apache.commons.httpclient.URIException; +import org.apache.commons.httpclient.methods.GetMethod; /** * @@ -72,7 +82,7 @@ * how to use the Jakarta HttpClient API. * * @author Jeff Dever - * + * @author Ortwin Gl�ck */ public class TrivialApp { @@ -96,9 +106,9 @@ } URL url = null; - try{ + try { url = new URL(args[0]); - }catch (MalformedURLException murle){ + } catch (MalformedURLException murle) { System.out.println(" argument '" + url + "' is not a valid URL"); System.exit(-2); @@ -120,8 +130,16 @@ client.getState().setCredentials(null, creds); } + // + HostConfiguration hc = new HostConfiguration(); + try { + hc.setHost(new URI(url)); + } catch(URIException e) { + throw new RuntimeException(e.toString()); + } + //start a session with the webserver - client.startSession(url); + client.setHostConfiguration(hc); //create a method object HttpMethod method = new GetMethod(url.getPath()); @@ -164,6 +182,7 @@ for (int i=0; i For additional commands, e-mail: