1
16
17 package org.apache.wsrp4j.exception;
18
19 import java.io.InputStream;
20 import java.util.Enumeration;
21 import java.util.HashMap;
22 import java.util.Properties;
23 import java.util.StringTokenizer;
24
25 import javax.xml.namespace.QName;
26
27 import oasis.names.tc.wsrp.v1.types.AccessDeniedFault;
28 import oasis.names.tc.wsrp.v1.types.Fault;
29 import oasis.names.tc.wsrp.v1.types.InconsistentParametersFault;
30 import oasis.names.tc.wsrp.v1.types.InvalidCookieFault;
31 import oasis.names.tc.wsrp.v1.types.InvalidHandleFault;
32 import oasis.names.tc.wsrp.v1.types.InvalidRegistrationFault;
33 import oasis.names.tc.wsrp.v1.types.InvalidSessionFault;
34 import oasis.names.tc.wsrp.v1.types.InvalidUserCategoryFault;
35 import oasis.names.tc.wsrp.v1.types.MissingParametersFault;
36 import oasis.names.tc.wsrp.v1.types.OperationFailedFault;
37 import oasis.names.tc.wsrp.v1.types.PortletStateChangeRequiredFault;
38 import oasis.names.tc.wsrp.v1.types.UnsupportedLocaleFault;
39 import oasis.names.tc.wsrp.v1.types.UnsupportedMimeTypeFault;
40 import oasis.names.tc.wsrp.v1.types.UnsupportedModeFault;
41 import oasis.names.tc.wsrp.v1.types.UnsupportedWindowStateFault;
42
43 import org.apache.axis.AxisFault;
44 import org.apache.axis.utils.XMLUtils;
45 import org.apache.wsrp4j.log.Logger;
46 import org.apache.wsrp4j.util.Constants;
47 import org.w3c.dom.Document;
48 import org.w3c.dom.Element;
49
50
57 public class WSRPXHelper {
58
59 private static final String EMPTY_STRING = "";
60
61 private static String EXCEPTION_MAPPING_FILE = "wsrp-exception-mapping.properties";
63
64 private static HashMap exceptionMapping = null;
66
67
74 public static void throwX(int errorCode) throws WSRPException {
75 WSRPException e = getException(errorCode, null);
76 throw e;
77 }
78
79
87 public static void throwX(int errorCode, Throwable t) throws WSRPException {
88 WSRPException e = getException(errorCode, t);
89 throw e;
90 }
91
92
106 public static void throwX(Logger logger, int logLevel, String method,
107 int errorCode) throws WSRPException {
108 WSRPException e = getException(errorCode, null);
109 if (logger != null) {
111 logger.text(logLevel, method, e, EMPTY_STRING);
112 }
113 throw e;
114 }
115
116
131 public static void throwX(Logger logger, int logLevel, String method,
132 int errorCode, Throwable t) throws WSRPException {
133 WSRPException e = getException(errorCode, t);
134 if (logger != null) {
136 if (t instanceof WSRPException) {
137 logger.text(logLevel, method, e.getMessage());
139 }
140 else {
141 logger.text(logLevel, method, e, EMPTY_STRING);
143 }
144 }
145 throw e;
146 }
147
148
155 public static void handleWSRPException(WSRPException exception)
156 throws java.rmi.RemoteException {
157
158 if (exceptionMapping == null) {
159 loadExceptionMapping();
160 }
161
162 AxisFault fault = null;
163 int errorCode = ((Integer) exceptionMapping.get(String
164 .valueOf(exception.getErrorCode()))).intValue();
165
166 if (errorCode == ErrorCodes.ACCESS_DENIED) {
167
168 fault = new AxisFault();
169 QName qname = new QName(Fault.getTypeDesc().getXmlType()
170 .getNamespaceURI(), Constants.ACCESS_DENIED_FAULT);
171 fault.setFaultCode(qname);
172 fault.setFaultString(exception.getMessage());
173
174 try {
175
176 Document doc = XMLUtils.newDocument();
177 Element element = doc.createElementNS(AccessDeniedFault
178 .getTypeDesc().getXmlType().getNamespaceURI(),
179 Constants.ACCESS_DENIED_FAULT);
180 fault.clearFaultDetails();
181 fault.setFaultDetail(new Element[] { element });
182
183 }
184 catch (Exception e) {
185
186 }
188
189 }
190 else if (errorCode == ErrorCodes.INCONSISTENT_PARAMETERS) {
191
192 fault = new AxisFault();
193 QName qname = new QName(Fault.getTypeDesc().getXmlType()
194 .getNamespaceURI(), Constants.INCONSISTENT_PARAMETERS_FAULT);
195 fault.setFaultCode(qname);
196 fault.setFaultString(exception.getMessage());
197
198 try {
199
200 Document doc = XMLUtils.newDocument();
201 Element element = doc.createElementNS(
202 InconsistentParametersFault.getTypeDesc().getXmlType()
203 .getNamespaceURI(),
204 Constants.INCONSISTENT_PARAMETERS_FAULT);
205 fault.clearFaultDetails();
206 fault.setFaultDetail(new Element[] { element });
207
208 }
209 catch (Exception e) {
210
211 }
213
214 }
215 else if (errorCode == ErrorCodes.INVALID_REGISTRATION) {
216
217 fault = new AxisFault();
218 QName qname = new QName(Fault.getTypeDesc().getXmlType()
219 .getNamespaceURI(), Constants.INVALID_REGISTRATION_FAULT);
220 fault.setFaultCode(qname);
221 fault.setFaultString(exception.getMessage());
222
223 try {
224
225 Document doc = XMLUtils.newDocument();
226 Element element = doc.createElementNS(InvalidRegistrationFault
227 .getTypeDesc().getXmlType().getNamespaceURI(),
228 Constants.INVALID_REGISTRATION_FAULT);
229 fault.clearFaultDetails();
230 fault.setFaultDetail(new Element[] { element });
231
232 }
233 catch (Exception e) {
234
235 }
237
238 }
239 else if (errorCode == ErrorCodes.INVALID_COOKIE) {
240
241 fault = new AxisFault();
242 QName qname = new QName(Fault.getTypeDesc().getXmlType()
243 .getNamespaceURI(), Constants.INVALID_COOKIE_FAULT);
244 fault.setFaultCode(qname);
245 fault.setFaultString(exception.getMessage());
246
247 try {
248
249 Document doc = XMLUtils.newDocument();
250 Element element = doc.createElementNS(InvalidCookieFault
251 .getTypeDesc().getXmlType().getNamespaceURI(),
252 Constants.INVALID_COOKIE_FAULT);
253 fault.clearFaultDetails();
254 fault.setFaultDetail(new Element[] { element });
255
256 }
257 catch (Exception e) {
258
259 }
261
262 }
263 else if (errorCode == ErrorCodes.INVALID_HANDLE) {
264 fault = new AxisFault();
265 QName qname = new QName(Fault.getTypeDesc().getXmlType()
266 .getNamespaceURI(), Constants.INVALID_HANDLE_FAULT);
267 fault.setFaultCode(qname);
268 fault.setFaultString(exception.getMessage());
269
270 try {
271
272 Document doc = XMLUtils.newDocument();
273 Element element = doc.createElementNS(InvalidHandleFault
274 .getTypeDesc().getXmlType().getNamespaceURI(),
275 Constants.INVALID_HANDLE_FAULT);
276 fault.clearFaultDetails();
277 fault.setFaultDetail(new Element[] { element });
278
279 }
280 catch (Exception e) {
281
282 }
284
285 }
286 else if (errorCode == ErrorCodes.INVALID_SESSION) {
287 fault = new AxisFault();
288 QName qname = new QName(Fault.getTypeDesc().getXmlType()
289 .getNamespaceURI(), Constants.INVALID_SESSION_FAULT);
290 fault.setFaultCode(qname);
291 fault.setFaultString(exception.getMessage());
292
293 try {
294
295 Document doc = XMLUtils.newDocument();
296 Element element = doc.createElementNS(InvalidSessionFault
297 .getTypeDesc().getXmlType().getNamespaceURI(),
298 Constants.INVALID_SESSION_FAULT);
299 fault.clearFaultDetails();
300 fault.setFaultDetail(new Element[] { element });
301
302 }
303 catch (Exception e) {
304
305 }
307
308 }
309 else if (errorCode == ErrorCodes.INVALID_USER_CATEGORY) {
310 fault = new AxisFault();
311 QName qname = new QName(Fault.getTypeDesc().getXmlType()
312 .getNamespaceURI(), Constants.INVALID_USER_CATEGORY_FAULT);
313 fault.setFaultCode(qname);
314 fault.setFaultString(exception.getMessage());
315
316 try {
317
318 Document doc = XMLUtils.newDocument();
319 Element element = doc.createElementNS(InvalidUserCategoryFault
320 .getTypeDesc().getXmlType().getNamespaceURI(),
321 Constants.INVALID_USER_CATEGORY_FAULT);
322 fault.clearFaultDetails();
323 fault.setFaultDetail(new Element[] { element });
324
325 }
326 catch (Exception e) {
327
328 }
330
331 }
332 else if (errorCode == ErrorCodes.MISSING_PARAMETERS) {
333
334 fault = new AxisFault();
335 QName qname = new QName(Fault.getTypeDesc().getXmlType()
336 .getNamespaceURI(), Constants.MISSING_PARAMETERS_FAULT);
337 fault.setFaultCode(qname);
338 fault.setFaultString(exception.getMessage());
339
340 try {
341
342 Document doc = XMLUtils.newDocument();
343 Element element = doc.createElementNS(MissingParametersFault
344 .getTypeDesc().getXmlType().getNamespaceURI(),
345 Constants.MISSING_PARAMETERS_FAULT);
346 fault.clearFaultDetails();
347 fault.setFaultDetail(new Element[] { element });
348
349 }
350 catch (Exception e) {
351
352 }
354
355 }
356 else if (errorCode == ErrorCodes.OPERATION_FAILED) {
357
358 fault = new AxisFault();
359 QName qname = new QName(Fault.getTypeDesc().getXmlType()
360 .getNamespaceURI(), Constants.OPERATION_FAILED_FAULT);
361 fault.setFaultCode(qname);
362 fault.setFaultString(exception.getMessage());
363
364 try {
365
366 Document doc = XMLUtils.newDocument();
367 Element element = doc.createElementNS(OperationFailedFault
368 .getTypeDesc().getXmlType().getNamespaceURI(),
369 Constants.OPERATION_FAILED_FAULT);
370 fault.clearFaultDetails();
371 fault.setFaultDetail(new Element[] { element });
372
373 }
374 catch (Exception e) {
375
376 }
378
379 }
380 else if (errorCode == ErrorCodes.PORTLET_STATE_CHANGE_REQUIRED) {
381 fault = new AxisFault();
382 QName qname = new QName(Fault.getTypeDesc().getXmlType()
383 .getNamespaceURI(),
384 Constants.PORTLET_STATE_CHANGE_REQUIRED_FAULT);
385 fault.setFaultCode(qname);
386 fault.setFaultString(exception.getMessage());
387
388 try {
389
390 Document doc = XMLUtils.newDocument();
391 Element element = doc.createElementNS(
392 PortletStateChangeRequiredFault.getTypeDesc()
393 .getXmlType().getNamespaceURI(),
394 Constants.PORTLET_STATE_CHANGE_REQUIRED_FAULT);
395 fault.clearFaultDetails();
396 fault.setFaultDetail(new Element[] { element });
397
398 }
399 catch (Exception e) {
400
401 }
403
404 }
405 else if (errorCode == ErrorCodes.UNSUPPORTED_LOCALE) {
406
407 fault = new AxisFault();
408 QName qname = new QName(Fault.getTypeDesc().getXmlType()
409 .getNamespaceURI(), Constants.UNSUPPORTED_LOCALE_FAULT);
410 fault.setFaultCode(qname);
411 fault.setFaultString(exception.getMessage());
412
413 try {
414
415 Document doc = XMLUtils.newDocument();
416 Element element = doc.createElementNS(UnsupportedLocaleFault
417 .getTypeDesc().getXmlType().getNamespaceURI(),
418 Constants.UNSUPPORTED_LOCALE_FAULT);
419 fault.clearFaultDetails();
420 fault.setFaultDetail(new Element[] { element });
421
422 }
423 catch (Exception e) {
424
425 }
427
428 }
429 else if (errorCode == ErrorCodes.UNSUPPORTED_MIME_TYPE) {
430
431 fault = new AxisFault();
432 QName qname = new QName(Fault.getTypeDesc().getXmlType()
433 .getNamespaceURI(), Constants.UNSUPPORTED_MIME_TYPE_FAULT);
434 fault.setFaultCode(qname);
435 fault.setFaultString(exception.getMessage());
436
437 try {
438
439 Document doc = XMLUtils.newDocument();
440 Element element = doc.createElementNS(UnsupportedMimeTypeFault
441 .getTypeDesc().getXmlType().getNamespaceURI(),
442 Constants.UNSUPPORTED_MIME_TYPE_FAULT);
443 fault.clearFaultDetails();
444 fault.setFaultDetail(new Element[] { element });
445
446 }
447 catch (Exception e) {
448
449 }
451
452 }
453 else if (errorCode == ErrorCodes.UNSUPPORTED_MODE) {
454
455 fault = new AxisFault();
456 QName qname = new QName(Fault.getTypeDesc().getXmlType()
457 .getNamespaceURI(), Constants.UNSUPPORTED_MODE_FAULT);
458 fault.setFaultCode(qname);
459 fault.setFaultString(exception.getMessage());
460
461 try {
462
463 Document doc = XMLUtils.newDocument();
464 Element element = doc.createElementNS(UnsupportedModeFault
465 .getTypeDesc().getXmlType().getNamespaceURI(),
466 Constants.UNSUPPORTED_MODE_FAULT);
467 fault.clearFaultDetails();
468 fault.setFaultDetail(new Element[] { element });
469
470 }
471 catch (Exception e) {
472
473 }
475
476 }
477 else if (errorCode == ErrorCodes.UNSUPPORTED_WINDOW_STATE) {
478
479 fault = new AxisFault();
480 QName qname = new QName(Fault.getTypeDesc().getXmlType()
481 .getNamespaceURI(),
482 Constants.UNSUPPORTED_WINDOW_STATE_FAULT);
483 fault.setFaultCode(qname);
484 fault.setFaultString(exception.getMessage());
485
486 try {
487
488 Document doc = XMLUtils.newDocument();
489 Element element = doc.createElementNS(
490 UnsupportedWindowStateFault.getTypeDesc().getXmlType()
491 .getNamespaceURI(),
492 Constants.UNSUPPORTED_WINDOW_STATE_FAULT);
493 fault.clearFaultDetails();
494 fault.setFaultDetail(new Element[] { element });
495
496 }
497 catch (Exception e) {
498
499 }
501
502 }
503 else {
504
505 throw new java.rmi.RemoteException();
506
507 }
508
509 throw fault;
510
511 }
512
513 private static void loadExceptionMapping() {
514 try {
515 exceptionMapping = new HashMap();
516
517 InputStream in = WSRPXHelper.class.getClassLoader()
519 .getResourceAsStream(EXCEPTION_MAPPING_FILE);
520 Properties props = new Properties();
521 props.load(in);
522
523 Enumeration keys = props.propertyNames();
525 StringTokenizer tokenizer = null;
526
527 while (keys.hasMoreElements()) {
528
529 String value = null;
530 String currentKey = (String) keys.nextElement();
531 exceptionMapping.put(currentKey, new Integer(currentKey));
532
533 value = props.getProperty(currentKey);
534 if (value != null && value.length() > 0) {
535 tokenizer = new StringTokenizer(value, ",");
536 String token = null;
537
538 while (tokenizer.hasMoreTokens()) {
539 token = tokenizer.nextToken();
540
541 if (token != null) {
542 exceptionMapping
543 .put(token, new Integer(currentKey));
544 }
545
546 }
547
548 }
549 }
550
551 }
552 catch (Exception e) {
553
554 e.printStackTrace();
556
557 }
558
559 }
560
561
570 public static void handleWSRPFault(Logger logger,
571 java.rmi.RemoteException wsrpFault) throws WSRPException {
572
573 if (wsrpFault instanceof AccessDeniedFault) {
574
575 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1100,
576 wsrpFault);
577
578 }
579 else if (wsrpFault instanceof InconsistentParametersFault) {
580
581 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1101,
582 wsrpFault);
583
584 }
585 else if (wsrpFault instanceof InvalidRegistrationFault) {
586
587 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1102,
588 wsrpFault);
589
590 }
591 else if (wsrpFault instanceof InvalidCookieFault) {
592
593 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1103,
594 wsrpFault);
595
596 }
597 else if (wsrpFault instanceof InvalidHandleFault) {
598
599 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1104,
600 wsrpFault);
601
602 }
603 else if (wsrpFault instanceof InvalidSessionFault) {
604
605 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1105,
606 wsrpFault);
607
608 }
609 else if (wsrpFault instanceof InvalidUserCategoryFault) {
610
611 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1106,
612 wsrpFault);
613
614 }
615 else if (wsrpFault instanceof MissingParametersFault) {
616
617 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1107,
618 wsrpFault);
619
620 }
621 else if (wsrpFault instanceof OperationFailedFault) {
622
623 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1108,
624 wsrpFault);
625
626 }
627 else if (wsrpFault instanceof PortletStateChangeRequiredFault) {
628
629 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1109,
630 wsrpFault);
631
632 }
633 else if (wsrpFault instanceof UnsupportedLocaleFault) {
634
635 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1110,
636 wsrpFault);
637
638 }
639 else if (wsrpFault instanceof UnsupportedMimeTypeFault) {
640
641 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1111,
642 wsrpFault);
643
644 }
645 else if (wsrpFault instanceof UnsupportedModeFault) {
646
647 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1112,
648 wsrpFault);
649
650 }
651 else if (wsrpFault instanceof UnsupportedWindowStateFault) {
652
653 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1113,
654 wsrpFault);
655
656 }
657 else {
658
659 WSRPXHelper.throwX(logger, Logger.ERROR, EMPTY_STRING, 1116,
661 wsrpFault);
662
663 }
664
665 }
666
667
672 protected static WSRPException getException(int errorCode, Throwable t) {
673 WSRPException e = null;
674 if (errorCode >= Messages.PRODUCER_LOWER_BOUND
675 && errorCode <= Messages.PRODUCER_UPPER_BOUND) {
676 if (t == null) {
677
678 e = new WSRPException(errorCode);
679 }
680 else {
681 e = new WSRPException(errorCode, t);
682 }
683 e.setProducerExceptionRange();
684 }
685 else if (errorCode >= Messages.PROVIDER_LOWER_BOUND
686 && errorCode <= Messages.PROVIDER_UPPER_BOUND) {
687 if (t == null) {
688
689 e = new WSRPException(errorCode);
690 }
691 else {
692 e = new WSRPException(errorCode, t);
693 }
694 e.setProviderExceptionRange();
695 }
696 else if (errorCode >= Messages.CONSUMER_LOWER_BOUND
697 && errorCode <= Messages.CONSUMER_UPPER_BOUND) {
698 if (t == null) {
699
700 e = new WSRPException(errorCode);
701 }
702 else {
703 e = new WSRPException(errorCode, t);
704 }
705 e.setConsumerExceptionRange();
706 }
707 else if (errorCode >= Messages.COMMON_LOWER_BOUND
708 && errorCode <= Messages.COMMON_UPPER_BOUND) {
709 if (t == null) {
710
711 e = new WSRPException(errorCode);
712 }
713 else {
714 e = new WSRPException(errorCode, t);
715 }
716 e.setCommonExceptionRange();
717 }
718 else {
719 if (t == null) {
720
721 e = new WSRPException(errorCode);
722 }
723 else {
724 e = new WSRPException(errorCode, t);
725 }
726 }
727 return e;
728 }
729 }