Commit afce5871 authored by Grégory Mantelet's avatar Grégory Mantelet
Browse files

[UWS] Fix shifted mapping for the MIME types and the file extensions (in

UWSToolBox).

_ See GitHub Issue #100 _
parent e10489b2
Loading
Loading
Loading
Loading
+95 −90
Original line number Original line Diff line number Diff line
@@ -16,7 +16,7 @@ package uws;
 * You should have received a copy of the GNU Lesser General Public License
 * You should have received a copy of the GNU Lesser General Public License
 * along with UWSLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * along with UWSLibrary.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Copyright 2012-2017 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
 * Copyright 2012-2019 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
 *                       Astronomisches Rechen Institut (ARI)
 *                       Astronomisches Rechen Institut (ARI)
 */
 */


@@ -55,7 +55,7 @@ import uws.service.request.UploadFile;
 * Some useful functions for the managing of a UWS service.
 * Some useful functions for the managing of a UWS service.
 *
 *
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 4.2 (09/2017)
 * @version 4.3 (01/2019)
 */
 */
public class UWSToolBox {
public class UWSToolBox {


@@ -218,7 +218,8 @@ public class UWSToolBox {
				try{
				try{
					queryPart.append(URLEncoder.encode(e.getKey(), "UTF-8") + "=" + URLEncoder.encode(val, "UTF-8"));
					queryPart.append(URLEncoder.encode(e.getKey(), "UTF-8") + "=" + URLEncoder.encode(val, "UTF-8"));
					queryPart.append("&");
					queryPart.append("&");
				}catch(UnsupportedEncodingException uee){}
				}catch(UnsupportedEncodingException uee){
				}
			}
			}
		}
		}


@@ -247,7 +248,8 @@ public class UWSToolBox {
						if (!keyValue[0].isEmpty() && !keyValue[1].isEmpty()){
						if (!keyValue[0].isEmpty() && !keyValue[1].isEmpty()){
							try{
							try{
								parameters.put(URLDecoder.decode(keyValue[0], "UTF-8"), URLDecoder.decode(keyValue[1], "UTF-8"));
								parameters.put(URLDecoder.decode(keyValue[0], "UTF-8"), URLDecoder.decode(keyValue[1], "UTF-8"));
							}catch(UnsupportedEncodingException uee){}
							}catch(UnsupportedEncodingException uee){
							}
						}
						}
					}
					}
				}
				}
@@ -281,7 +283,8 @@ public class UWSToolBox {
				if (indSep >= 0){
				if (indSep >= 0){
					try{
					try{
						parameters.put(URLDecoder.decode(p.substring(0, indSep), "UTF-8"), URLDecoder.decode(p.substring(indSep + 1), "UTF-8"));
						parameters.put(URLDecoder.decode(p.substring(0, indSep), "UTF-8"), URLDecoder.decode(p.substring(indSep + 1), "UTF-8"));
					}catch(UnsupportedEncodingException uee){}
					}catch(UnsupportedEncodingException uee){
					}
				}
				}
			}
			}
		}
		}
@@ -373,7 +376,8 @@ public class UWSToolBox {
				if ((!caseSensitive && e.getKey().equalsIgnoreCase(name)) || (caseSensitive && e.getKey().equals(name)))
				if ((!caseSensitive && e.getKey().equalsIgnoreCase(name)) || (caseSensitive && e.getKey().equals(name)))
					return (e.getValue() != null) ? e.getValue() : null;
					return (e.getValue() != null) ? e.getValue() : null;
			}
			}
		}catch(Exception ex){}
		}catch(Exception ex){
		}
		return null;
		return null;
	}
	}


@@ -407,7 +411,8 @@ public class UWSToolBox {
					try{
					try{
						((UploadFile)e.getValue()).deleteFile();
						((UploadFile)e.getValue()).deleteFile();
						cnt++;
						cnt++;
					}catch(IOException ioe){}
					}catch(IOException ioe){
					}
				}
				}
			}
			}
		}
		}
