View Javadoc

1   /*
2    * Copyright  1999-2004 The Apache Software Foundation.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.apache.sandesha.server;
18  
19  import org.apache.axis.AxisFault;
20  import org.apache.axis.MessageContext;
21  import org.apache.axis.components.logger.LogFactory;
22  import org.apache.axis.message.addressing.AddressingHeaders;
23  import org.apache.commons.logging.Log;
24  import org.apache.sandesha.Constants;
25  import org.apache.sandesha.IStorageManager;
26  import org.apache.sandesha.RMMessageContext;
27  import org.apache.sandesha.client.ClientStorageManager;
28  import org.apache.sandesha.ws.rm.RMHeaders;
29  import org.apache.sandesha.ws.rm.Sequence;
30  
31  import javax.xml.namespace.QName;
32  import javax.xml.soap.SOAPException;
33  
34  /***
35   * This will validate all the incoming messages receiving through the RMProvider.
36   *
37   * @author Jaliya Ekanayake
38   */
39  public final class MessageValidator {
40      private static IStorageManager storageMgr;
41      private static final Log log = LogFactory.getLog(MessageValidator.class.getName());
42  
43      public static void validate(RMMessageContext rmMsgContext, boolean client) throws AxisFault {
44  
45          if (client)
46              storageMgr = new ClientStorageManager();
47          else
48              storageMgr = new ServerStorageManager();
49  
50          MessageContext msgContext = rmMsgContext.getMsgContext();
51          try {
52              AddressingHeaders aHeaders = (AddressingHeaders) rmMsgContext.getMsgContext()
53                      .getProperty(org.apache.axis.message.addressing.Constants.ENV_ADDRESSING_REQUEST_HEADERS);
54              if (aHeaders == null)
55                  throw new AxisFault(new QName(Constants.FaultCodes.IN_CORRECT_MESSAGE),
56                          Constants.FaultMessages.NO_ADDRESSING_HEADERS, null, null);
57              AddressingHeaders addrHeaders = new AddressingHeaders(msgContext.getRequestMessage().getSOAPEnvelope());
58              validateAddrHeaders(addrHeaders);
59              rmMsgContext.setAddressingHeaders(addrHeaders);
60  
61              RMHeaders rmHeaders = new RMHeaders();
62              rmHeaders.fromSOAPEnvelope(msgContext.getRequestMessage().getSOAPEnvelope());
63              validateRMHeaders(rmHeaders);
64              rmMsgContext.setRMHeaders(rmHeaders);
65  
66              validateForFaults(rmMsgContext);
67          } catch (SOAPException e) {
68              log.error(e);
69              throw new AxisFault(new QName(Constants.FaultCodes.IN_CORRECT_MESSAGE), e.getMessage(),
70                      null, null);
71          } catch (Exception e) {
72              log.error(e);
73              throw new AxisFault(new QName(Constants.FaultCodes.IN_CORRECT_MESSAGE), e.getMessage(),
74                      null, null);
75  
76          }
77      }
78  
79  
80      private static void validateRMHeaders(RMHeaders rmHeaders) throws AxisFault {
81          if (rmHeaders.getSequence() != null)
82              return;
83          if (rmHeaders.getAckRequest() != null)
84              return;
85          if (rmHeaders.getSequenceAcknowledgement() != null)
86              return;
87          if (rmHeaders.getTerminateSequence() != null)
88              return;
89          if (rmHeaders.getCreateSequence() != null)
90              return;
91          if (rmHeaders.getCreateSequenceResponse() != null)
92              return;
93  
94          throw new AxisFault(new QName(Constants.FaultCodes.IN_CORRECT_MESSAGE),
95                  Constants.FaultMessages.NO_RM_HEADES, null, null);
96      }
97  
98      private static void validateForFaults(RMMessageContext rmMsgCtx) throws AxisFault {
99          RMHeaders rmHeaders = rmMsgCtx.getRMHeaders();
100         Sequence sequence = rmHeaders.getSequence();
101 
102         if (sequence != null) {
103             String seqId = sequence.getIdentifier().getIdentifier();
104             if (!storageMgr.isRequestedSeqPresent(seqId)) {
105                 throw new AxisFault(new QName(Constants.FaultCodes.WSRM_FAULT_UNKNOWN_SEQUENCE),
106                         Constants.FaultMessages.UNKNOWN_SEQUENCE, null, null);
107             }
108             if (sequence.getMessageNumber() != null) {
109                 long msgNo = sequence.getMessageNumber().getMessageNumber();
110                 if (storageMgr.hasLastIncomingMsgReceived(sequence.getIdentifier().getIdentifier())) {
111                     long lastMsg = storageMgr.getLastIncomingMsgNo(seqId);
112                     if (msgNo > lastMsg)
113                         throw new AxisFault(new QName(Constants.FaultCodes.WSRM_FAULR_LAST_MSG_NO_EXCEEDED),
114                                 Constants.FaultMessages.LAST_MSG_NO_EXCEEDED, null, null);
115                 }
116             }
117         }
118     }
119 
120 
121     private static void validateAddrHeaders(AddressingHeaders addrHeaders) throws AxisFault {
122         if (addrHeaders == null)
123             throw new AxisFault(new QName(Constants.FaultCodes.IN_CORRECT_MESSAGE),
124                     Constants.FaultMessages.NO_ADDRESSING_HEADERS, null, null);
125         if (addrHeaders.getTo() == null)
126             throw new AxisFault(new QName(Constants.FaultCodes.IN_CORRECT_MESSAGE),
127                     Constants.FaultMessages.NO_TO, null, null);
128         if (addrHeaders.getAction() == null)
129             throw new AxisFault(new QName(Constants.FaultCodes.IN_CORRECT_MESSAGE),
130                     Constants.FaultMessages.NO_TO, null, null);
131     }
132 }