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