001/**
002 *
003 * Copyright 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.rocksxmppprecis;
018
019import org.jxmpp.stringprep.XmppStringprep;
020import org.jxmpp.stringprep.XmppStringprepException;
021import org.jxmpp.stringprep.simple.SimpleXmppStringprep;
022
023import rocks.xmpp.precis.InvalidCodePointException;
024import rocks.xmpp.precis.PrecisProfiles;
025
026public class RocksXmppPrecisStringprep implements XmppStringprep {
027
028        public static final RocksXmppPrecisStringprep INSTANCE = new RocksXmppPrecisStringprep();
029
030        public static final String NAME = "rocks-xmpp-precis";
031
032        private RocksXmppPrecisStringprep() {
033        }
034
035        @Override
036        public String localprep(String string) throws XmppStringprepException {
037                // Workaround until https://bitbucket.org/sco0ter/precis/pull-requests/3 is merged.
038                SimpleXmppStringprep.ensureLocalpartDoesNotIncludeFurtherExcludedCharacters(string);
039                try {
040                        return PrecisProfiles.USERNAME_CASE_MAPPED.enforce(string);
041                } catch (InvalidCodePointException e) {
042                        throw new XmppStringprepException(string, e);
043                }
044        }
045
046        @Override
047        public String domainprep(String string) throws XmppStringprepException {
048                try {
049                        return PrecisProfiles.IDN.enforce(string);
050                } catch (IllegalArgumentException e) {
051                        throw new XmppStringprepException(string, e);
052                }
053        }
054
055        @Override
056        public String resourceprep(String string) throws XmppStringprepException {
057                try {
058                        return PrecisProfiles.OPAQUE_STRING.enforce(string);
059                } catch (InvalidCodePointException e) {
060                        throw new XmppStringprepException(string, e);
061                }
062        }
063}