Author: sergeyb
Date: Mon Dec 20 14:13:06 2010
New Revision: 1051115
URL: http://svn.apache.org/viewvc?rev=1051115&view=rev
Log:
[CXF-3207] Adding Response MessageBodyReader
Added:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
(with props)
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/ResponseReaderTest.java
(with props)
Modified:
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Added:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java?rev=1051115&view=auto
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
(added)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
Mon Dec 20 14:13:06 2010
@@ -0,0 +1,105 @@
+/**
+ * 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.client;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.List;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.Providers;
+
+import org.apache.cxf.jaxrs.ext.MessageContext;
+import org.apache.cxf.message.Message;
+
+public class ResponseReader implements MessageBodyReader<Response> {
+
+ @Context
+ private MessageContext context;
+
+ private Class<?> entityCls;
+ private Type entityGenericType;
+
+ public ResponseReader() {
+
+ }
+
+ public ResponseReader(Class<?> entityCls) {
+ this.entityCls = entityCls;
+ }
+
+ public boolean isReadable(Class<?> cls, Type genericType, Annotation[]
anns, MediaType mt) {
+ return cls.isAssignableFrom(Response.class);
+ }
+
+ @SuppressWarnings("unchecked")
+ public Response readFrom(Class<Response> cls, Type genericType,
Annotation[] anns, MediaType mt,
+ MultivaluedMap<String, String> headers, InputStream is)
+ throws IOException, WebApplicationException {
+
+ int status =
Integer.valueOf(getContext().get(Message.RESPONSE_CODE).toString());
+
+ ResponseBuilder rb = Response.status(status);
+
+ for (String header : headers.keySet()) {
+ List<String> values = headers.get(header);
+ for (String value : values) {
+ rb.header(header, value);
+ }
+ }
+
+ if (entityCls != null) {
+ Providers providers = getContext().getProviders();
+ MessageBodyReader<?> reader =
+ providers.getMessageBodyReader(entityCls,
getEntityGenericType(), anns, mt);
+ if (reader == null) {
+ throw new ClientWebApplicationException("No reader for
Response entity "
+ + entityCls.getName());
+ }
+
+ Object entity = reader.readFrom((Class)entityCls,
getEntityGenericType(),
+ anns, mt, headers, is);
+ rb.entity(entity);
+ }
+
+
+ return rb.build();
+ }
+
+ public void setEntityClass(Class<?> cls) {
+ entityCls = cls;
+ }
+
+ private Type getEntityGenericType() {
+ return entityGenericType == null ? entityCls : entityGenericType;
+ }
+
+ protected MessageContext getContext() {
+ return context;
+ }
+}
Propchange:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/ResponseReaderTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/ResponseReaderTest.java?rev=1051115&view=auto
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/ResponseReaderTest.java
(added)
+++
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/ResponseReaderTest.java
Mon Dec 20 14:13:06 2010
@@ -0,0 +1,92 @@
+/**
+ * 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.client;
+
+import java.io.ByteArrayInputStream;
+import java.lang.annotation.Annotation;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.MessageBodyReader;
+
+import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.jaxrs.ext.MessageContext;
+import org.apache.cxf.jaxrs.ext.MessageContextImpl;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
+import org.apache.cxf.jaxrs.resources.Book;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+import org.easymock.EasyMock;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ResponseReaderTest extends Assert {
+
+ @Test
+ public void testResponseReader() throws Exception {
+ String data = "<Book><id>123</id><name>CXF in Action</name></Book>";
+ MultivaluedMap<String, String> headers = new MetadataMap<String,
String>();
+ headers.add("a", "a1");
+ headers.add("a", "a2");
+ headers.add("b", "b1");
+
+ final Message m = new MessageImpl();
+ Exchange exc = new ExchangeImpl();
+ exc.setInMessage(m);
+
+ ProviderFactory instance = ProviderFactory.getInstance();
+
+ Endpoint endpoint = EasyMock.createMock(Endpoint.class);
+ endpoint.get(ProviderFactory.class.getName());
+ EasyMock.expectLastCall().andReturn(instance).anyTimes();
+ EasyMock.replay(endpoint);
+
+ exc.put(Endpoint.class, endpoint);
+
+ m.setExchange(exc);
+ m.put(Message.RESPONSE_CODE, "200");
+
+ MessageBodyReader<Response> reader = new ResponseReader(Book.class) {
+ protected MessageContext getContext() {
+ return new MessageContextImpl(m);
+ }
+ };
+ ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
+ Response r = reader.readFrom(Response.class, Response.class,
+ new Annotation[0], MediaType.TEXT_XML_TYPE, headers, is);
+
+ assertNotNull(r);
+ assertEquals(200, r.getStatus());
+ Book b = (Book)r.getEntity();
+
+ assertEquals(123, b.getId());
+ assertEquals("CXF in Action", b.getName());
+
+ MultivaluedMap<String, Object> respHeaders = r.getMetadata();
+ assertNotSame(headers, respHeaders);
+ assertEquals(headers, respHeaders);
+ }
+
+}
Propchange:
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/ResponseReaderTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/ResponseReaderTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1051115&r1=1051114&r2=1051115&view=diff
==============================================================================
---
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Mon Dec 20 14:13:06 2010
@@ -45,6 +45,7 @@ import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
+import org.apache.cxf.jaxrs.client.ResponseReader;
import org.apache.cxf.jaxrs.client.ServerWebApplicationException;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.ext.xml.XMLSource;
@@ -560,6 +561,20 @@ public class JAXRSClientServerBookTest e
}
@Test
+ public void testGetBookFromResponseWithProxy() throws Exception {
+ ResponseReader reader = new ResponseReader();
+ reader.setEntityClass(Book.class);
+
+ BookStore bs = JAXRSClientFactory.create("http://localhost:" + PORT,
+ BookStore.class,
+
Collections.singletonList(reader));
+ Response r = bs.getGenericResponseBook("123");
+ assertEquals(200, r.getStatus());
+ Book book = (Book)r.getEntity();
+ assertEquals(123L, book.getId());
+ }
+
+ @Test
public void testUpdateWithProxy() throws Exception {
BookStore bs = JAXRSClientFactory.create("http://localhost:" + PORT,
BookStore.class);
Book book = new Book();