Vikings

          2008年7月11日 #

          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 閱讀(2233) | 評論 (0)編輯 收藏

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

          轉自:
          http://www.aygfsteel.com/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 閱讀(389) | 評論 (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 閱讀(312) | 評論 (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 閱讀(4049) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 成都市| 舟曲县| 衡阳县| 来宾市| 平顶山市| 洛宁县| 三门县| 白玉县| 宝坻区| 桐柏县| 阳江市| 西乡县| 浦北县| 恩平市| 漯河市| 霍林郭勒市| 疏勒县| 修文县| 永丰县| 泰州市| 明光市| 汉中市| 紫阳县| 宜川县| 德格县| 文登市| 葵青区| 望谟县| 齐河县| 施秉县| 博湖县| 江西省| 会理县| 白沙| 南宫市| 临漳县| 轮台县| 文山县| 牟定县| 蓝山县| 珠海市|