2011年4月20日 星期三

初探RESTful-II

Resources
A RESTful resource is anything that is addressable over the Web. By addressable, we mean resources that can be accessed and transferred between clients and servers. Subsequently, a resource is a logical, temporal mapping to a concept in the problem domain for which we are implementing a solution.

if we request a text file from CNN, our browser receives a text file. If we request a Flash movie from YouTube, our browser receives a Flash movie. The data is streamed in both cases over TCP/IP and the browser knows how to interpret the binary streams because of the HTTP protocol response header Content-Type. Consequently, in a RESTful system, the representation of a resource depends on the caller's desired type(MIME type), which is specified within the communication protocol's request.

Representation
the life of a web service there may be a variety of clients requesting resources. Different clients are able to consume different representations of the same resource. Therefore, a representation can take various forms, such as an image, a text file, or an XML stream or a JSON stream, but has to be available through the same URI.

URI
A Uniform Resource Identifier, or URI, in a RESTful web service is a hyperlink to a resource, and it's the only means for clients and servers to exchange representations.(The set of RESTful constraints don't dictate that URIs must be hyperlinks.)

In a RESTful system, the URI is not meant to change over time, as the architecture's implementation is what manages the services, locates the resources, negotiates the representations, and then sends back responses with the requested resources.
ps.在沒有使用RESTful的情況下,若改變檔名,那麼link也會跟著變動

Example:Java讀JSON

Requirement
JSON的官網中,找出相對應的程式語言的套件(我是用這套org.json)

Implementation
import org.json.*;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
GenerateJSON myJsonString = new GenerateJSON();
JSONObject tempObj = null;

try {
JSONObject jsonObj = new JSONObject(myJsonString.generateJSON());
tempObj = new JSONObject(jsonObj.get("UserProfile").toString());
System.out.println("/*------------UserProfile---------------*/");
System.out.print(tempObj.get("userName") + " ");
System.out.print(tempObj.get("password") + " ");
System.out.print(tempObj.get("sex"));
System.out.println();

System.out.println("/*------------MoneyPlan---------------*/");
JSONObject moneyPlan = new JSONObject(jsonObj.get("MoneyPlan")
.toString());
tempObj = new JSONObject(moneyPlan.get("Monday").toString());
System.out.print("[Monday]");
System.out.print(tempObj.get("dinner") + "\t");
System.out.print(tempObj.get("breakfast") + "\t");
System.out.print(tempObj.get("lunch") + "\n");

tempObj = new JSONObject(moneyPlan.get("Tuesday").toString());
System.out.print("[Tuesday]");
System.out.print(tempObj.get("dinner") + "\t");
System.out.print(tempObj.get("breakfast") + "\t");
System.out.print(tempObj.get("lunch") + "\n");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


import org.json.*;
//{
// "MoneyPlan":{
// "Monday":{
// "dinner":"100",
// "breakfast":"50",
// "lunch":"100"
// },
// "Tuesday":{
// "dinner":"200",
// "breakfast":"55",
// "lunch":"150"
// }
// },
// "UserProfile":{
// "userName":"aaa",
// "password":"bbb",
// "Sex":"Boy"
// }
//}

public class GenerateJSON {

public String generateJSON() {
JSONObject jsonObj = new JSONObject();
JSONObject detailObj = new JSONObject();
JSONObject detailObj2 = new JSONObject();

try {
detailObj.put("userName", "9836002");
detailObj.put("password", "9836002");
detailObj.put("sex", "Boy");
jsonObj.put("UserProfile", detailObj);

detailObj2.put("breakfast", "50");
detailObj2.put("lunch", "100");
detailObj2.put("dinner", "100");

detailObj = new JSONObject();
detailObj.put("Monday", detailObj2);

detailObj2 = new JSONObject();
detailObj2.put("breakfast", "55");
detailObj2.put("lunch", "150");
detailObj2.put("dinner", "200");

detailObj.put("Tuesday", detailObj2);
jsonObj.put("MoneyPlan", detailObj);

} catch (JSONException ex) {
System.out.println(ex.toString());
}

return jsonObj.toString();
}
}


Reference:
JSON
JSON in JAVA 的簡單程式範例

2011年4月19日 星期二

初探RESTful-I

最近泰哥要我研究RESTful Service in Codeigniter,雖然tutorial看的懂,能夠寫基本的code,但對tutorial中所提到的REST/RESTful卻還是有點模糊,上網找了些資料來看,找到有一篇還不錯REST and RESTful web service,是石頭大寫的,對RESTful的說明簡單易懂.
後來找到了一本書"RESTful Java Web Services-Master core REST concepts and create RESTful web services in Java",當中也有對REST/REST-like/RESTful做說明.

What is REST?
The term REST comes from Roy Fielding's PhD dissertation, published in 2000, and it stands for REpresentational State Transfer. REST by itself is not an architecture;REST is a set of constraints that, when applied to the design of a system, creates a software architectural style.If we implement all the REST guidelines outlined in Fielding's work, we end up with a system that has specific roles for data, components, hyperlinks, communication protocols, and data consumers.

What is RESTful?
They only define how data is transferred between components and what are the benefits of following the guidelines.
  • It must be a client-server system
  • It has to be stateless—there should be no need for the service to keep users' sessions; in other words, each request should be independent of others
  • It has to support a caching system—the network infrastructure should support cache at different levels
  • It has to be uniformly accessible—each resource must have a unique address and a valid point of access
  • It has to be layered—it must support scalability
  • It should provide code on demand—although this is an optional constraint, applications can be extendable at runtime by allowing the downloading of code on demand, for example, Java Applets
Reference:
REST and RESTful web service
Working with RESTful Services in CodeIgniter

2011年3月1日 星期二

一次刪掉大量的Database和User

今天遇到了一個狀況,要一次刪上百個Database及上百個User,雖然手動一個一個刪也OK,但...我好懶,而且以後可能還會再遇到同樣的事,所以我寫了以下的SQL:
ps.本來是要用Cursor,因為涉及要一次取一個值,但...後來想想,還是算了,怕未來SQL Server不支援.


/*-----------------刪除大量的資料庫--------------------*/
--在sys.databases中有所有database的清單,
--透過@minDataBase_ID和@maxDataBase_ID介
--定Range(database_id有遞增的特性,且編號
--可重複使用)
--Important:"[9936018]UDT"->在刪除時格式為:
-- DROP DATABASE [[9936018]]UDT]
--所以不建議學生以"[9936018]UDT"格式命名
DECLARE @dataBaseName AS NVARCHAR(100)
DECLARE @minDataBase_ID AS INT
DECLARE @maxDataBase_ID AS INT

SET @minDataBase_ID=7--先在sys.databases中找出要刪除的下限

SELECT TOP 1 @maxDataBase_ID=database_id
FROM sys.databases
ORDER BY database_id DESC

WHILE(@minDataBase_ID<=@maxDataBase_ID)
BEGIN

SELECT @dataBaseName=sys.databases.name
FROM sys.databases
WHERE sys.databases.database_id=@minDataBase_ID

IF(LEN(@dataBaseName)>0)
BEGIN
PRINT @dataBaseName
EXEC('DROP DATABASE ['+@dataBaseName+']')
END

SET @minDataBase_ID=@minDataBase_ID+1
END
GO



/*-------------------刪除大量的User----------------------*/
--在sys.database_principals中有所有User的清單,
--透過@minUserID和@maxUserID介定Range
--Important:
--1.在撰寫時,有找到兩個View:sys.database_principals,
--sys.server_principals,一個是database-level一個是
--server-level
--2.DROP USER後,還要在DROP LOGIN,否則Account還在
DECLARE @userName AS NVARCHAR(100)
DECLARE @minUserID AS INT
DECLARE @maxUserID AS INT

SET @minUserID=10

SELECT TOP 1 @maxUserID=sys.database_principals.principal_id
FROM sys.database_principals
ORDER BY sys.database_principals.principal_id DESC

WHILE(@minUserID<=@maxUserID)
BEGIN

SELECT @userName=sys.database_principals.name
FROM sys.database_principals
WHERE sys.database_principals.principal_id=@minUserID

IF(LEN(@userName)>0)
BEGIN
EXEC('DROP USER ['+@userName+']')
EXEC('DROP LOGIN ['+@userName+']')
END

SET @minUserID=@minUserID+1
END
GO

2010年12月24日 星期五

