Vikings

2008年7月3日 #

Dom4j的CDATA问题与UTF-8字符集

 

本文转自:http://www.b9527.net/?q=node/1124
 
原文如下:
 

1. 写入文件的格式

写入 Xml 文件的时候默认是全部内容写为一行,这个可以通过加入 Format 来解决:

OutputFormat format = OutputFormat.createPrettyPrint();

2. Xml 中文问题

2.1 Xml 最好设为 UTF-8 格式,

format.setEncoding("utf-8");

2.2 不要用 FileWriter 输出双字节,改为 FileOutputStream 输出单字节:

XMLWriter output = new XMLWriter(new FileOutputStream(configFile), format);

3. CDATA类型文本输入

Element conTblOpr = rowElement.addElement(XmlDBConstants.CON_TBL_OPR);// 加入节点

DefaultCDATA conTblOprCdata = new DefaultCDATA(conTblOprField);// CDATA格式化

conTblOpr.add(conTblOprCdata );// 加入CDATA文本

Dom4j 里面已经内置了对 CDATA 类型文本的支持,不要硬编码去在文本两边加<![CDATA[***]]>。

 

posted @ 2011-07-05 00:12 Vikings 阅读(2215) | 评论 (0)编辑 收藏

实施WebService Security[WS-Security1.0]的Encrypt和Sign模式(XFire+WSS4J)

转自:
http://www.blogjava.net/security/archive/2006/08/08/xfire_wss4j.html

thanks for springside

鉴于很多系统需要实施WS-Security的标准,我们在SpringSide中提供了XFire+WSS4J的Demo,本文介绍SpringSide中Spring+XFire+WSS4J的基本配置

[WebService Server端配置]
第一,创建一个基本的BookService
public interface BookService {
    
/** *//**
     * 按书名模糊查询图书
     
*/

    List findBooksByName(String name);

    
/** *//**
     * 查找目录下的所有图书
     *
     * 
@param categoryId 如果category为null或“all”, 列出所有图书。
     
*/

    List findBooksByCategory(String categoryId);

    
/** *//**
     * 列出所有分类.
     *
     * 
@return List<Category>,或是null。
     
*/

    List getAllCategorys();
}
第二,接口扩展,即Extend基本的BookService,在XFire中,不同的WSS4J策略需要针对不同的ServiceClass,否则<inHandlers>里面的定义会Overlap。


   <!--BookService 基类-->
    
<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" abstract="true">
        
<property name="serviceFactory" ref="xfire.serviceFactory"/>
        
<property name="xfire" ref="xfire"/>
    
</bean>

    
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        
<property name="mappings">
            
<value>
                /BookService=bookService
                /BookServiceWSS4J=bookServiceWSS4J
                /BookServiceWSS4JEnc=bookServiceWSS4JEnc
                /BookServiceWSS4JSign=bookServiceWSS4JSign
            
</value>
        
</property>
    
</bean>

   
<!--(1)BookWebService 不需要认证-->
    
<bean id="bookService" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        
<property name="serviceFactory" ref="xfire.serviceFactory"/>
        
<property name="xfire" ref="xfire"/>
        
<property name="serviceBean" ref="bookManager"/>
        
<property name="serviceClass" value="org.springside.bookstore.plugins.xfire.service.BookService"/>
    
</bean>

    
<!--  (3)BookWebService 使用 WSS4J验证-->
    
<bean id="bookServiceWSS4J" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        
<property name="serviceBean" ref="bookManager"/>
        
<property name="serviceClass" value="org.springside.bookstore.plugins.xfire.service.BookServiceWSS4J"/>
        
<property name="inHandlers">
            
<list>
                
<ref bean="domInHandler"/>
                
<ref bean="wss4jInHandler"/>
                
<ref bean="validateUserTokenHandler"/>
            
</list>
        
</property>
    
</bean>

    
<bean id="domInHandler" class="org.codehaus.xfire.util.dom.DOMInHandler"/>

    
<bean id="wss4jInHandler" class="org.codehaus.xfire.security.wss4j.WSS4JInHandler">
        
<property name="properties">
            
<props>
                
<prop key="action">UsernameToken</prop>
                
<prop key="passwordCallbackClass">org.springside.bookstore.plugins.xfire.wss4j.PasswordHandler</prop>
            
