Commit 8c650059 authored by gmantele's avatar gmantele
Browse files

Merge branch 'vforchi-master'

Apply Pull Request #29 after a small adapation
(mostly: update of the JUnit class + addition of an error message).
parents 0422fb7d 36b4a8bf
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
apply plugin: 'java'
apply plugin: 'war'

compileJava.options.encoding = 'UTF-8'

test{
	forkEvery = 1
	include '**/Test*'
}

repositories {
    jcenter()
    mavenCentral()
@@ -28,5 +21,14 @@ dependencies {
    testCompile fileTree(dir: 'lib', include: 'astroh2-0.3.jar')
}

compileJava {
    options.encoding = "UTF-8"
}

test{
	forkEvery = 1
	include '**/Test*'
}

sourceSets.main.java.srcDirs = ["src"]
sourceSets.test.java.srcDirs = ["test"]
+30 −44
Original line number Diff line number Diff line
package tap.config;

/*
 * This file is part of TAPLibrary.
 * 
 * TAPLibrary 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.
 * 
 * TAPLibrary 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 TAPLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2016 - Astronomisches Rechen Institut (ARI)
 */

import static tap.config.TAPConfiguration.DEFAULT_ASYNC_FETCH_SIZE;
import static tap.config.TAPConfiguration.DEFAULT_DIRECTORY_PER_USER;
import static tap.config.TAPConfiguration.DEFAULT_EXECUTION_DURATION;
@@ -82,14 +63,31 @@ import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

/*
 * This file is part of TAPLibrary.
 * 
 * TAPLibrary 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.
 * 
 * TAPLibrary 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 TAPLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2016 - Astronomisches Rechen Institut (ARI)
 */

import adql.db.FunctionDef;
import adql.db.STCS;
import adql.parser.ParseException;
@@ -326,43 +324,31 @@ public final class ConfigurableServiceConnection implements ServiceConnection {
	/**
	 * <p>Resolve the given file name/path.</p>
	 * 
	 * <p>Only the URI protocol "file:" is allowed. If the protocol is different a {@link TAPException} is thrown.</p>
	 * 
	 * <p>
	 * 	If not an absolute URI, the given path may be either relative or absolute. A relative path is always considered
	 * 	If not an absolute path, the given path may be either relative or absolute. A relative path is always considered
	 * 	as relative from the Web Application directory (supposed to be given in 2nd parameter).
	 * </p>
	 * 
	 * @param filePath			URI/Path/Name of the file to get.
	 * @param filePath			Path/Name of the file to get.
	 * @param webAppRootPath	Web Application directory local path.
	 * @param propertyName		Name of the property which gives the given file path.
	 * 
	 * @return	The specified File instance.
	 *
	 * @throws TAPException	If the given URI is malformed or if the used URI scheme is different from "file:".
	 * @throws ParseException	If the given file path is a URI/URL.
	 */
	protected static final File getFile(final String filePath, final String webAppRootPath, final String propertyName) throws TAPException{
		if (filePath == null)
			return null;
		else if (filePath.matches(".*:.*"))
			throw new TAPException("Incorrect file path for the property \"" + propertyName + "\": \"" + filePath + "\"! URI/URLs are not expected here.");

		try{
			URI uri = new URI(filePath);
			if (uri.isAbsolute()){
				if (uri.getScheme().equalsIgnoreCase("file"))
					return new File(uri);
				else
					throw new TAPException("Incorrect file URI for the property \"" + propertyName + "\": \"" + filePath + "\"! Only URI with the protocol \"file:\" are allowed.");
			}else{
		File f = new File(filePath);
		if (f.isAbsolute())
			return f;
		else
			return new File(webAppRootPath, filePath);
	}
		}catch(URISyntaxException use){
			throw new TAPException("Incorrect file URI for the property \"" + propertyName + "\": \"" + filePath + "\"! Bad syntax for the given file URI.", use);
		}
	}

	/**
	 * Initialize the TAP logger with the given TAP configuration file.
+17 −17
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ package tap.resource;
 * You should have received a copy of the GNU Lesser General Public License
 * along with TAPLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2015 - Astronomisches Rechen Institut (ARI)
 * Copyright 2015-2017 - Astronomisches Rechen Institut (ARI)
 */

import java.io.BufferedReader;
@@ -51,7 +51,7 @@ import uws.service.log.UWSLog.LogLevel;
 * <p><i>See {@link #forward(String, String, HttpServletRequest, HttpServletResponse)} for more details</i></p>
 * 
 * @author Gr&eacute;gory Mantelet (ARI)
 * @version 2.1 (11/2015)
 * @version 2.1 (03/2017)
 * @since 2.1
 */
public abstract class ForwardResource implements TAPResource {
@@ -114,16 +114,16 @@ public abstract class ForwardResource implements TAPResource {

			URI uri = null;
			try{
				uri = new URI(file);
				uri = new URI(file.replaceAll(" ", "%20"));
				/* Note: the space replacement is just a convenient way to fix badly encoding URIs.
				 *       A proper way would be to encode all such incorrect URI characters (e.g. accents), but
				 *       the idea here is to focus on the most common mistake while writing manually 'file:' URIs. */

				/* If the servlet is set on the root Web Application path, a forward toward a WebContent resource won't work.
				 * The file then need to be copied "manually" in the HTTPServletResponse. For that, the trick consists to rewrite
				 * the given file path to a URI with the scheme "file://". */
				String tmpFile = null;
				if (request.getServletPath().length() == 0 && uri.getScheme() == null){
					tmpFile = "file://"+request.getServletContext().getRealPath(file);
					uri = new URI(tmpFile);
				}
				if (request.getServletPath().length() == 0 && uri.getScheme() == null)
					uri = new URI("file", null, request.getServletContext().getRealPath(file), null);

				/* CASE: FILE IN WebContent */
				if (uri.getScheme() == null){
+6 −9
Original line number Diff line number Diff line
@@ -1124,11 +1124,8 @@ public class TestConfigurableServiceConnection {
			// NULL test => NULL must be returned.
			assertNull(ConfigurableServiceConnection.getFile(null, rootPath, propertyName));

			// Valid file URI:
			path = "/custom/user/dir";
			assertEquals(path, ConfigurableServiceConnection.getFile("file://" + path, rootPath, propertyName).getAbsolutePath());

			// Valid absolute file path:
			path = "/custom/user/dir";
			assertEquals(path, ConfigurableServiceConnection.getFile(path, rootPath, propertyName).getAbsolutePath());

			// File name relative to the given rootPath:
@@ -1144,14 +1141,14 @@ public class TestConfigurableServiceConnection {
			fail("None of these tests should have failed!");
		}

		// Test with a file URI having a bad syntax:
		path = "file:#toto^foo";
		// Test with a file URI:
		path = "file:/custom/user/dir";
		try{
			ConfigurableServiceConnection.getFile(path, rootPath, propertyName);
			fail("This test should have failed, because the given file URI has a bad syntax!");
			fail("This test should have failed, because URIs are no longer supported!");
		}catch(Exception ex){
			assertEquals(TAPException.class, ex.getClass());
			assertEquals("Incorrect file URI for the property \"" + propertyName + "\": \"" + path + "\"! Bad syntax for the given file URI.", ex.getMessage());
			assertEquals("Incorrect file path for the property \"" + propertyName + "\": \"" + path + "\"! URI/URLs are not expected here.", ex.getMessage());
		}

		// Test with an URL:
@@ -1161,7 +1158,7 @@ public class TestConfigurableServiceConnection {
			fail("This test should have failed, because the given URI uses the HTTP protocol (actually, it uses a protocol different from \"file\"!");
		}catch(Exception ex){
			assertEquals(TAPException.class, ex.getClass());
			assertEquals("Incorrect file URI for the property \"" + propertyName + "\": \"" + path + "\"! Only URI with the protocol \"file:\" are allowed.", ex.getMessage());
			assertEquals("Incorrect file path for the property \"" + propertyName + "\": \"" + path + "\"! URI/URLs are not expected here.", ex.getMessage());
		}

	}