001package com.nimbusds.common.config;
002
003
004import java.net.URL;
005import java.util.Properties;
006
007import com.thetransactioncompany.util.PropertyParseException;
008import com.thetransactioncompany.util.PropertyRetriever;
009import org.apache.logging.log4j.LogManager;
010import org.apache.logging.log4j.Logger;
011
012
013/**
014 * Web service connect details.
015 *
016 * <p>The configuration is stored as public fields which become immutable 
017 * (final) after their initialisation.
018 *
019 * <p>Property keys: [prefix]*
020 */
021public class WebServiceDetails implements LoggableConfiguration {
022
023
024        /**
025         * The HTTP(S) URL of the web service.
026         *
027         * <p>Property key: [prefix]url
028         */
029        public final URL url;
030
031
032        /**
033         * Specifies whether to accept self-signed X.509 certificates presented
034         * by the web service (for HTTPS connections).
035         *
036         * <p>Property key: [prefix]trustSelfSignedCerts
037         */
038        public final boolean trustSelfSignedCerts;
039
040
041        /**
042         * The default self-signed certificate policy.
043         */
044        public static final boolean DEFAULT_SELF_SIGNED_CERT_TRUST = false;
045        
046        
047        /**
048         * Specifies an HTTP connect timeout for web service requests, in 
049         * milliseconds. Zero implies the option is disabled (timeout of 
050         * infinity).
051         *
052         * <p>Property key: [prefix]connectTimeout
053         */
054        public final int connectTimeout;
055        
056        
057        /**
058         * The default HTTP connect timeout for web service requests (disabled).
059         */
060        public static final int DEFAULT_CONNECT_TIMEOUT = 0;
061        
062        
063        /**
064         * Specifies an HTTP read timeout for web service requests, in
065         * milliseconds. Zero implies the option is disabled (timeout of
066         * infinity).
067         *
068         * <p>Property key: [prefix]readTimeout
069         */
070        public final int readTimeout;
071        
072        
073        /**
074         * The default HTTP read timeout for web service requests (disabled).
075         */
076        public static final int DEFAULT_READ_TIMEOUT = 0;
077        
078        
079        /**
080         * Optional API key to include with requests to the web service, 
081         * {@code null} if none.
082         *
083         * <p>Property key: [prefix]apiKey
084         */
085        public final String apiKey;
086
087
088        /**
089         * Creates a new web service details instance from the specified 
090         * properties.
091         *
092         * <p>Mandatory properties:
093         *
094         * <ul>
095         *     <li>[prefix]url
096         * </ul>
097         *
098         * <p>Optional properties, with defaults:
099         *
100         * <ul>
101         *     <li>[prefix]trustSelfSignedCerts = false
102         *     <li>[prefix]connectTimeout = 0
103         *     <li>[prefix]readTimeout = 0
104         *     <li>[prefix]apiKey = null
105         * </ul>
106         *
107         * @param prefix The properties prefix. Must not be {@code null}.
108         * @param props  The properties. Must not be {@code null}.
109         *
110         * @throws PropertyParseException On a missing or invalid property.
111         */
112        public WebServiceDetails(final String prefix, final Properties props)
113                throws PropertyParseException {
114
115                PropertyRetriever pr = new PropertyRetriever(props);
116                
117                url = pr.getURL(prefix + "url");
118
119                if (! url.getProtocol().equalsIgnoreCase("http") && 
120                    ! url.getProtocol().equalsIgnoreCase("https"))
121                        throw new PropertyParseException("URL protocol must be either HTTP or HTTPS", 
122                                                         prefix + "url", 
123                                                         url.toString());
124
125                trustSelfSignedCerts = pr.getOptBoolean(prefix + "trustSelfSignedCerts", 
126                                                        DEFAULT_SELF_SIGNED_CERT_TRUST);
127        
128                connectTimeout = pr.getOptInt(prefix + "connectTimeout",
129                                              DEFAULT_CONNECT_TIMEOUT);
130                
131                if (connectTimeout < 0)
132                        throw new PropertyParseException("The connect timeout value must be zero or positive",
133                                                         prefix + "connectTimeout");
134                
135                readTimeout = pr.getOptInt(prefix + "readTimeout",
136                                           DEFAULT_READ_TIMEOUT);
137        
138                if (readTimeout < 0)
139                        throw new PropertyParseException("The read timeout value must be zero or positive",
140                                                         prefix + "readTimeout");
141        
142                apiKey = pr.getOptString(prefix + "apiKey", null);
143        }
144
145
146        /**
147         * Logs the configuration details at INFO level.
148         */
149        @Override
150        public void log() {
151
152                Logger log = LogManager.getLogger(LOG_CATEGORY);
153                
154                log.info("[CM2000] Web service URL: {}", url);
155                
156                if (url.getProtocol().equalsIgnoreCase("http"))
157                        log.warn("[CM2001] Web service connection is not protected (plain HTTP), consider using SSL (HTTPS)");
158
159                if (url.getProtocol().equalsIgnoreCase("https"))
160                        log.info("[CM2002] Self-signed web service certificates are trusted: {}", trustSelfSignedCerts);
161        
162                if (connectTimeout > 0)
163                        log.info("[CM2003] Web service HTTP connect timeout: {} ms", connectTimeout);
164                else
165                        log.info("[CM2003] Web service HTTP connect timeout: disabled");
166                
167                if (readTimeout > 0)
168                        log.info("[CM2004] Web service HTTP read timeout: {} ms", readTimeout);
169                else
170                        log.info("[CM2004] Web service HTTP read timeout: disabled");
171                
172                if (apiKey != null)
173                        log.info("[CM2005] Web service API key: provided");
174                else
175                        log.info("[CM2005] Web service API key: not provided");
176        }
177}