소스 검색

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)
{


불러오는 중...
취소
저장