Author: bfoster
Date: Sat Apr 19 09:40:47 2014
New Revision: 1588629
URL: http://svn.apache.org/r1588629
Log:
- Added new GenericEmailParser
Added:
oodt/trunk/pushpull/src/main/java/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParser.java
(with props)
oodt/trunk/pushpull/src/test/
oodt/trunk/pushpull/src/test/org/
oodt/trunk/pushpull/src/test/org/apache/
oodt/trunk/pushpull/src/test/org/apache/oodt/
oodt/trunk/pushpull/src/test/org/apache/oodt/cas/
oodt/trunk/pushpull/src/test/org/apache/oodt/cas/pushpull/
oodt/trunk/pushpull/src/test/org/apache/oodt/cas/pushpull/filerestrictions/
oodt/trunk/pushpull/src/test/org/apache/oodt/cas/pushpull/filerestrictions/parsers/
oodt/trunk/pushpull/src/test/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParserTest.java
(with props)
oodt/trunk/pushpull/src/test/resources/
oodt/trunk/pushpull/src/test/resources/TestEmail.txt (with props)
Modified:
oodt/trunk/pushpull/pom.xml
oodt/trunk/pushpull/src/main/java/org/apache/oodt/cas/pushpull/exceptions/ParserException.java
Modified: oodt/trunk/pushpull/pom.xml
URL:
http://svn.apache.org/viewvc/oodt/trunk/pushpull/pom.xml?rev=1588629&r1=1588628&r2=1588629&view=diff
==============================================================================
--- oodt/trunk/pushpull/pom.xml (original)
+++ oodt/trunk/pushpull/pom.xml Sat Apr 19 09:40:47 2014
@@ -154,7 +154,19 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
- <version>3.8.2</version>
+ <version>4.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.9.5</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-all</artifactId>
+ <version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
Modified:
oodt/trunk/pushpull/src/main/java/org/apache/oodt/cas/pushpull/exceptions/ParserException.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/pushpull/src/main/java/org/apache/oodt/cas/pushpull/exceptions/ParserException.java?rev=1588629&r1=1588628&r2=1588629&view=diff
==============================================================================
---
oodt/trunk/pushpull/src/main/java/org/apache/oodt/cas/pushpull/exceptions/ParserException.java
(original)
+++
oodt/trunk/pushpull/src/main/java/org/apache/oodt/cas/pushpull/exceptions/ParserException.java
Sat Apr 19 09:40:47 2014
@@ -38,4 +38,8 @@ public class ParserException extends Pus
public ParserException(String msg) {
super(msg);
}
+
+ public ParserException(String msg, Throwable t) {
+ super(msg, t);
+ }
}
Added:
oodt/trunk/pushpull/src/main/java/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParser.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/pushpull/src/main/java/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParser.java?rev=1588629&view=auto
==============================================================================
---
oodt/trunk/pushpull/src/main/java/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParser.java
(added)
+++
oodt/trunk/pushpull/src/main/java/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParser.java
Sat Apr 19 09:40:47 2014
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.pushpull.filerestrictions.parsers;
+
+//OODT imports
+import org.apache.oodt.cas.pushpull.filerestrictions.Parser;
+import org.apache.oodt.cas.pushpull.filerestrictions.VirtualFile;
+import org.apache.oodt.cas.pushpull.filerestrictions.VirtualFileStructure;
+import org.apache.oodt.cas.pushpull.exceptions.ParserException;
+
+//Google imports
+import com.google.common.collect.Lists;
+
+//JDK imports
+import java.io.FileInputStream;
+import java.util.List;
+import java.util.Scanner;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * A generic email parser which generates file paths to be downloaded by using
a defined java
+ * Pattern. The pattern should specify pattern groups for file paths in the
matching pattern.
+ * These groups will then be extracted and added to the file structure.
+ *
+ * @author [email protected] (Brian Foster)
+ */
+public class GenericEmailParser implements Parser {
+
+ public static final String FILE_PATTERNS_PROPERTY_NAME =
+ "org.apache.oodt.cas.pushpull.generic.email.parser.file.pattern";
+
+ private final String filePattern;
+
+ public GenericEmailParser() {
+ filePattern = loadFilePattern();
+ }
+
+ public GenericEmailParser(String filePattern) {
+ this.filePattern = filePattern;
+ }
+
+ @Override
+ public VirtualFileStructure parse(FileInputStream emailFile) throws
ParserException {
+ VirtualFile root = VirtualFile.createRootDir();
+
+ List<String> filePaths = generateFilePaths(readEmail(emailFile));
+
+ for (String filePath : filePaths) {
+ new VirtualFile(root, filePath, false);
+ }
+
+ return new VirtualFileStructure(null, "/", root);
+ }
+
+ private String readEmail(FileInputStream emailFile) {
+ StringBuilder emailText = new StringBuilder("");
+ Scanner scanner = new Scanner(emailFile);
+ while (scanner.hasNextLine()) {
+ emailText.append(scanner.nextLine()).append("\n");
+ }
+ scanner.close();
+ return emailText.toString();
+ }
+
+ private List<String> generateFilePaths(String emailText) throws
ParserException {
+ List<String> filePaths = Lists.newArrayList();
+ Pattern pattern = Pattern.compile(filePattern);
+ Matcher m = pattern.matcher(emailText);
+ if (m.find()) {
+ // Ignore index 0, as that is the matching string for pattern.
+ for (int i = 1; i <= m.groupCount(); i++) {
+ filePaths.add(m.group(i));
+ }
+ }
+ return filePaths;
+ }
+
+ private String loadFilePattern() {
+ return System.getProperty(FILE_PATTERNS_PROPERTY_NAME);
+ }
+}
Propchange:
oodt/trunk/pushpull/src/main/java/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParser.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
oodt/trunk/pushpull/src/test/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParserTest.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/pushpull/src/test/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParserTest.java?rev=1588629&view=auto
==============================================================================
---
oodt/trunk/pushpull/src/test/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParserTest.java
(added)
+++
oodt/trunk/pushpull/src/test/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParserTest.java
Sat Apr 19 09:40:47 2014
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.pushpull.filerestrictions.parsers;
+
+// JUnit static imports
+import static org.junit.Assert.assertThat;
+
+// JDK imports
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import java.util.List;
+
+// OODT imports
+import org.apache.oodt.cas.pushpull.exceptions.ParserException;
+import org.apache.oodt.cas.pushpull.filerestrictions.FileRestrictions;
+import org.apache.oodt.cas.pushpull.filerestrictions.VirtualFileStructure;
+
+// JUnit imports
+import org.hamcrest.Matchers;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Test class for {@link GenericEmailParser}.
+ *
+ * @author [email protected] (Brian Foster)
+ */
+@RunWith(JUnit4.class)
+public class GenericEmailParserTest {
+
+ private File emailFile;
+
+ @Before
+ public void setUp() throws URISyntaxException {
+ emailFile = new
File(ClassLoader.getSystemResource("TestEmail.txt").toURI());
+ }
+
+ @Test
+ public void testGenericEmailParser() throws ParserException,
FileNotFoundException {
+ GenericEmailParser parser = new GenericEmailParser("Wav File: ([^\\s]+)");
+ VirtualFileStructure vfs = parser.parse(new FileInputStream(emailFile));
+ List<String> filePaths =
FileRestrictions.toStringList(vfs.getRootVirtualFile());
+ assertThat(filePaths.size(), Matchers.is(1));
+ assertThat(filePaths.get(0), Matchers.is("/some/path/to/a/wav/file.wav"));
+ }
+}
Propchange:
oodt/trunk/pushpull/src/test/org/apache/oodt/cas/pushpull/filerestrictions/parsers/GenericEmailParserTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/trunk/pushpull/src/test/resources/TestEmail.txt
URL:
http://svn.apache.org/viewvc/oodt/trunk/pushpull/src/test/resources/TestEmail.txt?rev=1588629&view=auto
==============================================================================
--- oodt/trunk/pushpull/src/test/resources/TestEmail.txt (added)
+++ oodt/trunk/pushpull/src/test/resources/TestEmail.txt Sat Apr 19 09:40:47
2014
@@ -0,0 +1,14 @@
+
+Dear Lousy Customer,
+
+Your file is ready to download.
+
+Wav File: /some/path/to/a/wav/file.wav
+
+MDB Hash File: /some/path/to/a/md5/file.md5
+
+Thanks!
+Files-R-Us
+
+PS
+You only get this notification once so download now!!!!!
Propchange: oodt/trunk/pushpull/src/test/resources/TestEmail.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain