[teiid-commits] teiid SVN: r3374 - in trunk: build/kits/jboss-container and 15 other directories.

teiid-commits at lists.jboss.org teiid-commits at lists.jboss.org
Wed Aug 10 16:39:41 EDT 2011


Author: shawkins
Date: 2011-08-10 16:39:40 -0400 (Wed, 10 Aug 2011)
New Revision: 3374

Added:
   trunk/connectors/connector-file/src/test/java/org/
   trunk/connectors/connector-file/src/test/java/org/teiid/
   trunk/connectors/connector-file/src/test/java/org/teiid/resource/
   trunk/connectors/connector-file/src/test/java/org/teiid/resource/adapter/
   trunk/connectors/connector-file/src/test/java/org/teiid/resource/adapter/file/
   trunk/connectors/connector-file/src/test/java/org/teiid/resource/adapter/file/TestFileConnection.java
Modified:
   trunk/api/src/main/java/org/teiid/translator/FileConnection.java
   trunk/build/kits/jboss-container/teiid-examples/jca/file-ds.xml
   trunk/build/kits/jboss-container/teiid-releasenotes.html
   trunk/common-core/src/main/java/org/teiid/core/util/StringUtil.java
   trunk/connectors/connector-file/src/main/java/org/teiid/resource/adapter/file/FileConnectionImpl.java
   trunk/connectors/connector-file/src/main/java/org/teiid/resource/adapter/file/FileManagedConnectionFactory.java
   trunk/connectors/connector-file/src/main/rar/META-INF/ra.xml
   trunk/connectors/connector-file/src/main/resources/org/teiid/resource/adapter/file/i18n.properties
   trunk/connectors/translator-file/src/main/java/org/teiid/translator/file/FileExecutionFactory.java
   trunk/connectors/translator-file/src/main/resources/org/teiid/translator/file/i18n.properties
   trunk/documentation/admin-guide/src/main/docbook/en-US/content/vdb-deployment.xml
   trunk/documentation/reference/src/main/docbook/en-US/content/translators.xml
Log:
TEIID-1634 TEIID-1702 adding enhancements to file logic.

Modified: trunk/api/src/main/java/org/teiid/translator/FileConnection.java
===================================================================
--- trunk/api/src/main/java/org/teiid/translator/FileConnection.java	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/api/src/main/java/org/teiid/translator/FileConnection.java	2011-08-10 20:39:40 UTC (rev 3374)
@@ -23,6 +23,7 @@
 
 import java.io.File;
 
+import javax.resource.ResourceException;
 import javax.resource.cci.Connection;
 
 import org.teiid.core.util.FileUtils;
@@ -37,7 +38,7 @@
 	 * @param path
 	 * @return
 	 */
-	File getFile(String path);
+	File getFile(String path) throws ResourceException;
 	
 	public static class Util {
 		
@@ -48,7 +49,7 @@
 		 * @param path
 		 * @return
 		 */
-		public static File[] getFiles(String location, FileConnection fc) {
+		public static File[] getFiles(String location, FileConnection fc) throws ResourceException {
 			File datafile = fc.getFile(location);
 	        
 	        if (datafile.isDirectory()) {

Modified: trunk/build/kits/jboss-container/teiid-examples/jca/file-ds.xml
===================================================================
--- trunk/build/kits/jboss-container/teiid-examples/jca/file-ds.xml	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/build/kits/jboss-container/teiid-examples/jca/file-ds.xml	2011-08-10 20:39:40 UTC (rev 3374)
@@ -13,6 +13,17 @@
 
        <!-- Directory where the data files are stored -->
       <config-property name="ParentDirectory" type="java.lang.String">directory</config-property>
+      
+      <!-- Optional properties -->
+      
+      <!-- Set FileMapping to redirect specific relative paths (case sensitive) to alternative locations.
+           The string value specifies a map in the format key=value(,key=value)*
+           -->
+      <!-- <config-property name="FileMapping" type="java.lang.String">file1.txt=fileX.txt,file2.txt=fileY.txt</config-property> -->
+      
+      <!-- Set AllowParentPaths to false to disallow .. in paths.
+           This prevent requesting files that are not contained in the parent directory -->
+      <config-property name="AllowParentPaths" type="java.lang.Boolean">true</config-property>
             
       <max-pool-size>20</max-pool-size>
 

Modified: trunk/build/kits/jboss-container/teiid-releasenotes.html
===================================================================
--- trunk/build/kits/jboss-container/teiid-releasenotes.html	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/build/kits/jboss-container/teiid-releasenotes.html	2011-08-10 20:39:40 UTC (rev 3374)
@@ -27,6 +27,7 @@
 <H2><A NAME="Highlights"></A>Highlights</H2>
 <UL>
   <LI><B>Procedure language features</B> - Added support for compound/block statements, BEGIN [[NOT] ATOMIC], loop/block labels, and the leave statement.  See the reference for more.
+  <LI><B>File Enhancements</B> - the file translator can now optionally (via the ExceptionIfFileNotFound property) throw an exception if the path refers to a file that doesn't exist.  The file resource adapter can be configured to map file names and can prevent parent path .. references.  See the Admin Guide or the file-ds.xml template for more.
 </UL>
 
 <h2><a name="Compatibility">Compatibility Issues</a></h2>
@@ -189,6 +190,7 @@
 
 <h4>From 7.4</h4>
 <ul>
+    <li>Saxon was upgraded to 9.2.1.5
     <li>nux 1.6, and xom 1.2 were added.
 </ul>
 <h4>From 7.1</h4>

Modified: trunk/common-core/src/main/java/org/teiid/core/util/StringUtil.java
===================================================================
--- trunk/common-core/src/main/java/org/teiid/core/util/StringUtil.java	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/common-core/src/main/java/org/teiid/core/util/StringUtil.java	2011-08-10 20:39:40 UTC (rev 3374)
@@ -947,7 +947,9 @@
      */
     @SuppressWarnings("unchecked")
 	public static <T> T valueOf(String value, Class type){
-    	
+    	if (value == null) {
+    		return null;
+    	}
     	if(type == String.class) {
     		return (T) value;
     	}

Modified: trunk/connectors/connector-file/src/main/java/org/teiid/resource/adapter/file/FileConnectionImpl.java
===================================================================
--- trunk/connectors/connector-file/src/main/java/org/teiid/resource/adapter/file/FileConnectionImpl.java	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/connectors/connector-file/src/main/java/org/teiid/resource/adapter/file/FileConnectionImpl.java	2011-08-10 20:39:40 UTC (rev 3374)
@@ -23,6 +23,9 @@
 package org.teiid.resource.adapter.file;
 
 import java.io.File;
+import java.util.Collections;
+import java.util.Map;
+import java.util.regex.Pattern;
 
 import javax.resource.ResourceException;
 
@@ -36,17 +39,32 @@
 public class FileConnectionImpl extends BasicConnection implements FileConnection {
 	
 	private File parentDirectory;
+	private Map<String, String> fileMapping;
+	private boolean allowParentPaths;
+	private static final Pattern parentRef = Pattern.compile("(^\\.\\.(\\\\{2}|/)?.*)|((\\\\{2}|/)\\.\\.)"); //$NON-NLS-1$
 	
-	public FileConnectionImpl(String parentDirectory) {
+	public FileConnectionImpl(String parentDirectory, Map<String, String> fileMapping, boolean allowParentPaths) {
 		this.parentDirectory = new File(parentDirectory);
+		if (fileMapping == null) {
+			fileMapping = Collections.emptyMap();
+		}
+		this.fileMapping = fileMapping;
+		this.allowParentPaths = allowParentPaths;
 	}
 	
 	@Override
-	public File getFile(String path) {
+	public File getFile(String path) throws ResourceException {
     	if (path == null) {
     		return this.parentDirectory;
         }
-    	return new File(parentDirectory, path);	
+		String altPath = fileMapping.get(path);
+		if (altPath != null) {
+			path = altPath;
+		}
+    	if (!allowParentPaths && parentRef.matcher(path).matches()) {	
+			throw new ResourceException(FileManagedConnectionFactory.UTIL.getString("parentpath_not_allowed", path)); //$NON-NLS-1$
+		}
+		return new File(parentDirectory, path);	
     }
 
 	@Override

Modified: trunk/connectors/connector-file/src/main/java/org/teiid/resource/adapter/file/FileManagedConnectionFactory.java
===================================================================
--- trunk/connectors/connector-file/src/main/java/org/teiid/resource/adapter/file/FileManagedConnectionFactory.java	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/connectors/connector-file/src/main/java/org/teiid/resource/adapter/file/FileManagedConnectionFactory.java	2011-08-10 20:39:40 UTC (rev 3374)
@@ -21,10 +21,13 @@
  */
 package org.teiid.resource.adapter.file;
 
+import java.util.Map;
+
 import javax.resource.ResourceException;
 import javax.resource.spi.InvalidPropertyException;
 
 import org.teiid.core.BundleUtil;
+import org.teiid.core.util.StringUtil;
 import org.teiid.resource.spi.BasicConnection;
 import org.teiid.resource.spi.BasicConnectionFactory;
 import org.teiid.resource.spi.BasicManagedConnectionFactory;
@@ -35,17 +38,20 @@
 	public static final BundleUtil UTIL = BundleUtil.getBundleUtil(FileManagedConnectionFactory.class);
 
 	private String parentDirectory;
+	private String fileMapping;
+	private boolean allowParentPaths = true;
 	
 	@Override
 	public BasicConnectionFactory createConnectionFactory() throws ResourceException {
 		if (this.parentDirectory == null) {
 			throw new InvalidPropertyException(UTIL.getString("parentdirectory_not_set")); //$NON-NLS-1$
 		}
+		final Map<String, String> map = StringUtil.valueOf(this.fileMapping, Map.class);
 		return new BasicConnectionFactory() {
 			
 			@Override
 			public BasicConnection getConnection() throws ResourceException {
-				return new FileConnectionImpl(parentDirectory);
+				return new FileConnectionImpl(parentDirectory, map, allowParentPaths);
 			}
 		};
 	}
@@ -58,4 +64,20 @@
 		this.parentDirectory = parentDirectory;
 	}
 	
+	public String getFileMapping() {
+		return fileMapping;
+	}
+	
+	public void setFileMapping(String fileMapping) {
+		this.fileMapping = fileMapping;
+	}
+	
+	public boolean isAllowParentPaths() {
+		return allowParentPaths;
+	}
+	
+	public void setAllowParentPaths(boolean allowParentPaths) {
+		this.allowParentPaths = allowParentPaths;
+	}
+	
 }

Modified: trunk/connectors/connector-file/src/main/rar/META-INF/ra.xml
===================================================================
--- trunk/connectors/connector-file/src/main/rar/META-INF/ra.xml	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/connectors/connector-file/src/main/rar/META-INF/ra.xml	2011-08-10 20:39:40 UTC (rev 3374)
@@ -46,6 +46,19 @@
                <config-property-type>java.lang.String</config-property-type>
             </config-property>
             
+            <config-property>
+               <description>{$display:"File Mapping"}</description>
+               <config-property-name>FileMapping</config-property-name>
+               <config-property-type>java.lang.String</config-property-type>
+            </config-property>
+            
+             <config-property>
+               <description>{$display:"Allow Parent Paths"}</description>
+               <config-property-name>AllowParentPaths</config-property-name>
+               <config-property-type>java.lang.Boolean</config-property-type>
+               <config-property-value>true</config-property-value>
+            </config-property>
+            
             <connectionfactory-interface>javax.resource.cci.ConnectionFactory</connectionfactory-interface>
             <connectionfactory-impl-class>org.teiid.resource.spi.WrappedConnectionFactory</connectionfactory-impl-class>
             <connection-interface>javax.resource.cci.Connection</connection-interface>

Modified: trunk/connectors/connector-file/src/main/resources/org/teiid/resource/adapter/file/i18n.properties
===================================================================
--- trunk/connectors/connector-file/src/main/resources/org/teiid/resource/adapter/file/i18n.properties	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/connectors/connector-file/src/main/resources/org/teiid/resource/adapter/file/i18n.properties	2011-08-10 20:39:40 UTC (rev 3374)
@@ -20,4 +20,5 @@
 # 02110-1301 USA.
 #
 
-parentdirectory_not_set=ParentDirectory is not set
\ No newline at end of file
+parentdirectory_not_set=ParentDirectory is not set
+parentpath_not_allowed=Parent path .. not allowed in file path {0}
\ No newline at end of file

Added: trunk/connectors/connector-file/src/test/java/org/teiid/resource/adapter/file/TestFileConnection.java
===================================================================
--- trunk/connectors/connector-file/src/test/java/org/teiid/resource/adapter/file/TestFileConnection.java	                        (rev 0)
+++ trunk/connectors/connector-file/src/test/java/org/teiid/resource/adapter/file/TestFileConnection.java	2011-08-10 20:39:40 UTC (rev 3374)
@@ -0,0 +1,68 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA.
+ */
+
+package org.teiid.resource.adapter.file;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+
+import javax.resource.ResourceException;
+
+import org.junit.Test;
+import org.teiid.resource.spi.BasicConnectionFactory;
+import org.teiid.translator.FileConnection;
+
+ at SuppressWarnings("nls")
+public class TestFileConnection {
+	
+	@Test public void testFileMapping() throws Exception {
+		FileManagedConnectionFactory fmcf = new FileManagedConnectionFactory();
+		fmcf.setParentDirectory("foo");
+		fmcf.setFileMapping("x=y,z=a");
+		BasicConnectionFactory bcf = fmcf.createConnectionFactory();
+		FileConnection fc = (FileConnection)bcf.getConnection();
+		File f = fc.getFile("x");
+		assertEquals("foo/y", f.getPath());
+		f = fc.getFile("n");
+		assertEquals("foo/n", f.getPath());
+	}
+	
+	@Test(expected=ResourceException.class) public void testParentPaths() throws Exception {
+		FileManagedConnectionFactory fmcf = new FileManagedConnectionFactory();
+		fmcf.setParentDirectory("foo");
+		fmcf.setAllowParentPaths(false);
+		BasicConnectionFactory bcf = fmcf.createConnectionFactory();
+		FileConnection fc = (FileConnection)bcf.getConnection();
+		fc.getFile("../x");
+	}
+	
+	@Test public void testParentPaths1() throws Exception {
+		FileManagedConnectionFactory fmcf = new FileManagedConnectionFactory();
+		fmcf.setParentDirectory("foo");
+		fmcf.setAllowParentPaths(true);
+		BasicConnectionFactory bcf = fmcf.createConnectionFactory();
+		FileConnection fc = (FileConnection)bcf.getConnection();
+		fc.getFile("../x");
+	}
+
+}


Property changes on: trunk/connectors/connector-file/src/test/java/org/teiid/resource/adapter/file/TestFileConnection.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Modified: trunk/connectors/translator-file/src/main/java/org/teiid/translator/file/FileExecutionFactory.java
===================================================================
--- trunk/connectors/translator-file/src/main/java/org/teiid/translator/file/FileExecutionFactory.java	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/connectors/translator-file/src/main/java/org/teiid/translator/file/FileExecutionFactory.java	2011-08-10 20:39:40 UTC (rev 3374)
@@ -34,6 +34,7 @@
 import java.util.Collections;
 import java.util.List;
 
+import javax.resource.ResourceException;
 import javax.resource.cci.ConnectionFactory;
 
 import org.teiid.core.BundleUtil;
@@ -80,7 +81,15 @@
 
 		@Override
 		public void execute() throws TranslatorException {
-			files = FileConnection.Util.getFiles((String)command.getArguments().get(0).getArgumentValue().getValue(), fc);
+			String path = (String)command.getArguments().get(0).getArgumentValue().getValue();
+			try {
+				files = FileConnection.Util.getFiles(path, fc);
+			} catch (ResourceException e) {
+				throw new TranslatorException(e);
+			}
+			if (files == null && exceptionIfFileNotFound) {
+				throw new TranslatorException(UTIL.getString("file_not_found", path)); //$NON-NLS-1$
+			}
 			LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Getting", files != null ? files.length : 0, "file(s)"); //$NON-NLS-1$ //$NON-NLS-2$
 			String name = command.getProcedureName();
 			if (name.equalsIgnoreCase(GETTEXTFILES)) {
@@ -136,6 +145,7 @@
 	public static final String SAVEFILE = "saveFile"; //$NON-NLS-1$
 	
 	private Charset encoding = Charset.defaultCharset();
+	private boolean exceptionIfFileNotFound;
 	
 	@TranslatorProperty(display="File Encoding",advanced=true)
 	public String getEncoding() {
@@ -146,6 +156,15 @@
 		this.encoding = Charset.forName(encoding);
 	}
 	
+	@TranslatorProperty(display="Exception if file not found",advanced=true)
+	public boolean isExceptionIfFileNotFound() {
+		return exceptionIfFileNotFound;
+	}
+	
+	public void setExceptionIfFileNotFound(boolean exceptionIfFileNotFound) {
+		this.exceptionIfFileNotFound = exceptionIfFileNotFound;
+	}
+	
 	//@Override
 	public ProcedureExecution createProcedureExecution(final Call command,
 			final ExecutionContext executionContext, final RuntimeMetadata metadata,
@@ -178,6 +197,8 @@
 						throw new TranslatorException(e, UTIL.getString("error_writing")); //$NON-NLS-1$
 					} catch (SQLException e) {
 						throw new TranslatorException(e, UTIL.getString("error_writing")); //$NON-NLS-1$
+					} catch (ResourceException e) {
+						throw new TranslatorException(e, UTIL.getString("error_writing")); //$NON-NLS-1$
 					}
 				}
 				

Modified: trunk/connectors/translator-file/src/main/resources/org/teiid/translator/file/i18n.properties
===================================================================
--- trunk/connectors/translator-file/src/main/resources/org/teiid/translator/file/i18n.properties	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/connectors/translator-file/src/main/resources/org/teiid/translator/file/i18n.properties	2011-08-10 20:39:40 UTC (rev 3374)
@@ -22,4 +22,5 @@
 
 non_null=Expected non-null values for filePath and file
 unknown_type=Unknown type for file, was expecting an XML, CLOB, or BLOB value
-error_writing=Error writing file
\ No newline at end of file
+error_writing=Error writing file
+file_not_found=File not found {0}
\ No newline at end of file

Modified: trunk/documentation/admin-guide/src/main/docbook/en-US/content/vdb-deployment.xml
===================================================================
--- trunk/documentation/admin-guide/src/main/docbook/en-US/content/vdb-deployment.xml	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/documentation/admin-guide/src/main/docbook/en-US/content/vdb-deployment.xml	2011-08-10 20:39:40 UTC (rev 3374)
@@ -134,6 +134,17 @@
       <rar-name>teiid-connector-file.rar</rar-name>
       <connection-definition>javax.resource.cci.ConnectionFactory</connection-definition>      
       <config-property name="ParentDirectory">path-to-the-directory-of-data-file</config-property>
+      
+      <!-- Optional properties -->
+      
+      <!-- Set FileMapping to redirect specific relative paths (case sensitive) to alternative locations.
+           The string value specifies a map in the format key=value(,key=value)*
+           -->
+      <!-- <config-property name="FileMapping" type="java.lang.String">file1.txt=fileX.txt,file2.txt=fileY.txt</config-property> -->
+      
+      <!-- Set AllowParentPaths to false to disallow .. in paths.
+           This prevent requesting files that are not contained in the parent directory -->
+      <config-property name="AllowParentPaths" type="java.lang.Boolean">true</config-property>
    </no-tx-connection-factory>
 </connection-factories>]]></programlisting></example>             
         </section>

Modified: trunk/documentation/reference/src/main/docbook/en-US/content/translators.xml
===================================================================
--- trunk/documentation/reference/src/main/docbook/en-US/content/translators.xml	2011-08-10 20:35:57 UTC (rev 3373)
+++ trunk/documentation/reference/src/main/docbook/en-US/content/translators.xml	2011-08-10 20:39:40 UTC (rev 3374)
@@ -162,6 +162,11 @@
                             <entry>The encoding that should be used for CLOBs returned by the getTextFiles procedure</entry>
                             <entry>The system default encoding</entry>
                         </row>
+                        <row>
+                            <entry>ExceptionIfFileNotFound</entry>
+                            <entry>Throw an exception in getFiles or getTextFiles if the specified file/directory does not exist.</entry>
+                            <entry>false</entry>
+                        </row>
                     </tbody>
                 </tgroup>
             </table>
@@ -178,7 +183,7 @@
                 If the extension path is specified, then it will filter all of the file in the directory referenced by the base path. 
                 If the extension pattern is not specified and the path is a directory, 
                 then all files in the directory will be returned.  Otherwise the single file referenced will be returned.  If the path 
-                doesn't exist, then no results will be returned.
+                doesn't exist, then no results will be returned if ExceptionIfFileNotFound is false, otherwise an exception will be raised.
                 </para>
                 
                 <para>



More information about the teiid-commits mailing list