提交 aedf603f 编写于 作者: F Frankie Wu

small refine in HTML codec and consumer

上级 b4e8fcf2
......@@ -8,6 +8,7 @@ import java.util.Map;
import com.dianping.cat.consumer.ip.model.entity.Ip;
import com.dianping.cat.consumer.ip.model.entity.IpReport;
import com.dianping.cat.consumer.ip.model.entity.Segment;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.spi.AbstractMessageAnalyzer;
......@@ -16,6 +17,8 @@ import com.dianping.cat.message.spi.MessageTree;
public class IpAnalyzer extends AbstractMessageAnalyzer<IpReport> {
private Map<String, IpReport> m_reports = new HashMap<String, IpReport>();
private static final String TOKEN = "RemoteIP=";
private IpReport findOrCreateReport(String domain) {
IpReport report = m_reports.get(domain);
......@@ -58,12 +61,28 @@ public class IpAnalyzer extends AbstractMessageAnalyzer<IpReport> {
private String getIpAddress(Transaction root) {
List<Message> children = ((Transaction) root).getChildren();
for (Message child : children) {
if (child instanceof Event && child.getType().equals("URL") && child.getName().equals("ClientInfo")) {
String data = child.getData().toString();
int off = data.indexOf(TOKEN);
if (off >= 0) {
int pos = data.indexOf('&', off + TOKEN.length());
if (pos > 0) {
return data.substring(off + TOKEN.length(), pos);
}
}
break;
}
}
return null;
}
@Override
protected boolean isTimeout() {
// TODO Auto-generated method stub
return false;
}
......@@ -97,5 +116,4 @@ public class IpAnalyzer extends AbstractMessageAnalyzer<IpReport> {
// TODO Auto-generated method stub
}
}
......@@ -57,13 +57,19 @@ public class StringRope {
@Override
public String toString() {
StringBuilder sb = new StringBuilder(256);
if (m_parts.isEmpty()) {
return "";
} else if (m_parts.size() == 1) {
return m_parts.get(0);
} else {
StringBuilder sb = new StringBuilder(256);
for (String part : m_parts) {
sb.append(part);
}
for (String part : m_parts) {
sb.append(part);
return sb.toString();
}
return sb.toString();
}
public int writeTo(ChannelBuffer buffer, BufferWriter writer) {
......
......@@ -2,7 +2,6 @@ package com.dianping.cat.message.spi.codec;
import org.jboss.netty.buffer.ChannelBuffer;
public class HtmlEncodingBufferWriter implements BufferWriter {
private static byte[] AMP = "&amp;".getBytes();
......@@ -24,17 +23,17 @@ public class HtmlEncodingBufferWriter implements BufferWriter {
if (b == '&') {
buffer.writeBytes(data, offset, i - offset);
buffer.writeBytes(AMP);
count += AMP.length;
count += AMP.length - 1;
offset = i + 1;
} else if (b == '<') {
buffer.writeBytes(data, offset, i - offset);
buffer.writeBytes(LT);
count += LT.length;
count += LT.length - 1;
offset = i + 1;
} else if (b == '>') {
buffer.writeBytes(data, offset, i - offset);
buffer.writeBytes(GT);
count += GT.length;
count += GT.length - 1;
offset = i + 1;
} else if (b == '\n') {
// we want '\n' be output again for better format
......
......@@ -19,11 +19,14 @@ import com.dianping.cat.message.spi.MessageTree;
import com.dianping.cat.message.spi.StringRope;
import com.site.lookup.annotation.Inject;
/**
* Local use only, do not use it over network since it only supports one-way encoding
*/
public class HtmlMessageCodec implements MessageCodec {
private static final String ID = "HT1"; // HTML version 1
@Inject
private BufferWriter m_writer = new HtmlEncodingBufferWriter();
private BufferWriter m_writer;
private BufferHelper m_bufferHelper = new BufferHelper(m_writer);
......@@ -243,9 +246,10 @@ public class HtmlMessageCodec implements MessageCodec {
count += TD1.length;
} else {
String tag = "<td class=\"" + styleClass + "\">";
byte[] bytes = tag.getBytes();
buf.writeBytes(tag.getBytes());
count += tag.length();
buf.writeBytes(bytes);
count += bytes.length;
}
buf.writeBytes(data);
......@@ -273,9 +277,10 @@ public class HtmlMessageCodec implements MessageCodec {
return TR1.length;
} else {
String tag = "<tr class=\"" + styleClass + "\">";
byte[] bytes = tag.getBytes();
buf.writeBytes(tag.getBytes());
return tag.length();
buf.writeBytes(bytes);
return bytes.length;
}
}
......
......@@ -38,11 +38,12 @@ public class DumpToHtmlConsumer implements MessageConsumer, Initializable, LogEn
ChannelBuffer buf = ChannelBuffers.dynamicBuffer(8192);
m_codec.encode(tree, buf);
fos = new FileOutputStream(file);
int length = buf.readInt();
fos = new FileOutputStream(file);
buf.getBytes(buf.readerIndex(), fos, length);
buf.clear();
} catch (Exception e) {
throw new RuntimeException(String.format("Error when dumping to HTML file(%s)!", file), e);
} finally {
......
......@@ -24,6 +24,7 @@ public class HtmlMessageCodecTest {
HtmlMessageCodec codec = new HtmlMessageCodec();
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
codec.setBufferWriter(new HtmlEncodingBufferWriter());
codec.encodeMessage(message, buf, 0, null);
String actual = buf.toString(Charset.forName("utf-8"));
......@@ -34,6 +35,7 @@ public class HtmlMessageCodecTest {
HtmlMessageCodec codec = new HtmlMessageCodec();
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
codec.setBufferWriter(new HtmlEncodingBufferWriter());
codec.encode(tree, buf);
buf.readInt(); // get rid of length
String actual = buf.toString(Charset.forName("utf-8"));
......@@ -90,8 +92,7 @@ public class HtmlMessageCodecTest {
long timestamp = 1325489621987L;
Event event = newEvent("type", "name", timestamp, "0", "here is the data.");
check(event,
"<tr><td>E15:33:41.987</td><td>type</td><td>name</td><td>0</td><td>here is the data.</td></tr>\r\n");
check(event, "<tr><td>E15:33:41.987</td><td>type</td><td>name</td><td>0</td><td>here is the data.</td></tr>\r\n");
}
@Test
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册