/**
* MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface.
* Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
/******************************************************************************\
*
* This product was developed by
*
* SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO),
*
* a government company established under Brazilian law (5.615/70),
* at Department of Development of Porto Alegre.
*
\******************************************************************************/
package serpro.mailarchiver.service.web;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPFault;
import javax.xml.ws.WebFault;
import javax.xml.ws.soap.SOAPFaultException;
import org.apache.commons.lang3.exception.ExceptionUtils;
import static com.google.common.base.Strings.*;
import serpro.mailarchiver.service.dto.TFault;
@WebFault(name="Fault")
public class ServiceFault extends Exception {
private TFault fault;
@XmlTransient
@Override
public StackTraceElement[] getStackTrace() {
return super.getStackTrace();
}
@XmlTransient
@Override
public Throwable getCause() {
return super.getCause();
}
public TFault getFaultInfo() {
return fault;
}
public ServiceFault(String message) {
super(message);
}
public ServiceFault(String message, TFault fault) {
super(message);
this.fault = fault;
}
public ServiceFault(String message, TFault fault, Throwable cause) {
super(message, cause);
this.fault = fault;
}
//--------------------------------------------------------------------------
public static Config invalidFolderName() {
return new Config("Client", "INVALID_FOLDER_NAME");
}
public static Config folderNameAlreadyExists() {
return new Config("Client", "FOLDER_NAME_ALREADY_EXISTS");
}
public static Config invalidFolderPath() {
return new Config("Client", "INVALID_FOLDER_PATH");
}
public static Config invalidFolderId() {
return new Config("Client", "INVALID_FOLDER_ID");
}
public static Config invalidMessageId() {
return new Config("Client", "INVALID_MESSAGE_ID");
}
public static Config invalidBodyId() {
return new Config("Client", "INVALID_BODY_ID");
}
public static Config invalidQueryConfig() {
return new Config("Client", "INVALID_QUERY_CONFIG");
}
public static Config invalidTagConfig() {
return new Config("Client", "INVALID_TAG_CONFIG");
}
public static Config invalidZipConfig() {
return new Config("Client", "INVALID_ZIP_CONFIG");
}
public static Config unsupportedArchiveFormat() {
return new Config("Client", "UNSUPPORTED_ARCHIVE_FORMAT");
}
public static Config folderNotFound() {
return new Config("Client", "FOLDER_NOT_FOUND");
}
public static Config folderNotEmpty() {
return new Config("Client", "FOLDER_NOT_EMPTY");
}
public static Config fileSystemFolderNotEmpty() {
return new Config("Server", "FILE_SYSTEM_FOLDER_NOT_EMPTY");
}
public static Config messageNotFound() {
return new Config("Client", "MESSAGE_NOT_FOUND");
}
public static Config binaryBodyNotFound() {
return new Config("Client", "BINARY_BODY_NOT_FOUND");
}
public static Config moreThanOneFolderFound() {
return new Config("Server", "MORE_THAN_ONE_FOLDER_FOUND");
}
public static Config expungedMessage() {
return new Config("Server", "EXPUNGED_MESSAGE");
}
public static Config archiveFailure() {
return new Config("Server", "ARCHIVE_FAILURE");
}
public static Config fileSystemFailure() {
return new Config("Server", "FILE_SYSTEM_FAILURE");
}
public static Config luceneFailure() {
return new Config("Server", "LUCENE_FAILURE");
}
public static Config loginFailure() {
return new Config("Server", "LOGIN_FAILURE");
}
public static Config runtimeException() {
return new Config("Server", "RUNTIME_EXCEPTION");
}
public static Config fakeException() {
return new Config("Server", "FAKE_EXCEPTION");
}
public static class Config {
private final String code;
private final String subCode;
private String message;
private String actor;
private Throwable cause;
private Map context = new HashMap();
private Config(String code, String subCode) {
this.code = code;
this.subCode = subCode;
}
public Config setMessage(String message) {
this.message = message;
return this;
}
public Config setActor(String actor) {
this.actor = actor;
return this;
}
public Config setCause(Throwable cause) {
this.cause = cause;
return this;
}
public Config addValue(String key, Object value) {
context.put(key, value.toString());
return this;
}
public void raise() throws ServiceFault {
throw create();
}
public ServiceFault create() {
TFault fault = new TFault();
if(code != null) {
fault.setSoapFaultCode(code);
}
if(subCode != null) {
fault.setSoapFaultSubCode(subCode);
}
if(message != null) {
fault.setSoapFaultString(message);
}
if(actor != null) {
fault.setSoapFaultActor(actor);
}
fault.setContext(context);
if(cause != null) {
fault.setCauseClass(cause.getClass().getName());
fault.setCauseMessage(cause.getLocalizedMessage());
fault.setCauseStackTrace(ExceptionUtils.getStackTrace(cause));
}
SOAPFault soapFault = null;
try {
soapFault = SOAPFactory.newInstance().createFault();
if(code != null) {
soapFault.setFaultCode(code + "." + subCode);
}
if(message != null) {
soapFault.setFaultString(message);
}
else {
soapFault.setFaultString(subCode);
}
if(actor != null) {
soapFault.setFaultActor(actor);
}
}
catch(SOAPException ex) {
ex.printStackTrace();
}
ServiceFault e = new ServiceFault(nullToEmpty(message), fault, new SOAPFaultException(soapFault));
fault.setStackTrace(ExceptionUtils.getStackTrace(e));
return e;
}
}
}