Skip to content

Commit 57883fd

Browse files
author
Nico Verwer
committed
add mail:get-mail-session#2 with authentication
1 parent c8fa495 commit 57883fd

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

extensions/modules/mail/src/main/java/org/exist/xquery/modules/mail/MailModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class MailModule extends AbstractInternalModule {
7171

7272
private final static FunctionDef[] functions = {
7373
new FunctionDef(MailSessionFunctions.signatures[0], MailSessionFunctions.class),
74+
new FunctionDef(MailSessionFunctions.signatures[1], MailSessionFunctions.class),
7475
new FunctionDef(MailStoreFunctions.signatures[0], MailStoreFunctions.class),
7576
new FunctionDef(MailStoreFunctions.signatures[1], MailStoreFunctions.class),
7677
new FunctionDef(MailFolderFunctions.signatures[0], MailFolderFunctions.class),

extensions/modules/mail/src/main/java/org/exist/xquery/modules/mail/MailSessionFunctions.java

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import java.util.Properties;
2626

27+
import jakarta.mail.Authenticator;
28+
import jakarta.mail.PasswordAuthentication;
2729
import jakarta.mail.Session;
2830

2931
import org.apache.logging.log4j.LogManager;
@@ -43,6 +45,8 @@
4345
import org.exist.xquery.value.SequenceType;
4446
import org.exist.xquery.value.Type;
4547

48+
import org.w3c.dom.Element;
49+
4650
/**
4751
* eXist Mail Module Extension GetSession
4852
*
@@ -68,28 +72,60 @@ public class MailSessionFunctions extends BasicFunction
6872
new FunctionParameterSequenceType( "properties", Type.ELEMENT, Cardinality.ZERO_OR_ONE, "An optional JavaMail session properties in the form <properties><property name=\"\" value=\"\"/></properties>. The JavaMail properties are spelled out in Appendix A of the JavaMail specifications." )
6973
},
7074
new FunctionReturnSequenceType( Type.LONG, Cardinality.ZERO_OR_ONE, "an xs:long representing the session handle." )
71-
)
75+
),
76+
77+
new FunctionSignature(
78+
new QName( "get-mail-session", MailModule.NAMESPACE_URI, MailModule.PREFIX ),
79+
"Opens a JavaMail session with authentication.",
80+
new SequenceType[]
81+
{
82+
new FunctionParameterSequenceType( "properties", Type.ELEMENT, Cardinality.ZERO_OR_ONE, "An optional JavaMail session properties in the form <properties><property name=\"\" value=\"\"/></properties>. The JavaMail properties are spelled out in Appendix A of the JavaMail specifications." ),
83+
new FunctionParameterSequenceType( "authentication", Type.ELEMENT, Cardinality.EXACTLY_ONE, "The username and password for authentication in the form <authentication username=\"\" password=\"\"/>." )
84+
},
85+
new FunctionReturnSequenceType( Type.LONG, Cardinality.ZERO_OR_ONE, "an xs:long representing the session handle." )
86+
)
7287
};
7388

7489
public MailSessionFunctions( XQueryContext context, FunctionSignature signature )
7590
{
7691
super( context, signature );
7792
}
7893

79-
@Override
80-
public Sequence eval( Sequence[] args, Sequence contextSequence ) throws XPathException
81-
{
82-
Properties props = new Properties();
83-
84-
if( args.length == 1 ) {
85-
// try and get the session properties
86-
props = ParametersExtractor.parseProperties( ((NodeValue) args[0].itemAt(0)).getNode() );
94+
@Override
95+
public Sequence eval( Sequence[] args, Sequence contextSequence ) throws XPathException
96+
{
97+
Properties props = new Properties();
98+
99+
if( args.length > 0 ) {
100+
// try and get the session properties
101+
props = ParametersExtractor.parseProperties( ((NodeValue) args[0].itemAt(0)).getNode() );
102+
}
103+
104+
Authenticator auth = null;
105+
106+
if( args.length > 1 ) {
107+
// get the authentication parameters
108+
Element authElement = (Element) ((NodeValue) args[1].itemAt(0)).getNode();
109+
if( authElement != null ) {
110+
String username = authElement.getAttribute("username");
111+
String password = authElement.getAttribute("password");
112+
if( username != null && password != null ) {
113+
auth = new Authenticator() {
114+
protected PasswordAuthentication getPasswordAuthentication() {
115+
return new PasswordAuthentication(username, password);
116+
}
117+
};
118+
} else {
119+
throw new IllegalArgumentException("'username' and 'password' attributes are mandatory in the 'authentication' element");
120+
}
121+
} else {
122+
throw new IllegalArgumentException("'authentication' element missing");
123+
}
124+
}
125+
126+
Session session = Session.getInstance( props, auth );
127+
128+
// store the session and return the handle of the session
129+
return new IntegerValue( this, MailModule.storeSession( context, session ), Type.LONG );
87130
}
88-
89-
Session session = Session.getInstance( props, null );
90-
91-
// store the session and return the handle of the session
92-
93-
return new IntegerValue( this, MailModule.storeSession( context, session ) );
94131
}
95-
}

0 commit comments

Comments
 (0)