</props>
        
</property>
    
</bean>

    
<bean id="validateUserTokenHandler" class="org.springside.bookstore.plugins.xfire.wss4j.WSS4JTokenHandler"/>
    
    
<!--  (4)BookWebService 使用 WSS4J验证 Encrypt模式-->
    
<bean id="bookServiceWSS4JEnc" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        
<property name="serviceBean" ref="bookManager"/>
        
<property name="serviceClass" value="org.springside.bookstore.plugins.xfire.service.BookServiceWSS4JEnc"/>
        
<property name="inHandlers">
            
<list>
                
<ref bean="domInHandler"/>
                
<ref bean="wss4jInHandlerEnc"/>
                
<ref bean="validateUserTokenHandler"/>
            
</list>
        
</property>
    
</bean>
        
    
<bean id="wss4jInHandlerEnc" class="org.codehaus.xfire.security.wss4j.WSS4JInHandler">
        
<property name="properties">
          
<props>
            
<prop key="action">Encrypt</prop>
            
<prop key="decryptionPropFile">org/springside/bookstore/plugins/xfire/wss4j/insecurity_enc.properties</prop>
            
<prop key="passwordCallbackClass">org.springside.bookstore.plugins.xfire.wss4j.PasswordHandler</prop>
          
</props>
        
</property>
    
</bean>
    
    
<!--  (5)BookWebService 使用 WSS4J验证 Signature模式-->
    
<bean id="bookServiceWSS4JSign" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        
<property name="serviceBean" ref="bookManager"/>
        
<property name="serviceClass" value="org.springside.bookstore.plugins.xfire.service.BookServiceWSS4JSign"/>
        
<property name="inHandlers">
            
<list>
                
<ref bean="domInHandler"/>
                
<ref bean="wss4jInHandlerSign"/>
                
<ref bean="validateUserTokenHandler"/>
            
</list>
        
</property>
    
</bean>
    
    
<bean id="wss4jInHandlerSign" class="org.codehaus.xfire.security.wss4j.WSS4JInHandler">
        
<property name="properties">
          
<props>
            
<prop key="action">Signature</prop>
            
<prop key="signaturePropFile">org/springside/bookstore/plugins/xfire/wss4j/insecurity_sign.properties</prop>
            
<prop key="passwordCallbackClass">org.springside.bookstore.plugins.xfire.wss4j.PasswordHandler</prop>
          
</props>
        
</property>
    
</bean>
    
</beans>

posted @ 2008-10-29 01:55 Vikings 阅读(373) | 评论 (0)编辑 收藏

简化spring中的事务管理配置(ZT)

<!-- Transactional proxy for the services -->  
    
<bean id="baseTxProxy" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
        
<property name="transactionManager"><ref bean="transactionManager"/></property>  
        
<property name="transactionAttributes">  
            
<props>  
                
<prop key="*">PROPAGATION_REQUIRED</prop>  
            
</props>  
        
</property>  
    
</bean>  
  
    
<bean id="itemService" parent="baseTxProxy">  
        
<property name="target">  
            
<bean class="ItemServiceImpl" autowire="byName"/>  
        
</property>  
    
</bean>  
这样的话baseTxProxy也可能被实例化。是不是加上abstract="true"属性,把baseTxProxy只是当作一个模板比较好?因为只需要itemservice这个bean。

posted @ 2008-08-07 00:12 Vikings 阅读(292) | 评论 (0)编辑 收藏

使用java.awt.RenderingHints类设置参数,改善图片质量

 

如果想设置几个呈现提示(RenderingHints),可以多次调用setRenderHint,或者创建值的完整映射,并使用Graphics2D的setRenderingHints方法一次把它们都设置好。

java.awt.RenderingHints类 javadoc文档连接:
http://gceclub.sun.com.cn/Java_Docs/jdk6/docs/zh/api/java/awt/RenderingHints.html

一般使用的代码如下:

RenderingHints rh=new RenderingHints(RenderingHints. KEY_ANTIALIASING,
                                                        RenderingHints. VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_STROKE_CONTROL
              , RenderingHints.VALUE_STROKE_PURE);
