From 29adc06cb1b57f3fe69600b5ba3152f1fcf7fd95 Mon Sep 17 00:00:00 2001
From: gmantele <gmantele@ari.uni-heidelberg.de>
Date: Thu, 2 Oct 2014 16:23:37 +0200
Subject: [PATCH] [TAP] Adapt the MIME type of VOTable output in function of
 the chosen data part serialization AND let setting the VOTable MIME type
 after creation (useful only when no known serialization is chosen).

---
 src/tap/formatter/VOTableFormat.java | 114 ++++++++++++++++++++++++---
 1 file changed, 103 insertions(+), 11 deletions(-)

diff --git a/src/tap/formatter/VOTableFormat.java b/src/tap/formatter/VOTableFormat.java
index eab9fc7..507a5d9 100644
--- a/src/tap/formatter/VOTableFormat.java
+++ b/src/tap/formatter/VOTableFormat.java
@@ -61,6 +61,16 @@ import adql.db.DBColumn;
  * 	The versions are: 1.0, 1.1, 1.2 and 1.3.
  * </p>
  * 
+ * <p>Note: The MIME type is automatically set in function of the given VOTable serialization:</p>
+ * <ul>
+ * 	<li><b>none or unknown</b>: equivalent to BINARY</li>
+ * 	<li><b>BINARY</b>:          "application/x-votable+xml" = "votable"</li>
+ * 	<li><b>BINARY2</b>:         "application/x-votable+xml;serialization=BINARY2" = "votable/b2"</li>
+ * 	<li><b>TABLEDATA</b>:       "application/x-votable+xml;serialization=TABLEDATA" = "votable/td"</li>
+ * 	<li><b>FITS</b>:            "application/x-votable+xml;serialization=FITS" = "votable/fits"</li>
+ * </ul>
+ * <p>It is however possible to change these default values thanks to {@link #setMimeType(String, String)}.</p>
+ * 
  * <p>In addition of the INFO elements for QUERY_STATUS="OK" and QUERY_STATUS="OVERFLOW", two additional INFO elements are written:</p>
  * <ul>
  * 	<li>PROVIDER = {@link ServiceConnection#getProviderName()} and {@link ServiceConnection#getProviderDescription()}</li>
@@ -74,7 +84,7 @@ import adql.db.DBColumn;
  * </p>
  * 
  * @author Gr&eacute;gory Mantelet (CDS;ARI)
- * @version 2.0 (09/2014)
+ * @version 2.0 (10/2014)
  */
 public class VOTableFormat implements OutputFormat {
 
@@ -90,8 +100,17 @@ public class VOTableFormat implements OutputFormat {
 	/** VOTable version in which table data must be formatted. By default, it is set to v13. */
 	protected final VOTableVersion votVersion;
 
+	protected String mimeType;
+
+	protected String shortMimeType;
+
 	/**
-	 * Creates a VOTable formatter without format report.
+	 * <p>Creates a VOTable formatter without format report.</p>
+	 * 
+	 * <p><i>Note:
+	 * 	The MIME type is automatically set to "application/x-votable+xml" = "votable".
+	 * 	It is however possible to change this default value thanks to {@link #setMimeType(String, String)}.
+	 * </i></p>
 	 * 
 	 * @param service				The service to use (for the log and to have some information about the service (particularly: name, description).
 	 * 
@@ -102,10 +121,20 @@ public class VOTableFormat implements OutputFormat {
 	}
 
 	/**
-	 * Creates a VOTable formatter.
+	 * <p>Creates a VOTable formatter.</p>
+	 * 
+	 * <i>Note: The MIME type is automatically set in function of the given VOTable serialization:</i>
+	 * <ul>
+	 * 	<li><i><b>none or unknown</b>: equivalent to BINARY</i></li>
+	 * 	<li><i><b>BINARY</b>:          "application/x-votable+xml" = "votable"</i></li>
+	 * 	<li><i><b>BINARY2</b>:         "application/x-votable+xml;serialization=BINARY2" = "votable/b2"</i></li>
+	 * 	<li><i><b>TABLEDATA</b>:       "application/x-votable+xml;serialization=TABLEDATA" = "votable/td"</i></li>
+	 * 	<li><i><b>FITS</b>:            "application/x-votable+xml;serialization=FITS" = "votable/fits"</i></li>
+	 * </ul>
+	 * <p><i>It is however possible to change these default values thanks to {@link #setMimeType(String, String)}.</i></p>
 	 * 
 	 * @param service				The service to use (for the log and to have some information about the service (particularly: name, description).
-	 * @param votFormat				Format of the VOTable data part. (TABLEDATA, BINARY, BINARY2 or FITS).
+	 * @param votFormat				Serialization of the VOTable data part. (TABLEDATA, BINARY, BINARY2 or FITS).
 	 * 
 	 * @throws NullPointerException	If the given service connection is <code>null</code>.
 	 */
@@ -114,10 +143,20 @@ public class VOTableFormat implements OutputFormat {
 	}
 
 	/**
-	 * Creates a VOTable formatter.
+	 * <p>Creates a VOTable formatter.</p>
+	 * 
+	 * <i>Note: The MIME type is automatically set in function of the given VOTable serialization:</i>
+	 * <ul>
+	 * 	<li><i><b>none or unknown</b>: equivalent to BINARY</i></li>
+	 * 	<li><i><b>BINARY</b>:          "application/x-votable+xml" = "votable"</i></li>
+	 * 	<li><i><b>BINARY2</b>:         "application/x-votable+xml;serialization=BINARY2" = "votable/b2"</i></li>
+	 * 	<li><i><b>TABLEDATA</b>:       "application/x-votable+xml;serialization=TABLEDATA" = "votable/td"</i></li>
+	 * 	<li><i><b>FITS</b>:            "application/x-votable+xml;serialization=FITS" = "votable/fits"</i></li>
+	 * </ul>
+	 * <p><i>It is however possible to change these default values thanks to {@link #setMimeType(String, String)}.</i></p>
 	 * 
 	 * @param service				The service to use (for the log and to have some information about the service (particularly: name, description).
-	 * @param votFormat				Format of the VOTable data part. (TABLEDATA, BINARY, BINARY2 or FITS).
+	 * @param votFormat				Serialization of the VOTable data part. (TABLEDATA, BINARY, BINARY2 or FITS).
 	 * @param votVersion			Version of the resulting VOTable.
 	 * 
 	 * @throws NullPointerException	If the given service connection is <code>null</code>.
@@ -127,7 +166,12 @@ public class VOTableFormat implements OutputFormat {
 	}
 
 	/**
-	 * Creates a VOTable formatter.
+	 * <p>Creates a VOTable formatter.</p>
+	 * 
+	 * <p><i>Note:
+	 * 	The MIME type is automatically set to "application/x-votable+xml" = "votable".
+	 * 	It is however possible to change this default value thanks to {@link #setMimeType(String, String)}.
+	 * </i></p>
 	 * 
 	 * @param service				The service to use (for the log and to have some information about the service (particularly: name, description).
 	 * @param logFormatReport		<code>true</code> to append a format report (start and end date/time) in the log output, <code>false</code> otherwise.
@@ -139,10 +183,20 @@ public class VOTableFormat implements OutputFormat {
 	}
 
 	/**
-	 * Creates a VOTable formatter.
+	 * <p>Creates a VOTable formatter.</p>
+	 * 
+	 * <i>Note: The MIME type is automatically set in function of the given VOTable serialization:</i>
+	 * <ul>
+	 * 	<li><i><b>none or unknown</b>: equivalent to BINARY</i></li>
+	 * 	<li><i><b>BINARY</b>:          "application/x-votable+xml" = "votable"</i></li>
+	 * 	<li><i><b>BINARY2</b>:         "application/x-votable+xml;serialization=BINARY2" = "votable/b2"</i></li>
+	 * 	<li><i><b>TABLEDATA</b>:       "application/x-votable+xml;serialization=TABLEDATA" = "votable/td"</i></li>
+	 * 	<li><i><b>FITS</b>:            "application/x-votable+xml;serialization=FITS" = "votable/fits"</i></li>
+	 * </ul>
+	 * <p><i>It is however possible to change these default values thanks to {@link #setMimeType(String, String)}.</i></p> 
 	 * 
 	 * @param service				The service to use (for the log and to have some information about the service (particularly: name, description).
-	 * @param votFormat				Format of the VOTable data part. (TABLEDATA, BINARY, BINARY2 or FITS).
+	 * @param votFormat				Serialization of the VOTable data part. (TABLEDATA, BINARY, BINARY2 or FITS).
 	 * @param votVersion			Version of the resulting VOTable.
 	 * @param logFormatReport		<code>true</code> to append a format report (start and end date/time) in the log output, <code>false</code> otherwise.
 	 * 
@@ -154,18 +208,56 @@ public class VOTableFormat implements OutputFormat {
 
 		this.service = service;
 		this.logFormatReport = logFormatReport;
+
+		// Set the VOTable serialization and version:
 		this.votFormat = (votFormat == null) ? DataFormat.BINARY : votFormat;
 		this.votVersion = (votVersion == null) ? VOTableVersion.V13 : votVersion;
+
+		// Deduce automatically the MIME type and its short expression:
+		if (this.votFormat.equals(DataFormat.BINARY)){
+			this.mimeType = "application/x-votable+xml";
+			this.shortMimeType = "votable";
+		}else if (this.votFormat.equals(DataFormat.BINARY2)){
+			this.mimeType = "application/x-votable+xml;serialization=BINARY2";
+			this.shortMimeType = "votable/b2";
+		}else if (this.votFormat.equals(DataFormat.TABLEDATA)){
+			this.mimeType = "application/x-votable+xml;serialization=TABLEDATA";
+			this.shortMimeType = "votable/td";
+		}else if (this.votFormat.equals(DataFormat.FITS)){
+			this.mimeType = "application/x-votable+xml;serialization=FITS";
+			this.shortMimeType = "votable/fits";
+		}else{
+			this.mimeType = "application/x-votable+xml";
+			this.shortMimeType = "votable";
+		}
 	}
 
 	@Override
 	public final String getMimeType(){
-		return "application/xml";
+		return mimeType;
 	}
 
 	@Override
 	public final String getShortMimeType(){
-		return "votable";
+		return shortMimeType;
+	}
+
+	/**
+	 * <p>Set the MIME type associated with this format.</p>
+	 * 
+	 * <p><i>Note:
+	 * 	Nothing will be done if the given MIME type is NULL.
+	 * 	But the short form may be NULL.
+	 * </i></p>
+	 * 
+	 * @param mimeType	Full MIME type of this VOTable format.	<i>note: if NULL, this function does nothing</i>
+	 * @param shortForm	Short form of this MIME type. <i>note: MAY be NULL</i>
+	 */
+	public final void setMimeType(final String mimeType, final String shortForm){
+		if (mimeType != null){
+			this.mimeType = mimeType;
+			this.shortMimeType = shortForm;
+		}
 	}
 
 	@Override
-- 
GitLab