RESTful 請求主程式:Client.java

package com.emprogria.rsContact; import com.sun.jersey.api.client.*; import java.util.ArrayList; import java.util.List; import javax.ws.rs.core.MediaType; import javax.xml.bind.JAXBElement; public class Client { private com.sun.jersey.api.client.Client myWebClient = null; private String wsHost = null; public Client(String wsURL) { this.wsHost = wsURL; this.init(); } public void init() { this.myWebClient = com.sun.jersey.api.client.Client.create(); } public Contact get(String msn) { Contact myContact = null; WebResource myWebResource = this.myWebClient.resource(this.wsHost + "/contacts"); if (myWebResource != null) { GenericType<JAXBElement<Contact>> generic = new GenericType<JAXBElement<Contact>>() { }; JAXBElement<Contact> jaxbContact = myWebResource.path(msn).type(MediaType.APPLICATION_XML).get(generic); myContact = jaxbContact.getValue(); } return myContact; } public int edit(Contact newContact) { int statusCode = -1; Contact myContact = null; WebResource myWebResource = this.myWebClient.resource(this.wsHost + "/contacts"); if (myWebResource != null) { GenericType<JAXBElement<Contact>> generic = new GenericType<JAXBElement<Contact>>() { }; JAXBElement<Contact> jaxbContact = myWebResource.path(newContact.getMsn()).type(MediaType.APPLICATION_XML).get(generic); myContact = jaxbContact.getValue(); if (newContact.getRealName() != null) { myContact.setRealName(newContact.getRealName()); } if (newContact.getTel() != null) { myContact.setTel(newContact.getTel()); } jaxbContact.setValue(myContact); myWebResource = this.myWebClient.resource(this.wsHost + "/contacts/" + myContact.getMsn()); ClientResponse myClientResponse = myWebResource.put(ClientResponse.class, jaxbContact); statusCode = myClientResponse.getStatus(); } return statusCode; } public int delete(String msn) { int statusCode = -1; WebResource myWebResource = this.myWebClient.resource(this.wsHost + "/contacts"); if (myWebResource != null) { ClientResponse myClientResponse = myWebResource.path(msn).delete(ClientResponse.class); statusCode = myClientResponse.getStatus(); } return statusCode; } public int add(Contact newContact) { int statusCode = -1; WebResource myWebResource = this.myWebClient.resource(this.wsHost + "/contacts"); if (myWebResource != null) { GenericType<JAXBElement<Contact>> generic = new GenericType<JAXBElement<Contact>>() { }; JAXBElement<Contact> jaxbContact = myWebResource.path("RichChihLee@hotmail.com").type(MediaType.APPLICATION_XML).get(generic); Contact myContact = jaxbContact.getValue(); myContact.setMsn(newContact.getMsn()); if (newContact.getRealName() != null) { myContact.setRealName(newContact.getRealName()); } if (newContact.getTel() != null) { myContact.setTel(newContact.getTel()); } jaxbContact.setValue(myContact); ClientResponse myClientResponse = myWebResource.post(ClientResponse.class, jaxbContact); statusCode = myClientResponse.getStatus(); } return statusCode; } public List<Contact> list() { List<Contact> contactList = new ArrayList<Contact>(); WebResource myWebResource = this.myWebClient.resource(this.wsHost + "/contacts"); if (myWebResource != null) { GenericType<JAXBElement<Contacts>> generic = new GenericType<JAXBElement<Contacts>>() { }; JAXBElement<Contacts> jaxbContacts = myWebResource.path("/").type(MediaType.APPLICATION_XML).get(generic); Contacts myContacts = jaxbContacts.getValue(); contactList = myContacts.getContactList(); } return contactList; } /** * @param args the command line arguments */ public static void main(String[] args) { String wsHost = "http://localhost:28080/rsContact/resources"; if (args.length > 1) { wsHost = args[0]; } Client myClient = new Client(wsHost); Contact myContact = null; System.out.println("---- List ----"); List<Contact> myContactList = myClient.list(); for (int i = 0; i < myContactList.size(); i++) { myContact = myContactList.get(i); if (myContact != null) { System.out.println("MSN=" + myContact.getMsn()); System.out.println("\tReal Name=" + myContact.getRealName()); System.out.println("\tTEL=" + myContact.getTel()); } } System.out.println("---- Get ----"); String msn = "RichChihLee@gmail.com"; myContact = myClient.get(msn); if (myContact != null) { System.out.println("MSN=" + msn); System.out.println("\tReal Name=" + myContact.getRealName()); System.out.println("\tTEL=" + myContact.getTel()); } System.out.println("---- Update ----"); myContact.setTel(String.valueOf(java.util.GregorianCalendar.getInstance().getTime().getTime())); int statusCode = myClient.edit(myContact); if (statusCode < 400) { System.out.println("---- Get ----"); myContact = myClient.get(myContact.getMsn()); if (myContact != null) { System.out.println("MSN=" + msn); System.out.println("\tReal Name=" + myContact.getRealName()); System.out.println("\tTEL=" + myContact.getTel()); } } System.out.println("---- Delete ----"); myContact.setMsn("RichChihLee2@aim.com"); statusCode = myClient.delete(myContact.getMsn()); if (statusCode != -1) { System.out.println(myContact.getMsn() + ":" + String.valueOf(statusCode)); } System.out.println("---- Add ----"); myContact.setMsn("RichChihLee2@aim.com"); myContact.setRealName("Rich Lee"); myContact.setTel(String.valueOf(java.util.GregorianCalendar.getInstance().getTime().getTime())); statusCode = myClient.add(myContact); if (statusCode < 400) { System.out.println("---- List ----"); myContactList.clear(); myContactList = myClient.list(); for (int i = 0; i < myContactList.size(); i++) { myContact = myContactList.get(i); if (myContact != null) { System.out.println("MSN=" + myContact.getMsn()); System.out.println("\tReal Name=" + myContact.getRealName()); System.out.println("\tTEL=" + myContact.getTel()); } } } } }