Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.
Open
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
24 changes: 13 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
</prerequisites>

<properties>
<project.build.sourceEncoding>ISO-8859-15</project.build.sourceEncoding>
<project.resources.sourceEncoding>ISO-8859-15</project.resources.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.resources.sourceEncoding>UTF-8</project.resources.sourceEncoding>
<spring.version>4.1.2.RELEASE</spring.version>
</properties>

Expand All @@ -31,8 +31,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
Expand All @@ -48,7 +48,7 @@
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
Expand Down Expand Up @@ -92,12 +92,6 @@
</plugins>
</build>

<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>

<repositories>
<repository>
Expand All @@ -120,6 +114,14 @@
<version>2.6</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>


<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mockmock/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public class Settings
/**
* A set of "From" email addresses to filter
*/
private Set<String> filterFromEmailAddresses = new HashSet<>();
private Set<String> filterFromEmailAddresses = new HashSet<String>();

/**
* A set of "To" email addresses to filter
*/
private Set<String> filterToEmailAddresses = new HashSet<>();
private Set<String> filterToEmailAddresses = new HashSet<String>();

/**
* Path to the static folder containing the images, css and js
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mockmock/console/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected void parseFilterFromEmailAddressesOption(CommandLine cmd, Settings set
{
String input = cmd.getOptionValue("ff");
String[] emailAddresses = input.split(",");
settings.setFilterFromEmailAddresses(new HashSet<>(Arrays.asList(emailAddresses)));
settings.setFilterFromEmailAddresses(new HashSet<String>(Arrays.asList(emailAddresses)));
}
}

Expand All @@ -136,7 +136,7 @@ protected void parseFilterToEmailAddressesOption(CommandLine cmd, Settings setti
{
String input = cmd.getOptionValue("ft");
String[] emailAddresses = input.split(",");
settings.setFilterToEmailAddresses(new HashSet<>(Arrays.asList(emailAddresses)));
settings.setFilterToEmailAddresses(new HashSet<String>(Arrays.asList(emailAddresses)));
}
}

Expand Down
39 changes: 33 additions & 6 deletions src/main/java/com/mockmock/htmlbuilder/MailViewHtmlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,47 @@ public String build()
" </div>\n";
}

if(mockMail.getBodyHtml() != null)
if(mockMail.getAttacheFileName() != null)
{
output +=
" <div class=\"span10\" name=\"bodyHTML_Unformatted\">\n" +
" <h3>HTML body unformatted</h3>\n" +
" <div class=\"well\">" + StringEscapeUtils.escapeHtml(mockMail.getBodyHtml()) + "</div>\n" +
" </div>\n";
" <div class=\"span10\" name=\"bodyPlainText\">\n" +
" <h3>Attachment</h3>\n" +
" <div class=\"well\"><a href=\"/attachment/" +mockMail.getId() + "\">" + StringEscapeUtils.escapeHtml(mockMail.getAttacheFileName()) + "</a></div>\n" +
" </div>\n";
}

if(mockMail.getBodyHtml() != null)
{
// output +=
// " <div class=\"span10\" name=\"bodyHTML_Unformatted\">\n" +
// " <h3>HTML body unformatted</h3>\n" +
// " <div class=\"well\">" + StringEscapeUtils.escapeHtml(mockMail.getBodyHtml()) + "</div>\n" +
// " </div>\n";

// also show a parsed version via an iframe
output +=
"<script type=\"text/javascript\">\n" +
" function SetCwinHeight(){\n" +
" var iframeid=document.getElementById(\"htmliframe\"); //iframe id\n" +
" if (document.getElementById){\n" +
" if (iframeid && !window.opera){\n" +
" if (iframeid.contentDocument && iframeid.contentDocument.body.offsetHeight){\n" +
" iframeid.height = iframeid.contentDocument.body.offsetHeight + 40;\n" +
" }else if(iframeid.Document && iframeid.Document.body.scrollHeight){\n" +
" iframeid.height = iframeid.Document.body.scrollHeight + 40;\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"</script>";

output +=
" <div class=\"span10\" name=\"iFrame\">\n" +
" <h3>HTML body formatted</h3>\n" +
" <iframe class=\"well\" src=\"/view/html/" + mockMail.getId() + "\" style=\"width: 780px; height: 700px; overflow: scroll;\" style=\"\" name=\"bodyHTML_iFrame\">\n" +
" <div>\n" +
" <iframe class=\"well\" width=\"100%\" id=\"htmliframe\" onload=\"Javascript:SetCwinHeight()\" height=\"1\" frameborder=\"0\" src=\"/view/html/" + mockMail.getId() + "\"name=\"bodyHTML_iFrame\">\n" +
" </iframe>\n" +
" <div>\n" +
" </div>";
}

Expand Down
121 changes: 121 additions & 0 deletions src/main/java/com/mockmock/http/MailAttachmentHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.mockmock.http;

import com.mockmock.mail.MailQueue;
import com.mockmock.mail.MockMail;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.server.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;

import javax.mail.internet.MimeUtility;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by Pengzili on 2016/12/30.
*/

