Return-Path: Delivered-To: apmail-jakarta-avalon-cvs-archive@apache.org Received: (qmail 38204 invoked from network); 18 Feb 2002 12:26:04 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 18 Feb 2002 12:26:04 -0000 Received: (qmail 1346 invoked by uid 97); 18 Feb 2002 12:26:03 -0000 Delivered-To: qmlist-jakarta-archive-avalon-cvs@jakarta.apache.org Received: (qmail 1299 invoked by uid 97); 18 Feb 2002 12:26:02 -0000 Mailing-List: contact avalon-cvs-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Avalon CVS List" Reply-To: "Avalon Developers List" Delivered-To: mailing list avalon-cvs@jakarta.apache.org Received: (qmail 1288 invoked by uid 97); 18 Feb 2002 12:26:02 -0000 Date: 18 Feb 2002 12:26:02 -0000 Message-ID: <20020218122602.79962.qmail@icarus.apache.org> From: cziegeler@apache.org To: jakarta-avalon-excalibur-cvs@apache.org Subject: cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source SourceParameters.java URLSource.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 cziegeler 02/02/18 04:26:02 Modified: src/scratchpad/org/apache/avalon/excalibur/source SourceParameters.java URLSource.java Log: Added POST method Revision Changes Path 1.7 +2 -2 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/SourceParameters.java Index: SourceParameters.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/SourceParameters.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- SourceParameters.java 5 Feb 2002 09:48:07 -0000 1.6 +++ SourceParameters.java 18 Feb 2002 12:26:02 -0000 1.7 @@ -15,11 +15,11 @@ /** * This class holds parameters for a Source object. - * It differs from the usual Parameters object because in can hold + * It differs from the usual Parameters object because it can hold * more than one value for a parameter. * * @author Carsten Ziegeler - * @version $Id: SourceParameters.java,v 1.6 2002/02/05 09:48:07 cziegeler Exp $ + * @version $Id: SourceParameters.java,v 1.7 2002/02/18 12:26:02 cziegeler Exp $ */ public final class SourceParameters implements Serializable, Cloneable { 1.12 +187 -43 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/URLSource.java Index: URLSource.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/URLSource.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- URLSource.java 8 Jan 2002 13:43:48 -0000 1.11 +++ URLSource.java 18 Feb 2002 12:26:02 -0000 1.12 @@ -24,6 +24,8 @@ import java.io.*; import java.lang.reflect.Method; +import java.util.Iterator; +import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; @@ -31,12 +33,17 @@ * Description of a source which is described by an URL. * * @author Carsten Ziegeler - * @version CVS $Revision: 1.11 $ $Date: 2002/01/08 13:43:48 $ + * @version CVS $Revision: 1.12 $ $Date: 2002/02/18 12:26:02 $ */ public final class URLSource implements Composable, ModifiableSource, XMLizable, Monitorable { + /** With this parameter you can specify the method to use for a http request. + * Default is GET. + */ + public final String HTTP_METHOD = "org.apache.avalon.excalibur.source.Source.http.method"; + /** Identifier for file urls */ private final String FILE = "file:"; @@ -64,17 +71,60 @@ /** The ComponentManager needed for streaming */ private ComponentManager manager; + /** The SourceParameters */ + private SourceParameters parameters; + + /** Is this a post? */ + private boolean isPost = false; + + /** - * Construct a new object + * Construct a new object from a URL. * @param parameters This is optional */ public URLSource(URL url, SourceParameters parameters) - throws IOException { + throws IOException + { this.systemId = url.toExternalForm(); this.isFile = systemId.startsWith(FILE); this.url = url; this.gotInfos = false; + this.isPost = false; + if ( null != parameters ) { + this.parameters = (SourceParameters)parameters.clone(); + if ("POST".equalsIgnoreCase( this.parameters.getParameter(HTTP_METHOD, "GET")) ) + this.isPost = true; + this.parameters.removeParameter(HTTP_METHOD); + } + if ( !isFile + && null != this.parameters + && this.parameters.hasParameters() + && this.isPost ) + { + StringBuffer urlBuffer = new StringBuffer(this.systemId); + String key; + final Iterator i = this.parameters.getParameterNames(); + Iterator values; + String value; + boolean first = (this.systemId.indexOf('?') == -1); + if (first == true) urlBuffer.append('?'); + while ( i.hasNext() ) + { + key = (String)i.next(); + values = this.parameters.getParameterValues(key); + while (values.hasNext() == true) + { + value = SourceUtil.encode((String)values.next()); + if (first == false) urlBuffer.append('&'); + first = false; + urlBuffer.append(key); + urlBuffer.append('='); + urlBuffer.append(value); + } + } + this.url = new URL( urlBuffer.toString() ); + } } public void compose(ComponentManager manager ) @@ -87,23 +137,38 @@ * Any exceptions are ignored. */ private void getInfos() { - if (this.gotInfos == false) { - if (this.isFile == true) { + if ( !this.gotInfos ) { + if ( this.isFile ) + { File file = new File(this.systemId.substring(FILE.length())); this.lastModificationDate = file.lastModified(); this.contentLength = file.length(); - } else { - try { - if (this.connection == null) { - this.connection = this.url.openConnection(); - String userInfo = this.getUserInfo(); - if (this.url.getProtocol().startsWith("http") == true && userInfo != null) { - this.connection.setRequestProperty("Authorization","Basic "+SourceUtil.encodeBASE64(userInfo)); + } + else + { + if ( !this.isPost ) + { + try { + if ( null == this.connection ) + { + this.connection = this.url.openConnection(); + String userInfo = this.getUserInfo(); + if (this.url.getProtocol().startsWith("http") && userInfo != null) + { + this.connection.setRequestProperty("Authorization","Basic "+SourceUtil.encodeBASE64(userInfo)); + } } + this.lastModificationDate = this.connection.getLastModified(); + this.contentLength = this.connection.getContentLength(); + } + catch (IOException ignore) + { + this.lastModificationDate = 0; + this.contentLength = -1; } - this.lastModificationDate = this.connection.getLastModified(); - this.contentLength = this.connection.getContentLength(); - } catch (IOException ignore) { + } else + { + // do not open connection when using post! this.lastModificationDate = 0; this.contentLength = -1; } @@ -116,7 +181,8 @@ * Get the last modification date of the source or 0 if it * is not possible to determine the date. */ - public long getLastModified() { + public long getLastModified() + { this.getInfos(); return this.lastModificationDate; } @@ -125,11 +191,15 @@ * Get the corresponding Resource object for monitoring. */ public Resource getResource() - throws Exception { + throws Exception + { this.getInfos(); - if (this.isFile == true) { + if (this.isFile == true) + { return new FileResource(this.systemId.substring(FILE.length())); - } else { + } + else + { return new SourceResource(this); } } @@ -138,7 +208,8 @@ * Get the content length of the source or -1 if it * is not possible to determine the length. */ - public long getContentLength() { + public long getContentLength() + { this.getInfos(); return this.contentLength; } @@ -151,21 +222,70 @@ * @throws IOException if I/O error occured. */ public InputStream getInputStream() - throws IOException { + throws IOException + { this.getInfos(); InputStream input = null; - if (this.isFile == true) { + if (this.isFile == true) + { input = new FileInputStream(this.systemId.substring(FILE.length())); - } else { - if (this.connection == null) { + } + else + { + if (this.connection == null) + { this.connection = this.url.openConnection(); /* The following requires a jdk 1.3 */ String userInfo = this.getUserInfo(); - if (this.url.getProtocol().startsWith("http") == true && userInfo != null) { + if (this.url.getProtocol().startsWith("http") && userInfo != null) { this.connection.setRequestProperty("Authorization","Basic "+SourceUtil.encodeBASE64(userInfo)); } - } + // do a post operation + if (this.connection instanceof HttpURLConnection + && this.isPost ) + { + StringBuffer buffer = new StringBuffer(2000); + String key; + Iterator i = this.parameters.getParameterNames(); + Iterator values; + String value; + boolean first = true; + while ( i.hasNext() ) + { + key = (String)i.next(); + values = this.parameters.getParameterValues(key); + while (values.hasNext() == true) + { + value = SourceUtil.encode((String)values.next()); + if (first == false) buffer.append('&'); + first = false; + buffer.append(key.toString()); + buffer.append('='); + buffer.append(value); + } + } + HttpURLConnection httpCon = (HttpURLConnection)connection; + httpCon.setDoInput(true); + + if (buffer.length() > 1) + { // only post if we have parameters + String postString = buffer.toString(); + httpCon.setRequestMethod("POST"); // this is POST + httpCon.setDoOutput(true); + httpCon.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); + + // A content-length header must be contained in a POST request + httpCon.setRequestProperty("Content-length", Integer.toString(postString.length())); + java.io.OutputStream out = new java.io.BufferedOutputStream(httpCon.getOutputStream()); + out.write(postString.getBytes()); + out.close(); + } + input = httpCon.getInputStream(); + this.connection = null; // make sure a new connection is created next time + return input; + } + } input = this.connection.getInputStream(); this.connection = null; // make sure a new connection is created next time } @@ -181,25 +301,36 @@ * Check if the URL class supports the getUserInfo() * method which is introduced in jdk 1.3 */ - private String getUserInfo() { - if (URLSource.checkedURLClass == true) { - if (URLSource.urlSupportsGetUserInfo == true) { - try { + private String getUserInfo() + { + if (URLSource.checkedURLClass == true) + { + if (URLSource.urlSupportsGetUserInfo == true) + { + try + { return (String) URLSource.urlGetUserInfo.invoke(this.url, URLSource.emptyParams); - } catch (Exception e){ + } + catch (Exception e) + { // ignore this anyway } } return null; - } else { + } + else + { // test if the url class supports the getUserInfo method - try { + try + { URLSource.urlGetUserInfo = URL.class.getMethod("getUserInfo", null); String ui = (String)URLSource.urlGetUserInfo.invoke(this.url, URLSource.emptyParams); URLSource.checkedURLClass = true; URLSource.urlSupportsGetUserInfo = true; return ui; - } catch (Exception e){ + } + catch (Exception e) + { } URLSource.checkedURLClass = true; URLSource.urlSupportsGetUserInfo = false; @@ -211,7 +342,8 @@ /** * Return the unique identifer for this source */ - public String getSystemId() { + public String getSystemId() + { return this.systemId; } @@ -221,11 +353,15 @@ * If it is currently not possible to calculate such an information * null is returned. */ - public SourceValidity getValidity() { + public SourceValidity getValidity() + { final long lm = this.getLastModified(); - if (lm == -1) { + if (lm == -1) + { return null; - } else { + } + else + { return new TimeStampValidity(lm); } } @@ -234,7 +370,8 @@ * Refresh this object and update the last modified date * and content length. */ - public void discardValidity() { + public void discardValidity() + { // reset connection this.connection = null; this.gotInfos = false; @@ -264,17 +401,24 @@ throws SAXException { Parser parser = null; - try { + try + { parser = (Parser)this.manager.lookup(Parser.ROLE); parser.parse(this.getInputSource(), handler); - } catch (SAXException e) { + } + catch (SAXException e) + { // Preserve original exception throw e; - } catch (Exception e){ + } + catch (Exception e) + { throw new SAXException("Exception during processing of " + this.systemId, e); - } finally { + } + finally + { if (parser != null) this.manager.release(parser); } } -- To unsubscribe, e-mail: For additional commands, e-mail: