2010年4月21日 星期三

2010年4月12日 星期一

接收 EPCIS 傳來的 HTTP POST 內容成功了

接收 EPCIS 傳來的 HTTP POST 內容成功了, 並將內容存到 servlet 所在的 local disk。
明天趕快來做筆記。

工具:

tomcat + netbean

2010年4月11日 星期日

EPCIS 的訂閱查詢有成功的送到 指定的 url

EPCIS 的訂閱查詢有成功的送到 指定的 url

工具:
1. EPCIS repository 0.4.2
2. reader-rp-client-0.5.0.rar


收到 http header 的內容如下:
 POST / HTTP/1.1
content-type: text/xml
content-length: 7290
User-Agent: Java/1.5.0_17
Host: 192.168.0.195:9000
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive

來試看看 EPCIS 的 SUBSCRIBED QUERY RESULTS 是否能正確的傳到指定的 port

來試看看 EPCIS 的 SUBSCRIBED QUERY RESULTS 是否能正確的傳到指定的 port

看看這一端是否有問題。

工具: reader-rp-client-0.5.0.rar

感謝 ycchen

取得 HTTP post 的 body

要取得 HTTP POST 的 body 可以使用 ServletRequest interface 的兩個方法: 

BufferedReader getReader()
          Retrieves the body of the request as character data using a BufferedReader.

ServletInputStream getInputStream()
          Retrieves the body of the request as binary data using a ServletInputStream.


getReader() 方法用於字元資料, getInputStream() 方法用於 binary data

2010年4月10日 星期六

設定 tomcat 處理某個 request 所使用的 Charset

要設定 Tomcat 處理某個 request 時所使用的 Charset, 要使用 ServletRequest interface 中的方法:

void setCharacterEncoding(String env)
          Overrides the name of the character encoding used in the body of this request.

例如,要處理 UTF8 的 request:

request.setCharacterEncoding("UTF-8");

Tomcat 所預設的 Charset 為: ISO-8859-1.

Encoding and decoding

對 input stream 內容進行解碼 (decoding) 要用到類別: Charset. input stream 所讀進來的內容為 bytes, 要解碼成不同的格式(如 utf8), 便要用類別 Charset,及 CharsetDecoder


類別的使用

使用 forName(String charsetName) 方法取得一個 Charset物件。 可以使用的 charsetName 有:

Charset
Description
US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
ISO-8859-1   ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
UTF-8 Eight-bit UCS Transformation Format
UTF-16BE Sixteen-bit UCS Transformation Format, big-endian byte order
UTF-16LE Sixteen-bit UCS Transformation Format, little-endian byte order
UTF-16 Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark

取得 Charset 物件後, 要建立一個新的解碼器物件。使用方法

abstract  CharsetDecoder newDecoder()
          Constructs a new decoder for this charset.

傳回一個 CharsetDecoder 物件。之後,使用 CharsetDecoder 物件的 decode() 方法:

CharBuffer decode(ByteBuffer in)
          Convenience method that decodes the remaining content of a single input byte buffer into a newly-allocated character buffer.





進行解碼,解碼的結果會儲存在一個 CharBuffer 物件。注意, 此 decode() 方法需要一個 ByteBuffer 物件。

Code Example:

// read, decode, and parse xml content (UTF-8 encoded!)
        byte[] xml = new byte[len];
        is.read(xml);
        ByteBuffer buf = ByteBuffer.wrap(xml);    // 將 byte[]  包裝成 ByteBuffer
       Charset charset = Charset.forName("UTF-8");   // 取得 Charset 物件
        CharsetDecoder decoder = charset.newDecoder();  // 取得 Decoder
        CharBuffer charBuffer = decoder.decode(buf);   // 進行 解碼
     

接收 epcis 所傳來的 query result XML

EPCIS 傳回來的 query result 內容是以 xml 表示。 編碼格式為 utf8。

不知道接收內容時, 是否要將內容 encoding 成 utf 8 才能讀到正確的內容? 尚在研究中。

EPCIS 的 Callback interface

當 epcis 會利用 Query callback interface 來將 schedule query 的結果傳到指定的 url.