ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seanfi...@apache.org
Subject svn commit: r1885305 - in /ctakes/trunk: ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/ ctakes-core/src/main/java/org/apache/ctakes/core/util/doc/ ctakes-dictionary-lookup-fast-res/src/main/resources/org/apache/ctakes/dictionary/lookup/fast...
Date Sat, 09 Jan 2021 18:01:25 GMT
Author: seanfinan
Date: Sat Jan  9 18:01:24 2021
New Revision: 1885305

URL: http://svn.apache.org/viewvc?rev=1885305&view=rev
Log:
CliOptions : added umls api key
JCasBuilder : always set doc text
UmlsUserApprover : Umls Api Key updates
DictionarySubPipe.piper : added umlsKey
TODO : remove hardcoded dictionary cli in favor of piper cli

Modified:
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/CliOptionalsHandler.java
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/StandardCliOptions.java
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/doc/JCasBuilder.java
    ctakes/trunk/ctakes-dictionary-lookup-fast-res/src/main/resources/org/apache/ctakes/dictionary/lookup/fast/pipeline/DictionarySubPipe.piper
    ctakes/trunk/ctakes-dictionary-lookup-fast-res/src/main/resources/org/apache/ctakes/dictionary/lookup/fast/pipeline/TsDictionarySubPipe.piper
    ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/util/UmlsUserApprover.java
    ctakes/trunk/ctakes-dictionary-lookup-fast/src/test/java/org/apache/ctakes/dictionary/lookup2/util/UmlsUserTest.java

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/CliOptionalsHandler.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/CliOptionalsHandler.java?rev=1885305&r1=1885304&r2=1885305&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/CliOptionalsHandler.java
(original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/CliOptionalsHandler.java
Sat Jan  9 18:01:24 2021
@@ -31,14 +31,18 @@ final public class CliOptionalsHandler {
             return optionals.getSubDirectory();
          case "l":
             return optionals.getLookupXml();
-         case "user":
+         case StandardCliOptions.UMLS_USER:
             return optionals.getUmlsUserName();
-         case "pass":
+         case StandardCliOptions.UMLS_PASS:
             return optionals.getUmlsPassword();
-         case "xmiOut":
+         case StandardCliOptions.UMLS_KEY:
+            return optionals.getUmlsApiKey();
+         case StandardCliOptions.XMI_OUT_DIR:
             return optionals.getXmiOutDirectory();
-         case "htmlOut":
+         case StandardCliOptions.HTML_OUT_DIR:
             return optionals.getHtmlOutDirectory();
+         case StandardCliOptions.SUB_DIR:
+            return optionals.getSubDirectory();
          case "a":
             return optionals.getOption_a();
          case "b":

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/StandardCliOptions.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/StandardCliOptions.java?rev=1885305&r1=1885304&r2=1885305&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/StandardCliOptions.java
(original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/StandardCliOptions.java
Sat Jan  9 18:01:24 2021
@@ -9,70 +9,92 @@ import com.lexicalscope.jewel.cli.Option
  * @version %I%
  * @since 1/7/2017
  */
-interface StandardCliOptions {
+public interface StandardCliOptions {
+
+   static public final String UMLS_KEY = "key";
+   static public final String UMLS_USER = "user";
+   static public final String UMLS_PASS = "pass";
+   static public final String LOOKUP_XML = "lookupXml";
+   static public final String PIPER_FILE = "piper";
+   static public final String INPUT_DIR = "inputDir";
+   static public final String OUTPUT_DIR = "outputDir";
+   static public final String SUB_DIR = "subDir";
+   static public final String XMI_OUT_DIR = "xmiOut";
+   static public final String HTML_OUT_DIR = "htmlOut";
+
+
    @Option(
          shortName = "p",
-         longName = "piper",
+         longName = PIPER_FILE,
          description = "path to the piper file containing commands and parameters for pipeline
configuration.",
          defaultValue = "" )
    String getPiperPath();
 
    @Option(
          shortName = "i",
-         longName = "inputDir",
+         longName = INPUT_DIR,
          description = "path to the directory containing the clinical notes to be processed.",
          defaultValue = "" )
    String getInputDirectory();
 
    @Option(
          shortName = "o",
-         longName = "outputDir",
+         longName = OUTPUT_DIR,
          description = "path to the directory where the output files are to be written.",
          defaultValue = "" )
    String getOutputDirectory();
 
    @Option(
          shortName = "s",
-         longName = "subDir",
+         longName = SUB_DIR,
          description = "path to a subdirectory for output files.",
          defaultValue = "" )
    String getSubDirectory();
 
    @Option(
-         longName = "xmiOut",
+         longName = XMI_OUT_DIR,
          description = "path to the directory where xmi files are to be written.  Adds XmiWriter
to pipeline.",
          defaultValue = "" )
    String getXmiOutDirectory();
 
    @Option(
-         longName = "htmlOut",
+         longName = HTML_OUT_DIR,
          description = "path to the directory where html files are to be written.  Adds HtmlWriter
to pipeline.",
          defaultValue = "" )
    String getHtmlOutDirectory();
 
    @Option(
          shortName = "l",
-         longName = "lookupXml",
+         longName = LOOKUP_XML,
          description = "path to the xml file containing information for dictionary lookup
configuration.",
          defaultValue = "" )
    String getLookupXml();
 
    @Option(
-         longName = "user",
+         longName = UMLS_USER,
          description = "UMLS username.",
          defaultValue = "" )
    String getUmlsUserName();
 
    @Option(
-         longName = "pass",
+         longName = UMLS_PASS,
          description = "UMLS user password.",
          defaultValue = "" )
    String getUmlsPassword();
 
+
+   @Option(
+         longName = UMLS_KEY,
+         description = "UMLS API Key.",
+         defaultValue = "" )
+   String getUmlsApiKey();
+
+
    @Option(
          shortName = "?",
          longName = "help",
          description = "print usage.",
          helpRequest = true )
    boolean isHelpWanted();
+
 }

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/doc/JCasBuilder.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/doc/JCasBuilder.java?rev=1885305&r1=1885304&r2=1885305&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/doc/JCasBuilder.java
(original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/doc/JCasBuilder.java
Sat Jan  9 18:01:24 2021
@@ -290,9 +290,9 @@ final public class JCasBuilder {
          documentPath.addToIndexes();
       }
 
-      if ( ifWrite( _docText, "" ) ) {
+//      if ( ifWrite( _docText, "" ) ) {
          jCas.setDocumentText( _docText );
-      }
+//      }
 
       return jCas;
    }

Modified: ctakes/trunk/ctakes-dictionary-lookup-fast-res/src/main/resources/org/apache/ctakes/dictionary/lookup/fast/pipeline/DictionarySubPipe.piper
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-dictionary-lookup-fast-res/src/main/resources/org/apache/ctakes/dictionary/lookup/fast/pipeline/DictionarySubPipe.piper?rev=1885305&r1=1885304&r2=1885305&view=diff
==============================================================================
--- ctakes/trunk/ctakes-dictionary-lookup-fast-res/src/main/resources/org/apache/ctakes/dictionary/lookup/fast/pipeline/DictionarySubPipe.piper
(original)
+++ ctakes/trunk/ctakes-dictionary-lookup-fast-res/src/main/resources/org/apache/ctakes/dictionary/lookup/fast/pipeline/DictionarySubPipe.piper
Sat Jan  9 18:01:24 2021
@@ -5,6 +5,7 @@ cli LookupXml=l
 // umls credentials
 cli umlsUser=user
 cli umlsPass=pass
+cli umlsKey=key
 
 // Default fast dictionary lookup
 add DefaultJCasTermAnnotator

Modified: ctakes/trunk/ctakes-dictionary-lookup-fast-res/src/main/resources/org/apache/ctakes/dictionary/lookup/fast/pipeline/TsDictionarySubPipe.piper
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-dictionary-lookup-fast-res/src/main/resources/org/apache/ctakes/dictionary/lookup/fast/pipeline/TsDictionarySubPipe.piper?rev=1885305&r1=1885304&r2=1885305&view=diff
==============================================================================
--- ctakes/trunk/ctakes-dictionary-lookup-fast-res/src/main/resources/org/apache/ctakes/dictionary/lookup/fast/pipeline/TsDictionarySubPipe.piper
(original)
+++ ctakes/trunk/ctakes-dictionary-lookup-fast-res/src/main/resources/org/apache/ctakes/dictionary/lookup/fast/pipeline/TsDictionarySubPipe.piper
Sat Jan  9 18:01:24 2021
@@ -5,6 +5,7 @@ cli LookupXml=l
 // umls credentials
 cli umlsUser=user
 cli umlsPass=pass
+cli umlsKey=key
 
 // Default fast dictionary lookup
 add concurrent.ThreadSafeFastLookup

Modified: ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/util/UmlsUserApprover.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/util/UmlsUserApprover.java?rev=1885305&r1=1885304&r2=1885305&view=diff
==============================================================================
--- ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/util/UmlsUserApprover.java
(original)
+++ ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/util/UmlsUserApprover.java
Sat Jan  9 18:01:24 2021
@@ -18,7 +18,7 @@
  */
 package org.apache.ctakes.dictionary.lookup2.util;
 
-import org.apache.ctakes.core.ae.UmlsEnvironmentConfiguration;
+import org.apache.ctakes.core.pipeline.StandardCliOptions;
 import org.apache.ctakes.core.util.log.DotLogger;
 import org.apache.ctakes.utils.env.EnvironmentVariable;
 import org.apache.log4j.Logger;
@@ -39,12 +39,11 @@ import java.util.Properties;
  * <p/>
  * TODO Authentication before download would be nice, or perhaps an encrypted
  * download Author: SPF Affiliation: CHIP-NLP Date: 2/19/14
- * 
- * UPDATED to use the API_KEY based authentication scheme of the UMLS
- * For maximum compatibility with existing configurations we permit
- * umls_user = "umls_api_key"  and umls_password = "<APIKEY>"  settings
- * or simply set the ctakes.umls_apikey system property
- * pabramowitsch (11/2020)
+ * <p/>
+ * UPDATED to use the API_KEY based authentication scheme of the UMLS For
+ * maximum compatibility with existing configurations we permit umls_user =
+ * "umls_api_key" and umls_password = "<APIKEY>" settings or simply set the
+ * ctakes.umls_apikey system property pabramowitsch (11/2020)
  */
 public enum UmlsUserApprover {
 
@@ -54,228 +53,207 @@ public enum UmlsUserApprover {
 		return INSTANCE;
 	}
 
-	// cli, matches new
-	static private final String USER_CLI = "--user";
-	static private final String PASS_CLI = "--pass";
-
 	// properties, matches new
+	public final static String OLD_USER_PARAM = "ctakes.umlsuser";
+	public final static String OLD_PASS_PARAM = "ctakes.umlspw";
+
 	public final static String URL_PARAM = "umlsUrl";
-	public final static String VENDOR_PARAM = "umlsVendor";
 	public final static String USER_PARAM = "umlsUser";
 	public final static String PASS_PARAM = "umlsPass";
+	public final static String KEY_PARAM = "umlsKey";
+
 	public final static String API_KEY_LABEL = "umls_api_key";
-	public final static String API_KEY_PROP = "ctakes.umls_apikey";
+	public final static String OLDY_KEY_PROP = "ctakes.umls_apikey";
 
-	static final private Logger LOGGER = Logger.getLogger("UmlsUserApprover");
+	static final private Logger LOGGER = Logger.getLogger( "UmlsUserApprover" );
 
 	static final private String CHANGEME = "CHANGEME";
 	static final private String CHANGE_ME = "CHANGE_ME";
 	// forget about copies of this URL sprinkled around the other libraries
+	static private final String OLD_USER_PASS_URL = "https://uts-ws.nlm.nih.gov/restful/isValidUMLSUser";
 	static final private String UTS_APIKEY_URL = "https://utslogin.nlm.nih.gov/cas/v1/api-key";
 
+	static private final String WHERE_DEFAULT = "Default Value";
+	static private final String WHERE_ENV = "User Environment or Piper Variable";
+	static private final String WHERE_XML = "Property Xml";
+
+	static private final String NEW_AUTH_MESSAGE
+			= "\n\n\n"
+			  + "Starting 2021 NIH is using a new method for UMLS license authentication.  \n\n"
+			  + "To use the UMLS dictionary you must obtain a UMLS API Key.  \n"
+			  + "After obtaining a Key, there are several methods to utilize it with Apache cTAKES.
 \n\n"
+			  + "You may specify the value of your Key with a single parameter: \n"
+			  + OLDY_KEY_PROP + " in your Operating System, \n"
+			  + KEY_PARAM + " in your Operating System, \n"
+			  + KEY_PARAM + " in your Piper File, \n"
+			  + KEY_PARAM + " in your Dictionary Properties XML, \n"
+			  + "--" + StandardCliOptions.UMLS_KEY + " in your Piper Runner command line, \n"
+			  + "--" + StandardCliOptions.UMLS_KEY + " in your Clinical Pipeline Script command line,
\n"
+			  + "-D" + OLDY_KEY_PROP + " in your Java command parameters, or \n"
+			  + "-D" + KEY_PARAM + " in your Java command parameters.  \n\n"
+			  + "The single key settings above will be preferred over the pre-2021 [UserName , Password]
method.  \n"
+			  + "The pre-2021 [UserName , Password] method may still be used if the username is set
to \n"
+			  + API_KEY_LABEL + " and the password is set to the value of your Key.  \n\n"
+			  + "For more information visit \n"
+			  + "https://cwiki.apache.org/confluence/display/CTAKES/cTAKES+4.0.0.1 \n"
+			  + "https://uts.nlm.nih.gov/ \n\n\n";
 
 	// cache of valid users
 	static private final Collection<String> _validUsers = new ArrayList<>();
 
+
 	/**
 	 * validate the UMLS license / user
 	 *
-	 * @param uimaContext
-	 *            contains information about the UMLS license / user
-	 * @param properties
-	 *            possibly containing the attribs we need
-	 *            If not, we will look in the environment and sysprops.
-	 * @return true if the server at umlsaddr approves of the vendor, user,
-	 *         password combination
+	 * @param uimaContext contains information about the UMLS license / user
+	 * @param properties  possibly containing the attribs we need If not, we will
+	 *                    look in the environment and sysprops.
+	 * @return true if the server at umlsaddr approves of the vendor, user, password
+	 * combination
 	 */
-	public boolean isValidUMLSUser(final UimaContext uimaContext,
-			final Properties properties) {
-
-		String apiUrl = getUrl(properties);
-		String umlsApiKey = getSingleApiProp(properties, uimaContext);
-		
-		if (umlsApiKey == null) {
-			// emulate U&P style 
-			String user = getUser( uimaContext, properties );
-			if ( user == null || !user.equals( API_KEY_LABEL ) ) {
-				LOGGER.error( "USER AND PASSWORD MUST BE umls_api_key and your <umls_api_key>"
);
-				return false;
-			}
-
-			String pass = getPassOrKey( uimaContext, properties );
-			if ( pass == null || pass.contains( "CHANGE" ) || pass.length() <= 24 ) {
-				LOGGER.error( "Upgrade to a UMLS API KEY.  Password no longer accepted" );
-				return false;
-			}
-			return isValidUMLSUser( apiUrl, pass );
-		}
-		return isValidUMLSUser(apiUrl, umlsApiKey);
+	public boolean isValidUMLSUser( final UimaContext uimaContext, final Properties properties
) {
+		final String apiUrl = getUrl( properties );
+		final String umlsApiKey = getApiKey( uimaContext, properties );
+		if ( umlsApiKey != null ) {
+			return isValidUMLSUser( apiUrl, umlsApiKey );
+		}
+		// emulate U&P style
+		final String user = getUser( uimaContext, properties );
+		final String pass = getPassOrKey( uimaContext, properties );
+		return isValidUMLSUser( apiUrl, null, user, pass );
 	}
 
 	/**
-	 * See if the user has simply supplied the API key using its own property name
+	 * validate the UMLS license / user Functionality overridden to deal with new
+	 * UMLS API
 	 *
-	 * @param properties  -
-	 * @param uimaContext -
-	 * @return -
+	 * @param umlsUrl -
+	 * @param vendor  IGNORED -
+	 * @param user    NOW needs to be the value "umls_api_key" -
+	 * @param apikey  THE API KEY -
+	 * @return true if the server at umlsaddr approves of the vendor, user, password
+	 * combination
 	 */
-	private String getSingleApiProp(final Properties properties, UimaContext uimaContext) {
-		String umlsApiKey = EnvironmentVariable.getEnv(API_KEY_PROP, uimaContext);
-		LOGGER.debug("The apikey was " + umlsApiKey );
-		return umlsApiKey;
-	}
-	
-	private String getUrl() {
-		// get explicitly from the JVM in case any component
-		// still has the old URL mentioned
-		return getUrl(System.getProperties());
-	}
-
-	private String getUrl(final Properties properties) {
-		String where = "environment";
-		String umlsUrl = EnvironmentVariable.getEnv(
-				UmlsEnvironmentConfiguration.URL.toString());
-		if (umlsUrl == null || umlsUrl.equals(EnvironmentVariable.NOT_PRESENT)) {
-			umlsUrl = properties.getProperty(URL_PARAM);
-			if (umlsUrl != null) {
-				where = "properties";
-			}
-		}
-		if (umlsUrl == null) {
-			umlsUrl = UTS_APIKEY_URL;
-			where = "default";
-		} else {
-			LOGGER.warn("Using alternate umlsURL found via: " + where);
+	public boolean isValidUMLSUser( String umlsUrl, final String vendor, final String user,
final String apikey ) {
+		if ( !isValid( USER_PARAM, user ) || !user.equals( API_KEY_LABEL ) ) {
+			LOGGER.error( NEW_AUTH_MESSAGE );
+			return false;
 		}
-		LOGGER.debug("umlsUrl found via :" + where);
-		return umlsUrl;
-	}
-
-	private String getPassOrKey(final UimaContext uimaContext,
-			final Properties properties ) {
-		String where = "environment";
-		String pass = EnvironmentVariable.getEnv(
-				UmlsEnvironmentConfiguration.PASSWORD.toString(),
-				uimaContext );
-		if ( pass == null || pass.equals( EnvironmentVariable.NOT_PRESENT )
-			  || pass.contains( "CHANGE" ) ) {
-			pass = EnvironmentVariable.getEnv( PASS_PARAM, uimaContext );
-			if ( pass == null
-				  || pass.equals( EnvironmentVariable.NOT_PRESENT )
-				  || pass.contains( "CHANGE" ) ) {
-				pass = properties.getProperty( PASS_PARAM );
-				where = "properties";
-			}
+		if ( !isValid( PASS_PARAM, apikey ) || apikey.length() <= 24 ) {
+			LOGGER.error( NEW_AUTH_MESSAGE );
+			return false;
 		}
-		if ( pass != null ) {
-			LOGGER.debug( "ApiKey value found via: " + where );
+		if ( _validUsers.contains( apikey ) ) {
+			return true;
 		}
-		return pass;
-	}
-
-	private String getUser(final UimaContext uimaContext,
-			final Properties properties) {
-		String where = "environment";
-		String user = EnvironmentVariable.getEnv(
-				UmlsEnvironmentConfiguration.USER.toString(), uimaContext);
-		if (user == null || user.equals(EnvironmentVariable.NOT_PRESENT)
-				|| user.equals(CHANGEME) || user.equals(CHANGE_ME)) {
-			user = EnvironmentVariable.getEnv( USER_PARAM, uimaContext );
-			if ( user == null
-				  || user.equals( EnvironmentVariable.NOT_PRESENT )
-				  || user.contains( "CHANGE" ) ) {
-				user = properties.getProperty( USER_PARAM );
-				where = "properties";
+		// last chance for an override
+		if ( !isValid( umlsUrl ) ) {
+			umlsUrl = getUrl();
+			if ( !isValid( umlsUrl ) ) {
+				umlsUrl = UTS_APIKEY_URL;
 			}
 		}
-		if (user != null)
-			LOGGER.debug("ApiKey Label found via: " + where);
-		return user;
+		return isValidUMLSUser( umlsUrl, apikey );
 	}
 
 	/**
 	 * New UTS authentication method
-	 * @param umlsUrl -
+	 *
+	 * @param umlsUrl    -
 	 * @param umlsApiKey -
-	 * @return -
+	 * @return true if the umls api key can be authenticated
 	 */
-	private boolean isValidUMLSUser(String umlsUrl, String umlsApiKey) {
-		if (_validUsers.contains(umlsApiKey)) {
+	static private boolean isValidUMLSUser( final String umlsUrl, final String umlsApiKey )
{
+		if ( _validUsers.contains( umlsApiKey ) ) {
 			return true;
 		}
-		return doAuth(umlsUrl, umlsApiKey );
+		return authenticate( umlsUrl, umlsApiKey );
 	}
 
 	/**
-	 * validate the UMLS license / user
-	 *  Functionality overridden to deal with new UMLS API
-	 *
-	 * @param umlsUrl
-	 *            -
-	 * @param vendor   IGNORED
-	 *            -
-	 * @param user     NOW needs to be the value "umls_api_key"
-	 *            -
-	 * @param apikey      THE API KEY
-	 *            -
-	 * @return true if the server at umlsaddr approves of the vendor, user,
-	 *         password combination
+	 * @return the url to the authentication service
 	 */
-	public boolean isValidUMLSUser(String umlsUrl, final String vendor,
-			final String user, final String apikey ) {
+	static private String getUrl() {
+		// get explicitly from the JVM in case any component
+		// still has the old URL mentioned
+		return getUrl( System.getProperties() );
+	}
 
-		if ( user == null || user.trim().isEmpty() ||
-			  !user.equals( API_KEY_LABEL ) ) {
-			LOGGER.error( "The user property must now be set to \'umls_api_key\' " );
-			logCheckUser();
-			return false;
-		}
-		
-		if (apikey == null || apikey.trim().isEmpty()) {
-			LOGGER.error("No Api Key supplied");
-			logCheckPass();
-			return false;
+	/**
+	 * @param properties properties from either the xml file or the user environment
+	 * @return a user defined url or the default
+	 */
+	static private String getUrl( final Properties properties ) {
+		String where = WHERE_ENV;
+		String umlsUrl = EnvironmentVariable.getEnv( URL_PARAM );
+		if ( !isValid( umlsUrl ) || umlsUrl.equals( OLD_USER_PASS_URL ) ) {
+			umlsUrl = properties.getProperty( URL_PARAM );
+			if ( isValid( umlsUrl ) && !umlsUrl.equals( OLD_USER_PASS_URL ) ) {
+				where = WHERE_XML;
+			}
 		}
-		
-		if (apikey.length() < 24) {
-			LOGGER.error("The password property should be a valid UTS apikey string");
-			return false;
+		if ( !isValid( umlsUrl ) || umlsUrl.equals( OLD_USER_PASS_URL ) ) {
+			umlsUrl = UTS_APIKEY_URL;
+			where = WHERE_DEFAULT;
 		}
+		LOGGER.debug( "Using umlsURL set using: " + where );
+		return umlsUrl;
+	}
 
-		if (_validUsers.contains(apikey)) {
-			return true;
-		}
-		
-		// Potentially someone could have a user ID of CHANGEME or a password of
-		// CHANGEME but don't allow those
-		// to make it easy for us to detect that the user or password was not
-		// set correctly.
-		
-		if (user.equals(CHANGEME) || user.equals(CHANGE_ME)) {
-			LOGGER.error("  User " + user
-					+ " not allowed.  It is a placeholder reminder.");
-			logCheckUser();
-			return false;
+
+	/**
+	 * Check for parameter value in the uimacontext, environment and properties xml.
+	 *
+	 * @param uimaContext -
+	 * @param properties  -
+	 * @param name        -
+	 * @param oldParam    the old environment variable style.  ctakes.umls{something}
+	 * @param param       the new environment variable or piper file style.  umls{something}
+	 * @return the obtained value or null
+	 */
+	static private String getParamValue( final UimaContext uimaContext, final Properties properties,
+													 final String name,
+													 final String oldParam, final String param ) {
+		String where = WHERE_ENV + " " + param;
+		String value = EnvironmentVariable.getEnv( param, uimaContext );
+		if ( !isValid( value ) ) {
+			value = properties.getProperty( param );
+			if ( isValid( param, value ) ) {
+				where = WHERE_XML + " " + param;
+			} else {
+				value = null;
+			}
 		}
-		
-		if (apikey.equals(CHANGEME) || apikey.equals(CHANGE_ME)) {
-			LOGGER.error("  Password " + apikey
-					+ " not allowed.  It is a placeholder reminder.");
-			logCheckPass();
-			return false;
+		if ( value == null ) {
+			value = EnvironmentVariable.getEnv( oldParam, uimaContext );
+			if ( isValid( value ) ) {
+				where = WHERE_ENV + " " + oldParam;
+			}
 		}
-		
-		// last chance for an override
-		if (umlsUrl == null || umlsUrl.trim().isEmpty()) {
-			umlsUrl = getUrl();
-			if (umlsUrl == null) 
-				umlsUrl = UTS_APIKEY_URL;
+		if ( value != null ) {
+			LOGGER.debug( name + " set using: " + where );
 		}
+		return value;
+	}
 
-		return doAuth(umlsUrl, apikey);
+	static private String getApiKey( final UimaContext uimaContext, final Properties properties
) {
+		return getParamValue( uimaContext, properties, "UMLS API Key", OLDY_KEY_PROP, KEY_PARAM
);
 	}
 
-	private boolean doAuth( final String umlsUrl, String apiKey ) {
+	static private String getPassOrKey( final UimaContext uimaContext, final Properties properties
) {
+		return getParamValue( uimaContext, properties, "UMLS API Key", OLD_PASS_PARAM, PASS_PARAM
);
+	}
+
+	static private String getUser( final UimaContext uimaContext, final Properties properties
) {
+		return getParamValue( uimaContext, properties, "UMLS User", OLD_USER_PARAM, USER_PARAM
);
+	}
+
+	/**
+	 * @param umlsUrl -
+	 * @param apiKey  -
+	 * @return true if the api key is valid according to the server at umlsUrl
+	 */
+	static private boolean authenticate( final String umlsUrl, String apiKey ) {
 		try ( DotLogger dotter = new DotLogger() ) {
 			apiKey = apiKey.trim();
 			LOGGER.info( "Checking UMLS Account at " + umlsUrl + ":" );
@@ -284,41 +262,39 @@ public enum UmlsUserApprover {
 			final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 			connection.setRequestMethod( "POST" );
 			connection.setDoOutput( true );
-			final OutputStreamWriter writer = new OutputStreamWriter(
-					connection.getOutputStream() );
-			writer.write(data);
+			final OutputStreamWriter writer = new OutputStreamWriter( connection.getOutputStream()
);
+			writer.write( data );
 			writer.flush();
 			boolean isValidUser = false;
-			final BufferedReader reader = new BufferedReader(
-					new InputStreamReader(connection.getInputStream()));
+			final BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream()
) );
 			String line;
-			while ((line = reader.readLine()) != null) {
+			while ( ( line = reader.readLine() ) != null ) {
 				final String trimline = line.trim();
-				if (trimline.isEmpty()) {
+				if ( trimline.isEmpty() ) {
 					break;
 				}
 			}
 			// not used, but in case of problems we may want to see what is returned
-			LOGGER.debug("UTS response: " + line);
+			LOGGER.debug( "UTS response: " + line );
 			writer.close();
 			reader.close();
-			// This method gets a ticket getting token.  If it's successful, thats all we need to
know
-			isValidUser = (connection.getResponseCode() == HttpURLConnection.HTTP_CREATED);
-			if (isValidUser) {
-				LOGGER.info("  UMLS Account has been validated");
-				_validUsers.add(apiKey);
+			// This method gets a ticket getting token. If it's successful, thats all we
+			// need to know
+			isValidUser = ( connection.getResponseCode() == HttpURLConnection.HTTP_CREATED );
+			if ( isValidUser ) {
+				LOGGER.info( "  UMLS Account has been validated" );
+				_validUsers.add( apiKey );
 			} else {
-				LOGGER.error("  UMLS Account at " + umlsUrl + " is not valid.");
-				logCheckUser();
-				logCheckPass();
+				LOGGER.error( "  UMLS Account at " + umlsUrl + " is not valid." );
+				LOGGER.error( NEW_AUTH_MESSAGE );
 			}
 			return isValidUser;
-		} catch (IOException ioE) {
-			LOGGER.error(ioE.getMessage());
+		} catch ( IOException ioE ) {
+			LOGGER.error( ioE.getMessage() );
 			return false;
 		}
 	}
-	
+
 	/**
 	 * used for unit testing
 	 */
@@ -326,20 +302,38 @@ public enum UmlsUserApprover {
 		_validUsers.clear();
 	}
 
-	static private String createLogMessage(String cliOption, String property,
-			UmlsEnvironmentConfiguration envConfig) {
-		return String
-				.format(" Verify that you are setting command-line option %s, or ctakes property %s,
or environment variable %s properly.",
-						cliOption, property, envConfig);
+	/**
+	 * @param value -
+	 * @return true if the value is not null, not empty and not a "NOT_PRESENT" constant
+	 */
+	static private boolean isValid( final String value ) {
+		return value != null
+				 && !value.trim()
+							 .isEmpty()
+				 && !value.equals( EnvironmentVariable.NOT_PRESENT );
 	}
 
-	static private void logCheckUser() {
-		LOGGER.error(createLogMessage(USER_CLI, USER_PARAM,
-				UmlsEnvironmentConfiguration.USER));
+	/**
+	 * If the value is equal to "CHANGEME" or "CHANGE_ME" a message is logged telling the user
to set a value.
+	 *
+	 * @param name  -
+	 * @param value -
+	 * @return true if the value is not null, not empty and not a "NOT_PRESENT" constant
+	 */
+	static private boolean isValid( final String name, final String value ) {
+		if ( isValid( value ) ) {
+			if ( value.trim()
+						 .equals( CHANGEME ) || value.trim()
+															  .equals( CHANGE_ME ) ) {
+				// Potentially someone could have a user ID of CHANGEME or a password of CHANGEME but
don't allow those
+				// to make it easy for us to detect that the user or password was not set correctly.
+				LOGGER.error( "  " + name + " " + value + " not allowed.  It is a placeholder reminder."
);
+				return false;
+			}
+			return true;
+		}
+		return false;
 	}
 
-	static private void logCheckPass() {
-		LOGGER.error(createLogMessage(PASS_CLI, PASS_PARAM,
-				UmlsEnvironmentConfiguration.PASSWORD));
-	}
+
 }

Modified: ctakes/trunk/ctakes-dictionary-lookup-fast/src/test/java/org/apache/ctakes/dictionary/lookup2/util/UmlsUserTest.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-dictionary-lookup-fast/src/test/java/org/apache/ctakes/dictionary/lookup2/util/UmlsUserTest.java?rev=1885305&r1=1885304&r2=1885305&view=diff
==============================================================================
--- ctakes/trunk/ctakes-dictionary-lookup-fast/src/test/java/org/apache/ctakes/dictionary/lookup2/util/UmlsUserTest.java
(original)
+++ ctakes/trunk/ctakes-dictionary-lookup-fast/src/test/java/org/apache/ctakes/dictionary/lookup2/util/UmlsUserTest.java
Sat Jan  9 18:01:24 2021
@@ -1,26 +1,23 @@
 package org.apache.ctakes.dictionary.lookup2.util;
-import java.io.File;
-import java.util.Properties;
 
-import org.junit.Assert;
-import org.junit.Ignore;
 import org.apache.ctakes.core.ae.UmlsEnvironmentConfiguration;
-import org.apache.ctakes.utils.env.EnvironmentVariable;
-import org.apache.ctakes.utils.test.TestUtil;
 import org.apache.log4j.Logger;
 import org.apache.uima.UIMAFramework;
 import org.apache.uima.UimaContext;
 import org.apache.uima.analysis_engine.AnalysisEngine;
 import org.apache.uima.resource.ResourceInitializationException;
+import org.junit.Assert;
+import org.junit.Ignore;
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import java.util.Properties;
+
 
 /**
  * @author SPF , chip-nlp, pabramowitsch
  * @version %I%
  * @since 8/24/2015
- * 
+ *
  * To run this test suite, please substitute CHANGE_ME for your API Key
  * then add the following to your environment
  *  umlsPass = <your api key>, 
@@ -104,12 +101,12 @@ final public class UmlsUserTest {
    
    @Test
    public void testViaSingleProp() {
- 	   Properties props = new Properties();
- 	   props.setProperty(UmlsUserApprover.API_KEY_PROP, apiKeyForTesting);
- 	   UmlsUserApprover approver = UmlsUserApprover.getInstance();
- 	   Assert.assertEquals(true, approver.isValidUMLSUser(_uimaContext, props));
- 	   approver.resetUserCache();
-    }
+      Properties props = new Properties();
+      props.setProperty( UmlsUserApprover.OLDY_KEY_PROP, apiKeyForTesting );
+      UmlsUserApprover approver = UmlsUserApprover.getInstance();
+      Assert.assertEquals( true, approver.isValidUMLSUser( _uimaContext, props ) );
+      approver.resetUserCache();
+   }
    
    /**
     * for this test you will need to make sure any user/pass env vars have been removed and
instead



Mime
View raw message