001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2007, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.wsrp;
023:
024: import org.jboss.logging.Logger;
025: import org.jboss.portal.wsrp.servlet.ServletAccess;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.xml.namespace.QName;
029: import javax.xml.rpc.soap.SOAPFaultException;
030: import javax.xml.soap.Detail;
031: import javax.xml.soap.SOAPException;
032: import javax.xml.soap.SOAPFactory;
033:
034: /**
035: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
036: * @version $Revision: 9360 $
037: * @since 2.6
038: */
039: public class WSRPExceptionFactory {
040: private static Logger log = Logger
041: .getLogger(WSRPExceptionFactory.class);
042:
043: private static final String NS = "urn:oasis:names:tc:wsrp:v1:types";
044:
045: public static final String ACCESS_DENIED = "AccessDenied";
046: public static final String INCONSISTENT_PARAMETERS = "InconsistentParameters";
047: public static final String INVALID_REGISTRATION = "InvalidRegistration";
048: public static final String INVALID_COOKIE = "InvalidCookie";
049: public static final String INVALID_HANDLE = "InvalidHandle";
050: public static final String INVALID_SESSION = "InvalidSession";
051: public static final String INVALID_USER_CATEGORY = "InvalidUserCategory";
052: public static final String MISSING_PARAMETERS = "MissingParameters";
053: public static final String OPERATION_FAILED = "OperationFailed";
054: public static final String PORTLET_STATE_CHANGE_REQUIRED = "PortletStateChangeRequired";
055: public static final String UNSUPPORTED_LOCALE = "UnsupportedLocale";
056: public static final String UNSUPPORTED_MIME_TYPE = "UnsupportedMimeType";
057: public static final String UNSUPPORTED_MODE = "UnsupportedMode";
058: public static final String UNSUPPORTED_WINDOW_STATE = "UnsupportedWindowState";
059: private static final String NS_PREFIX = "wsrp";
060:
061: private WSRPExceptionFactory() {
062: }
063:
064: public static void throwMissingParametersFaultIfValueIsMissing(
065: Object valueToCheck, String valueName, String context)
066: throws SOAPFaultException {
067: if (valueToCheck == null) {
068: throw createSOAPFaultException(
069: MISSING_PARAMETERS,
070: "Missing required " + valueName
071: + (context != null ? " in " + context : ""),
072: null);
073: }
074: }
075:
076: public static void throwOperationFailedFaultIfValueIsMissing(
077: Object valueToCheck, String valueName)
078: throws SOAPFaultException {
079: if (valueToCheck == null) {
080: throw createSOAPFaultException(OPERATION_FAILED,
081: "Missing required " + valueName, null);
082: }
083: }
084:
085: public static SOAPFaultException throwSOAPFaultException(
086: String errorCode, String message, Throwable cause) {
087: throw createSOAPFaultException(errorCode, message, cause);
088: }
089:
090: private static SOAPFaultException createSOAPFaultException(
091: String errorCode, String message, Throwable cause) {
092: Detail detail = null;
093: if (cause != null) {
094: String causeMessage = cause.getLocalizedMessage();
095: if (causeMessage != null) {
096: message += ". " + causeMessage;
097: }
098: }
099:
100: try {
101: SOAPFactory soapFactory = SOAPFactory.newInstance();
102: detail = soapFactory.createDetail();
103:
104: // using a DetailEntry with the proper errorCode and NS will cause JBossWS to create a user created exception
105: // for ex: errorCode == InvalidRegistration => InvalidRegistrationFault on the client side...
106: // however, due to the crappy nature of faults in WSRP, this is highly uninformative as all extra information
107: // is not propagated once the user exception is created :(
108: detail.addDetailEntry(soapFactory.createName(errorCode,
109: NS_PREFIX, NS));
110: } catch (SOAPException e) {
111: log.debug("Couldn't create exception detail: ", e);
112: }
113:
114: String actor = null;
115: HttpServletRequest req = ServletAccess.getRequest();
116: if (req != null) {
117: actor = req.getScheme() + "://" + req.getServerName() + ":"
118: + req.getServerPort() + req.getContextPath()
119: + req.getServletPath();
120: }
121:
122: return new SOAPFaultException(new QName(NS, errorCode),
123: message, actor, detail);
124: }
125: }
|