001/**
002 *
003 * Copyright © 2014-2019 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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.jxmpp.stringprep.libidn;
018
019import org.jxmpp.JxmppContext;
020import org.jxmpp.stringprep.XmppStringprep;
021import org.jxmpp.stringprep.XmppStringprepException;
022
023import gnu.inet.encoding.Stringprep;
024import gnu.inet.encoding.StringprepException;
025
026public class LibIdnXmppStringprep implements XmppStringprep {
027
028        private static LibIdnXmppStringprep instance;
029
030        public static final String NAME = "libidn";
031
032        /**
033         * Setup the libidn Stringprep implementation as active Stringprep implementation.
034         */
035        public static void setup() {
036                JxmppContext.setDefaultXmppStringprep(getInstance());
037        }
038
039        /**
040         * Get the libidn Stringprep implementation singleton.
041         * @return the libidn Stringprep implementation.
042         */
043        public static LibIdnXmppStringprep getInstance() {
044                if (instance == null) {
045                        instance = new LibIdnXmppStringprep();
046                }
047                return instance;
048        }
049
050        private LibIdnXmppStringprep() {
051        }
052
053        @Override
054        public String localprep(String string) throws XmppStringprepException {
055                try {
056                        // Allow unassigned codepoints as of RFC6122 A.2
057                        return Stringprep.nodeprep(string, true);
058                } catch (StringprepException e) {
059                        throw new XmppStringprepException(string, e);
060                }
061        }
062
063        @Override
064        public String domainprep(String string) throws XmppStringprepException {
065                try {
066                        // Don't allow unassigned because this is a "stored string". See
067                        // RFC3453 7, RFC3490 4 1) and RFC6122 2.2
068                        return Stringprep.nameprep(string);
069                } catch (StringprepException e) {
070                        throw new XmppStringprepException(string, e);
071                }
072        }
073
074        @Override
075        public String resourceprep(String string) throws XmppStringprepException {
076                try {
077                        // Allow unassigned codepoints as of RFC6122 B.2
078                        return Stringprep.resourceprep(string, true);
079                } catch (StringprepException e) {
080                        throw new XmppStringprepException(string, e);
081                }
082        }
083}