From 404db993968173ae20734170be3a74b65ed7b0b6 Mon Sep 17 00:00:00 2001
From: gmantele <gmantele@ari.uni-heidelberg.de>
Date: Wed, 27 Sep 2017 15:46:59 +0200
Subject: [PATCH] [UWS] Ensure optional MIME-type and size are set in the XML
 and JSON serialization of all results if such information are available.

This commit also fix few comments about the result's XML serialization.

The processing of xlink:type of a result reference is made similar as the one of
jobRef.
---
 src/org/json/Json4Uws.java                | 27 ++++++++--------
 src/uws/job/serializer/XMLSerializer.java | 38 +++++++++++++++--------
 2 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/src/org/json/Json4Uws.java b/src/org/json/Json4Uws.java
index 0dea60d..676c237 100644
--- a/src/org/json/Json4Uws.java
+++ b/src/org/json/Json4Uws.java
@@ -2,20 +2,20 @@ package org.json;
 
 /*
  * This file is part of UWSLibrary.
- * 
+ *
  * UWSLibrary 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 3 of the License, or
  * (at your option) any later version.
- * 
+ *
  * UWSLibrary 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 UWSLibrary.  If not, see <http://www.gnu.org/licenses/>.
- * 
+ *
  * Copyright 2012-2017 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
  *                       Astronomisches Rechen Institut (ARI)
  */
@@ -35,9 +35,9 @@ import uws.service.UWSUrl;
 
 /**
  * Useful conversion functions from UWS to JSON.
- * 
+ *
  * @author Gr&eacute;gory Mantelet (CDS;ARI)
- * @version 4.2 (06/2017)
+ * @version 4.2 (09/2017)
  */
 public final class Json4Uws {
 
@@ -150,24 +150,24 @@ public final class Json4Uws {
 
 	/**
 	 * Gets the JSON representation of the jobInfo of the given job.
-	 * 
+	 *
 	 * <p><b>Important note:</b>
 	 * 	This function transforms the XML returned by
 	 * 	{@link JobInfo#getXML(String)} into a JSON object
 	 * 	(see {@link XML#toJSONObject(String)}).
 	 * </p>
-	 * 
+	 *
 	 * @param job				The job whose the jobInfo must be represented
 	 *           				in JSON.
-	 * 
+	 *
 	 * @return					The JSON representation of its jobInfo.
-	 * 
+	 *
 	 * @throws JSONException	If there is an error while building the JSON
 	 *                      	object.
-	 * 
+	 *
 	 * @see JobInfo#getXML(String)
 	 * @see XML#toJSONObject(String)
-	 * 
+	 *
 	 * @since 4.2
 	 */
 	public final static JSONObject getJobInfoJson(final UWSJob job) throws JSONException{
@@ -242,7 +242,8 @@ public final class Json4Uws {
 			resultJson.put("id", r.getId());
 			resultJson.put("type", r.getType());
 			resultJson.put("href", r.getHref());
-			resultJson.put("mime", r.getMimeType());
+			if (r.getMimeType() != null)
+				resultJson.put("mime-type", r.getMimeType());
 			if (r.getSize() >= 0)
 				resultJson.put("size", r.getSize());
 			resultJson.put("redirection", r.isRedirectionRequired());
diff --git a/src/uws/job/serializer/XMLSerializer.java b/src/uws/job/serializer/XMLSerializer.java
index 159572d..3386b61 100644
--- a/src/uws/job/serializer/XMLSerializer.java
+++ b/src/uws/job/serializer/XMLSerializer.java
@@ -393,10 +393,21 @@ public class XMLSerializer extends UWSSerializer {
 				}
 				// otherwise, just return the XML parameter description:
 				else{
-					buf.append("<parameter").append(getUWSNamespace(root)).append(" id=\"").append(escapeXMLAttribute(paramName));
+					buf.append("<parameter").append(getUWSNamespace(root)).append(" id=\"").append(escapeXMLAttribute(paramName)).append('"');
+
+					// set if it is an uploaded file:
 					if (paramValue instanceof UploadFile)
-						buf.append("\" byReference=\"true");
-					buf.append("\">").append(escapeXMLData(paramValue.toString())).append("</parameter>");
+						buf.append(" byReference=\"true");
+
+					/*
+					 * Note:
+					 *   The attribute isPost is not supported in this library.
+					 *   This information is not stored by the UWS Library and
+					 *   so is never reported in the XML serialization of the
+					 *   job. Besides, this attribute is optional.
+					 */
+
+					buf.append('>').append(escapeXMLData(paramValue.toString())).append("</parameter>");
 				}
 				return buf.toString();
 			}
@@ -423,20 +434,21 @@ public class XMLSerializer extends UWSSerializer {
 	public String getResult(final Result result, final boolean root){
 		StringBuffer xml = new StringBuffer(root ? getHeader() : "");
 		xml.append("<result").append(getUWSNamespace(root)).append(" id=\"").append(escapeXMLAttribute(result.getId())).append('"');
+
+		/* The XLink attributes are optional. So if no URL is available, none
+		 * will be written here: */
 		if (result.getHref() != null){
-			if (result.getType() != null)
-				xml.append(" xlink:type=\"").append(escapeXMLAttribute(result.getType())).append('"');
+			xml.append(" xlink:type=\"").append((result.getType() == null) ? "simple" : escapeXMLAttribute(result.getType())).append('"');
 			xml.append(" xlink:href=\"").append(escapeXMLAttribute(result.getHref())).append('"');
 		}
 
-		/* NOTE: THE FOLLOWING ATTRIBUTES MAY PROVIDE USEFUL INFORMATION TO USERS, BUT THEY ARE NOT ALLOWED BY THE CURRENT UWS STANDARD.
-		 *       HOWEVER, IF, ONE DAY, THEY ARE, THE FOLLOWING LINES SHOULD BE UNCOMNENTED.
-		 *
-		 * if (result.getMimeType() != null)
-		 * 	xml.append(" mime=\"").append(escapeXMLAttribute(result.getMimeType())).append("\"");
-		 * if (result.getSize() >= 0)
-		 * 	xml.append(" size=\"").append(result.getSize()).append("\"");
-		 */
+		// Append the MIME type, if any:
+		if (result.getMimeType() != null)
+			xml.append(" mime-type=\"").append(escapeXMLAttribute(result.getMimeType())).append('"');
+
+		// Append the result size (in bytes), if any:
+		if (result.getSize() >= 0)
+			xml.append(" size=\"").append(result.getSize()).append('"');
 
 		return xml.append(" />").toString();
 	}
-- 
GitLab