lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2007年11月29日

用Spring,smppapi,apache mina, commons ssl快速实现安全的smpp smsc (二)

Filed under: Java,SSL — 标签:, , , , , , — lizongbo @ 14:39

接上一篇: http://618119.com/archives/2007/11/27/31.html

对smpp 数据包进行编码和解码的相关代码如下:

ProtocolCodecFactory的代码为:

[code]
package com.lizongbo.smpp.server.codec;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolDecoder;
public class SMPPProtocolCodecFactory
implements ProtocolCodecFactory {
ProtocolDecoder decoder = new SMPPProtocolDecoder();
ProtocolEncoder encoder = new SMPPProtocolEncoder();
public ProtocolEncoder getEncoder() throws Exception {
return encoder;
}
public ProtocolDecoder getDecoder() throws Exception {
return decoder;
}
}

[/code]

数据包解码的ProtocolDecoder为

[code]

package com.lizongbo.smpp.server.codec;

import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.ByteBuffer;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import ie.omk.smpp.message.SMPPProtocolException;
import ie.omk.smpp.BadCommandIDException;
import ie.omk.smpp.util.PacketFactory;
import org.apache.log4j.Logger;
import ie.omk.smpp.message.SMPPPacket;
import java.io.IOException;

public class SMPPProtocolDecoder
implements ProtocolDecoder {
protected Logger log = Logger.getLogger(getClass());
public void decode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception {

try {
if (in.remaining() >= 4 && (in.remaining() >= in.getInt(in.position()))) {
byte[] byteLen = new byte[4];
in.get(byteLen);
int len = readInt(byteLen);
//System.out.println(“消息长度为:” + len);
if (in.remaining() >= (len – 4)) {
byte[] cmds = new byte[len – 4];
in.get(cmds);
SMPPPacket pak = null;
int id = -1;
id = readInt(cmds);
pak = PacketFactory.newInstance(id);
if (pak != null) {
byte[] reqb = new byte[4 + len];
System.arraycopy(byteLen, 0, reqb, 0, 4);
System.arraycopy(cmds, 0, reqb, 4, cmds.length);
pak.readFrom(reqb, 0);
if (log.isDebugEnabled()) {
StringBuilder sb = new StringBuilder(“收到的包为: “);
int l = pak.getLength();
int s = pak.getCommandStatus();
int n = pak.getSequenceNum();
sb.append(“commandId:”).append(Integer.toHexString(id))
.append(” len:”).append(Integer.toString(l))
.append(” commandStatus:”).append(Integer.toString(s))
.append(” sequenceNum:”).append(Integer.toString(n));
log.debug(sb);
}
out.write(pak);
}
}
}
}
catch (BadCommandIDException x) {
throw new SMPPProtocolException(“Unrecognised command received”, x);
}

}

public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws
Exception {
}

public void dispose(IoSession session) throws Exception {
}

public final int readInt(byte[] b) throws IOException {
int ch1 = b[0];
int ch2 = b[1];
int ch3 = b[2];
int ch4 = b[3];
// if ( (ch1 | ch2 | ch3 | ch4) < 0) {
// throw new EOFException();
// }
// see: http://618119.com/archives/2007/10/27/18.html
return ( ( (ch1 & 0xff) << 24) + ( (ch2 & 0xff) << 16) +
( (ch3 & 0xff) << 8) + (ch4 << 0));
}
}
[/code]

数据包的ProtocolEncoder非常简单了:

[code]

package com.lizongbo.smpp.server.codec;

import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.common.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.apache.mina.common.ByteBuffer;
import ie.omk.smpp.message.SMPPPacket;
import java.io.ByteArrayOutputStream;

public class SMPPProtocolEncoder
implements ProtocolEncoder {
public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
if (message == null) {
return;
}
SMPPPacket packet = (SMPPPacket) message;
ByteArrayOutputStream outb = new ByteArrayOutputStream(packet.getLength());
packet.writeTo(outb);
byte[] b = outb.toByteArray();
ByteBuffer buf = ByteBuffer.allocate(b.length, true);
buf.setAutoExpand(true);
buf.put(b);
buf.flip();
session.write(buf);
}

