Skip to content

GH-3421: Resolve throws Exception; in the API #3916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,16 +39,14 @@
* @author Gary Russell
* @author Artem Bilan
*/
public abstract class AbstractMailMessageTransformer<T> implements Transformer,
BeanFactoryAware {
public abstract class AbstractMailMessageTransformer<T> implements Transformer, BeanFactoryAware {

private BeanFactory beanFactory;

private MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory();

private boolean messageBuilderFactorySet;


@Override
public final void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
Expand All @@ -67,28 +65,19 @@ protected MessageBuilderFactory getMessageBuilderFactory() {
@Override
public Message<?> transform(Message<?> message) {
Object payload = message.getPayload();
if (!(payload instanceof jakarta.mail.Message)) {
if (!(payload instanceof jakarta.mail.Message mailMessage)) {
throw new MessageTransformationException(message, getClass().getSimpleName()
+ " requires a jakarta.mail.Message payload");
}
jakarta.mail.Message mailMessage = (jakarta.mail.Message) payload;
AbstractIntegrationMessageBuilder<T> builder;
try {
builder = this.doTransform(mailMessage);
}
catch (Exception e) {
throw new MessageTransformationException(message, "failed to transform mail message", e);
}
builder = doTransform(mailMessage);
if (builder == null) {
throw new MessageTransformationException(message, "failed to transform mail message");
}
builder.copyHeaders(extractHeaderMapFromMailMessage(mailMessage));
return builder.build();
return builder.copyHeaders(extractHeaderMapFromMailMessage(mailMessage)).build();
}

protected abstract AbstractIntegrationMessageBuilder<T> doTransform(jakarta.mail.Message mailMessage)
throws Exception; // NOSONAR

protected abstract AbstractIntegrationMessageBuilder<T> doTransform(jakarta.mail.Message mailMessage);

private static Map<String, Object> extractHeaderMapFromMailMessage(jakarta.mail.Message mailMessage) {
return MailUtils.extractStandardHeaders(mailMessage);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import jakarta.mail.Part;

import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
import org.springframework.integration.transformer.MessageTransformationException;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -51,27 +52,34 @@ public void setCharset(String charset) {
}

@Override
protected AbstractIntegrationMessageBuilder<String> doTransform(jakarta.mail.Message mailMessage)
throws Exception { // NOSONAR
protected AbstractIntegrationMessageBuilder<String> doTransform(jakarta.mail.Message mailMessage) {
try {
String payload;
Object content = mailMessage.getContent();
if (content instanceof String value) {
payload = value;
}
else if (content instanceof Multipart multipart) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
multipart.writeTo(outputStream);
payload = outputStream.toString(this.charset);
}
else if (content instanceof Part part) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
part.writeTo(outputStream);
payload = outputStream.toString(this.charset);
}
else {
throw new IllegalArgumentException("failed to transform contentType ["
+ mailMessage.getContentType() + "] to String.");
}

Object content = mailMessage.getContent();
if (content instanceof String) {
return this.getMessageBuilderFactory().withPayload((String) content);
return getMessageBuilderFactory().withPayload(payload);
}
if (content instanceof Multipart) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
((Multipart) content).writeTo(outputStream);
return this.getMessageBuilderFactory().withPayload(
new String(outputStream.toByteArray(), this.charset));
catch (Exception ex) {
throw new MessageTransformationException("Cannot transform mail message", ex);
}
else if (content instanceof Part) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
((Part) content).writeTo(outputStream);
return this.getMessageBuilderFactory().withPayload(
new String(outputStream.toByteArray(), this.charset));
}
throw new IllegalArgumentException("failed to transform contentType ["
+ mailMessage.getContentType() + "] to String.");

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,25 +42,20 @@ public interface WebSocketListener extends SubProtocolCapable {
* Handle the received {@link WebSocketMessage}.
* @param session the WebSocket session
* @param message the WebSocket message
* @throws Exception the 'onMessage' Exception
*/
void onMessage(WebSocketSession session, WebSocketMessage<?> message)
throws Exception; // NOSONAR Remove in 5.2
void onMessage(WebSocketSession session, WebSocketMessage<?> message);

/**
* Invoked after a {@link WebSocketSession} has started.
* @param session the WebSocket session
* @throws Exception the 'afterSessionStarted' Exception
*/
void afterSessionStarted(WebSocketSession session) throws Exception; // NOSONAR Remove in 5.2
void afterSessionStarted(WebSocketSession session);

/**
* Invoked after a {@link WebSocketSession} has ended.
* @param session the WebSocket session
* @param closeStatus the reason why the session was closed
* @throws Exception the 'afterSessionEnded' Exception
*/
void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus)
throws Exception; // NOSONAR Remove in 5.2
void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -224,39 +224,54 @@ public List<String> getSubProtocols() {
}

@Override
public void afterSessionStarted(WebSocketSession session) throws Exception { // NOSONAR Thrown from the delegate
public void afterSessionStarted(WebSocketSession session) {
if (isActive()) {
SubProtocolHandler protocolHandler = this.subProtocolHandlerRegistry.findProtocolHandler(session);
protocolHandler.afterSessionStarted(session, this.subProtocolHandlerChannel);
if (!this.server && protocolHandler instanceof StompSubProtocolHandler) {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
accessor.setSessionId(session.getId());
accessor.setLeaveMutable(true);
accessor.setAcceptVersion("1.1,1.2");

Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
protocolHandler.handleMessageToClient(session, connectMessage);
try {
SubProtocolHandler protocolHandler = this.subProtocolHandlerRegistry.findProtocolHandler(session);
protocolHandler.afterSessionStarted(session, this.subProtocolHandlerChannel);
if (!this.server && protocolHandler instanceof StompSubProtocolHandler) {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
accessor.setSessionId(session.getId());
accessor.setLeaveMutable(true);
accessor.setAcceptVersion("1.1,1.2");

Message<?> connectMessage =
MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
protocolHandler.handleMessageToClient(session, connectMessage);
}
}
catch (Exception ex) {
logger.error(ex, () -> "WebSocketHandler.afterConnectionEstablished threw exception in " + this);
}
}
}

@Override
public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus)
throws Exception { // NOSONAR Thrown from the delegate
public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus) {

if (isActive()) {
this.subProtocolHandlerRegistry.findProtocolHandler(session)
.afterSessionEnded(session, closeStatus, this.subProtocolHandlerChannel);
try {
this.subProtocolHandlerRegistry.findProtocolHandler(session)
.afterSessionEnded(session, closeStatus, this.subProtocolHandlerChannel);
}
catch (Exception ex) {
logger.warn(ex, () -> "Unhandled exception after connection closed for " + this);
}
}
}

@Override
public void onMessage(WebSocketSession session, WebSocketMessage<?> webSocketMessage)
throws Exception { // NOSONAR Thrown from the delegate

public void onMessage(WebSocketSession session, WebSocketMessage<?> webSocketMessage) {
if (isActive()) {
this.subProtocolHandlerRegistry.findProtocolHandler(session)
.handleMessageFromClient(session, webSocketMessage, this.subProtocolHandlerChannel);
try {
this.subProtocolHandlerRegistry.findProtocolHandler(session)
.handleMessageFromClient(session, webSocketMessage, this.subProtocolHandlerChannel);
}
catch (Exception ex) {
logger.error(ex,
() -> "SubProtocolHandler.handleMessageFromClient threw an exception on message: " +
webSocketMessage + " in " + this);
}
}
}

Expand Down