|
1 | 1 | package com.devonfw.module.cxf.common.impl.client.rest;
|
2 | 2 |
|
| 3 | +import java.lang.reflect.Method; |
3 | 4 | import java.net.URI;
|
| 5 | +import java.util.Map; |
4 | 6 |
|
5 | 7 | import javax.ws.rs.core.MediaType;
|
6 | 8 | import javax.ws.rs.core.Response;
|
7 | 9 | import javax.ws.rs.ext.Provider;
|
8 | 10 |
|
9 | 11 | import org.apache.cxf.jaxrs.client.ResponseExceptionMapper;
|
| 12 | +import org.apache.cxf.jaxrs.impl.ResponseImpl; |
| 13 | +import org.apache.cxf.jaxrs.model.OperationResourceInfo; |
| 14 | +import org.apache.cxf.message.Message; |
| 15 | +import org.apache.cxf.transport.Conduit; |
| 16 | +import org.apache.cxf.ws.addressing.AttributedURIType; |
| 17 | +import org.apache.cxf.ws.addressing.EndpointReferenceType; |
10 | 18 |
|
11 | 19 | import com.devonfw.module.service.common.api.client.ServiceClientErrorFactory;
|
12 | 20 | import com.devonfw.module.service.common.api.client.context.ServiceContext;
|
@@ -45,13 +53,83 @@ public Throwable fromResponse(Response response) {
|
45 | 53 | String data = response.readEntity(String.class);
|
46 | 54 | if ((data != null) && !data.isEmpty()) {
|
47 | 55 | MediaType mediaType = response.getMediaType();
|
48 |
| - URI url = response.getLocation(); |
49 |
| - String operation = null; |
50 |
| - String serviceDetails = this.context.getServiceDescription(operation, url.toString()); |
| 56 | + String url = getUrl(response); |
| 57 | + String operation = getOperation(response); |
| 58 | + String serviceDetails = this.context.getServiceDescription(operation, url); |
51 | 59 | return this.errorUnmarshaller.unmarshall(data, mediaType.toString(), response.getStatus(), serviceDetails);
|
52 | 60 | }
|
53 | 61 | }
|
54 | 62 | return null;
|
55 | 63 | }
|
56 | 64 |
|
| 65 | + private String getOperation(Response response) { |
| 66 | + |
| 67 | + if (response instanceof ResponseImpl) { |
| 68 | + Message message = ((ResponseImpl) response).getOutMessage(); |
| 69 | + if (message != null) { |
| 70 | + Object invocationContext = message.get(Message.INVOCATION_CONTEXT); |
| 71 | + Object requestContext = getFromMap(invocationContext, "RequestContext"); |
| 72 | + OperationResourceInfo operation = getFromMap(requestContext, OperationResourceInfo.class); |
| 73 | + if (operation != null) { |
| 74 | + Method method = operation.getAnnotatedMethod(); |
| 75 | + if (method != null) { |
| 76 | + return method.getName(); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return ""; |
| 82 | + } |
| 83 | + |
| 84 | + @SuppressWarnings("rawtypes") |
| 85 | + private static Object getFromMap(Object map, Object key) { |
| 86 | + |
| 87 | + if (map instanceof Map) { |
| 88 | + return ((Map) map).get(key); |
| 89 | + } |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + @SuppressWarnings("rawtypes") |
| 94 | + private static <T> T getFromMap(Object map, Class<T> key) { |
| 95 | + |
| 96 | + if (map instanceof Map) { |
| 97 | + Object value = ((Map) map).get(key.getName()); |
| 98 | + if (value != null) { |
| 99 | + try { |
| 100 | + return key.cast(value); |
| 101 | + } catch (Exception e) { |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + private String getUrl(Response response) { |
| 109 | + |
| 110 | + URI url = response.getLocation(); |
| 111 | + if (url != null) { |
| 112 | + return url.toString(); |
| 113 | + } else if (response instanceof ResponseImpl) { |
| 114 | + Message message = ((ResponseImpl) response).getOutMessage(); |
| 115 | + if (message != null) { |
| 116 | + Object uri = message.get(Message.REQUEST_URI); |
| 117 | + if (uri instanceof String) { |
| 118 | + return (String) uri; |
| 119 | + } |
| 120 | + Conduit conduit = message.get(Conduit.class); |
| 121 | + if (conduit != null) { |
| 122 | + EndpointReferenceType target = conduit.getTarget(); |
| 123 | + if (target != null) { |
| 124 | + AttributedURIType address = target.getAddress(); |
| 125 | + if (address != null) { |
| 126 | + return address.getValue(); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + return ""; |
| 133 | + } |
| 134 | + |
57 | 135 | }
|
0 commit comments