public void dispose(IoSession session) throws Exception {
}

}
[/code]

2007年11月28日

参考Google Syntax Highlighter for WordPress 定制纯Javascript实现的代码语法高亮

Filed under: JavaScript,Web — 标签:, , — lizongbo @ 12:30

Google Syntax Highlighter for WordPress. 是代码加亮插件,但是该插件与文章的可视化编辑存在冲突,
参考 : http://www.ajaxgo.org/press/?p=6

并且在 pre标签内如果写 xml代码,在可视化编辑的情况下,就会被SyntaxHighlighter转出一堆转码字符,
而在源代码模式下,如果浏览器禁用了 Javascript,则无法看到完整的xml内容.
加之我喜欢在代码里使用 [ code ] [ /code ] 这样用中括号的标记,
SyntaxHighlighter却只支持 pre标记,于是我根据这个插件,针对自己的博客做了改造.

实现简洁方便的语法高亮,既不影响后台对文章的编辑效果,也不影响文章在启用javascript和禁用 Javascript两种情况下的显示.

实现的思路如下.

在显示文章的页面里,
通过Javascript动态查找到 class为entry的div,这个就是显示文章内容的地方了,
然后操作innerHtml,将[ code ]与 [ /code ]之间的html内容进行解析替换,
自动识别代码类型然后将其转换为 pre 标记,再调用dp.SyntaxHighlighter.HighlightAll(‘code’),进行代码高亮.

我的具体操作步骤如下:

1.下载 http://downloads.wordpress.org/plugin/google-syntax-highlighter.1.5.1.zip
(也可以直接下载 http://syntaxhighlighter.googlecode.com/files/SyntaxHighlighter_1.5.1.rar,不过需要调整一下相关目录
上传到服务器之后保持google-syntax-highlighter目录即可.)
2.解压上传到 http:///618119.com/wp-content/plugins/
(后台管理中不要启用插件)
3.编写三个js文件,并上传到服务器.
highlighter.js  http://618119.com/wp-content/plugins/google-syntax-highlighter/Scripts/highlighter.js
mySyntaxHighlighter.js http://618119.com/wp-content/plugins/google-syntax-highlighter/Scripts/mySyntaxHighlighter.js
doSyntaxHighlighter.js http://618119.com/wp-content/plugins/google-syntax-highlighter/Scripts/doSyntaxHighlighter.js

4.在wp-content/themes/default/single.php中加上

<script class=”javascript” src=”/wp-content/plugins/google-syntax-highlighter/Scripts/highlighter.js”></script>

这行加在 <?php get_footer(); ?> 的前面.

这样就实现了纯js的 语法高亮,且不对现有的可视化编辑造成影响,也支持用户在禁止了 Javascript的情况下正常浏览.
由于使用的动态增加css和js,因此对于禁止Javascript的情况下,浏览速度非常快,和没启用语法高亮一样.

我写的js代码大致有:

[code]

//如果存在code标记,则加载语法高亮的JavaScript
var codeTag=”code”;//code标记名
var highlighterUriPerfix=”/wp-content/plugins/google-syntax-highlighter/”;
var postElement=null;
//找到class 为 entry的对象进行处理
var tags=document.getElementsByTagName(“div”); // class 为 entry
for(var i = 0; i < tags.length; i++)
if(tags[i].className.indexOf(“entry”) >=0)
postElement= tags[i];
if(postElement!=null && postElement.innerHTML.indexOf(“[“+codeTag+”]”)>0){
document.write(“<script class=\”javascript\” src=\””+highlighterUriPerfix+”Scripts/mySyntaxHighlighter.js\”><\/script>”);
}

[/code]

不过代码中对html标记过滤掉处理还不够完善,,对代码的类型自动识别没怎么处理,但基本够用了.

