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