rh.put(RenderingHints.KEY_ALPHA_INTERPOLATION
              , RenderingHints.ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHints(rh);

找出一个给定系统的方法是判断特定的绘制硬件(比如显卡)在系统中是否可用,假设有一个假想的isAccelerated方法告诉系统是否可以使用一种类型的图像加速。下面的代码允许根据isAccelerated方法的结果来设置提示:
//假设renderQuality是RenderingHints的私有类成员
if(isAccelerated()){
       renderQuality
=new RenderingHints(RenderingHints. KEY_RENDERING, 
                                                               RenderingHints. VALUE_RENDER_QUALITY);
}
else{
       renderQuality
=new RenderingHints(RenderingHints. KEY_RENDERING, 
                                                               RenderingHints. VALUE_RENDER_SPEED);
}

这样设置后比没有设置效果会好点。但是和acdsee等图片工具看起来还有差距。比较奇怪还需要设置什么参数才能优化图片质量。。。

另外,关于性能今天看到的一篇文章有点作用。。
现在图片预览一样存在Jprofile的大量内存使用的问题.
看到javatar的blog: http://javatar.javaeye.com/blog/41098
提及使用第三方的包 JMagicK: http://www.yeo.id.au/jmagick/ (Java接口)
生成图片预览的方法,因为目前项目面临上线的压力如果改变另外一种实现方式等于是从根部重构,面临测试的压力。。

posted @ 2008-07-11 16:11 Vikings 阅读(4026) | 评论 (0)编辑 收藏

501 port not allow after EPSV ALL, active mode off错误

用sun的ftp包连接IBM aix服务器抛如下错误:
501 port not allow after EPSV ALL, active mode off

原因:
ftp要改为被动passtive模式.

解决方法:
使用第三方包edtftpj-1.5.1.jar连接服务器,在连接前设置模式为passtive

Demo
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package app4;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPMessageCollector;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.util.debug.Level;
import com.enterprisedt.util.debug.Logger;
import java.util.Date;
import java.io.File;
public class Demo {
    /**
     *  Log stream
     */
    private static Logger log = Logger.getLogger(Demo.class);
    public static void main(String[] args) {
        // we want remote host, user name and password
//        if (args.length < 3) {
//            usage();
//            System.exit(1);
//        }
        // assign args to make it clear
        String host = "localhost";//args[0];
        String user = "lijun2";//args[1];
        String password = "836301";//args[2];
        Logger.setLevel(Level.ALL);
        FTPClient ftp = null;
        try {
            // set up client
            log.info("Connecting");
            ftp = new FTPClient(host);
            FTPMessageCollector listener = new FTPMessageCollector();
            ftp.setMessageListener(listener);
            // login
            log.info("Logging in");
            ftp.login(user, password);
            // set up passive ASCII transfers
            log.debug("Setting up passive, ASCII transfers");
            ftp.setConnectMode(FTPConnectMode.PASV);
            ftp.setType(FTPTransferType.BINARY);
            // get directory and print it to console           
            log.debug("Directory before put:");
            String[] files = ftp.dir(".", true);
            for (int i = 0; i < files.length; i++)
                log.debug(files[i]);
            // copy file to server
            System.out.println(new Date());
            log.info("Putting file");
            File loadfile = new File("a.ISO");
           
            ftp.put("a.ISO", "a.ISO");
           
            System.out.println(new Date());
            // get directory and print it to console           
            log.debug("Directory after put");
            files = ftp.dir(".", true);
            for (int i = 0; i < files.length; i++)
                log.debug(files[i]);
            // copy file from server
           // log.info("Getting file");
           // ftp.get("2005.doc" + ".copy", "2005.doc");
            // delete file from server
            //log.info("Deleting file");
           // ftp.delete("test.txt");
            // get directory and print it to console           
           // log.debug("Directory after delete");
            files = ftp.dir("", true);
            for (int i = 0; i < files.length; i++)
                log.debug(files[i]);
            // Shut down client               
            log.info("Quitting client");
            ftp.quit();
            String messages = listener.getLog();
            log.debug("Listener log:");
            log.debug(messages);
            log.info("Test complete");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void usage() {
        System.out.println("Usage: Demo remotehost user password");
    }
}

posted @ 2008-07-03 00:18 Vikings 阅读(1974) | 评论 (0)编辑 收藏