001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.protocol.https;
018
019import java.util.Locale;
020
021/**
022 * Url scheme
023 * 
024 * @author igor
025 */
026public enum Scheme {
027        /** https */
028        HTTPS {
029                @Override
030                public boolean usesStandardPort(HttpsConfig config)
031                {
032                        return getPort(config) == 443;
033                }
034
035                @Override
036                public int getPort(HttpsConfig config)
037                {
038                        return config.getHttpsPort();
039                }
040        },
041        /** http */
042        HTTP {
043                @Override
044                public boolean usesStandardPort(HttpsConfig config)
045                {
046                        return getPort(config) == 80;
047                }
048
049                @Override
050                public int getPort(HttpsConfig config)
051                {
052                        return config.getHttpPort();
053                }
054        },
055        /** any, aka preserve current */
056        ANY {
057                @Override
058                public String urlName()
059                {
060                        throw new UnsupportedOperationException();
061                }
062
063                @Override
064                public boolean isCompatibleWith(Scheme other)
065                {
066                        return true;
067                }
068
069                @Override
070                public boolean usesStandardPort(HttpsConfig config)
071                {
072                        throw new UnsupportedOperationException();
073                }
074
075                @Override
076                public int getPort(HttpsConfig config)
077                {
078                        throw new UnsupportedOperationException();
079                }
080        };
081
082
083        /**
084         * @return scheme's url name
085         */
086        public String urlName()
087        {
088                return name().toLowerCase(Locale.ROOT);
089        }
090
091        /**
092         * Checks if two schemes are compatible. Compatible schemes do not require a redirect from the
093         * current scheme to the {@code other}.
094         * 
095         * @param other
096         * @return {@code true} iff the schemes are compatible.
097         */
098        public boolean isCompatibleWith(Scheme other)
099        {
100                return this == other;
101        }
102
103        public abstract boolean usesStandardPort(HttpsConfig config);
104
105        public abstract int getPort(HttpsConfig config);
106}