@Service
public class MailAttachmentHandler extends BaseHandler{

private String pattern = "^/attachment/([0-9]+)/?$";

private MailQueue mailQueue;

@Override
public void handle(String target, Request request, HttpServletRequest httpServletRequest, HttpServletResponse response) throws IOException, ServletException {

if(!isMatch(target))
{
return;
}

long mailId = getMailId(target);
if(mailId == 0)
{
return;
}

MockMail mockMail = this.mailQueue.getById(mailId);

if(mockMail == null)
{
return;
}

if(mockMail.getBodyHtml() == null)
{
return;
}

String fileName = mockMail.getAttacheFileName();
String fileNameExt =fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
String mimeType = "application/octet-stream";

if (fileNameExt.equals("pdf")){
mimeType = "application/pdf";
}else if(fileNameExt.equals("doc")){
mimeType = "application/msword";
}else if(fileNameExt.equals("xls")){
mimeType = "application/vnd.ms-excel";
}

response.setContentType(mimeType);
response.setHeader("Content-Disposition", " attachment;filename="+ MimeUtility.encodeWord(mockMail.getAttacheFileName()));
response.getOutputStream().write(mockMail.getAttachment());
response.setStatus(HttpServletResponse.SC_OK);

request.setHandled(true);

}

/**
* Checks if this handler should be used for the given target
* @param target String
* @return boolean
*/
private boolean isMatch(String target)
{
return target.matches(pattern);
}

/**
* Returns the mail id if it is part of the target
* @param target String
* @return long
*/
private long getMailId(String target)
{
Pattern compiledPattern = Pattern.compile(pattern);

Matcher matcher = compiledPattern.matcher(target);
if(matcher.find())
{
String result = matcher.group(1);
try
{
return Long.valueOf(result);
}
catch (NumberFormatException e)
{
return 0;
}
}

return 0;
}

@Autowired
public void setMailQueue(MailQueue mailQueue) {
this.mailQueue = mailQueue;
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/mockmock/mail/MailQueue.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mockmock.mail;

import com.google.common.eventbus.Subscribe;
import com.mockmock.AppStarter;
import com.mockmock.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -13,7 +12,7 @@
@Service
public class MailQueue
{
private static ArrayList<MockMail> mailQueue = new ArrayList<>();
private static ArrayList<MockMail> mailQueue = new ArrayList<MockMail>();

private Settings settings;

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/mockmock/mail/MockMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class MockMail implements Comparable<MockMail>
private String rawMail;
private MimeMessage mimeMessage;
private long receivedTime;
private byte[] attachment;
private String attacheFileName;

public long getId()
{
Expand Down Expand Up @@ -113,4 +115,20 @@ public void setReceivedTime(long receivedTime)
{
this.receivedTime = receivedTime;
}

public byte[] getAttachment() {
return attachment;
}

public void setAttachment(byte[] attachment) {
this.attachment = attachment;
}

public String getAttacheFileName() {
return attacheFileName;
}

public void setAttacheFileName(String attacheFileName) {
this.attacheFileName = attacheFileName;
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/mockmock/mail/MockMockMessageHandlerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.eventbus.EventBus;
import com.mockmock.Settings;
import org.apache.commons.io.IOUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -15,6 +16,7 @@
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.*;
import java.util.Properties;

Expand Down Expand Up @@ -126,6 +128,9 @@ public void data(InputStream data) throws RejectException, IOException
{
BodyPart bodyPart = multipart.getBodyPart(i);
String contentType = bodyPart.getContentType();
contentType = contentType.replaceAll("\t|\r|\n", "");
System.out.println(contentType);

if(contentType.matches("text/plain.*"))
{
mockMail.setBody(convertStreamToString(bodyPart.getInputStream()));
Expand All @@ -134,6 +139,33 @@ else if(contentType.matches("text/html.*"))
{
mockMail.setBodyHtml(convertStreamToString(bodyPart.getInputStream()));
}
else if(contentType.matches("multipart/related.*")){
// compound documents
Multipart contentMulti = (Multipart)bodyPart.getContent();
for (int j = 0; j < contentMulti.getCount(); j++){
BodyPart subPart = contentMulti.getBodyPart(i);
String subContentType = subPart.getContentType();
System.out.println(subContentType);
String encoding = "UTF-8";

if(subContentType.matches("text/html.*")){
String originalBodyHtml = IOUtils.toString(subPart.getInputStream(), "utf-8");
String replacedBodyHtml = originalBodyHtml.replaceAll("(?<!\\r)\\n", "\r\n");
InputStream inputStream = MimeUtility.decode(IOUtils.toInputStream(replacedBodyHtml), "quoted-printable");
String bodyHtml = IOUtils.toString(inputStream, "utf-8");

// String bodyHtml = IOUtils.toString(MimeUtility.decode(subPart.getInputStream(), "quoted-printable"), "utf-8");
mockMail.setBodyHtml(bodyHtml);
}
}

} else if (contentType.matches("application/octet-stream.*") || contentType.matches("application/pdf.*")) {
// attachment
String strFileName = MimeUtility.decodeText(bodyPart.getFileName());
mockMail.setAttacheFileName(strFileName);
byte[] attachContent = IOUtils.toByteArray(bodyPart.getInputStream());
mockMail.setAttachment(attachContent);
}
}
}
else if(messageContent instanceof InputStream)
Expand Down
Loading