浏览代码

Added capability of handling mal-formed UTF-8 characters in request XML, and logging XML without reading it into a huge String.

tags/Production_5_15_2012
ymlam 13 年前
父节点
当前提交
43e10b6845
共有 1 个文件被更改,包括 5 次插入21 次删除
  1. +5
    -21
      src/altk/comm/engine/XMLDOMBroadcast.java

+ 5
- 21
src/altk/comm/engine/XMLDOMBroadcast.java 查看文件

@@ -1,11 +1,7 @@
package altk.comm.engine;

import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
@@ -86,7 +82,7 @@ public abstract class XMLDOMBroadcast extends Broadcast
// parse request into multiple BroadcastSMS pass then to Dispatcher
Document xmlDoc;
Document xmlDoc = null;
try
{
InputStream inb;
@@ -102,28 +98,16 @@ public abstract class XMLDOMBroadcast extends Broadcast
inb = request.getInputStream();
}

int contentLength = request.getContentLength();
CommonLogger.activity.info("Receiving " + contentLength + " bytes of data");
byte[] byteContent = new byte[contentLength];
int offset = 0;
int length = contentLength;
int read;
while (length > 0)
{
read = inb.read(byteContent, offset, length);
if (read < 0) break;
offset += read;
length -= read;
}

// 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())));
LoggingInputStreamReader ins = new LoggingInputStreamReader(inb, utf8Decoder);
InputSource is = new InputSource(ins);
xmlDoc = builder.parse(is);
}
catch (SAXException e)
{


正在加载...
取消
保存