Expose SOAP WS
#9662
Replies: 1 comment 1 reply
-
We have created our own poor man's soap handling for Micronaut 4.x: import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.core.type.Argument;
import io.micronaut.core.type.Headers;
import io.micronaut.core.type.MutableHeaders;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.body.MessageBodyHandler;
import io.micronaut.http.codec.CodecException;
import jakarta.inject.Singleton;
import jakarta.xml.soap.MessageFactory;
import jakarta.xml.soap.MimeHeaders;
import jakarta.xml.soap.SOAPException;
import jakarta.xml.soap.SOAPMessage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Body handler for SOAP.
*
* @param <T> The type to read/write
*/
@Singleton
@Produces({MediaType.TEXT_XML})
@Consumes({MediaType.TEXT_XML})
@BootstrapContextCompatible
public final class SOAPMessageHandler<T> implements MessageBodyHandler<T> {
private static final Logger LOGGER = LoggerFactory.getLogger(SOAPMessageHandler.class);
private final MessageFactory factory;
/**
* Create a new soap message handler.
*
* @param factory the message factory
*/
public SOAPMessageHandler(MessageFactory factory) {
this.factory = factory; // MessageFactory.newInstance()
}
@Override
public boolean isReadable(Argument<T> type, MediaType mediaType) {
return isWriteable(type, mediaType);
}
@SuppressWarnings("unchecked")
@Override
public T read(Argument<T> type, MediaType mediaType, Headers httpHeaders, InputStream inputStream)
throws CodecException {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(String.format("decode type %s", type));
}
T decoded;
if (type.getType().isAssignableFrom(SOAPMessage.class)) {
try {
decoded = (T) factory.createMessage(new MimeHeaders(), inputStream);
} catch (SOAPException | IOException e) {
throw new CodecException(
"Error decoding XML stream for type [" + type.getName() + "]: " + e.getMessage(), e);
}
} else {
throw new UnsupportedOperationException("Codec only supports decoding SOAPMessage objects");
}
return decoded;
}
@Override
public boolean isWriteable(Argument<T> type, MediaType mediaType) {
return mediaType != null
&& mediaType.getExtension().equals(MediaType.EXTENSION_XML)
&& type != null
&& SOAPMessage.class.isAssignableFrom(type.getType());
}
@Override
public void writeTo(
Argument<T> type,
MediaType mediaType,
T object,
MutableHeaders outgoingHeaders,
OutputStream outputStream)
throws CodecException {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(String.format("encode object %s", object));
}
outgoingHeaders.set(
HttpHeaders.CONTENT_TYPE, mediaType != null ? mediaType : MediaType.TEXT_XML);
if (object instanceof SOAPMessage message) {
try {
message.writeTo(outputStream);
} catch (SOAPException | IOException e) {
throw new CodecException(
"Error encoding object [" + object + "] to XML: " + e.getMessage(), e);
}
} else {
throw new UnsupportedOperationException("Codec only supports encoding SOAPMessage objects");
}
}
} This allows us using |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
is it possible to expose a soap ws in a micronaut app ?
Thks
Beta Was this translation helpful? Give feedback.
All reactions