001/** 002 * 003 * Copyright 2019-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.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 026/** 027 * XMPP string preparation using rocks-xmpp-precis. 028 */ 029public class RocksXmppPrecisStringprep implements XmppStringprep { 030 031 /** 032 * The ready to use instance of this. 033 */ 034 public static final RocksXmppPrecisStringprep INSTANCE = new RocksXmppPrecisStringprep(); 035 036 /** 037 * The name of the stringprep implementation. 038 */ 039 public static final String NAME = "rocks-xmpp-precis"; 040 041 private RocksXmppPrecisStringprep() { 042 } 043 044 @Override 045 public String localprep(String string) throws XmppStringprepException { 046 // Workaround until https://bitbucket.org/sco0ter/precis/pull-requests/3 is merged. 047 SimpleXmppStringprep.ensureLocalpartDoesNotIncludeFurtherExcludedCharacters(string); 048 try { 049 return PrecisProfiles.USERNAME_CASE_MAPPED.enforce(string); 050 } catch (InvalidCodePointException e) { 051 throw new XmppStringprepException(string, e); 052 } 053 } 054 055 @Override 056 public String domainprep(String string) throws XmppStringprepException { 057 try { 058 return PrecisProfiles.IDN.enforce(string); 059 } catch (IllegalArgumentException e) { 060 throw new XmppStringprepException(string, e); 061 } 062 } 063 064 @Override 065 public String resourceprep(String string) throws XmppStringprepException { 066 try { 067 return PrecisProfiles.OPAQUE_STRING.enforce(string); 068 } catch (InvalidCodePointException e) { 069 throw new XmppStringprepException(string, e); 070 } 071 } 072}