Google Syntax Highlighter for WordPress 在: http://wordpress.org/extend/plugins/google-syntax-highlighter/
SyntaxHighlighter在 : http://code.google.com/p/syntaxhighlighter/

2007年11月27日

用Spring,smppapi,apache mina, commons ssl快速实现安全的smpp smsc (一)

Filed under: Java,SSL — 标签:, , , , , , — lizongbo @ 13:19

需要用到的java库有:

Apache mina : http://mina.apache.org

Spring: http://www.springframework.org/

smppapi http://smppapi.sourceforge.net/

commons ssl: http://juliusdavies.ca/commons-ssl/

smpp smsc Server的服务端启动代码:
[code]
package com.lizongbo.smpp.server;

import org.apache.mina.filter.LoggingFilter;
import com.lizongbo.smpp.server.codec.SMPPProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.common.DefaultIoFilterChainBuilder;
import java.net.InetSocketAddress;
import org.apache.mina.common.IoAcceptorConfig;
import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Set;
import org.apache.mina.common.IoSession;
import ie.omk.smpp.*;
import ie.omk.smpp.message.*;
import ie.omk.smpp.util.PacketFactory;
import java.util.*;
import com.lizongbo.smpp.server.handlers.*;
/**
*
*Title: java smpp smsc server
*
*Description: smpp短消息中心
*
*Copyright: Copyright (c) 2007
*
*Company: http://618119.com
*
* @author lizongbo
* @version 1.0
*/

public class Main {
public static void main(String[] args) {
//以 spring配置方式启动
ApplicationContext context = new ClassPathXmlApplicationContext(
“classpath*:/springmina.xml”);
IoAcceptor acceptor = (IoAcceptor) context.getBean(“ioAcceptor”);
test(acceptor);
}
//普通方式启动
public static void commonmain(String[] args) throws IOException {
IoAcceptor acceptor = new SocketAcceptor();
IoAcceptorConfig config = new SocketAcceptorConfig();
DefaultIoFilterChainBuilder chain = config.getFilterChain();
chain.addLast(“logger”, new LoggingFilter());
chain.addLast(“codec”,
new ProtocolCodecFilter(new SMPPProtocolCodecFactory()));
chain.addLast(“threadpool”,
new org.apache.mina.filter.executor.ExecutorFilter());
InetSocketAddress serverAddr = new InetSocketAddress(5432);
SMPPServerSessionHandler handler = new SMPPServerSessionHandler();
Map<long> handles = new HashMap</long><long> ();
handles.put(Long.valueOf(0x00000002), new BindTransmitterHandler());
handles.put(Long.valueOf(0x00000001), new BindReceiverHandler());
handles.put(Long.valueOf(0x00000004), new SubmitSMHandler());
handles.put(Long.valueOf(0x00000006), new UnbindHandler());
handles.put(Long.valueOf(0x80000005), new DeliverSMRespHandler());
acceptor.bind(serverAddr, handler, config);
System.out.println(“Listening on port ” + 5432);
test(acceptor);
}</long>

private static void test(IoAcceptor acceptor) throws
InvalidParameterValueException {
try {
Thread.sleep(20000);
}
catch (InterruptedException ex) {
}
Set<iosession> ss = acceptor.getManagedSessions(new InetSocketAddress(5432));
System.out.println(“ss.size() == ” + ss.size());
if (ss.size() > 0) {
//模拟主动下发一条消息状态报告
DeliverSM rs = null;
try {
rs = (DeliverSM) PacketFactory.newInstance(SMPPPacket.DELIVER_SM);
}
catch (BadCommandIDException ex1) {
}
//这里还有很多字段没填,需要补上
rs.setSequenceNum(9999);
rs.setMessageId(“618119”);
rs.setServiceType(“aaa”);
rs.setSource(new Address(0, 0, “618119”));
rs.setDestination(new Address(0, 0, “10086”));
rs.setEsmClass(0);
rs.setProtocolID(0);
rs.setPriority(0);
rs.setRegistered(0);
rs.setReplaceIfPresent(0);
rs.setDataCoding(15);
rs.setDefaultMsg(1);
rs.setMessage(“succ”.getBytes());
System.out.println(“主动发回一个消息应答”);
ss.iterator().next().write(rs);
}
}</iosession>
}
[/code]
spring 的xml配置文件:

