Skip to content
This repository was archived by the owner on Dec 11, 2023. 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
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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
56 changes: 35 additions & 21 deletions src/main/java/com/mockmock/htmlbuilder/MailViewHtmlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String build()
subjectOutput = StringEscapeUtils.escapeHtml(mockMail.getSubject());
}

subjectOutput += " <small class=\"deleteLink\"><a href=\"/delete/" + mockMail.getId() + "\">Delete</a></small>";
subjectOutput += " <small class=\"deleteLink\"><a href=\"/delete/" + mockMail.getId() + "\">Delete</a></small>";

StringBuilder output = new StringBuilder("<div class=\"container\">\n");

Expand All @@ -57,41 +57,55 @@ public String build()
{
output.append(" <div class=\"span10\" name=\"bodyPlainText\">\n")
.append(" <h3>Plain text body</h3>\n")
.append(" <div class=\"well\">")
.append(StringEscapeUtils.escapeHtml(mockMail.getBody()))
.append("</div>\n")
.append(" <div class=\"well\">").append(StringEscapeUtils.escapeHtml(mockMail.getBody())).append("</div>\n")
.append(" </div>\n");
}

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

if(mockMail.getBodyHtml() != null)
{
// also show a parsed version via an iframe
output.append(" <div class=\"span10\" name=\"iFrame\">\n" + " <h3>HTML body formatted</h3>\n")
.append(" <iframe class=\"well\" src=\"/view/html/")
.append(mockMail.getId())
.append("\" style=\"width: 780px; height: 700px; overflow: scroll;\" style=\"\" name=\"bodyHTML_iFrame\">\n")
output.append(" <script type=\"text/javascript\">\n")
.append(" function SetCwinHeight(){\n")
.append(" var iframeid=document.getElementById(\"htmliframe\"); //iframe id\n")
.append(" if (document.getElementById){\n")
.append(" if (iframeid && !window.opera){\n")
.append(" if (iframeid.contentDocument && iframeid.contentDocument.body.offsetHeight){\n")
.append(" iframeid.height = iframeid.contentDocument.body.offsetHeight + 40;\n")
.append(" }else if(iframeid.Document && iframeid.Document.body.scrollHeight){\n")
.append(" iframeid.height = iframeid.Document.body.scrollHeight + 40;\n")
.append(" }\n")
.append(" }\n")
.append(" }\n")
.append(" }\n")
.append(" </script>")
.append(" <div class=\"span10\" name=\"iFrame\">\n")
.append(" <h3>HTML body formatted</h3>\n")
.append(" <div>\n")
.append(" <iframe class=\"well\" id=\"htmliframe\" src=\"/view/html/").append(mockMail.getId()).append("\" style=\"width: 780px; height: 700px; overflow: scroll;\" style=\"\" name=\"bodyHTML_iFrame\">\n")
.append(" </iframe>\n")
.append(" </div>\n")
.append(" </div>");
}

// just output the raw mail so we're sure everything is on the screen
if(mockMail.getRawMail() != null)
{
// output complete raw mail
output.append(" <div class=\"span10\" name=\"rawOutput\">\n")
// just output the raw mail so we're sure everything is on the screen
if(mockMail.getRawMail() != null)
{
// output complete raw mail
output.append(" <div class=\"span10\" name=\"rawOutput\">\n")
.append(" <h3>Complete raw mail output</h3>\n")
.append(" <div class=\"well\">")
.append(StringEscapeUtils.escapeHtml(mockMail.getRawMail()))
.append("</div>\n")
.append(" </div>\n");
}
}

output.append(" </div>\n")
.append("</div>\n");
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;
}
}
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;
}
}
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,13 +16,14 @@
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;

@Service
public class MockMockMessageHandlerFactory implements MessageHandlerFactory
{
private EventBus eventBus;
private final EventBus eventBus;
private Settings settings;

@Autowired
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,32 @@ 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(), encoding);
String replacedBodyHtml = originalBodyHtml.replaceAll("(?<!\\r)\\n", "\r\n");
InputStream inputStream = MimeUtility.decode(IOUtils.toInputStream(replacedBodyHtml), "quoted-printable");
String bodyHtml = IOUtils.toString(inputStream, encoding);

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
7 changes: 7 additions & 0 deletions src/main/java/com/mockmock/server/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class HttpServer implements com.mockmock.server.Server
private MailDetailHtmlHandler mailDetailHtmlHandler;
private MailDeleteHandler mailDeleteHandler;
private DeleteHandler deleteHandler;
private MailAttachmentHandler attachmentHandler;

public void setPort(int port)
{
Expand Down Expand Up @@ -62,6 +63,7 @@ public void start()
this.mailDetailHtmlHandler,
this.mailDeleteHandler,
this.deleteHandler,
this.attachmentHandler,
resourceHandler
};
HandlerList handlerList = new HandlerList();
Expand Down Expand Up @@ -105,6 +107,11 @@ public void setDeleteHandler(DeleteHandler deleteHandler) {
this.deleteHandler = deleteHandler;
}

@Autowired
private void setAttachmentHandler(MailAttachmentHandler attachmentHandler) {
this.attachmentHandler = attachmentHandler;
}

@Autowired
public void setSettings(Settings settings) {
this.settings = settings;
Expand Down