Author: jliu
Date: Mon Sep 10 02:46:34 2007
New Revision: 574195
URL: http://svn.apache.org/viewvc?rev=574195&view=rev
Log:
Added PUT/POST/DELETE into JAX-RS sample.
Added:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book.txt
(with props)
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book.txt
(with props)
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_update_book.txt
(with props)
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book.txt
(with props)
Modified:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSDispatchInterceptor.java
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Modified:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSDispatchInterceptor.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSDispatchInterceptor.java?rev=574195&r1=574194&r2=574195&view=diff
==============================================================================
---
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSDispatchInterceptor.java
(original)
+++
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSDispatchInterceptor.java
Mon Sep 10 02:46:34 2007
@@ -123,7 +123,8 @@
String httpMethod,
Map<String, String> values) {
for (OperationResourceInfo ori :
resource.getMethodDispatcher().getOperationResourceInfos()) {
URITemplate uriTemplate = ori.getURITemplate();
- if (uriTemplate != null && uriTemplate.match(path, values)) {
+ if (uriTemplate != null && uriTemplate.match(path, values)
+ && ori.getHttpMethod().equalsIgnoreCase(httpMethod)) {
return ori;
} /*
* else { //URITemplate == null means match by default if
Modified:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=574195&r1=574194&r2=574195&view=diff
==============================================================================
---
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
(original)
+++
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
Mon Sep 10 02:46:34 2007
@@ -24,50 +24,97 @@
import java.util.ArrayList;
import java.util.List;
+
import javax.ws.rs.HttpMethod;
import javax.ws.rs.UriParam;
import javax.ws.rs.UriTemplate;
import javax.ws.rs.core.HttpContext;
+import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
-
@UriTemplate("/bookstore/")
public class BookStore {
-
+
+ private static List<Book> books = new ArrayList<Book>();
+ private static long bookId = 123;
+
@HttpContext UriInfo uriInfo;
+ static {
+ Book book = new Book();
+ book.setId(bookId);
+ book.setName("CXF in Action");
+ books.add(book);
+ }
+
public BookStore() {
}
@HttpMethod("GET")
- public List<Book> getBooks() {
+ public List<Book> getAllItems() {
System.out.println("----invoking getBooks");
- List<Book> books = new ArrayList<Book>(1);
- Book book = new Book();
- book.setId(123);
- book.setName("CXF in Action");
- books.add(book);
-/*
- Book book1 = new Book();
- book1.setId(124);
- book1.setName("CXF in Action - 2");
- books.add(book1);*/
-
+
return books;
+ }
+
+ @HttpMethod("GET")
+ @UriTemplate("/books/{bookId}/")
+ public Book getBook(@UriParam("bookId") String id) {
+ System.out.println("----invoking getBook with cdId: " + id);
+ long idNumber = Long.parseLong(id);
+ for (Book b : books) {
+ if (idNumber == b.getId()) {
+ return b;
+ }
+ }
+
+ return null;
}
- @HttpMethod("GET")
- @UriTemplate("/{bookId}/")
- public Book getBook(@UriParam("bookId") String bookId) {
- System.out.println("----invoking getBook with bookId: " + bookId);
-
- Book book = new Book();
- book.setId(123);
- book.setName("CXF in Action");
+ @HttpMethod("POST")
+ @UriTemplate("/books")
+ public Book addBook(Book book) {
+ System.out.println("----invoking addBook, book name is: " +
book.getName());
+ book.setId(++bookId);
+ books.add(book);
+
return book;
}
+ @HttpMethod("PUT")
+ @UriTemplate("/books/")
+ public Response updateBook(Book book) {
+ System.out.println("----invoking updateBook, book name is: " +
book.getName());
+ for (int i = 0; i < books.size(); i++) {
+ Book b = books.get(i);
+ if (b.getId() == book.getId()) {
+ books.set(i, book);
+ break;
+ }
+ }
+
+ return null;
+ }
+
+
+ @HttpMethod("DELETE")
+ @UriTemplate("/books/{bookId}/")
+ public Response deleteBook(@UriParam("bookId") String id) {
+ System.out.println("----invoking deleteBook with bookId: " + id);
+ long idNumber = Long.parseLong(id);
+ for (int i = 0; i < books.size(); i++) {
+ Book b = books.get(i);
+ if (idNumber == b.getId()) {
+ books.remove(i);
+ break;
+ }
+ }
+
+ return null;
+ }
+
+
@UriTemplate("/cd/{CDId}/")
public CD getCD(@UriParam("CDId") String cdId) {
System.out.println("----invoking getCD with cdId: " + cdId);
@@ -77,5 +124,6 @@
return cd;
}
-
}
+
+
Modified:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=574195&r1=574194&r2=574195&view=diff
==============================================================================
---
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Mon Sep 10 02:46:34 2007
@@ -19,10 +19,16 @@
package org.apache.cxf.systest.jaxrs;
+import java.io.File;
import java.io.InputStream;
import java.net.URL;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.FileRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
@@ -55,7 +61,7 @@
@Test
public void testGetBook123() throws Exception {
String endpointAddress =
- "http://localhost:9080/xml/bookstore/123";
+ "http://localhost:9080/xml/bookstore/books/123";
URL url = new URL(endpointAddress);
InputStream in = url.openStream();
assertNotNull(in);
@@ -66,6 +72,82 @@
//System.out.println("---" + getStringFromInputStream(in));
assertEquals(getStringFromInputStream(expected),
getStringFromInputStream(in));
}
+
+ @Test
+ public void testAddBook() throws Exception {
+ String endpointAddress =
+ "http://localhost:9080/xml/bookstore/books";
+
+ String inputFile =
getClass().getResource("resources/add_book.txt").getFile();
+ File input = new File(inputFile);
+ PostMethod post = new PostMethod(endpointAddress);
+ RequestEntity entity = new FileRequestEntity(input, "text/xml;
charset=ISO-8859-1");
+ post.setRequestEntity(entity);
+ HttpClient httpclient = new HttpClient();
+
+ try {
+ int result = httpclient.executeMethod(post);
+ assertEquals(200, result);
+ //System.out.println("Response status code: " + result);
+ //System.out.println("Response body: ");
+ //System.out.println(post.getResponseBodyAsString());
+
+ InputStream expected =
getClass().getResourceAsStream("resources/expected_add_book.txt");
+
+ assertEquals(getStringFromInputStream(expected),
post.getResponseBodyAsString());
+ } finally {
+ // Release current connection to the connection pool once you are
done
+ post.releaseConnection();
+ }
+ }
+
+ @Test
+ public void testUpdateBook() throws Exception {
+ String endpointAddress =
+ "http://localhost:9080/xml/bookstore/books";
+
+ String inputFile =
getClass().getResource("resources/update_book.txt").getFile();
+ File input = new File(inputFile);
+ PutMethod post = new PutMethod(endpointAddress);
+ RequestEntity entity = new FileRequestEntity(input, "text/xml;
charset=ISO-8859-1");
+ post.setRequestEntity(entity);
+ HttpClient httpclient = new HttpClient();
+
+ try {
+ int result = httpclient.executeMethod(post);
+ assertEquals(200, result);
+ System.out.println("Response status code: " + result);
+ System.out.println("Response body: ");
+ System.out.println(post.getResponseBodyAsString());
+
+ //Verify result
+ endpointAddress =
+ "http://localhost:9080/xml/bookstore/books/123";
+ URL url = new URL(endpointAddress);
+ InputStream in = url.openStream();
+ assertNotNull(in);
+
+ InputStream expected = getClass()
+ .getResourceAsStream("resources/expected_update_book.txt");
+
+ //System.out.println("---" + getStringFromInputStream(in));
+ assertEquals(getStringFromInputStream(expected),
getStringFromInputStream(in));
+
+ //Roll back changes:
+ String inputFile1 = getClass().getResource(
+
"resources/expected_get_book123.txt").getFile();
+ File input1 = new File(inputFile1);
+ PutMethod post1 = new PutMethod(endpointAddress);
+ RequestEntity entity1 = new FileRequestEntity(input1, "text/xml;
charset=ISO-8859-1");
+ post1.setRequestEntity(entity1);
+ HttpClient httpclient1 = new HttpClient();
+ httpclient1.executeMethod(post);
+
+ } finally {
+ // Release current connection to the connection pool once you are
done
+ post.releaseConnection();
+ }
+ }
private String getStringFromInputStream(InputStream in) throws Exception {
CachedOutputStream bos = new CachedOutputStream();
Added:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book.txt
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book.txt?rev=574195&view=auto
==============================================================================
---
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book.txt
(added)
+++
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book.txt
Mon Sep 10 02:46:34 2007
@@ -0,0 +1 @@
+<Book><name>CXF in Action - 2</name></Book>
\ No newline at end of file
Propchange:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book.txt
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book.txt?rev=574195&view=auto
==============================================================================
---
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book.txt
(added)
+++
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book.txt
Mon Sep 10 02:46:34 2007
@@ -0,0 +1 @@
+<Book><id>124</id><name>CXF in Action - 2</name></Book>
\ No newline at end of file
Propchange:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_update_book.txt
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_update_book.txt?rev=574195&view=auto
==============================================================================
---
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_update_book.txt
(added)
+++
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_update_book.txt
Mon Sep 10 02:46:34 2007
@@ -0,0 +1 @@
+<Book><id>123</id><name>CXF in Action - 3</name></Book>
\ No newline at end of file
Propchange:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_update_book.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_update_book.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book.txt
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book.txt?rev=574195&view=auto
==============================================================================
---
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book.txt
(added)
+++
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book.txt
Mon Sep 10 02:46:34 2007
@@ -0,0 +1 @@
+<Book><id>123</id><name>CXF in Action - 3</name></Book>
\ No newline at end of file
Propchange:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain