|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.ws.dsl; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.springframework.expression.Expression; |
| 23 | +import org.springframework.integration.dsl.MessageHandlerSpec; |
| 24 | +import org.springframework.integration.util.JavaUtils; |
| 25 | +import org.springframework.integration.ws.AbstractWebServiceOutboundGateway; |
| 26 | +import org.springframework.integration.ws.SoapHeaderMapper; |
| 27 | +import org.springframework.ws.WebServiceMessageFactory; |
| 28 | +import org.springframework.ws.client.core.FaultMessageResolver; |
| 29 | +import org.springframework.ws.client.core.WebServiceMessageCallback; |
| 30 | +import org.springframework.ws.client.core.WebServiceTemplate; |
| 31 | +import org.springframework.ws.client.support.destination.DestinationProvider; |
| 32 | +import org.springframework.ws.client.support.interceptor.ClientInterceptor; |
| 33 | +import org.springframework.ws.transport.WebServiceMessageSender; |
| 34 | + |
| 35 | +/** |
| 36 | + * The base {@link MessageHandlerSpec} for {@link AbstractWebServiceOutboundGateway}s. |
| 37 | + * |
| 38 | + * @param <S> the target {@link BaseWsOutboundGatewaySpec} implementation type. |
| 39 | + * @param <E> the target {@link AbstractWebServiceOutboundGateway} implementation type. |
| 40 | + * |
| 41 | + * @author Gary Russell |
| 42 | + * @since 5.3 |
| 43 | + * |
| 44 | + */ |
| 45 | +public abstract class BaseWsOutboundGatewaySpec< |
| 46 | + S extends BaseWsOutboundGatewaySpec<S, E>, E extends AbstractWebServiceOutboundGateway> |
| 47 | + extends MessageHandlerSpec<S, E> { |
| 48 | + |
| 49 | + private final Map<String, Expression> uriVariableExpressions = new HashMap<>(); |
| 50 | + |
| 51 | + protected WebServiceTemplate template; // NOSONAR |
| 52 | + |
| 53 | + protected DestinationProvider destinationProvider; // NOSONAR |
| 54 | + |
| 55 | + protected String uri; // NOSONAR |
| 56 | + |
| 57 | + protected WebServiceMessageFactory webServiceMessageFactory; // NOSONAR |
| 58 | + |
| 59 | + private SoapHeaderMapper headerMapper; |
| 60 | + |
| 61 | + private boolean encodeUri = true; |
| 62 | + |
| 63 | + private boolean ignoreEmptyResponses = true; |
| 64 | + |
| 65 | + private WebServiceMessageCallback requestCallback; |
| 66 | + |
| 67 | + protected FaultMessageResolver faultMessageResolver; // NOSONAR |
| 68 | + |
| 69 | + protected WebServiceMessageSender[] messageSenders; // NOSONAR |
| 70 | + |
| 71 | + protected ClientInterceptor[] gatewayInterceptors; // NOSONAR |
| 72 | + |
| 73 | + protected boolean extractPayload = true; // NOSONAR |
| 74 | + |
| 75 | + /** |
| 76 | + * Configure with a destination provider; |
| 77 | + * @param destinationProvider the destination provider. |
| 78 | + * @return the spec. |
| 79 | + */ |
| 80 | + public S destinationProvider(DestinationProvider destinationProvider) { |
| 81 | + this.destinationProvider = destinationProvider; |
| 82 | + return _this(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Configure with a URI. |
| 87 | + * @param uri the uri. |
| 88 | + * @return the spec. |
| 89 | + */ |
| 90 | + public S uri(String uri) { |
| 91 | + this.uri = uri; |
| 92 | + return _this(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Configure the header mapper. |
| 97 | + * @param headerMapper the mapper. |
| 98 | + * @return the spec. |
| 99 | + */ |
| 100 | + public S headerMapper(SoapHeaderMapper headerMapper) { |
| 101 | + this.headerMapper = headerMapper; |
| 102 | + return _this(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Set the Map of URI variable expressions to evaluate against the outbound message |
| 107 | + * when replacing the variable placeholders in a URI template. |
| 108 | + * @param uriVariableExpressions The URI variable expressions. |
| 109 | + * @return the spec. |
| 110 | + */ |
| 111 | + public S uriVariableExpressions(Map<String, Expression> uriVariableExpressions) { |
| 112 | + this.uriVariableExpressions.putAll(uriVariableExpressions); |
| 113 | + return _this(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Specify whether the URI should be encoded after any <code>uriVariables</code> |
| 118 | + * are expanded and before sending the request. The default value is <code>true</code>. |
| 119 | + * @param encodeUri true if the URI should be encoded. |
| 120 | + * @return the spec. |
| 121 | + * @see org.springframework.web.util.UriComponentsBuilder |
| 122 | + */ |
| 123 | + public S encodeUri(boolean encodeUri) { |
| 124 | + this.encodeUri = encodeUri; |
| 125 | + return _this(); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Specify whether empty String response payloads should be ignored. |
| 130 | + * The default is <code>true</code>. Set this to <code>false</code> if |
| 131 | + * you want to send empty String responses in reply Messages. |
| 132 | + * @param ignoreEmptyResponses true if empty responses should be ignored. |
| 133 | + * @return the spec. |
| 134 | + */ |
| 135 | + public S ignoreEmptyResponses(boolean ignoreEmptyResponses) { |
| 136 | + this.ignoreEmptyResponses = ignoreEmptyResponses; |
| 137 | + return _this(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Specify the {@link WebServiceMessageCallback} to use. |
| 142 | + * @param requestCallback the call back. |
| 143 | + * @return the spec. |
| 144 | + */ |
| 145 | + public S requestCallback(WebServiceMessageCallback requestCallback) { |
| 146 | + this.requestCallback = requestCallback; |
| 147 | + return _this(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + protected E doGet() { |
| 152 | + return assemble(create()); |
| 153 | + } |
| 154 | + |
| 155 | + protected abstract E create(); |
| 156 | + |
| 157 | + protected E assemble(E gateway) { |
| 158 | + gateway.setUriVariableExpressions(this.uriVariableExpressions); |
| 159 | + JavaUtils.INSTANCE |
| 160 | + .acceptIfNotNull(this.headerMapper, gateway::setHeaderMapper); |
| 161 | + gateway.setEncodeUri(this.encodeUri); |
| 162 | + gateway.setIgnoreEmptyResponses(this.ignoreEmptyResponses); |
| 163 | + gateway.setRequestCallback(this.requestCallback); |
| 164 | + return gateway; |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments