|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 The Feign Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package feign.soap; |
| 15 | + |
| 16 | +import feign.Response; |
| 17 | +import feign.Util; |
| 18 | +import feign.codec.DecodeException; |
| 19 | +import feign.codec.Decoder; |
| 20 | +import feign.jaxb.JAXBContextFactory; |
| 21 | +import jakarta.xml.bind.JAXBException; |
| 22 | +import jakarta.xml.bind.Unmarshaller; |
| 23 | +import jakarta.xml.soap.*; |
| 24 | +import jakarta.xml.ws.soap.SOAPFaultException; |
| 25 | +import java.io.IOException; |
| 26 | +import java.lang.reflect.ParameterizedType; |
| 27 | +import java.lang.reflect.Type; |
| 28 | + |
| 29 | +/** |
| 30 | + * Decodes SOAP responses using SOAPMessage and JAXB for the body part. <br> |
| 31 | + * |
| 32 | + * <p> |
| 33 | + * The JAXBContextFactory should be reused across requests as it caches the created JAXB contexts. |
| 34 | + * |
| 35 | + * <p> |
| 36 | + * A SOAP Fault can be returned with a 200 HTTP code. Hence, faults could be handled with no error |
| 37 | + * on the HTTP layer. In this case, you'll certainly have to catch {@link SOAPFaultException} to get |
| 38 | + * fault from your API client service. In the other case (Faults are returned with 4xx or 5xx HTTP |
| 39 | + * error code), you may use {@link SOAPErrorDecoder} in your API configuration. |
| 40 | + * |
| 41 | + * <pre> |
| 42 | + * |
| 43 | + * public interface MyApi { |
| 44 | + * |
| 45 | + * @RequestLine("POST /getObject") |
| 46 | + * @Headers({ |
| 47 | + * "SOAPAction: getObject", |
| 48 | + * "Content-Type: text/xml" |
| 49 | + * }) |
| 50 | + * MyJaxbObjectResponse getObject(MyJaxbObjectRequest request); |
| 51 | + * |
| 52 | + * } |
| 53 | + * |
| 54 | + * ... |
| 55 | + * |
| 56 | + * JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder() |
| 57 | + * .withMarshallerJAXBEncoding("UTF-8") |
| 58 | + * .withMarshallerSchemaLocation("http://apihost http://apihost/schema.xsd") |
| 59 | + * .build(); |
| 60 | + * |
| 61 | + * api = Feign.builder() |
| 62 | + * .decoder(new SOAPDecoder(jaxbFactory)) |
| 63 | + * .target(MyApi.class, "http://api"); |
| 64 | + * |
| 65 | + * ... |
| 66 | + * |
| 67 | + * try { |
| 68 | + * api.getObject(new MyJaxbObjectRequest()); |
| 69 | + * } catch (SOAPFaultException faultException) { |
| 70 | + * log.info(faultException.getFault().getFaultString()); |
| 71 | + * } |
| 72 | + * </pre> |
| 73 | + * |
| 74 | + * @see SOAPErrorDecoder |
| 75 | + * @see SOAPFaultException |
| 76 | + */ |
| 77 | +public class SOAPDecoder implements Decoder { |
| 78 | + |
| 79 | + private final JAXBContextFactory jaxbContextFactory; |
| 80 | + private final String soapProtocol; |
| 81 | + private final boolean useFirstChild; |
| 82 | + |
| 83 | + public SOAPDecoder(JAXBContextFactory jaxbContextFactory) { |
| 84 | + this.jaxbContextFactory = jaxbContextFactory; |
| 85 | + this.soapProtocol = SOAPConstants.DEFAULT_SOAP_PROTOCOL; |
| 86 | + this.useFirstChild = false; |
| 87 | + } |
| 88 | + |
| 89 | + private SOAPDecoder(Builder builder) { |
| 90 | + this.soapProtocol = builder.soapProtocol; |
| 91 | + this.jaxbContextFactory = builder.jaxbContextFactory; |
| 92 | + this.useFirstChild = builder.useFirstChild; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Object decode(Response response, Type type) throws IOException { |
| 97 | + if (response.status() == 404) |
| 98 | + return Util.emptyValueOf(type); |
| 99 | + if (response.body() == null) |
| 100 | + return null; |
| 101 | + while (type instanceof ParameterizedType) { |
| 102 | + ParameterizedType ptype = (ParameterizedType) type; |
| 103 | + type = ptype.getRawType(); |
| 104 | + } |
| 105 | + if (!(type instanceof Class)) { |
| 106 | + throw new UnsupportedOperationException( |
| 107 | + "SOAP only supports decoding raw types. Found " + type); |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + SOAPMessage message = |
| 112 | + MessageFactory.newInstance(soapProtocol) |
| 113 | + .createMessage(null, response.body().asInputStream()); |
| 114 | + if (message.getSOAPBody() != null) { |
| 115 | + if (message.getSOAPBody().hasFault()) { |
| 116 | + throw new SOAPFaultException(message.getSOAPBody().getFault()); |
| 117 | + } |
| 118 | + |
| 119 | + Unmarshaller unmarshaller = jaxbContextFactory.createUnmarshaller((Class<?>) type); |
| 120 | + |
| 121 | + if (this.useFirstChild) { |
| 122 | + return unmarshaller.unmarshal(message.getSOAPBody().getFirstChild()); |
| 123 | + } else { |
| 124 | + return unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument()); |
| 125 | + } |
| 126 | + } |
| 127 | + } catch (SOAPException | JAXBException e) { |
| 128 | + throw new DecodeException(response.status(), e.toString(), response.request(), e); |
| 129 | + } finally { |
| 130 | + if (response.body() != null) { |
| 131 | + response.body().close(); |
| 132 | + } |
| 133 | + } |
| 134 | + return Util.emptyValueOf(type); |
| 135 | + } |
| 136 | + |
| 137 | + public static class Builder { |
| 138 | + String soapProtocol = SOAPConstants.DEFAULT_SOAP_PROTOCOL; |
| 139 | + JAXBContextFactory jaxbContextFactory; |
| 140 | + boolean useFirstChild = false; |
| 141 | + |
| 142 | + public Builder withJAXBContextFactory(JAXBContextFactory jaxbContextFactory) { |
| 143 | + this.jaxbContextFactory = jaxbContextFactory; |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * The protocol used to create message factory. Default is "SOAP 1.1 Protocol". |
| 149 | + * |
| 150 | + * @param soapProtocol a string constant representing the MessageFactory protocol. |
| 151 | + * @see SOAPConstants#SOAP_1_1_PROTOCOL |
| 152 | + * @see SOAPConstants#SOAP_1_2_PROTOCOL |
| 153 | + * @see SOAPConstants#DYNAMIC_SOAP_PROTOCOL |
| 154 | + * @see MessageFactory#newInstance(String) |
| 155 | + */ |
| 156 | + public Builder withSOAPProtocol(String soapProtocol) { |
| 157 | + this.soapProtocol = soapProtocol; |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Alters the behavior of the code to use the {@link SOAPBody#getFirstChild()} in place of |
| 163 | + * {@link SOAPBody#extractContentAsDocument()}. |
| 164 | + * |
| 165 | + * @return the builder instance. |
| 166 | + */ |
| 167 | + public Builder useFirstChild() { |
| 168 | + this.useFirstChild = true; |
| 169 | + return this; |
| 170 | + } |
| 171 | + |
| 172 | + public SOAPDecoder build() { |
| 173 | + if (jaxbContextFactory == null) { |
| 174 | + throw new IllegalStateException("JAXBContextFactory must be non-null"); |
| 175 | + } |
| 176 | + return new SOAPDecoder(this); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments