From e447a4878a5bef98553b7f2c815dc001016040bb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gr=C3=A9gory=20Mantelet?=
 <gregory.mantelet@astro.unistra.fr>
Date: Fri, 10 Aug 2018 15:12:44 +0200
Subject: [PATCH] [TAP] Return a clear error message in case of duplicated
 items in UPLOADs.

Before correction, if two uploaded tables have been submitted by the user with
the same name, or if one uploaded table contained duplicated column names, an
obscure error message coming from the database was returned to the user.

Now, duplicated items (tables and columns) are searched before ingestion in the
database. When one is detected, an error is immediately returned to the user and
the query is aborted.
---
 src/tap/upload/Uploader.java | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/tap/upload/Uploader.java b/src/tap/upload/Uploader.java
index 5abd21f..aaa8455 100644
--- a/src/tap/upload/Uploader.java
+++ b/src/tap/upload/Uploader.java
@@ -22,6 +22,7 @@ package tap.upload;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.HashSet;
 
 import com.oreilly.servlet.multipart.ExceededSizeException;
 
@@ -167,12 +168,18 @@ public class Uploader {
 	public TAPSchema upload(final DALIUpload[] uploads) throws TAPException{
 		TableIterator dataIt = null;
 		InputStream votable = null;
+		HashSet<String> tableNames = new HashSet<String>(uploads.length);
 		String tableName = null;
 		try{
 			// Iterate over the full list of uploaded tables:
 			for(DALIUpload upl : uploads){
 				tableName = upl.label;
 
+				// Check uniqueness of the table name inside TAP_UPLOAD:
+				boolean uniqueTableName = tableNames.add(tableName.toLowerCase());
+				if (!uniqueTableName)
+					throw new TAPException("Non unique table name (case insensitive) among all tables to upload: \"" + tableName + "\"!", UWSException.BAD_REQUEST);
+
 				// Open a stream toward the VOTable:
 				votable = upl.open();
 
@@ -181,6 +188,15 @@ public class Uploader {
 
 				// Define the table to upload:
 				TAPColumn[] columns = dataIt.getMetadata();
+
+				// Check uniqueness of all column names:
+				HashSet<String> columnNames = new HashSet<String>(columns.length);
+				for(TAPColumn col : columns){
+					boolean uniqueColumnName = columnNames.add(col.getADQLName().toLowerCase());
+					if (!uniqueColumnName)
+						throw new TAPException("Non unique column name (case insensitive) among all columns of the table \"" + tableName + "\": \"" + col.getADQLName() + "\"!", UWSException.BAD_REQUEST);
+				}
+
 				TAPTable table = new TAPTable(tableName);
 				table.setDBName(tableName + "_" + System.currentTimeMillis());
 				for(TAPColumn col : columns)
-- 
GitLab