Skip to content

Commit

Permalink
[MPIR-401] Mailing list subscribe and unsubscribe links
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Apr 25, 2021
1 parent 2e187d0 commit 98db5e5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.util.StringUtils;

import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -169,7 +170,7 @@ public void renderBody()

if ( StringUtils.isNotEmpty( mailingList.getSubscribe() ) )
{
textRow.add( createEmailLinkPatternedText( subscribe, mailingList.getSubscribe(), null ) );
textRow.add( createURILinkPatternedText( subscribe, mailingList.getSubscribe(), null ) );
}
else
{
Expand All @@ -178,7 +179,7 @@ public void renderBody()

if ( StringUtils.isNotEmpty( mailingList.getUnsubscribe() ) )
{
textRow.add( createEmailLinkPatternedText( unsubscribe, mailingList.getUnsubscribe(), null ) );
textRow.add( createURILinkPatternedText( unsubscribe, mailingList.getUnsubscribe(), null ) );
}
else
{
Expand All @@ -187,7 +188,7 @@ public void renderBody()

if ( StringUtils.isNotEmpty( mailingList.getPost() ) )
{
textRow.add( createEmailLinkPatternedText( post, mailingList.getPost(), null ) );
textRow.add( createURILinkPatternedText( post, mailingList.getPost(), null ) );
}
else
{
Expand Down Expand Up @@ -262,23 +263,31 @@ public void renderBody()
}

/**
* Create a link pattern text for email addresses defined by <code>{text, mailto:href}</code>. If href is null,
* then <code>defaultHref</code> is used instead.
* Create a URI link pattern text for a mailing list. If no scheme is provided {@code mailto:}
* will be prepended by default. If href is null, then <code>defaultHref</code> is used instead.
*
* @param text a text.
* @param href the email address to use.
* @param href the potential URI to use.
* @param defaultHref the String to use in case href is null.
* @return an email link pattern.
* @return a link pattern.
* @see #createLinkPatternedText(String,String)
*/
private String createEmailLinkPatternedText( String text, String href, String defaultHref )
private String createURILinkPatternedText( String text, String href, String defaultHref )
{
if ( href == null || href.isEmpty() )
{
return createLinkPatternedText( text, defaultHref );
}
return createLinkPatternedText( text,
href.toLowerCase( Locale.ENGLISH ).startsWith( "mailto:" ) ? href : "mailto:" + href );

URI hrefUri = URI.create( href );
if ( StringUtils.isNotEmpty( hrefUri.getScheme() ) )
{
return createLinkPatternedText( text, href );
}
else
{
return createLinkPatternedText( text, "mailto:" + href );
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.TextBlock;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

Expand Down Expand Up @@ -73,13 +74,19 @@ public void testReport()
assertEquals( getString( "report.mailing-lists.title" ), textBlocks[0].getText() );
assertEquals( getString( "report.mailing-lists.intro" ), textBlocks[1].getText() );

// MPIR-385: Test emails starts with 'mailto:'
// MPIR-385 + MPIR-401: Test links are URIs otherwise assume a plain email address
String post = getString("report.mailing-lists.column.post");
assertEquals( "mailto:[email protected]", response.getLinkWith( post ).getAttribute( "href" ) );
WebLink[] postLinks = response.getMatchingLinks( WebLink.MATCH_CONTAINED_TEXT, post );
assertEquals( "mailto:[email protected]", postLinks[0].getAttribute( "href" ) );
assertEquals( "mailto:[email protected]", postLinks[1].getAttribute( "href" ) );
String subscribe = getString("report.mailing-lists.column.subscribe");
assertEquals( "MAILTO:[email protected]", response.getLinkWith( subscribe ).getAttribute( "href" ) );
WebLink[] subscribeLinks = response.getMatchingLinks( WebLink.MATCH_CONTAINED_TEXT, subscribe );
assertEquals( "MAILTO:[email protected]", subscribeLinks[0].getAttribute( "href" ) );
assertEquals( "MAILTO:[email protected]", subscribeLinks[1].getAttribute( "href" ) );
String unsubscribe = getString("report.mailing-lists.column.unsubscribe");
assertNull( response.getLinkWith( unsubscribe ) );
WebLink[] unsubscribeLinks = response.getMatchingLinks( WebLink.MATCH_CONTAINED_TEXT, unsubscribe );
assertTrue( unsubscribeLinks.length == 1 );
assertEquals( "https://example.com/unsubscribe", unsubscribeLinks[0].getAttribute( "href" ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ under the License.
<post>[email protected]</post>
<subscribe>MAILTO:[email protected]</subscribe>
</mailingList>
<mailingList>
<name>Test List 2</name>
<post>[email protected]</post>
<subscribe>MAILTO:[email protected]</subscribe>
<unsubscribe>https://example.com/unsubscribe</unsubscribe>
</mailingList>
</mailingLists>
<build>
<plugins>
Expand All @@ -51,4 +57,4 @@ under the License.
</plugin>
</plugins>
</build>
</project>
</project>

0 comments on commit 98db5e5

Please sign in to comment.