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        @SuppressWarnings("this-escape")
040        public XmppStringprepException(String causingString, Exception exception) {
041                super("XmppStringprepException caused by '" + causingString + "': " + exception);
042                initCause(exception);
043                this.causingString = causingString;
044        }
045
046        /**
047         * Construct a new XMPP Stringprep exception with the given causing String and exception message.
048         *
049         * @param causingString the String causing the exception.
050         * @param message the message of the exception.
051         */
052        public XmppStringprepException(String causingString, String message) {
053                super(message);
054                this.causingString = causingString;
055        }
056
057        /**
058         * Get the String causing the XMPP Stringprep exception.
059         *
060         * @return the causing String.
061         */
062        public String getCausingString() {
063                return causingString;
064        }
065
066        /**
067         * The input string does not contain a domainpart.
068         */
069        public static class MissingDomainpart extends XmppStringprepException {
070                /**
071                 * 
072                 */
073                private static final long serialVersionUID = 1L;
074
075                private MissingDomainpart(String causingString) {
076                        super(causingString, "The provided string does not have a domainpart");
077                }
078
079                /**
080                 * Create a new "missing domainpart" exception from the give parts.
081                 *
082                 * @param localpart the localpart.
083                 * @param resourcepart the resourcepart.
084                 * @return a new "missing domanipart" exception.
085                 */
086                public static MissingDomainpart from(String localpart, String resourcepart) {
087                        StringBuilder causingString = new StringBuilder();
088                        if (localpart != null) {
089                                causingString.append(localpart).append('@');
090                        }
091                        if (resourcepart != null) {
092                                causingString.append('/').append(resourcepart);
093                        }
094                        return new MissingDomainpart(causingString.toString());
095                }
096        }
097}