Author: sergeyb
Date: Mon Dec 6 17:10:01 2010
New Revision: 1042724
URL: http://svn.apache.org/viewvc?rev=1042724&view=rev
Log:
[CXF-3150] Introducing ServerWebApplicationException and
ClientWebApplicationException
Added:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientWebApplicationException.java
(with props)
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java
(with props)
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/Messages.properties
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java?rev=1042724&r1=1042723&r2=1042724&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
(original)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
Mon Dec 6 17:10:01 2010
@@ -37,7 +37,6 @@ import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Logger;
-import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.HttpHeaders;
@@ -315,7 +314,8 @@ public class AbstractClient implements C
protected ResponseBuilder setResponseBuilder(HttpURLConnection conn,
Exchange exchange) throws Throwable {
Message inMessage = exchange.getInMessage();
if (conn == null) {
- throw new WebApplicationException();
+ // unlikely to occur
+ throw new ClientWebApplicationException("HTTP Connection is
null");
}
Integer responseCode = (Integer)exchange.get(Message.RESPONSE_CODE);
if (responseCode == null) {
@@ -403,11 +403,10 @@ public class AbstractClient implements C
os.flush();
}
} catch (Exception ex) {
- throw new WebApplicationException(ex);
+ reportMessageHandlerProblem("MSG_WRITER_PROBLEM", cls,
contentType, ex, null);
}
-
} else {
- reportNoMessageHandler("NO_MSG_WRITER", cls, contentType);
+ reportMessageHandlerProblem("NO_MSG_WRITER", cls, contentType,
null, null);
}
}
@@ -426,7 +425,7 @@ public class AbstractClient implements C
Object length =
r.getMetadata().getFirst(HttpHeaders.CONTENT_LENGTH);
if (length == null || Integer.parseInt(length.toString()) == 0
|| status >= 400) {
- return cls == Response.class ? r : cls ==
InputStream.class ? inputStream : null;
+ return cls == Response.class ? r : inputStream;
}
}
} catch (IOException ex) {
@@ -446,13 +445,12 @@ public class AbstractClient implements C
return mbr.readFrom(cls, type, anns, contentType,
new MetadataMap<String, Object>(r.getMetadata(), true,
true), inputStream);
} catch (Exception ex) {
- throw new WebApplicationException(ex);
+ reportMessageHandlerProblem("MSG_READER_PROBLEM", cls,
contentType, ex, r);
}
-
} else if (cls == Response.class) {
return r;
} else {
- reportNoMessageHandler("NO_MSG_READER", cls, contentType);
+ reportMessageHandlerProblem("NO_MSG_READER", cls, contentType,
null, null);
}
return null;
}
@@ -497,14 +495,15 @@ public class AbstractClient implements C
}
}
- protected static void reportNoMessageHandler(String name, Class<?> cls,
MediaType ct) {
+ protected static void reportMessageHandlerProblem(String name, Class<?>
cls, MediaType ct,
+ Throwable cause,
Response response) {
org.apache.cxf.common.i18n.Message errorMsg =
new org.apache.cxf.common.i18n.Message(name,
BUNDLE,
cls,
ct.toString());
LOG.severe(errorMsg.toString());
- throw new WebApplicationException(415);
+ throw new ClientWebApplicationException(errorMsg.toString(), cause,
response);
}
private static MediaType getResponseContentType(Response r) {
@@ -523,7 +522,7 @@ public class AbstractClient implements C
connect.setRequestMethod(methodName);
return connect;
} catch (Exception ex) {
- throw new WebApplicationException(ex);
+ throw new
ClientWebApplicationException("REMOTE_CONNECTION_PROBLEM", ex, null);
}
}
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java?rev=1042724&r1=1042723&r2=1042724&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
(original)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
Mon Dec 6 17:10:01 2010
@@ -35,7 +35,6 @@ import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Logger;
-import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
@@ -230,7 +229,7 @@ public class ClientProxyImpl extends Abs
}
if (t == null) {
- t = new WebApplicationException(r);
+ t = new ServerWebApplicationException(r);
}
@@ -475,7 +474,7 @@ public class ClientProxyImpl extends Abs
m.getDeclaringClass().getName(),
m.getName());
LOG.severe(errorMsg.toString());
- throw new WebApplicationException(405);
+ throw new ClientWebApplicationException(errorMsg.toString());
}
// TODO : what we really need to do is to refactor JAXRSOutInterceptor so
that
Added:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientWebApplicationException.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientWebApplicationException.java?rev=1042724&view=auto
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientWebApplicationException.java
(added)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientWebApplicationException.java
Mon Dec 6 17:10:01 2010
@@ -0,0 +1,59 @@
+/**
+ * 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 javax.ws.rs.core.Response;
+
+/**
+ * This exception indicates that the problem has occurred on the client side
only,
+ * possibly as part of processing the successful server response or even before
+ * the request has been sent
+ */
+public class ClientWebApplicationException extends RuntimeException {
+
+ private Response response;
+
+ public ClientWebApplicationException() {
+
+ }
+
+ public ClientWebApplicationException(String message) {
+ super(message);
+ }
+
+ public ClientWebApplicationException(Throwable cause) {
+ super(cause);
+ }
+
+ public ClientWebApplicationException(String message, Throwable cause,
Response response) {
+ super(message, cause);
+ this.response = response;
+ }
+
+ /**
+ * Returns the server response if any, for example, if the problem has
+ * occurred during reading the response then this method will return
+ * JAX-RS Response object representing the actual server response
+ * @return server response, can be null
+ */
+ public Response getResponse() {
+ return response;
+ }
+}
Propchange:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientWebApplicationException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientWebApplicationException.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/Messages.properties
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/Messages.properties?rev=1042724&r1=1042723&r2=1042724&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/Messages.properties
(original)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/Messages.properties
Mon Dec 6 17:10:01 2010
@@ -25,4 +25,7 @@ ONLY_FORM_ALLOWED=Resource method {0}.{1
NO_BODY_IN_SUBRESOURCE=SubResource method {0}.{1} expects request body, only
URI-bound parameters are supported
NO_CONTEXT_PARAMETERS=Resource method {0}.{1} expects JAXRS Context parameter
which is not supported on the client side
NO_MSG_READER =.No message body reader found for class : {0}, ContentType :
{1}.
+MSG_READER_PROBLEM =.Problem with reading the response message, class : {0},
ContentType : {1}.
NO_MSG_WRITER =.No message body writer found for class : {0}.
+MSG_WRITER_PROBLEM =.Problem with writing the request message, class : {0}.
+REMOTE_CONNECTION_PROBLEM=Problem with creating a remote connection
\ No newline at end of file
Added:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java?rev=1042724&view=auto
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java
(added)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java
Mon Dec 6 17:10:01 2010
@@ -0,0 +1,114 @@
+/**
+ * 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.util.Iterator;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+
+/**
+ * Utility Exception class which makes it easier to get to the status,
+ * headers and error message if any
+ */
+public class ServerWebApplicationException extends WebApplicationException {
+
+ private String errorMessage;
+
+ public ServerWebApplicationException() {
+
+ }
+
+ public ServerWebApplicationException(Response response) {
+ super(response);
+ }
+
+ public ServerWebApplicationException(Throwable cause, Response response) {
+ super(cause, response);
+ }
+
+ public int getStatus() {
+ return getResponse().getStatus();
+ }
+
+ public MultivaluedMap<String, String> getHeaders() {
+ MultivaluedMap<String, Object> metadata = getResponse().getMetadata();
+ MultivaluedMap<String, String> headers = new MetadataMap<String,
String>(metadata.size());
+ for (String key : metadata.keySet()) {
+ for (Object strObject : metadata.get(key)) {
+ headers.add(key, strObject.toString());
+ }
+ }
+ return headers;
+ }
+
+ @Override
+ public String getMessage() {
+ if (errorMessage == null) {
+ errorMessage = readErrorMessage();
+ }
+ return errorMessage;
+ }
+
+ private String readErrorMessage() {
+ Object entity = getResponse().getEntity();
+ try {
+ return entity == null ? "" : entity instanceof InputStream
+ ? IOUtils.readStringFromStream((InputStream)entity) :
entity.toString();
+ } catch (IOException ex) {
+ return "";
+ }
+ }
+
+ @Override
+ public String toString() {
+ String lineSep = System.getProperty("line.separator");
+
+ StringBuilder sb = new StringBuilder();
+ sb.append("Status : " + getStatus()).append(lineSep);
+ sb.append("Headers : ").append(lineSep);
+
+ MultivaluedMap<String, String> headers = getHeaders();
+ for (String header : headers.keySet()) {
+ sb.append(header + " :");
+ for (Iterator<String> it = headers.get(header).iterator();
it.hasNext();) {
+ sb.append(' ').append(it.next());
+ if (it.hasNext()) {
+ sb.append(',');
+ }
+ }
+ sb.append(lineSep);
+ }
+
+ String message = getMessage();
+ if (!StringUtils.isEmpty(message)) {
+ sb.append("Error message : ").append(lineSep);
+ sb.append(message).append(lineSep);
+ }
+ return sb.toString();
+ }
+}
Propchange:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java?rev=1042724&r1=1042723&r2=1042724&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
(original)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
Mon Dec 6 17:10:01 2010
@@ -31,7 +31,6 @@ import java.util.Date;
import java.util.List;
import java.util.Map;
-import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.HttpHeaders;
@@ -325,7 +324,7 @@ public class WebClient extends AbstractC
Response r = doInvoke(httpMethod, body, responseClass, responseClass);
if (r.getStatus() >= 400 && responseClass != null) {
- throw new WebApplicationException(r);
+ throw new ServerWebApplicationException(r);
}
return responseClass.cast(r.getEntity());
@@ -344,7 +343,7 @@ public class WebClient extends AbstractC
new ParameterizedCollectionType<T>(memberClass));
if (r.getStatus() >= 400) {
- throw new WebApplicationException(r);
+ throw new ServerWebApplicationException(r);
}
return CastUtils.cast((Collection)r.getEntity(), memberClass);
@@ -625,7 +624,7 @@ public class WebClient extends AbstractC
HttpURLConnection connect =
(HttpURLConnection)m.get(HTTPConduit.KEY_HTTP_CONNECTION);
if (connect == null && primaryError != null) {
/** do we have a pre-connect error ? */
- throw new WebApplicationException(primaryError);
+ throw new ClientWebApplicationException(primaryError);
}
return handleResponse(connect, m, responseClass, genericType);
}
@@ -642,7 +641,7 @@ public class WebClient extends AbstractC
return rb.build();
} catch (Throwable ex) {
- throw new WebApplicationException(ex);
+ throw new ClientWebApplicationException(ex);
}
}
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=1042724&r1=1042723&r2=1042724&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 6 17:10:01 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.ServerWebApplicationException;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.ext.xml.XMLSource;
import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
@@ -168,6 +169,33 @@ public class JAXRSClientServerBookTest e
}
@Test
+ public void testServerWebApplicationException() throws Exception {
+ WebClient wc = WebClient.create("http://localhost:" + PORT +
"/bookstore/webappexception");
+ wc.accept("application/xml");
+ try {
+ wc.get(Book.class);
+ fail("Exception expected");
+ } catch (ServerWebApplicationException ex) {
+ assertEquals(500, ex.getStatus());
+ assertEquals("This is a WebApplicationException", ex.getMessage());
+ assertTrue(ex.toString().contains("This is a
WebApplicationException"));
+ }
+ }
+
+ @Test
+ public void testServerWebApplicationExceptionWithProxy() throws Exception {
+ BookStore store = JAXRSClientFactory.create("http://localhost:" +
PORT, BookStore.class);
+ try {
+ store.throwException();
+ fail("Exception expected");
+ } catch (ServerWebApplicationException ex) {
+ assertEquals(500, ex.getStatus());
+ assertEquals("This is a WebApplicationException", ex.getMessage());
+ assertTrue(ex.toString().contains("This is a
WebApplicationException"));
+ }
+ }
+
+ @Test
public void testWebApplicationException() throws Exception {
getAndCompare("http://localhost:" + PORT +
"/bookstore/webappexception",
"This is a WebApplicationException",