001package com.thetransactioncompany.cors;
002
003
004import java.net.URI;
005import java.net.URISyntaxException;
006import java.net.IDN;
007
008
009/**
010 * Validated resource request origin, as defined in The Web Origin Concept 
011 * (RFC 6454). Supported schemes are {@code http} and {@code https}.
012 *
013 * @author Vladimir Dzhuvinov
014 * @author Luis Sala
015 * @author Jared Ottley
016 * @author EdraĆ­ Brosa
017 */
018public class ValidatedOrigin extends Origin {
019        
020        
021        /**
022         * The origin scheme.
023         */
024        private String scheme;
025        
026        
027        /**
028         * The origin host.
029         */
030        private String host;
031        
032        
033        /**
034         * The parsed origin port, -1 for default port.
035         */
036        private int port = -1;
037
038        
039        /**
040         * Creates a new validated origin.
041         *
042         * @param origin The origin to validate. Must not be {@code null}.
043         *
044         * @throws OriginException If the value doesn't represent a valid URI
045         *                         and a supported origin.
046         */
047        public ValidatedOrigin(final Origin origin)
048                throws OriginException {
049        
050                super(origin.toString());
051
052                // Parse URI value
053        
054                URI uri;
055        
056                try {
057                        uri = new URI(origin.toString());
058                        
059                } catch (URISyntaxException e) {
060                
061                        throw new OriginException("Bad origin URI: " + e.getMessage());
062                }
063                
064                scheme = uri.getScheme();
065                host = uri.getHost();
066                port = uri.getPort();
067                
068                if (scheme == null)
069                        throw new OriginException("Bad origin URI: Missing scheme, such as http or https");
070                
071                // Canonicalise scheme and host
072                scheme = scheme.toLowerCase();
073
074                if (host == null)
075                        throw new OriginException("Bad origin URI: Missing authority (host)");
076                
077                // Apply the IDNA toASCII algorithm [RFC3490] to /host/
078                host = IDN.toASCII(host, IDN.ALLOW_UNASSIGNED | IDN.USE_STD3_ASCII_RULES);
079        
080                // Finally, convert to lower case
081                host = host.toLowerCase();
082        }
083        
084        
085        /**
086         * Returns the scheme.
087         *
088         * @return The scheme.
089         */
090        public String getScheme() {
091
092                return scheme;
093        }
094        
095        
096        /**
097         * Returns the host (name or IP address).
098         *
099         * @return The host name or IP address.
100         */
101        public String getHost() {
102        
103                return host;
104        }
105        
106        
107        /**
108         * Returns the port number.
109         *
110         * @return The port number, -1 for default port.
111         */
112        public int getPort() {
113        
114                return port;
115        }
116        
117        
118        /**
119         * Returns the suffix which is made up of the host name / IP address 
120         * and port (if a non-default port is specified).
121         *
122         * <p>Example:
123         *
124         * <pre>
125         * http://example.com =&gt; example.com
126         * http://example.com:8080 =&gt; example.com:8080
127         * </pre>
128         *
129         * @return The suffix.
130         */
131        public String getSuffix() {
132                
133                String s = host;
134                
135                if (port != -1)
136                        s = s + ":" + port;
137                
138                return s;
139        }
140}