001/**
002 *
003 * Copyright © 2015-2023 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.icu4j;
018
019import org.jxmpp.JxmppContext;
020import org.jxmpp.stringprep.XmppStringprep;
021import org.jxmpp.stringprep.XmppStringprepException;
022
023import com.ibm.icu.text.StringPrep;
024import com.ibm.icu.text.StringPrepParseException;
025
026/**
027 * XMPP string preparation using libidn.
028 */
029public class Icu4jXmppStringprep implements XmppStringprep {
030
031        /**
032         * The name of the stringprep implementation.
033         */
034        public static final String NAME = "icu4j";
035
036        private static final StringPrep NODEPREP = StringPrep.getInstance(StringPrep.RFC3920_NODEPREP);
037        private static final StringPrep DOMAINPREP = StringPrep.getInstance(StringPrep.RFC3491_NAMEPREP);
038        private static final StringPrep RESOURCEPREP = StringPrep.getInstance(StringPrep.RFC3920_RESOURCEPREP);
039
040        private static Icu4jXmppStringprep instance;
041
042        /**
043         * Setup the ICU4J Stringprep implementation as active Stringprep implementation.
044         */
045        public static void setup() {
046                JxmppContext.setDefaultXmppStringprep(getInstance());
047        }
048
049        /**
050         * Get the ICU4J Stringprep implementation singleton.
051         * @return the ICU4J Stringprep implementation.
052         */
053        public static Icu4jXmppStringprep getInstance() {
054                if (instance == null) {
055                        instance = new Icu4jXmppStringprep();
056                }
057                return instance;
058        }
059
060        private Icu4jXmppStringprep() {
061        }
062
063        @Override
064        public String localprep(String string) throws XmppStringprepException {
065                try {
066                        return NODEPREP.prepare(string, StringPrep.DEFAULT);
067                } catch (StringPrepParseException e) {
068                        throw new XmppStringprepException(string, e);
069                }
070        }
071
072        @Override
073        public String domainprep(String string) throws XmppStringprepException {
074                try {
075                        return DOMAINPREP.prepare(string, StringPrep.DEFAULT);
076                } catch (StringPrepParseException e) {
077                        throw new XmppStringprepException(string, e);
078                }
079        }
080
081        @Override
082        public String resourceprep(String string) throws XmppStringprepException {
083                try {
084                        return RESOURCEPREP.prepare(string, StringPrep.DEFAULT);
085                } catch (StringPrepParseException e) {
086                        throw new XmppStringprepException(string, e);
087                }
088        }
089}