| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Object decode(Response response, Type type) throws IOException { |
| 102 | if (response.status() == 404) return Util.emptyValueOf(type); |
| 103 | if (response.body() == null) return null; |
| 104 | while (type instanceof ParameterizedType) { |
| 105 | ParameterizedType ptype = (ParameterizedType) type; |
| 106 | type = ptype.getRawType(); |
| 107 | } |
| 108 | if (!(type instanceof Class)) { |
| 109 | throw new UnsupportedOperationException( |
| 110 | "SOAP only supports decoding raw types. Found " + type); |
| 111 | } |
| 112 | |
| 113 | try { |
| 114 | SOAPMessage message = |
| 115 | MessageFactory.newInstance(soapProtocol) |
| 116 | .createMessage(null, response.body().asInputStream()); |
| 117 | if (message.getSOAPBody() != null) { |
| 118 | if (message.getSOAPBody().hasFault()) { |
| 119 | throw new SOAPFaultException(message.getSOAPBody().getFault()); |
| 120 | } |
| 121 | |
| 122 | Unmarshaller unmarshaller = jaxbContextFactory.createUnmarshaller((Class<?>) type); |
| 123 | |
| 124 | if (this.useFirstChild) { |
| 125 | return unmarshaller.unmarshal(message.getSOAPBody().getFirstChild()); |
| 126 | } else { |
| 127 | return unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument()); |
| 128 | } |
| 129 | } |
| 130 | } catch (SOAPException | JAXBException e) { |
| 131 | throw new DecodeException(response.status(), e.toString(), response.request(), e); |
| 132 | } finally { |
| 133 | if (response.body() != null) { |
| 134 | response.body().close(); |
| 135 | } |
| 136 | } |
| 137 | return Util.emptyValueOf(type); |
| 138 | } |
| 139 | |
| 140 | public static class Builder { |
| 141 | String soapProtocol = SOAPConstants.DEFAULT_SOAP_PROTOCOL; |