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.strings.testframework; 018 019import java.lang.reflect.Field; 020import java.util.ArrayList; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.jxmpp.JxmppContext; 026import org.jxmpp.stringprep.XmppStringprep; 027import org.jxmpp.stringprep.icu4j.Icu4jXmppStringprep; 028import org.jxmpp.stringprep.libidn.LibIdnXmppStringprep; 029import org.jxmpp.stringprep.rocksxmppprecis.RocksXmppPrecisStringprep; 030import org.jxmpp.stringprep.simple.SimpleXmppStringprep; 031 032public class XmppStringPrepper { 033 034 private static final Map<String, XmppStringPrepper> KNOWN_STRINGPREPPERS = new HashMap<>(); 035 036 public static final XmppStringPrepper ICU4J = new XmppStringPrepper(Icu4jXmppStringprep.getInstance()); 037 public static final XmppStringPrepper LIBIDN = new XmppStringPrepper(LibIdnXmppStringprep.getInstance()); 038 public static final XmppStringPrepper SIMPLE = new XmppStringPrepper(SimpleXmppStringprep.getInstance()); 039 public static final XmppStringPrepper ROCKS_XMPP_PRECIS = new XmppStringPrepper(RocksXmppPrecisStringprep.INSTANCE); 040 041 public final String name; 042 public final Class<? extends XmppStringprep> xmppStringprepClass; 043 public final JxmppContext context; 044 045 public final String toString; 046 047 XmppStringPrepper(XmppStringprep xmppStringprep) { 048 this.xmppStringprepClass = xmppStringprep.getClass(); 049 050 String name; 051 try { 052 Field field = xmppStringprepClass.getDeclaredField("NAME"); 053 name = (String) field.get(null); 054 } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) { 055 name = xmppStringprepClass.getSimpleName(); 056 } 057 this.name = name; 058 059 this.context = JxmppContext.builder().withXmppStringprep(xmppStringprep).build(); 060 this.toString = XmppStringPrepper.class.getSimpleName() + " '" + name + "\'"; 061 062 XmppStringPrepper previous; 063 synchronized (KNOWN_STRINGPREPPERS) { 064 previous = KNOWN_STRINGPREPPERS.putIfAbsent(name, this); 065 } 066 if (previous != null) { 067 throw new IllegalArgumentException("And XMPP Stringprepper with the name '" + name + "' already exists"); 068 } 069 } 070 071 /** 072 * Get the name of this XMPP String Prepper. 073 * 074 * @return the name of this XMPP String Prepper. 075 */ 076 public String getName() { 077 return name; 078 } 079 080 @Override 081 public String toString() { 082 return toString; 083 } 084 085 /** 086 * Lookup an XMPP String Prepper by name. 087 * 088 * @param name the name of the XMPP String Prepper. 089 * @return a prepper or <code>null</code> 090 */ 091 public static XmppStringPrepper lookup(String name) { 092 synchronized (KNOWN_STRINGPREPPERS) { 093 return KNOWN_STRINGPREPPERS.get(name); 094 } 095 } 096 097 /** 098 * Get a list of all known XMPP String Preppers. 099 * 100 * @return a list of XMPP String Preppers. 101 */ 102 public static List<XmppStringPrepper> getKnownXmppStringpreppers() { 103 List<XmppStringPrepper> res; 104 synchronized (KNOWN_STRINGPREPPERS) { 105 res = new ArrayList<>(KNOWN_STRINGPREPPERS.size()); 106 res.addAll(KNOWN_STRINGPREPPERS.values()); 107 } 108 return res; 109 } 110}