001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, 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.producer;
023:
024: import org.jboss.portal.common.util.ParameterValidation;
025: import org.jboss.portal.registration.Consumer;
026: import org.jboss.portal.registration.ConsumerCapabilities;
027: import org.jboss.portal.registration.NoSuchRegistrationException;
028: import org.jboss.portal.registration.Registration;
029: import org.jboss.portal.registration.RegistrationException;
030: import org.jboss.portal.registration.RegistrationStatus;
031: import org.jboss.portal.registration.RegistrationUtils;
032: import org.jboss.portal.wsrp.WSRPExceptionFactory;
033: import org.jboss.portal.wsrp.WSRPTypeFactory;
034: import org.jboss.portal.wsrp.WSRPUtils;
035: import org.jboss.portal.wsrp.core.InvalidRegistrationFault;
036: import org.jboss.portal.wsrp.core.MissingParametersFault;
037: import org.jboss.portal.wsrp.core.ModifyRegistration;
038: import org.jboss.portal.wsrp.core.OperationFailedFault;
039: import org.jboss.portal.wsrp.core.Property;
040: import org.jboss.portal.wsrp.core.RegistrationContext;
041: import org.jboss.portal.wsrp.core.RegistrationData;
042: import org.jboss.portal.wsrp.core.RegistrationState;
043: import org.jboss.portal.wsrp.core.ReturnAny;
044: import org.jboss.portal.wsrp.core.WSRP_v1_Registration_PortType;
045: import org.jboss.portal.wsrp.producer.config.ProducerRegistrationRequirements;
046:
047: import javax.xml.namespace.QName;
048: import java.rmi.RemoteException;
049: import java.util.ArrayList;
050: import java.util.Arrays;
051: import java.util.Collections;
052: import java.util.HashMap;
053: import java.util.List;
054: import java.util.Map;
055:
056: /**
057: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
058: * @version $Revision: 9179 $
059: * @since 2.4
060: */
061: class RegistrationHandler extends ServiceHandler implements
062: WSRP_v1_Registration_PortType {
063: RegistrationHandler(WSRPProducerImpl producer) {
064: super (producer);
065: }
066:
067: public RegistrationContext register(
068: RegistrationData registrationData)
069: throws MissingParametersFault, OperationFailedFault,
070: RemoteException {
071: if (producer.getProducerRegistrationRequirements()
072: .isRegistrationRequired()) {
073: WSRPExceptionFactory
074: .throwOperationFailedFaultIfValueIsMissing(
075: registrationData, "RegistrationData");
076: String consumerName = registrationData.getConsumerName();
077: WSRPExceptionFactory
078: .throwMissingParametersFaultIfValueIsMissing(
079: consumerName, "consumer name",
080: "RegistrationData");
081:
082: String consumerAgent = registrationData.getConsumerAgent();
083: WSRPExceptionFactory
084: .throwMissingParametersFaultIfValueIsMissing(
085: consumerAgent, "consumer agent",
086: "RegistrationData");
087:
088: Registration registration;
089: try {
090: log.debug("Attempting to register consumer named '"
091: + consumerName + "', agent '" + consumerAgent
092: + "'.");
093:
094: // check that the consumer agent is valid before trying to register
095: RegistrationUtils.validateConsumerAgent(consumerAgent);
096:
097: registration = producer
098: .getRegistrationManager()
099: .addRegistrationTo(
100: consumerName,
101: createRegistrationProperties(registrationData),
102: true);
103: updateRegistrationInformation(registration,
104: registrationData);
105: } catch (Exception e) {
106: log.debug(e);
107: throw WSRPExceptionFactory.throwSOAPFaultException(
108: WSRPExceptionFactory.OPERATION_FAILED,
109: "Could not register consumer named '"
110: + consumerName + "'", e);
111: }
112:
113: RegistrationContext registrationContext = WSRPTypeFactory
114: .createRegistrationContext(registration.getId());
115: log.debug("Registration completed without error.");
116: return registrationContext;
117: }
118:
119: throw WSRPExceptionFactory
120: .throwSOAPFaultException(
121: WSRPExceptionFactory.OPERATION_FAILED,
122: "Registration shouldn't be attempted if registration is not required",
123: null);
124: }
125:
126: private void updateRegistrationInformation(
127: Registration registration, RegistrationData registrationData) {
128: registration.setStatus(RegistrationStatus.VALID);
129: Consumer consumer = registration.getConsumer();
130: consumer.setConsumerAgent(registrationData.getConsumerAgent());
131: ConsumerCapabilities capabilities = consumer.getCapabilities();
132:
133: String[] modeStrings = registrationData.getConsumerModes();
134: if (modeStrings != null && modeStrings.length > 0) {
135: List modes = new ArrayList(modeStrings.length);
136: for (int i = 0; i < modeStrings.length; i++) {
137: modes
138: .add(WSRPUtils
139: .getJSR168PortletModeFromWSRPName(modeStrings[i]));
140: }
141: capabilities.setSupportedModes(modes);
142: }
143:
144: String[] wsStrings = registrationData.getConsumerWindowStates();
145: if (wsStrings != null && wsStrings.length > 0) {
146: List windowStates = new ArrayList(wsStrings.length);
147: for (int i = 0; i < wsStrings.length; i++) {
148: windowStates
149: .add(WSRPUtils
150: .getJSR168WindowStateFromWSRPName(wsStrings[i]));
151: }
152: capabilities.setSupportedWindowStates(windowStates);
153: }
154:
155: //
156: String[] customUserProfileData = registrationData
157: .getCustomUserProfileData();
158: String[] consumerUserScopes = registrationData
159: .getConsumerUserScopes();
160: boolean isMethodGetSupported = registrationData
161: .isMethodGetSupported();
162:
163: //
164: capabilities.setSupportedUserProfileData(getListFromArray(
165: customUserProfileData, false));
166: capabilities.setSupportedUserScopes(getListFromArray(
167: consumerUserScopes, false));
168: capabilities.setSupportsGetMethod(isMethodGetSupported);
169: }
170:
171: public ReturnAny deregister(RegistrationContext deregister)
172: throws OperationFailedFault, InvalidRegistrationFault,
173: RemoteException {
174: if (producer.getProducerRegistrationRequirements()
175: .isRegistrationRequired()) {
176: WSRPExceptionFactory
177: .throwOperationFailedFaultIfValueIsMissing(
178: deregister, "RegistrationContext");
179:
180: String registrationHandle = deregister
181: .getRegistrationHandle();
182: if (ParameterValidation.isNullOrEmpty(registrationHandle)) {
183: throwInvalidRegistrationFault("Null or empty registration handle");
184: }
185:
186: log
187: .debug("Attempting to deregister registration with handle '"
188: + registrationHandle + "'");
189:
190: try {
191: producer.getRegistrationManager().removeRegistration(
192: registrationHandle);
193: } catch (NoSuchRegistrationException e) {
194: log.debug(e);
195: throwInvalidRegistrationFault(e.getLocalizedMessage());
196: } catch (RegistrationException e) {
197: log.debug(e);
198: throw WSRPExceptionFactory.throwSOAPFaultException(
199: WSRPExceptionFactory.OPERATION_FAILED,
200: "Could not deregister registration with handle '"
201: + registrationHandle + "'", e);
202: }
203:
204: return new ReturnAny();
205: }
206:
207: throw WSRPExceptionFactory
208: .throwSOAPFaultException(
209: WSRPExceptionFactory.OPERATION_FAILED,
210: "Deregistration shouldn't be attempted if registration is not required",
211: null);
212: }
213:
214: public RegistrationState modifyRegistration(
215: ModifyRegistration modifyRegistration)
216: throws MissingParametersFault, OperationFailedFault,
217: InvalidRegistrationFault, RemoteException {
218: if (producer.getProducerRegistrationRequirements()
219: .isRegistrationRequired()) {
220: WSRPExceptionFactory
221: .throwOperationFailedFaultIfValueIsMissing(
222: modifyRegistration, "ModifyRegistration");
223:
224: RegistrationContext registrationContext = modifyRegistration
225: .getRegistrationContext();
226: WSRPExceptionFactory
227: .throwMissingParametersFaultIfValueIsMissing(
228: registrationContext, "RegistrationContext",
229: "ModifyRegistration");
230: String registrationHandle = registrationContext
231: .getRegistrationHandle();
232: if (ParameterValidation.isNullOrEmpty(registrationHandle)) {
233: throwInvalidRegistrationFault("Null or empty registration handle");
234: }
235:
236: RegistrationData registrationData = modifyRegistration
237: .getRegistrationData();
238: WSRPExceptionFactory
239: .throwMissingParametersFaultIfValueIsMissing(
240: registrationData, "RegistrationData",
241: "ModifyRegistration");
242:
243: String consumerName = registrationData.getConsumerName();
244: WSRPExceptionFactory
245: .throwMissingParametersFaultIfValueIsMissing(
246: consumerName, "consumer name",
247: "RegistrationData");
248:
249: String consumerAgent = registrationData.getConsumerAgent();
250: WSRPExceptionFactory
251: .throwMissingParametersFaultIfValueIsMissing(
252: consumerAgent, "consumer agent",
253: "RegistrationData");
254:
255: log.debug("Attempting to modify registration with handle '"
256: + registrationHandle + "'");
257: try {
258: Registration registration = producer
259: .getRegistrationManager().getRegistration(
260: registrationHandle);
261:
262: Map properties = createRegistrationProperties(registrationData);
263:
264: // check that the given registration properties are acceptable according to expectations and policy
265: ProducerRegistrationRequirements req = producer
266: .getProducerRegistrationRequirements();
267: req.getPolicy().validateRegistrationDataFor(properties,
268: consumerName);
269:
270: registration.updateProperties(properties);
271: updateRegistrationInformation(registration,
272: registrationData);
273: } catch (NoSuchRegistrationException e) {
274: log.debug(e);
275: throwInvalidRegistrationFault(e.getLocalizedMessage());
276: } catch (RegistrationException e) {
277: log.debug(e);
278: throw WSRPExceptionFactory.throwSOAPFaultException(
279: WSRPExceptionFactory.OPERATION_FAILED,
280: "Could not modify registration with handle '"
281: + registrationHandle + "'", e);
282: }
283:
284: log.debug("Modified registration with handle '"
285: + registrationHandle + "'");
286: return null;
287: }
288:
289: throw WSRPExceptionFactory
290: .throwSOAPFaultException(
291: WSRPExceptionFactory.OPERATION_FAILED,
292: "Modifying a registration shouldn't be attempted if registration is not required",
293: null);
294: }
295:
296: /**
297: * @param reg
298: * @param throwExceptionIfExistsAndNotValid
299: *
300: * @return
301: * @since 2.6.2
302: */
303: boolean isRegistrationValid(Registration reg,
304: boolean throwExceptionIfExistsAndNotValid) {
305: if (reg == null) {
306: if (producer.getProducerRegistrationRequirements()
307: .isRegistrationRequired()) {
308: log
309: .debug("Registration is required yet no RegistrationContext was provided: registration invalid!");
310: return false;
311: }
312:
313: log
314: .debug("Registration not required, no registration: registration valid!");
315: return true;
316: } else {
317: boolean isValid = RegistrationStatus.VALID.equals(reg
318: .getStatus());
319: boolean isPending = RegistrationStatus.PENDING.equals(reg
320: .getStatus());
321: log.debug("Registration required: registration is "
322: + (isValid ? "valid!" : (isPending ? "pending!"
323: : "invalid!")));
324:
325: if (throwExceptionIfExistsAndNotValid) {
326: if (isPending) {
327: throwOperationFailedFault(
328: "Registration with handle '"
329: + reg.getRegistrationHandle()
330: + "' is pending. Consumer needs to call modifyRegistration().",
331: null);
332: } else {
333: if (!isValid) {
334: throwInvalidRegistrationFault("Registration with handle '"
335: + reg.getRegistrationHandle()
336: + "' is not valid!");
337: }
338: }
339: }
340:
341: return isValid;
342: }
343: }
344:
345: /**
346: * @param registrationContext
347: * @return
348: * @since 2.6.2
349: */
350: Registration getRegistrationFrom(
351: RegistrationContext registrationContext) {
352: if (producer.getProducerRegistrationRequirements()
353: .isRegistrationRequired()) {
354: if (registrationContext == null) {
355: return null;
356: }
357:
358: String regHandle = registrationContext
359: .getRegistrationHandle();
360: WSRPExceptionFactory
361: .throwOperationFailedFaultIfValueIsMissing(
362: regHandle, "registration handle");
363:
364: try {
365: Registration registration = producer
366: .getRegistrationManager().getRegistration(
367: regHandle);
368: if (registration == null) {
369: throwInvalidRegistrationFault("provided registration handle '"
370: + regHandle
371: + "' is not registered with this producer");
372: }
373: return registration;
374: } catch (RegistrationException e) {
375: throwOperationFailedFault(
376: "Failed to retrieve registration information associated with handle "
377: + regHandle, e);
378: return null;
379: }
380: } else {
381: if (registrationContext != null) {
382: throwInvalidRegistrationFault("no registration necessary yet one was provided!");
383: }
384: return null;
385: }
386: }
387:
388: private void throwOperationFailedFault(String message,
389: RegistrationException e) {
390: throw WSRPExceptionFactory.throwSOAPFaultException(
391: WSRPExceptionFactory.OPERATION_FAILED, message, e);
392: }
393:
394: boolean throwInvalidRegistrationFault(String message) {
395: throw WSRPExceptionFactory.throwSOAPFaultException(
396: WSRPExceptionFactory.INVALID_REGISTRATION,
397: "Invalid registration: " + message, null);
398: }
399:
400: private Map createRegistrationProperties(
401: RegistrationData registrationData) {
402: Property[] regProperties = registrationData
403: .getRegistrationProperties();
404: Map properties;
405: if (regProperties != null) {
406: properties = new HashMap(regProperties.length);
407: for (int i = 0; i < regProperties.length; i++) {
408: Property property = regProperties[i];
409: // todo: should be more detailed here... use the language, allow other value types...
410: properties.put(new QName(property.getName()), property
411: .getStringValue());
412: }
413: } else {
414: properties = Collections.EMPTY_MAP;
415: }
416:
417: return properties;
418: }
419:
420: private List getListFromArray(String[] array,
421: boolean useEmptyForNull) {
422: if (array == null) {
423: return useEmptyForNull ? Collections.EMPTY_LIST : null;
424: }
425: return Arrays.asList(array);
426: }
427: }
|