001/**
002 *
003 * Copyright © 2014-2015 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;
018
019import java.io.IOException;
020
021/**
022 * XMPP Stringprep exceptions signal an error when performing a particular Stringprep profile on a String.
023 */
024public class XmppStringprepException extends IOException {
025
026        /**
027         *
028         */
029        private static final long serialVersionUID = -8491853210107124624L;
030
031        private final String causingString;
032
033        /**
034         * Construct a new XMPP Stringprep exception with the given causing String and exception.
035         *
036         * @param causingString the String causing the exception.
037         * @param exception the exception.
038         */
039        public XmppStringprepException(String causingString, Exception exception) {
040                super("XmppStringprepException caused by '" + causingString + "': " + exception);
041                initCause(exception);
042                this.causingString = causingString;
043        }
044
045        /**
046         * Construct a new XMPP Stringprep exception with the given causing String and exception message.
047         *
048         * @param causingString the String causing the exception.
049         * @param message the message of the exception.
050         */
051        public XmppStringprepException(String causingString, String message) {
052                super(message);
053                this.causingString = causingString;
054        }
055
056        /**
057         * Get the String causing the XMPP Stringprep exception.
058         *
059         * @return the causing String.
060         */
061        public String getCausingString() {
062                return causingString;
063        }
064
065        /**
066         * The input string does not contain a domainpart.
067         */
068        public static class MissingDomainpart extends XmppStringprepException {
069                /**
070                 * 
071                 */
072                private static final long serialVersionUID = 1L;
073
074                private MissingDomainpart(String causingString) {
075                        super(causingString, "The provided string does not have a domainpart");
076                }
077
078                /**
079                 * Create a new "missing domainpart" exception from the give parts.
080                 *
081                 * @param localpart the localpart.
082                 * @param resourcepart the resourcepart.
083                 * @return a new "missing domanipart" exception.
084                 */
085                public static MissingDomainpart from(String localpart, String resourcepart) {
086                        StringBuilder causingString = new StringBuilder();
087                        if (localpart != null) {
088                                causingString.append(localpart).append('@');
089                        }
090                        if (resourcepart != null) {
091                                causingString.append('/').append(resourcepart);
092                        }
093                        return new MissingDomainpart(causingString.toString());
094                }
095        }
096}