[code]

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:aop=”http://www.springframework.org/schema/aop” xmlns:util=”http://www.springframework.org/schema/util” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd “>
<util:constant id=”ie.omk.smpp.message.BIND_TRANSMITTER” static-field=”ie.omk.smpp.message.SMPPPacket.BIND_TRANSMITTER”/>
<bean id=”com.lizongbo.smpp.server.handlers.BindTransmitterHandler” class=”com.lizongbo.smpp.server.handlers.BindTransmitterHandler”/>
<bean id=”com.lizongbo.smpp.server.handlers.BindReceiverHandler” class=”com.lizongbo.smpp.server.handlers.BindReceiverHandler”/>
<bean id=”com.lizongbo.smpp.server.handlers.SubmitSMHandler” class=”com.lizongbo.smpp.server.handlers.SubmitSMHandler”/>
<bean id=”com.lizongbo.smpp.server.handlers.UnbindHandler” class=”com.lizongbo.smpp.server.handlers.UnbindHandler”/>
<bean id=”com.lizongbo.smpp.server.handlers.DeliverSMRespHandler” class=”com.lizongbo.smpp.server.handlers.DeliverSMRespHandler”/>
<util:map id=”handlers” key-type=”java.lang.Long”>
<!– Integer.decode(“0x80000005″)要出错,被迫用Long类型 –>
<entry key=”0x00000002″>
<ref local=”com.lizongbo.smpp.server.handlers.BindTransmitterHandler”/>
</entry>
<entry key=”0x00000001″>
<ref local=”com.lizongbo.smpp.server.handlers.BindReceiverHandler”/>
</entry>
<entry key=”0x00000004″>
<ref local=”com.lizongbo.smpp.server.handlers.SubmitSMHandler”/>
</entry>
<entry key=”0x00000006″>
<ref local=”com.lizongbo.smpp.server.handlers.UnbindHandler”/>
</entry>
<entry key=”0x80000005″>
<ref local=”com.lizongbo.smpp.server.handlers.DeliverSMRespHandler”/>
</entry>
</util:map>
<bean class=”org.springframework.beans.factory.config.CustomEditorConfigurer”>
<property name=”customEditors”>
<map>
<entry key=”java.net.SocketAddress”>
<bean class=”org.apache.mina.integration.spring.InetSocketAddressEditor”/>
</entry>
</map>
</property>
</bean>
<!– The IoHandler delegate implementation –>
<bean id=”smppHandler” class=”com.lizongbo.smpp.server.SMPPServerSessionHandler”>
<property name=”handles”>
<ref local=”handlers”/>
</property>
<property name=”readerIdleTimeout”>
<value>600</value>
</property>
</bean>
<!– The protocol codec factory for smpp –>
<bean id=”smppProtocolCodecFactory” class=”com.lizongbo.smpp.server.codec.SMPPProtocolCodecFactory”> </bean>
<bean id=”smppFilterThreadPoolFactory” class=”org.apache.mina.integration.spring.ThreadPoolExecutorFactoryBean”>
<property name=”corePoolSize” value=”10″/>
<property name=”maxPoolSize” value=”100″/>
<property name=”keepAliveSeconds” value=”30″/>
</bean>
<bean id=”smppFilterChainBuilder” class=”org.apache.mina.integration.spring.DefaultIoFilterChainBuilderFactoryBean”>
<property name=”filters”>
<list>
<bean class=”org.apache.mina.filter.executor.ExecutorFilter”>
<constructor-arg>
<ref bean=”smppFilterThreadPoolFactory”/>
</constructor-arg>
</bean>
<bean class=”org.apache.mina.filter.codec.ProtocolCodecFilter”>
<constructor-arg ref=”smppProtocolCodecFactory”/>
</bean>
<bean class=”org.apache.mina.filter.LoggingFilter”/>
</list>
</property>
<property name=”filterNamePrefix” value=”smppFilterPool”/>
</bean>
<bean id=”smppThreadModel” class=”org.apache.mina.integration.spring.ExecutorThreadModelFactoryBean”>
<property name=”serviceName” value=”smppService”/>
<property name=”executor”>
<bean class=”org.apache.mina.integration.spring.ThreadPoolExecutorFactoryBean”>
<property name=”corePoolSize” value=”6″/>
<property name=”maxPoolSize” value=”10″/>
<property name=”keepAliveSeconds” value=”30″/>
</bean>
</property>
</bean>
<bean id=”smppSocketAcceptorConfig” class=”org.apache.mina.transport.socket.nio.SocketAcceptorConfig”>
<property name=”filterChainBuilder” ref=”smppFilterChainBuilder”/>
<property name=”threadModel” ref=”smppThreadModel”/>
<property name=”reuseAddress” value=”true”/>
</bean>
<bean id=”threadExecutor” class=”java.util.concurrent.Executors” factory-method=”newCachedThreadPool”/>
<bean id=”ioAcceptor” class=”org.apache.mina.integration.spring.IoAcceptorFactoryBean”>
<property name=”target”>
<bean class=”org.apache.mina.transport.socket.nio.SocketAcceptor”>
<constructor-arg index=”0″>
<value>3</value>
</constructor-arg>
<constructor-arg index=”1″>
<ref bean=”threadExecutor”/>
</constructor-arg>
</bean>
</property>
<property name=”bindings”>
<list>
<bean class=”org.apache.mina.integration.spring.Binding”>
<property name=”address” value=”:5432″/>
<property name=”handler” ref=”smppHandler”/>
<property name=”serviceConfig” ref=”smppSocketAcceptorConfig”/>
</bean>
</list>
</property>
</bean>
<!– SMSC BEANS –>
<bean id=”smscFilterThreadPoolFactory” class=”org.apache.mina.integration.spring.ThreadPoolExecutorFactoryBean”>
<property name=”corePoolSize” value=”10″/>
<property name=”maxPoolSize” value=”100″/>
<property name=”keepAliveSeconds” value=”30″/>
</bean>
<bean id=”smscFilterChainBuilder” class=”org.apache.mina.integration.spring.DefaultIoFilterChainBuilderFactoryBean”>
<property name=”filters”>
<list>
<bean class=”org.apache.mina.filter.codec.ProtocolCodecFilter”>
<constructor-arg ref=”smppProtocolCodecFactory”/>
</bean>
<bean class=”org.apache.mina.filter.executor.ExecutorFilter”>
<constructor-arg>
<ref bean=”smscFilterThreadPoolFactory”/>
</constructor-arg>
</bean>
<bean class=”org.apache.mina.filter.LoggingFilter”/>
</list>
</property>
<property name=”filterNamePrefix” value=”smscFilterPool”/>
</bean>
<bean id=”smscThreadModel” class=”org.apache.mina.integration.spring.ExecutorThreadModelFactoryBean”>
<property name=”serviceName” value=”smscService”/>
<property name=”executor”>
<bean class=”org.apache.mina.integration.spring.ThreadPoolExecutorFactoryBean”>
<property name=”corePoolSize” value=”6″/>
<property name=”maxPoolSize” value=”10″/>
<property name=”keepAliveSeconds” value=”30″/>
</bean>
</property>
</bean>
<bean id=”smscSocketConnectorConfig” class=”org.apache.mina.transport.socket.nio.SocketConnectorConfig”>
<property name=”filterChainBuilder” ref=”smscFilterChainBuilder”/>
<property name=”threadModel” ref=”smscThreadModel”/>
</bean>
</beans>
[/code]

Older Posts »

Powered by WordPress