@@ -646,7 +651,7 @@ public class UWSToolBox {
	protected static final String[] fileExts = new String[]{ "", "vot", "json", "json", "csv", "tsv", "txt", "xml", "xml", "pdf", "ai", "eps", "ps", "html", "zip", "gzip", "gz", "tar", "gif", "jpeg", "jpg", "png", "bmp" };
	protected static final String[] fileExts = new String[]{ "", "vot", "json", "json", "csv", "tsv", "txt", "xml", "xml", "pdf", "ai", "eps", "ps", "html", "zip", "gzip", "gz", "tar", "gif", "jpeg", "jpg", "png", "bmp" };


	/** List of known MIME types (see {@link #fileExts}). */
	/** List of known MIME types (see {@link #fileExts}). */
	protected static final String[] mimeTypes = new String[]{"application/octet-stream","application/x-votable+xml","application/json","text/json","text/csv","text/tab-separated-values","text/plain","application/xml","text/xml","application/pdf","application/postscript","application/postscript","application/postscript","text/html","application/zip","application/x-gzip","application/x-tar","image/gif","image/jpeg","image/jpeg","image/png","image/x-portable-bitmap"};
	protected static final String[] mimeTypes = new String[]{ "application/octet-stream", "application/x-votable+xml", "application/json", "text/json", "text/csv", "text/tab-separated-values", "text/plain", "application/xml", "text/xml", "application/pdf", "application/postscript", "application/postscript", "application/postscript", "text/html", "application/zip", "application/x-gzip", "application/x-gzip", "application/x-tar", "image/gif", "image/jpeg", "image/jpeg", "image/png", "image/x-portable-bitmap" };


	/**
	/**
	 * Gets the MIME type corresponding to the given file extension.
	 * Gets the MIME type corresponding to the given file extension.
+95 −0
Original line number Original line Diff line number Diff line
package uws;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Test;

public class TestUWSToolBox {

	@Test
	public void testGetMimeType(){
		// TEST: NULL => NULL
		assertNull(UWSToolBox.getMimeType(null));

		// TEST: empty string => binary
		assertEquals("application/octet-stream", UWSToolBox.getMimeType(""));
		assertEquals("application/octet-stream", UWSToolBox.getMimeType(" 	 "));

		/*
		 * Note: each test is also performed with the file name and file
		 *       extension separator(.).
		 */

		// TEST: unknown file extension => NULL
		assertNull(UWSToolBox.getMimeType("foo"));
		assertNull(UWSToolBox.getMimeType(".foo"));

		/* Ensure length of known MIME types and file extensions have the same
		 * number of items: */
		assertEquals(UWSToolBox.fileExts.length, UWSToolBox.mimeTypes.length);

		// Roughly check the MIME types and file extensions mapping:
		String ext, mime;
		/* ...1st item (i.e. binary) */
		ext = UWSToolBox.fileExts[0];
		mime = UWSToolBox.mimeTypes[0];
		assertEquals("", ext);
		assertEquals("application/octet-stream", mime);
		assertEquals(mime, UWSToolBox.getMimeType(ext));
		assertEquals(mime, UWSToolBox.getMimeType("." + ext));
		/* ...last item (i.e. bmp) */
		ext = UWSToolBox.fileExts[UWSToolBox.fileExts.length - 1];
		mime = UWSToolBox.mimeTypes[UWSToolBox.mimeTypes.length - 1];
		assertEquals("bmp", ext);
		assertEquals("image/x-portable-bitmap", mime);
		assertEquals(mime, UWSToolBox.getMimeType(ext));
		assertEquals(mime, UWSToolBox.getMimeType("." + ext));

		// TEST: VOTable mapping => ok
		assertEquals("application/x-votable+xml", UWSToolBox.getMimeType("vot"));
		assertEquals("application/x-votable+xml", UWSToolBox.getMimeType(".vot"));

		// Ensure it is not not case sensitive:
		assertEquals("application/x-votable+xml", UWSToolBox.getMimeType(".VOt"));
	}

	@Test
	public void testGetFileExtension(){
		// TEST: NULL => NULL
		assertNull(UWSToolBox.getFileExtension(null));

		// TEST: empty string => NULL
		assertNull(UWSToolBox.getFileExtension(""));
		assertNull(UWSToolBox.getFileExtension(" 	 "));

		// TEST: unknown MIME type => NULL
		assertNull(UWSToolBox.getMimeType("foo"));

		/* Ensure length of known MIME types and file extensions have the same
		 * number of items: */
		assertEquals(UWSToolBox.mimeTypes.length, UWSToolBox.fileExts.length);

		// Roughly check the MIME types and file extensions mapping:
		String ext, mime;
		/* ...1st item (i.e. binary) */
		mime = UWSToolBox.mimeTypes[0];
		ext = UWSToolBox.fileExts[0];
		assertEquals("application/octet-stream", mime);
		assertEquals("", ext);
		assertEquals(ext, UWSToolBox.getFileExtension(mime));
		/* ...last item (i.e. bmp) */
		mime = UWSToolBox.mimeTypes[UWSToolBox.mimeTypes.length - 1];
		ext = UWSToolBox.fileExts[UWSToolBox.fileExts.length - 1];
		assertEquals("image/x-portable-bitmap", mime);
		assertEquals("bmp", ext);
		assertEquals(ext, UWSToolBox.getFileExtension(mime));

		// TEST: VOTable mapping => ok
		assertEquals("vot", UWSToolBox.getFileExtension("application/x-votable+xml"));

		// Ensure it is not not case sensitive:
		assertEquals("vot", UWSToolBox.getFileExtension("application/x-VOTable+XML"));
	}

}