Post和Get編碼的差別?

編寫JSP時,最麻煩的就是編碼的問題,而今天我就遇到了"明明所有的Page都設成UTF-8",但顯示出來仍然是亂碼!
研究了好多天,終於知道問題在那了!原來是Post/Get的差別,後來Google了一下,有位大大這麼寫到:

原文:
Get 和Post都是HTTP的标准协议动词,用于编码和传送变量名和变量值,无论Get 和Post都是通过编码后传送数据。
是这个问题解释起来有点难,但有两点是可以肯定的:
一是:get方式传送数据时附加参数被认为是一个查询字符串,变量名/变量值作为URL的一部分被传送,假如读取数据后,不立即跳转到别的页面上,地址栏上会看得到有关的参数和值,并与表单一一对应,如地址栏上会看到xx=??&xx=??。而post方式传送,虽然也是被URL编码的。然而,变量名/变量值不作为URL的一部分被传送,而是放在实际的HTTP请求消息内部被传送。从地址栏上看不到。
二是:get 默认(只是默认)是以使用MIME类型application/x-www-form-urlencoded的urlencoded文本的格式传递参数。Urlencoding是一种字符编码,保证被传送的参数由遵循规范的文本组成。post则可以通过页面指定的编码方式进行编码。目前大体有两类:一是ANSI的ASCII字符集,不同的国家和地区制定了不同的标准,由此产生了GB2312、BIG5, JIS等等,且互不兼容,并且与之后出现的国际性的UNICODE标准编码的UTF-8等也不兼容,因此稍有不慎便出现乱码。


繁體:
Get 和Post都是HTTP的標準協議動詞,用於編碼和傳送變量名和變量值,無論Get 和Post都是通過編碼後傳送數據。
是這個問題解釋起來有點難,但有兩點是可以肯定的:
一是:get方式傳送數據時附加參數被認為是一個查詢字符串,變量名/變量值作為URL的一部分被傳送,假如讀取數據後,不立即跳轉到別的頁面上,地址欄上會看得到有關的參數和值,並與表單一一對應,如地址欄上會看到xx=??&xx=??。 而post方式傳送,雖然也是被URL編碼的。 然而,變量名/變量值不作為URL的一部分被傳送,而是放在實際的HTTP請求消息內部被傳送。 從地址欄上看不到。
二是:get 默認(只是默認)是以使用MIME類型application/x-www-form-urlencoded的urlencoded文本的格式傳遞參數。 Urlencoding是一種字符編碼,保證被傳送的參數由遵循規範的文本組成。 post則可以通過頁面指定的編碼方式進行編碼。 目前大體有兩類:一是ANSI的ASCII字符集,不同的國家和地區製定了不同的標準,由此產生了GB2312、BIG5, JIS等等,且互不兼容,並且與之後出現的國際性的UNICODE標準編碼的UTF-8等也不兼容,因此稍有不慎便出現亂碼。


資料來源:http://wenwen.soso.com/z/q188275796.htm

2010年3月14日 星期日

Free WMA to MP3 Converter

今天為了要把英文課本的CD轉出來,然後放在電子詞典中,所以在Google上找了一下WMA轉MP3的程式,而經過本人使用還不錯,簡單易用,而是FREE的!
jodix


Main Window while Converting.


MP3 Settings.


Edit ID3 Tag.


After Converting.

資料來源:jodix

2010年2月6日 星期六

無名相簿 下載程式

由於之前使用了WretchXD這個挺有名的"無名相簿下載程式",我只能說太強了,所以也想說自己寫一個小的玩一玩,在撰寫的過程中,所遇到的因難可真不少,畢竟自己程度不好,而我是以C#所開發的(.NET 2.0),功能目前也挺簡單的,但都有達到最基本的要求-"下載",若寫的不好,在此還請各位大大多多包含,若有什麼要改進的,可以留言,謝謝!

WretchAlbumDownload v1.1
目前已發現的問題如下:
1.相簿名稱複選時,圖片無法正確顯示->Modified 2010/02/23
2.在相簿還為載入完全時,點選相簿名稱時會當掉->Modified 2010/02/23
3.訊息文字排列有問題
------------------------
WretchAlbumDownload v1.0

參考資料:
CCN's Soft