diff --git a/src/java/org/jivesoftware/multiplexer/net/ConnectionHandler.java b/src/java/org/jivesoftware/multiplexer/net/ConnectionHandler.java index ce77e8b..1bfc7e6 100644 --- a/src/java/org/jivesoftware/multiplexer/net/ConnectionHandler.java +++ b/src/java/org/jivesoftware/multiplexer/net/ConnectionHandler.java @@ -20,6 +20,10 @@ package org.jivesoftware.multiplexer.net; +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + import org.apache.mina.common.IdleStatus; import org.apache.mina.common.IoHandlerAdapter; import org.apache.mina.common.IoSession; @@ -27,16 +31,13 @@ import org.jivesoftware.multiplexer.Connection; import org.jivesoftware.multiplexer.ConnectionManager; import org.jivesoftware.multiplexer.PacketRouter; +import org.jivesoftware.multiplexer.StreamError; import org.jivesoftware.multiplexer.spi.ServerRouter; import org.jivesoftware.util.Log; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; -import java.io.IOException; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - /** * A ConnectionHandler is responsible for creating new sessions, destroying sessions and delivering * received XML stanzas to the proper StanzaHandler. @@ -110,14 +111,24 @@ public void exceptionCaught(IoSession session, Throwable cause) throws Exception { if (cause instanceof IOException) { // TODO Verify if there were packets pending to be sent and decide what to do with them - Log.debug(cause); + Log.debug("ConnectionHandler: ",cause); + } + else if (cause instanceof XMLNotWellFormedException) { + Log.warn("Closing session due to malformed XML: " + session, cause); + final Connection connection = (Connection) session.getAttribute(CONNECTION); + final StreamError error = new StreamError(StreamError.Condition.xml_not_well_formed); + connection.deliverRawText(error.toXML()); + session.close(); } else if (cause instanceof ProtocolDecoderException) { Log.warn("Closing session due to exception: " + session, cause); + final Connection connection = (Connection) session.getAttribute(CONNECTION); + final StreamError error = new StreamError(StreamError.Condition.internal_server_error); + connection.deliverRawText(error.toXML()); session.close(); } else { - Log.error(cause); + Log.error(cause.getMessage(), cause); } } diff --git a/src/java/org/jivesoftware/multiplexer/net/XMLLightweightParser.java b/src/java/org/jivesoftware/multiplexer/net/XMLLightweightParser.java index 14a0a7f..d32f579 100644 --- a/src/java/org/jivesoftware/multiplexer/net/XMLLightweightParser.java +++ b/src/java/org/jivesoftware/multiplexer/net/XMLLightweightParser.java @@ -155,11 +155,11 @@ /* * Method that add a message to the list and reinit parser. */ - protected void foundMsg(String msg) throws Exception { + protected void foundMsg(String msg) throws XMLNotWellFormedException { // Add message to the complete message list if (msg != null) { if (hasIllegalCharacterReferences(msg)) { - throw new Exception("Illegal character reference found in: " + msg); + throw new XMLNotWellFormedException("Illegal character reference found in: " + msg); } msgs.add(msg); } @@ -205,7 +205,7 @@ if (ch < 0x20 && ch != 0x9 && ch != 0xA && ch != 0xD && ch != 0x0) { //Unicode characters in the range 0x0000-0x001F other than 9, A, and D are not allowed in XML //We need to allow the NULL character, however, for Flash XMLSocket clients to work. - throw new Exception("Disallowed character"); + throw new XMLNotWellFormedException("Character is invalid in: " + ch); } if (isHighSurrogate) { if (Character.isLowSurrogate(ch)) { diff --git a/src/java/org/jivesoftware/multiplexer/net/XMLNotWellFormedException.java b/src/java/org/jivesoftware/multiplexer/net/XMLNotWellFormedException.java new file mode 100644 index 0000000..57c5777 --- /dev/null +++ b/src/java/org/jivesoftware/multiplexer/net/XMLNotWellFormedException.java @@ -0,0 +1,45 @@ +/** + * $Revision: $ + * $Date: $ + * + * Copyright (C) 2005-2008 Jive Software. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.multiplexer.net; + +/** + * An Exception indicating that evaluated content is not valid XML. + * + * @author Guus der Kinderen, guus.der.kinderen@gmail.com + */ +public class XMLNotWellFormedException extends Exception { + + private static final long serialVersionUID = 1L; + + public XMLNotWellFormedException() { + super(); + } + + public XMLNotWellFormedException(String message, Throwable cause) { + super(message, cause); + } + + public XMLNotWellFormedException(String message) { + super(message); + } + + public XMLNotWellFormedException(Throwable cause) { + super(cause); + } +}