Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrescobar2011-08-09 22:09:58 +0000
committerRyan D. Brooks2011-08-09 22:09:58 +0000
commit38631733afd9c0068ed37347911b38420bf47187 (patch)
treed64f57f9080e3e58fdcdc952c5996e165ed0aa27 /plugins/org.eclipse.osee.mail
parent21b74825437ec6ac1a8b96ec474ab8f4140a102e (diff)
downloadorg.eclipse.osee-38631733afd9c0068ed37347911b38420bf47187.tar.gz
org.eclipse.osee-38631733afd9c0068ed37347911b38420bf47187.tar.xz
org.eclipse.osee-38631733afd9c0068ed37347911b38420bf47187.zip
refinement[bgz_349095]: Make DataSource classes top-level classes
Diffstat (limited to 'plugins/org.eclipse.osee.mail')
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java191
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MultiPartDataSource.java74
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/StringDataSource.java84
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/UrlDataSource.java55
4 files changed, 250 insertions, 154 deletions
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java
index b0a3920ad4c..3232ab58de2 100644
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java
+++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java
@@ -10,20 +10,14 @@
*******************************************************************************/
package org.eclipse.osee.mail;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
+import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import javax.activation.CommandMap;
import javax.activation.DataSource;
import javax.activation.MailcapCommandMap;
-import javax.mail.BodyPart;
-import javax.mail.MessagingException;
-import javax.mail.MultipartDataSource;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import org.eclipse.osee.framework.jdk.core.util.Lib;
@@ -34,6 +28,8 @@ import org.eclipse.osee.framework.jdk.core.util.windows.OutlookCalendarEvent;
*/
public final class MailUtils {
+ private static final String OUTLOOK_CALENDAR_EXTENSION = ".vcs";
+
private MailUtils() {
// Utility Class
}
@@ -52,7 +48,7 @@ public final class MailUtils {
return mc;
}
- public static DataSource createFromString(String name, String message, Object... args) {
+ public static StringDataSource createFromString(String name, String message, Object... args) {
String data;
if (args.length > 0) {
data = String.format(message, args);
@@ -65,48 +61,12 @@ public final class MailUtils {
return dataSource;
}
- private static String toFileName(String value, String extension) throws UnsupportedEncodingException {
- String fileName = value;
- if (fileName.endsWith(".vcs")) {
- fileName = Lib.removeExtension(fileName);
- }
- String validName = URLEncoder.encode(fileName, "UTF-8");
- StringBuilder builder = new StringBuilder();
- builder.append(validName);
- builder.append(".vcs");
-
- return builder.toString();
- }
-
- public static DataSource createOutlookEvent(String eventName, String location, Date date, String startTime, String endTime) throws UnsupportedEncodingException {
- OutlookCalendarEvent calendarEvent = new OutlookCalendarEvent(location, eventName, date, startTime, endTime);
- String fileName = toFileName(eventName, ".vcs");
-
- StringDataSource dataSource = new StringDataSource(fileName, calendarEvent.getEvent()) {
- @Override
- public OutputStream getOutputStream() {
- throw new UnsupportedOperationException("OutputStream is not available for this source");
- }
- };
- dataSource.setCharset("UTF-8");
- dataSource.setContentType("text/plain");
- return dataSource;
- }
-
- public static DataSource createFromHtml(final String name, String htmlData) throws MessagingException {
+ public static DataSource createFromHtml(final String name, String htmlData) throws Exception {
String plainText = stripHtmlTags(htmlData);
return createAlternativeDataSource(name, htmlData, plainText);
}
- private static String stripHtmlTags(String html) {
- String plainText = html.replaceAll("<(.|\n)*?>", "");
- plainText = plainText.replaceAll("\t", " ");
- plainText = plainText.replaceAll("\r\n", "");
- plainText = plainText.replaceAll(" +", " ");
- return plainText;
- }
-
- public static DataSource createAlternativeDataSource(String name, String htmlText, String plainText) throws MessagingException {
+ public static DataSource createAlternativeDataSource(String name, String htmlText, String plainText) throws Exception {
final MimeMultipart content = new MimeMultipart("alternative");
MimeBodyPart html = new MimeBodyPart();
@@ -121,119 +81,42 @@ public final class MailUtils {
return new MultiPartDataSource(name, content);
}
- private static final class MultiPartDataSource implements MultipartDataSource {
- private final String name;
- private final MimeMultipart content;
-
- public MultiPartDataSource(String name, MimeMultipart content) {
- this.name = name;
- this.content = content;
- }
-
- @Override
- public String getContentType() {
- return content.getContentType();
- }
-
- @Override
- public InputStream getInputStream() throws IOException {
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- try {
- content.writeTo(os);
- } catch (MessagingException ex) {
- throw new IOException(ex);
- }
- return new ByteArrayInputStream(os.toByteArray());
- }
-
- @Override
- public String getName() {
- return name;
- }
-
- @Override
- public OutputStream getOutputStream() {
- throw new UnsupportedOperationException("OutputStream is not available for this source");
- }
-
- @Override
- public int getCount() {
- try {
- return content.getCount();
- } catch (MessagingException ex) {
- return 0;
- }
- }
-
- @Override
- public BodyPart getBodyPart(int index) throws MessagingException {
- return content.getBodyPart(index);
- }
-
- };
-
- private static class StringDataSource implements javax.activation.DataSource {
-
- private final String name;
- private String data;
- private String charset;
- private String contentType;
- private ByteArrayOutputStream outputStream;
-
- public StringDataSource(String name, String data) {
- super();
- this.name = name;
- this.data = data;
- }
-
- @Override
- public String getName() {
- return name;
- }
-
- public void setCharset(String charset) {
- this.charset = charset;
- }
-
- public void setContentType(String contentType) {
- this.contentType = contentType.toLowerCase();
- }
+ public static UrlDataSource createFromUrl(String name, URL url, String contentType) {
+ return new UrlDataSource(name, url, contentType);
+ }
- @Override
- public InputStream getInputStream() throws IOException {
- if (data == null && outputStream == null) {
- throw new IOException("No data");
- }
- if (outputStream != null) {
- String encodedOut = outputStream.toString(charset);
- if (data == null) {
- data = encodedOut;
- } else {
- data = data.concat(encodedOut);
- }
- outputStream = null;
- }
- return new ByteArrayInputStream(data.getBytes(charset));
- }
+ public static StringDataSource createOutlookEvent(String eventName, String location, Date date, String startTime, String endTime) throws UnsupportedEncodingException {
+ OutlookCalendarEvent calendarEvent = new OutlookCalendarEvent(location, eventName, date, startTime, endTime);
+ String fileName = toFileName(eventName, OUTLOOK_CALENDAR_EXTENSION);
- @Override
- public OutputStream getOutputStream() {
- if (outputStream == null) {
- outputStream = new ByteArrayOutputStream();
+ StringDataSource dataSource = new StringDataSource(fileName, calendarEvent.getEvent()) {
+ @Override
+ public OutputStream getOutputStream() {
+ throw new UnsupportedOperationException("OutputStream is not available for this source");
}
- return outputStream;
- }
+ };
+ dataSource.setCharset("UTF-8");
+ dataSource.setContentType("text/plain");
+ return dataSource;
+ }
- @Override
- public String getContentType() {
- String toReturn;
- if (contentType != null && contentType.indexOf("charset") > 0 && contentType.startsWith("text/")) {
- toReturn = contentType;
- } else {
- toReturn = String.format("%s; charset=%s", contentType != null ? contentType : "text/plain", charset);
- }
- return toReturn;
+ private static String toFileName(String value, String extension) throws UnsupportedEncodingException {
+ String fileName = value;
+ if (fileName.endsWith(OUTLOOK_CALENDAR_EXTENSION)) {
+ fileName = Lib.removeExtension(fileName);
}
+ String validName = URLEncoder.encode(fileName, "UTF-8");
+ StringBuilder builder = new StringBuilder();
+ builder.append(validName);
+ builder.append(OUTLOOK_CALENDAR_EXTENSION);
+ return builder.toString();
+ }
+ private static String stripHtmlTags(String html) {
+ String plainText = html.replaceAll("<(.|\n)*?>", "");
+ plainText = plainText.replaceAll("\t", " ");
+ plainText = plainText.replaceAll("\r\n", "");
+ plainText = plainText.replaceAll(" +", " ");
+ return plainText;
}
}
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MultiPartDataSource.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MultiPartDataSource.java
new file mode 100644
index 00000000000..b5a8c09b38f
--- /dev/null
+++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MultiPartDataSource.java
@@ -0,0 +1,74 @@
+/*******************************************************************************
+ * Copyright (c) 2004, 2007 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.mail;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import javax.mail.BodyPart;
+import javax.mail.MessagingException;
+import javax.mail.MultipartDataSource;
+import javax.mail.internet.MimeMultipart;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public class MultiPartDataSource implements MultipartDataSource {
+ private final String name;
+ private final MimeMultipart content;
+
+ public MultiPartDataSource(String name, MimeMultipart content) {
+ this.name = name;
+ this.content = content;
+ }
+
+ @Override
+ public String getContentType() {
+ return content.getContentType();
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ try {
+ content.writeTo(os);
+ } catch (MessagingException ex) {
+ throw new IOException(ex);
+ }
+ return new ByteArrayInputStream(os.toByteArray());
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public OutputStream getOutputStream() {
+ throw new UnsupportedOperationException("OutputStream is not available for this source");
+ }
+
+ @Override
+ public int getCount() {
+ try {
+ return content.getCount();
+ } catch (MessagingException ex) {
+ return 0;
+ }
+ }
+
+ @Override
+ public BodyPart getBodyPart(int index) throws MessagingException {
+ return content.getBodyPart(index);
+ }
+}; \ No newline at end of file
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/StringDataSource.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/StringDataSource.java
new file mode 100644
index 00000000000..8770fb8d3bb
--- /dev/null
+++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/StringDataSource.java
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Copyright (c) 2004, 2007 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.mail;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public class StringDataSource implements javax.activation.DataSource {
+
+ private final String name;
+ private String data;
+ private String charset;
+ private String contentType;
+ private ByteArrayOutputStream outputStream;
+
+ public StringDataSource(String name, String data) {
+ super();
+ this.name = name;
+ this.data = data;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ public void setCharset(String charset) {
+ this.charset = charset;
+ }
+
+ public void setContentType(String contentType) {
+ this.contentType = contentType.toLowerCase();
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ if (data == null && outputStream == null) {
+ throw new IOException("No data");
+ }
+ if (outputStream != null) {
+ String encodedOut = outputStream.toString(charset);
+ if (data == null) {
+ data = encodedOut;
+ } else {
+ data = data.concat(encodedOut);
+ }
+ outputStream = null;
+ }
+ return new ByteArrayInputStream(data.getBytes(charset));
+ }
+
+ @Override
+ public OutputStream getOutputStream() {
+ if (outputStream == null) {
+ outputStream = new ByteArrayOutputStream();
+ }
+ return outputStream;
+ }
+
+ @Override
+ public String getContentType() {
+ String toReturn;
+ if (contentType != null && contentType.indexOf("charset") > 0 && contentType.startsWith("text/")) {
+ toReturn = contentType;
+ } else {
+ toReturn = String.format("%s; charset=%s", contentType != null ? contentType : "text/plain", charset);
+ }
+ return toReturn;
+ }
+} \ No newline at end of file
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/UrlDataSource.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/UrlDataSource.java
new file mode 100644
index 00000000000..3ac2552c17f
--- /dev/null
+++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/UrlDataSource.java
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * Copyright (c) 2004, 2007 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.mail;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import javax.activation.DataSource;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public class UrlDataSource implements DataSource {
+
+ private final String name;
+ private final URL url;
+ private final String contentType;
+
+ public UrlDataSource(String name, URL url, String contentType) {
+ super();
+ this.name = name;
+ this.url = url;
+ this.contentType = contentType;
+ }
+
+ @Override
+ public String getContentType() {
+ return contentType;
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ return new BufferedInputStream(url.openStream());
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public OutputStream getOutputStream() {
+ throw new UnsupportedOperationException();
+ }
+}

Back to the top