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