org.apache.jsieve.util.check.ScriptChecker Maven / Gradle / Ivy
Go to download
Apache jSieve is a server side mail filtering system
implementing RFC3028. Apache jSieve is developed by the
James project. This module contains additional utilities.
The newest version!
/****************************************************************
* 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.jsieve.util.check;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.jsieve.ConfigurationManager;
import org.apache.jsieve.exception.SieveException;
import org.apache.jsieve.mail.ActionFileInto;
import org.apache.jsieve.mail.ActionKeep;
import org.apache.jsieve.mail.ActionRedirect;
import org.apache.jsieve.mail.ActionReject;
import org.apache.jsieve.parser.generated.ParseException;
/**
* Checks a sieve
script by executing it against a given mail and
* reporting the results.
*/
public class ScriptChecker {
private final ScriptCheckMailAdapter adapter;
public ScriptChecker() {
adapter = new ScriptCheckMailAdapter();
}
/**
* Checks the sieve
script contained in the given file by
* executing it against the given message.
*
* @param message
* File
containing the mail message to be fed
* to the script, not null
* @param script
* File
containing the script to be checked
* @return Results
of that execution
* @throws IOException
* @throws MessageException
*/
public Results check(final File message, final File script)
throws IOException, MessagingException {
final FileInputStream messageStream = new FileInputStream(message);
final FileInputStream scriptStream = new FileInputStream(script);
return check(messageStream, scriptStream);
}
/**
* Checks the sieve
script contained in the given file by
* executing it against the given message.
*
* @param script
* InputStream
, not null
* @return Results
of the check, not null
* @throws IOException
* @throws MessagingException
*/
public Results check(final InputStream message, final InputStream script)
throws IOException, MessagingException {
MimeMessage mimeMessage = new MimeMessage(null, message);
adapter.setMail(mimeMessage);
Results results;
try {
new ConfigurationManager().build().interpret(adapter, script);
final List executedActions = adapter.getExecutedActions();
results = new Results(executedActions);
} catch (ParseException e) {
e.printStackTrace();
results = new Results(e);
} catch (SieveException e) {
e.printStackTrace();
results = new Results(e);
}
return results;
}
/**
* Contains results of script execution.
*/
public final static class Results {
private final boolean pass;
private final Exception exception;
private final List actionsExecuted;
public Results(final Exception ex) {
this.exception = ex;
pass = false;
actionsExecuted = null;
}
public Results(final List actions) {
this.pass = true;
exception = null;
this.actionsExecuted = actions;
}
/**
* Is this a pass?
*
* @return true if the script executed without error, false if errors
* were encountered
*/
public boolean isPass() {
return pass;
}
/**
* Gets the exception which was thrown during execution.
*
* @return Exception
or null if no exception was thrown
*/
public Exception getException() {
return exception;
}
/**
* Gets the actions executed by the script.
*
* @return List
of actions or null if the script failed
*/
public List getActionsExecuted() {
return actionsExecuted;
}
/**
* Is the n'th action a FileInto
* with given destination?
* @param destination String
destination for the file into
* @param n index to check
* @return true if the n'th action is a FileInto
* with given destination
*/
public boolean isActionFileInto(String destination, int n) {
boolean result = false;
Object action = actionsExecuted.get(n);
if (action != null && action instanceof ActionFileInto) {
ActionFileInto actionFileInto = (ActionFileInto) action;
result = destination.equals(actionFileInto.getDestination());
}
return result;
}
/**
* Is the n'th action a Redirect
* with given address?
* @param address String
redirect address
* @param n index to check
* @return true if the n'th action is a Redirect
* with given redirect address
*/
public boolean isActionRedirect(String address, int n) {
boolean result = false;
Object action = actionsExecuted.get(n);
if (action != null && action instanceof ActionRedirect) {
ActionRedirect actionRedirect = (ActionRedirect) action;
result = address.equals(actionRedirect.getAddress());
}
return result;
}
/**
* Is the n'th action a Reject
* with given message?
* @param message String
rejection message
* @param n index to check
* @return true if the n'th action is a Reject
* with given reject message
*/
public boolean isActionReject(String message, int n) {
boolean result = false;
Object action = actionsExecuted.get(n);
if (action != null && action instanceof ActionReject) {
ActionReject actionReject = (ActionReject) action;
result = message.equals(actionReject.getMessage());
}
return result;
}
/**
* Is the n'th action a Keep
?
* @param n index to check
* @return true if the n'th action is a Keep
*/
public boolean isActionKeep(int n) {
boolean result = false;
Object action = actionsExecuted.get(n);
if (action != null && action instanceof ActionKeep) {
result = true;
}
return result;
}
/**
* Prints out details of results.
*/
public String toString() {
StringBuilder buffer = new StringBuilder("Results: ");
if (pass) {
buffer.append("PASS");
} else {
buffer.append("FAIL: ");
if (exception != null) {
if (exception instanceof ParseException) {
buffer.append("Cannot parse script");
} else {
buffer.append("Cannot excute script");
}
buffer.append(exception.getMessage());
}
}
return buffer.toString();
}
}
}