You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "KARR, DAVID" <dk...@att.com> on 2017/04/11 19:33:43 UTC

How to handle "happy path" form parameters and show fatal html responses?

I now have my little client prototype using ClientBuilder and a MessageBodyReader to unmarshal form parameters into a POJO.  This works fine in normal circumstances.

When something goes really bad, I don't get a form response, I get an HTML response, but I've nicely abstracted my media so much, that I now can't easily get to the plain HTML result.

How can I have both the "happy path", and an error handler that prints out the plain text of the response?

My present method using the client is this:
--------------------
	public StuffResponse validateToken(String token) {
		WebTarget	target	= client.target(getStuffHostPort()).path(getStuffPath());
		Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
		Form  form    = new Form();
		form.param("TokenID", token);
		StuffResponse    response    = null;
		return request.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), StuffResponse.class);
	}
--------------

When I call it with a "manipulated" environment so the service fails with an HTML response, I get the following:
---------------
SEVERE: No message body reader has been found for class com.att.detsusl.stuffrest.StuffResponse, ContentType: text/html
Exception in thread "main" javax.ws.rs.client.ResponseProcessingException: No message body reader has been found for class com.att.detsusl.stuffrest.StuffResponse, ContentType: text/html
	at org.apache.cxf.jaxrs.impl.ResponseImpl.reportMessageHandlerProblem(ResponseImpl.java:439)
	at org.apache.cxf.jaxrs.impl.ResponseImpl.doReadEntity(ResponseImpl.java:390)
	at org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:524)
	at org.apache.cxf.jaxrs.client.WebClient.handleResponse(WebClient.java:1122)
	at org.apache.cxf.jaxrs.client.WebClient.doResponse(WebClient.java:1110)
	at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1035)
	at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:892)
	at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:863)
	at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:426)
	at org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.method(WebClient.java:1562)
	at org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.post(WebClient.java:1502)
	at org.apache.cxf.jaxrs.client.spec.InvocationBuilderImpl.post(InvocationBuilderImpl.java:150)
	at com.att.detsusl.stuffrest.StuffService.validateToken(StuffService.java:105)
----------

The HTML response is not very likely to happen, but when it happens, I'd like to make sure it gets into the log.

I know that if I instead post without qualifying with the type, like this:
----------------
Response  postResponse    = request.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
----------------

Then I can get the text of the response.  That might not be helpful here.

RE: How to handle "happy path" form parameters and show fatal html responses?

Posted by "KARR, DAVID" <dk...@att.com>.
> -----Original Message-----
> From: KARR, DAVID
> Sent: Tuesday, April 11, 2017 12:34 PM
> To: users@cxf.apache.org
> Subject: How to handle "happy path" form parameters and show fatal html
> responses?
> 
> I now have my little client prototype using ClientBuilder and a
> MessageBodyReader to unmarshal form parameters into a POJO.  This works
> fine in normal circumstances.
> 
> When something goes really bad, I don't get a form response, I get an
> HTML response, but I've nicely abstracted my media so much, that I now
> can't easily get to the plain HTML result.
> 
> How can I have both the "happy path", and an error handler that prints
> out the plain text of the response?

Sigh.  Never mind.  Sometimes I think only posting these notes is what makes me realize what the answer is. :)

Just adding a catch clause for "ResponseProcessingException" and calling "ex.getResponse().readEntity(String.class)" gives me what I need.

> 
> My present method using the client is this:
> --------------------
> 	public StuffResponse validateToken(String token) {
> 		WebTarget	target	=
> client.target(getStuffHostPort()).path(getStuffPath());
> 		Builder request =
> target.request(MediaType.APPLICATION_JSON_TYPE);
> 		Form  form    = new Form();
> 		form.param("TokenID", token);
> 		StuffResponse    response    = null;
> 		return request.post(Entity.entity(form,
> MediaType.APPLICATION_FORM_URLENCODED_TYPE), StuffResponse.class);
> 	}
> --------------
> 
> When I call it with a "manipulated" environment so the service fails
> with an HTML response, I get the following:
> ---------------
> SEVERE: No message body reader has been found for class
> com.att.detsusl.stuffrest.StuffResponse, ContentType: text/html
> Exception in thread "main"
> javax.ws.rs.client.ResponseProcessingException: No message body reader
> has been found for class com.att.detsusl.stuffrest.StuffResponse,
> ContentType: text/html
> 	at
> org.apache.cxf.jaxrs.impl.ResponseImpl.reportMessageHandlerProblem(Respo
> nseImpl.java:439)
> 	at
> org.apache.cxf.jaxrs.impl.ResponseImpl.doReadEntity(ResponseImpl.java:39
> 0)
> 	at
> org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:
> 524)
> 	at
> org.apache.cxf.jaxrs.client.WebClient.handleResponse(WebClient.java:1122
> )
> 	at
> org.apache.cxf.jaxrs.client.WebClient.doResponse(WebClient.java:1110)
> 	at
> org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java
> :1035)
> 	at
> org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:892)
> 	at
> org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:863)
> 	at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:426)
> 	at
> org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.method(WebClient.j
> ava:1562)
> 	at
> org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.post(WebClient.jav
> a:1502)
> 	at
> org.apache.cxf.jaxrs.client.spec.InvocationBuilderImpl.post(InvocationBu
> ilderImpl.java:150)
> 	at
> com.att.detsusl.stuffrest.StuffService.validateToken(StuffService.java:1
> 05)
> ----------
> 
> The HTML response is not very likely to happen, but when it happens, I'd
> like to make sure it gets into the log.
> 
> I know that if I instead post without qualifying with the type, like
> this:
> ----------------
> Response  postResponse    = request.post(Entity.entity(form,
> MediaType.APPLICATION_FORM_URLENCODED_TYPE));
> ----------------
> 
> Then I can get the text of the response.  That might not be helpful
> here.