From dev-return-39202-apmail-harmony-dev-archive=harmony.apache.org@harmony.apache.org Tue Apr 20 09:55:49 2010 Return-Path: Delivered-To: apmail-harmony-dev-archive@www.apache.org Received: (qmail 41957 invoked from network); 20 Apr 2010 09:55:49 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 20 Apr 2010 09:55:49 -0000 Received: (qmail 75778 invoked by uid 500); 20 Apr 2010 09:55:49 -0000 Delivered-To: apmail-harmony-dev-archive@harmony.apache.org Received: (qmail 75563 invoked by uid 500); 20 Apr 2010 09:55:46 -0000 Mailing-List: contact dev-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list dev@harmony.apache.org Received: (qmail 75554 invoked by uid 99); 20 Apr 2010 09:55:45 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 20 Apr 2010 09:55:45 +0000 X-ASF-Spam-Status: No, hits=2.2 required=10.0 tests=FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of zhoukevin83@gmail.com designates 209.85.210.187 as permitted sender) Received: from [209.85.210.187] (HELO mail-yx0-f187.google.com) (209.85.210.187) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 20 Apr 2010 09:55:38 +0000 Received: by yxe17 with SMTP id 17so3250227yxe.20 for ; Tue, 20 Apr 2010 02:55:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=wVbt0cAdFCrlOOOCilVGAx/N3G3pr/3KG7I+0OdIyus=; b=nOlf8qT44+z27Jnnj1ITB2OX1iPYzI9ddrImPFwr/3Uhqf2VS3gF1EdLygI0TftSSn 1YGSisLUSLsGIXFVHfxDpV95doQHc9KKVF9FvYdEj508cGFDj+yjIwTfQ0KJRlxuMeLZ 74GBCQT+RU2o/Y33bRfZYllhxgZywne6VzNSM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; b=J/RGFBP7f4uFWYi733NLQYRmPdDXwoVFmiAfW+3mJftfa8BsvaFUTJRDxS1UxLRzXt Ju694WkWPozBjxzn4/MGu/XxTztejCFn1VPVFBv+2z8bH7mnOHHvWhoK8BmmEVWhA42k 1TxYGjO3Emrb2NBbIWua1L4m8lADpRoTt4Yx4= Received: by 10.101.128.25 with SMTP id f25mr10387795ann.20.1271757316511; Tue, 20 Apr 2010 02:55:16 -0700 (PDT) Received: from [9.123.233.68] ([220.248.0.145]) by mx.google.com with ESMTPS id 26sm55573255anx.3.2010.04.20.02.55.13 (version=SSLv3 cipher=RC4-MD5); Tue, 20 Apr 2010 02:55:15 -0700 (PDT) Message-ID: <4BCD79F9.2010806@gmail.com> Date: Tue, 20 Apr 2010 17:55:05 +0800 From: Kevin Zhou User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.9) Gecko/20100317 Thunderbird/3.0.4 MIME-Version: 1.0 To: dev@harmony.apache.org Subject: Re: svn commit: r935847 - in /harmony/enhanced/java/trunk/classlib/modules/luni/src: main/java/org/apache/harmony/luni/internal/net/www/protocol/file/ test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/ References: <20100420093805.2EF422388A40@eris.apache.org> In-Reply-To: <20100420093805.2EF422388A40@eris.apache.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2010-4-20 17:38, zhoukevin@apache.org wrote: > Author: zhoukevin > Date: Tue Apr 20 09:38:04 2010 > New Revision: 935847 > > URL: http://svn.apache.org/viewvc?rev=935847&view=rev > Log: > This patch implements the header related functions [getHeaderField(int)/getHeaderFieldKey(int)/getHeaderField(String)/getLastModified()] to prevent Harmony to return null when file URL is handled. > > Modified: > harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/FileURLConnection.java > harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/FileURLConnectionTest.java > > Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/FileURLConnection.java > URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/FileURLConnection.java?rev=935847&r1=935846&r2=935847&view=diff > ============================================================================== > --- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/FileURLConnection.java (original) > +++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/FileURLConnection.java Tue Apr 20 09:38:04 2010 > @@ -28,6 +28,9 @@ import java.io.InputStream; > import java.io.PrintStream; > import java.net.URL; > import java.net.URLConnection; > +import java.text.SimpleDateFormat; > +import java.util.Date; > +import java.util.LinkedHashMap; > > import org.apache.harmony.luni.internal.net.www.MimeTable; > import org.apache.harmony.luni.util.Util; > @@ -48,8 +51,12 @@ public class FileURLConnection extends U > > private boolean isDir; > > + private long lastModified = 0; > + > private FilePermission permission; > > + private LinkedHashMap header; > + > /** > * Creates an instance ofFileURLConnection for establishing > * a connection to the file pointed by thisURL > @@ -63,6 +70,7 @@ public class FileURLConnection extends U > fileName = ""; //$NON-NLS-1$ > } > fileName = Util.decode(fileName, false); > + header = new LinkedHashMap(); > } > > /** > @@ -76,15 +84,90 @@ public class FileURLConnection extends U > @Override > public void connect() throws IOException { > File f = new File(fileName); > - if (f.isDirectory()) { > - isDir = true; > + if (isDir = f.isDirectory()) { > is = getDirectoryListing(f); > - // use -1 for the contentLength > } else { > is = new BufferedInputStream(new FileInputStream(f)); > length = is.available(); > + lastModified = f.lastModified(); > } > connected = true; > + // updaetHeader must be after "connected = true", for updateHeader > + // invokes getContentType which back invokes connect causing > + // StackOverflow > + updateHeader(); > + } > + > + /** > + * Function updates the header fields of a file. > + */ > + private void updateHeader() { > + String contentType = getContentType(); > + if (contentType != null) { > + header.put("content-type", contentType); //$NON-NLS-1$ > + } > + if (length>= 0) { > + header.put("content-length", Integer.toString(length)); //$NON-NLS-1$ > + } > + if (lastModified != 0) { > + SimpleDateFormat dateFormat = new SimpleDateFormat( > + "EEE, dd MMM yyyy HH:mm:ss 'GMT'"); //$NON-NLS-1$ > + header.put("last-modified", dateFormat.format(new Date( //$NON-NLS-1$ > + lastModified))); > + } > + } > + > + @Override > + public String getHeaderField(String key) { > + try { > + if (!connected) { > + connect(); > + } > + } catch (IOException e) { > + // Ignored > + } > + return header.get(key); > + } > + > + @Override > + public String getHeaderField(int index) { > + try { > + if (!connected) { > + connect(); > + } > + } catch (IOException e) { > + // Ignored > + } > + if (index> -1&& index< header.size()) { > + return header.values().toArray(new String[0])[index]; > + } > + return null; > + } > + > + @Override > + public String getHeaderFieldKey(int index) { > + try { > + if (!connected) { > + connect(); > + } > + } catch (IOException e) { > + // Ignored > + } > + if (index> -1&& index< header.size()) { > + return header.keySet().toArray(new String[0])[index]; > + } > + return null; > + } > + > + public long getLastModified() { > + try { > + if (!connected) { > + connect(); > + } > + } catch (IOException e) { > + // default is -1 > + } > + return lastModified; > } > > /** > > Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/FileURLConnectionTest.java > URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/FileURLConnectionTest.java?rev=935847&r1=935846&r2=935847&view=diff > ============================================================================== > --- harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/FileURLConnectionTest.java (original) > +++ harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/FileURLConnectionTest.java Tue Apr 20 09:38:04 2010 > @@ -18,6 +18,7 @@ package org.apache.harmony.luni.tests.in > > import java.io.IOException; > import java.net.URL; > +import java.net.URLConnection; > > import junit.framework.TestCase; > > @@ -63,4 +64,41 @@ public class FileURLConnectionTest exten > assertNotNull(conn.getInputStream()); > assertEquals("file",conn.getURL().getProtocol()); > } > + > + public void testHeaderFunctions() throws IOException { > + String resourceName = "org/apache/harmony/luni/tests/"; // folder name > + URL url = ClassLoader.getSystemClassLoader().getResource(resourceName); > + FileURLConnection conn = new FileURLConnection(url); > + assertNotNull(conn.getInputStream()); > + assertEquals(conn.getContentType(), conn.getHeaderField("content-type")); > + > + resourceName = "org/apache/harmony/luni/tests/" + "test.rtf"; > + ; // folder name > + url = ClassLoader.getSystemClassLoader().getResource(resourceName); > + conn = new FileURLConnection(url); > + assertNotNull(conn.getInputStream()); > + assertEquals(conn.getContentType(), conn.getHeaderField("content-type")); > + assertEquals(Integer.toString(conn.getContentLength()), conn > + .getHeaderField("content-length")); > + assertEquals(conn.getHeaderField(0), conn > + .getHeaderField("content-type")); > + assertEquals(conn.getHeaderField(1), conn > + .getHeaderField("content-length")); > + assertEquals(conn.getHeaderField(2), conn > + .getHeaderField("last-modified")); > + assertEquals("last-modified", conn.getHeaderFieldKey(2)); > + assertEquals("content-length", conn.getHeaderFieldKey(1)); > + assertEquals("content-type", conn.getHeaderFieldKey(0)); > + } > + > + public void testHeader_BoundaryCheck() throws IOException { > + String resourceName = "org/apache/harmony/luni/tests/"; > + URL url = ClassLoader.getSystemClassLoader().getResource(resourceName); > + URLConnection urlConnection = url.openConnection(); > + assertNull(urlConnection.getHeaderField(Integer.MIN_VALUE)); > + assertNull(urlConnection.getHeaderField(Integer.MAX_VALUE)); > + assertNull(urlConnection.getHeaderFieldKey(Integer.MIN_VALUE)); > + assertNull(urlConnection.getHeaderFieldKey(Integer.MAX_VALUE)); > + assertNull(urlConnection.getHeaderField(null)); > + } > } > > > This patch is provided by Mohanraj Loganathan, Thanks.