| @@ -1,9 +1,14 @@ | |||||
| package altk.comm.engine; | package altk.comm.engine; | ||||
| import java.io.ByteArrayInputStream; | import java.io.ByteArrayInputStream; | ||||
| import java.io.CharArrayReader; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.InputStream; | import java.io.InputStream; | ||||
| import java.io.InputStreamReader; | |||||
| import java.nio.ByteBuffer; | |||||
| import java.nio.CharBuffer; | |||||
| import java.nio.charset.Charset; | |||||
| import java.nio.charset.CharsetDecoder; | |||||
| import java.nio.charset.CodingErrorAction; | |||||
| import java.util.HashMap; | import java.util.HashMap; | ||||
| import java.util.Map; | import java.util.Map; | ||||
| import java.util.Vector; | import java.util.Vector; | ||||
| @@ -12,9 +17,6 @@ import java.util.zip.ZipInputStream; | |||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
| import javax.xml.parsers.DocumentBuilder; | import javax.xml.parsers.DocumentBuilder; | ||||
| import javax.xml.parsers.DocumentBuilderFactory; | import javax.xml.parsers.DocumentBuilderFactory; | ||||
| import javax.xml.parsers.ParserConfigurationException; | |||||
| import javax.xml.parsers.SAXParser; | |||||
| import javax.xml.parsers.SAXParserFactory; | |||||
| import javax.xml.xpath.XPath; | import javax.xml.xpath.XPath; | ||||
| import javax.xml.xpath.XPathConstants; | import javax.xml.xpath.XPathConstants; | ||||
| import javax.xml.xpath.XPathExpressionException; | import javax.xml.xpath.XPathExpressionException; | ||||
| @@ -27,7 +29,6 @@ import org.w3c.dom.Node; | |||||
| import org.w3c.dom.NodeList; | import org.w3c.dom.NodeList; | ||||
| import org.xml.sax.InputSource; | import org.xml.sax.InputSource; | ||||
| import org.xml.sax.SAXException; | import org.xml.sax.SAXException; | ||||
| import org.xml.sax.helpers.DefaultHandler; | |||||
| import altk.comm.engine.exception.BroadcastError; | import altk.comm.engine.exception.BroadcastError; | ||||
| import altk.comm.engine.exception.BroadcastException; | import altk.comm.engine.exception.BroadcastException; | ||||
| @@ -100,43 +101,29 @@ public abstract class XMLDOMBroadcast extends Broadcast | |||||
| { | { | ||||
| inb = request.getInputStream(); | inb = request.getInputStream(); | ||||
| } | } | ||||
| /* | |||||
| // alternate code to find out where are the offending characters in input | |||||
| myLogger.info("Begin parsing"); | |||||
| SAXParser parser; | |||||
| try | |||||
| { | |||||
| parser = SAXParserFactory.newInstance().newSAXParser(); | |||||
| // constructs an empty ArrayList for use by parser. | |||||
| // Convert InputStream inb into InputSource, using the UTF-8 | |||||
| // character set, and for use by SAX parser | |||||
| InputSource is = new InputSource(new InputStreamReader(inb, "UTF-8")); | |||||
| DefaultHandler parserHandler = new DefaultHandler(); | |||||
| parser.parse(is, parserHandler); | |||||
| } | |||||
| catch (ParserConfigurationException e) | |||||
| { | |||||
| // TODO Auto-generated catch block | |||||
| e.printStackTrace(); | |||||
| } | |||||
| */ | |||||
| int contentLength = request.getContentLength(); | int contentLength = request.getContentLength(); | ||||
| CommonLogger.activity.info("Receiving " + contentLength + " bytes of data"); | CommonLogger.activity.info("Receiving " + contentLength + " bytes of data"); | ||||
| byte[] content = new byte[contentLength]; | |||||
| byte[] byteContent = new byte[contentLength]; | |||||
| int offset = 0; | int offset = 0; | ||||
| int length = contentLength; | int length = contentLength; | ||||
| int read; | int read; | ||||
| while (length > 0) | while (length > 0) | ||||
| { | { | ||||
| read = inb.read(content, offset, length); | |||||
| read = inb.read(byteContent, offset, length); | |||||
| if (read < 0) break; | if (read < 0) break; | ||||
| offset += read; | offset += read; | ||||
| length -= read; | length -= read; | ||||
| } | } | ||||
| CommonLogger.activity.info("Received: " + new String(content, "UTF-8")); | |||||
| xmlDoc = builder.parse(new ByteArrayInputStream(content), "UTF-8"); | |||||
| // Clean content off bad UTF-8 characters. | |||||
| Charset utf8Charset = Charset.forName("UTF-8"); | |||||
| CharsetDecoder utf8Decoder = utf8Charset.newDecoder(); | |||||
| utf8Decoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); | |||||
| CharBuffer charContent = utf8Decoder.decode(ByteBuffer.wrap(byteContent)); | |||||
| CommonLogger.activity.info("Received: " + charContent); | |||||
| //Do not use this, as it can be killed by non UTF=8 chars: xmlDoc = builder.parse(new ByteArrayInputStream(byteContent), "UTF-8"); | |||||
| xmlDoc = builder.parse(new InputSource(new CharArrayReader(charContent.array()))); | |||||
| } | } | ||||
| catch (SAXException e) | catch (SAXException e) | ||||
| { | { | ||||
| @@ -144,10 +131,9 @@ public abstract class XMLDOMBroadcast extends Broadcast | |||||
| } | } | ||||
| catch (IOException e) | catch (IOException e) | ||||
| { | { | ||||
| throw new BroadcastException(BroadcastError.READ_REQUEST_ERROR, "Problem in reading request"); | |||||
| throw new BroadcastException(BroadcastError.READ_REQUEST_ERROR, "Problem in reading request: " + e.getMessage()); | |||||
| } | } | ||||
| //String xpath = "//" + broadcastName; // get all first level elements | |||||
| NodeList broadcastNodes = null; | NodeList broadcastNodes = null; | ||||
| broadcastNodes = xmlDoc.getElementsByTagName(broadcastType); | broadcastNodes = xmlDoc.getElementsByTagName(broadcastType); | ||||
| if (broadcastNodes.getLength() == 0) | if (broadcastNodes.getLength() == 0) | ||||