Author: jliu
Date: Tue Sep 11 03:12:13 2007
New Revision: 574540
URL: http://svn.apache.org/viewvc?rev=574540&view=rev
Log:
preliminary support of javax.ws.rs.core.Response.
Added:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BuilderImpl.java
(with props)
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ResponseImpl.java
(with props)
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_not_exist.txt
(with props)
Modified:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/pom.xml
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.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/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/pom.xml?rev=574540&r1=574539&r2=574540&view=diff
==============================================================================
--- incubator/cxf/branches/jliu/rt/frontend/jaxrs/pom.xml (original)
+++ incubator/cxf/branches/jliu/rt/frontend/jaxrs/pom.xml Tue Sep 11 03:12:13
2007
@@ -73,6 +73,11 @@
<version>0.9</version>
</dependency>
<dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-servlet_2.5_spec</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-testutils</artifactId>
<version>${project.version}</version>
Modified:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java?rev=574540&r1=574539&r2=574540&view=diff
==============================================================================
---
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
(original)
+++
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
Tue Sep 11 03:12:13 2007
@@ -23,6 +23,8 @@
import java.io.OutputStream;
import java.util.List;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.Response;
import javax.ws.rs.ext.EntityProvider;
import javax.ws.rs.ext.ProviderFactory;
@@ -55,21 +57,35 @@
OutputStream out = message.getContent(OutputStream.class);
- if (objs.get(0) != null) {
- Class targetType = objs.get(0).getClass();
- if (objs.get(0).getClass().isArray()) {
- targetType = objs.get(0).getClass().getComponentType();
- } else if (objs.get(0) instanceof List &&
((List)objs.get(0)).get(0) != null) {
- targetType = ((List)objs.get(0)).get(0).getClass();
+ if (objs.get(0) != null) {
+ Object responseObj = objs.get(0);
+ if (objs.get(0) instanceof Response) {
+ Response response = (Response)responseObj;
+ responseObj = response.getEntity();
+
+ HttpServletResponse hsr =
(HttpServletResponse)message.get("HTTP.RESPONSE");
+ hsr.setStatus(response.getStatus());
+
+ if (responseObj == null) {
+ return;
+ }
+ }
+
+ Class targetType = responseObj.getClass();
+ if (responseObj.getClass().isArray()) {
+ targetType = responseObj.getClass().getComponentType();
+ } else if (responseObj instanceof List &&
((List)responseObj).get(0) != null) {
+ targetType = ((List)responseObj).get(0).getClass();
}
EntityProvider provider =
ProviderFactory.getInstance().createEntityProvider(targetType);
try {
- provider.writeTo(objs.get(0), null, out);
+ provider.writeTo(responseObj, null, out);
} catch (IOException e) {
e.printStackTrace();
- }
+ }
+
}
}
Added:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BuilderImpl.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BuilderImpl.java?rev=574540&view=auto
==============================================================================
---
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BuilderImpl.java
(added)
+++
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BuilderImpl.java
Tue Sep 11 03:12:13 2007
@@ -0,0 +1,106 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxrs.provider;
+
+import java.net.URI;
+import java.util.Date;
+import javax.ws.rs.core.CacheControl;
+import javax.ws.rs.core.EntityTag;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.NewCookie;
+import javax.ws.rs.core.Response;
+
+
+public final class BuilderImpl extends Response.Builder {
+ private int status = 200;
+ private Object entity;
+
+ public BuilderImpl() {
+ }
+
+ public BuilderImpl(Response r) {
+ this.status = r.getStatus();
+ this.entity = r.getEntity();
+ }
+
+ static Response.Builder create(Response response) {
+ return new BuilderImpl(response);
+ }
+
+ public Response build() {
+ Response r = new ResponseImpl(status, entity);
+ return r;
+ }
+
+ public Response.Builder status(int s) {
+ status = s;
+ return this;
+ }
+
+ public Response.Builder entity(Object e) {
+ entity = e;
+ return this;
+ }
+
+ public Response.Builder type(MediaType type) {
+ return null;
+ }
+
+ public Response.Builder type(String type) {
+ return null;
+ }
+
+ public Response.Builder language(String language) {
+ return null;
+ }
+
+ public Response.Builder location(URI location) {
+ return null;
+ }
+
+ public Response.Builder contentLocation(URI location) {
+ return null;
+ }
+
+ public Response.Builder tag(EntityTag tag) {
+ return null;
+ }
+
+ public Response.Builder tag(String tag) {
+ return null;
+ }
+
+ public Response.Builder lastModified(Date lastModified) {
+ return null;
+ }
+
+ public Response.Builder cacheControl(CacheControl cacheControl) {
+ return null;
+ }
+
+ public Response.Builder cookie(NewCookie cookie) {
+ return null;
+ }
+
+ public Response.Builder header(String name, Object value) {
+ return null;
+ }
+
+}
Propchange:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BuilderImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BuilderImpl.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java?rev=574540&r1=574539&r2=574540&view=diff
==============================================================================
---
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
(original)
+++
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
Tue Sep 11 03:12:13 2007
@@ -22,11 +22,13 @@
import java.util.ArrayList;
import java.util.List;
+import javax.ws.rs.core.Response.Builder;
import javax.ws.rs.ext.EntityProvider;
import javax.ws.rs.ext.HeaderProvider;
import javax.ws.rs.ext.ProviderFactory;
+
//NOTE: ProviderFactory should provide a method that can pass in media types
public class ProviderFactoryImpl extends ProviderFactory {
protected List<EntityProvider> entityProviders = new
ArrayList<EntityProvider>();
@@ -39,7 +41,9 @@
}
public <T> T createInstance(Class<T> type) {
- //TODO:
+ if (type.isAssignableFrom(Builder.class)) {
+ return type.cast(new BuilderImpl());
+ }
return null;
}
Added:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ResponseImpl.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ResponseImpl.java?rev=574540&view=auto
==============================================================================
---
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ResponseImpl.java
(added)
+++
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ResponseImpl.java
Tue Sep 11 03:12:13 2007
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxrs.provider;
+
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+
+public final class ResponseImpl implements Response {
+ private final int status;
+ private final Object entity;
+
+ ResponseImpl(int s, Object e) {
+ this.status = s;
+ this.entity = e;
+ }
+
+ public Object getEntity() {
+ return entity;
+ }
+
+ public int getStatus() {
+ return status;
+ }
+
+ public void addMetadata(MultivaluedMap<String, Object> metadata) {
+ }
+}
\ No newline at end of file
Propchange:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ResponseImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ResponseImpl.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
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=574540&r1=574539&r2=574540&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
Tue Sep 11 03:12:13 2007
@@ -86,15 +86,24 @@
@UriTemplate("/books/")
public Response updateBook(Book book) {
System.out.println("----invoking updateBook, book name is: " +
book.getName());
+ boolean found = false;
for (int i = 0; i < books.size(); i++) {
Book b = books.get(i);
if (b.getId() == book.getId()) {
books.set(i, book);
+ found = true;
break;
}
}
- return null;
+ Response r;
+ if (found) {
+ r = Response.Builder.ok().build();
+ } else {
+ r = Response.Builder.notModified().build();
+ }
+
+ return r;
}
@@ -103,15 +112,24 @@
public Response deleteBook(@UriParam("bookId") String id) {
System.out.println("----invoking deleteBook with bookId: " + id);
long idNumber = Long.parseLong(id);
+ boolean found = false;
for (int i = 0; i < books.size(); i++) {
Book b = books.get(i);
if (idNumber == b.getId()) {
books.remove(i);
+ found = true;
break;
}
}
- return null;
+ Response r;
+ if (found) {
+ r = Response.Builder.ok().build();
+ } else {
+ r = Response.Builder.notModified().build();
+ }
+
+ return r;
}
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=574540&r1=574539&r2=574540&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
Tue Sep 11 03:12:13 2007
@@ -38,10 +38,10 @@
public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
- @BeforeClass
+/* @BeforeClass
public static void startServers() throws Exception {
assertTrue("server did not launch correctly",
launchServer(BookServer.class));
- }
+ }*/
@Test
public void testGetBooks() throws Exception {
@@ -148,6 +148,31 @@
post.releaseConnection();
}
}
+
+ @Test
+ public void testUpdateBookFailed() throws Exception {
+ String endpointAddress =
+ "http://localhost:9080/xml/bookstore/books";
+
+ String inputFile =
getClass().getResource("resources/update_book_not_exist.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());
+
+ } 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/update_book_not_exist.txt
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_not_exist.txt?rev=574540&view=auto
==============================================================================
---
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_not_exist.txt
(added)
+++
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_not_exist.txt
Tue Sep 11 03:12:13 2007
@@ -0,0 +1 @@
+<Book><id>223</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_not_exist.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_not_exist.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain