KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > handlers > soap > MustUnderstandChecker


1 /*
2  * Copyright 2001-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.axis.handlers.soap;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Constants;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.Message;
23 import org.apache.axis.description.OperationDesc;
24 import org.apache.axis.components.logger.LogFactory;
25 import org.apache.axis.handlers.BasicHandler;
26 import org.apache.axis.message.SOAPEnvelope;
27 import org.apache.axis.message.SOAPHeaderElement;
28 import org.apache.axis.soap.SOAPConstants;
29 import org.apache.axis.utils.Messages;
30 import org.apache.commons.logging.Log;
31
32 import javax.xml.namespace.QName JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 /**
38  * MustUnderstandChecker is used to inject SOAP semantics just before
39  * the pivot handler.
40  */

41 public class MustUnderstandChecker extends BasicHandler {
42
43     private static Log log =
44             LogFactory.getLog(MustUnderstandChecker.class.getName());
45
46     private SOAPService service = null;
47
48     public MustUnderstandChecker(SOAPService service) {
49         this.service = service;
50     }
51
52     public void invoke(MessageContext msgContext) throws AxisFault {
53         // Do SOAP semantics here
54
if (log.isDebugEnabled()) {
55             log.debug(Messages.getMessage("semanticCheck00"));
56         }
57         
58         Message msg = msgContext.getCurrentMessage();
59         if (msg == null)
60             return; // nothing to do if there's no message
61

62         SOAPEnvelope env = msg.getSOAPEnvelope();
63         Vector JavaDoc headers = null;
64         if (service != null) {
65             ArrayList JavaDoc acts = service.getActors();
66             headers = env.getHeadersByActor(acts);
67         } else {
68             headers = env.getHeaders();
69         }
70             
71         // 1. Check mustUnderstands
72
Vector JavaDoc misunderstoodHeaders = null;
73         Enumeration JavaDoc enumeration = headers.elements();
74         while (enumeration.hasMoreElements()) {
75             SOAPHeaderElement header = (SOAPHeaderElement) enumeration.
76                     nextElement();
77             
78             // Ignore header, if it is a parameter to the operation
79
if(msgContext != null && msgContext.getOperation() != null) {
80                 OperationDesc oper = msgContext.getOperation();
81                 if(oper.getParamByQName(header.getQName())!=null) {
82                     continue;
83                 }
84             }
85             if (header.getMustUnderstand() && !header.isProcessed()) {
86                 if (misunderstoodHeaders == null)
87                     misunderstoodHeaders = new Vector JavaDoc();
88                 misunderstoodHeaders.addElement(header);
89             }
90         }
91         SOAPConstants soapConstants = msgContext.getSOAPConstants();
92         // !!! we should indicate SOAP1.2 compliance via the
93
// MessageContext, not a boolean here....
94

95         if (misunderstoodHeaders != null) {
96             AxisFault fault =
97                     new AxisFault(soapConstants.getMustunderstandFaultQName(),
98                             null, null,
99                             null, null,
100                             null);
101             StringBuffer JavaDoc whatWasMissUnderstood = new StringBuffer JavaDoc(256);
102
103             // !!! If SOAP 1.2, insert misunderstood fault headers here
104
if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
105                 enumeration = misunderstoodHeaders.elements();
106                 while (enumeration.hasMoreElements()) {
107                     SOAPHeaderElement badHeader = (SOAPHeaderElement) enumeration.
108                             nextElement();
109                     QName JavaDoc badQName = new QName JavaDoc(badHeader.getNamespaceURI(),
110                             badHeader.getName());
111                     if (whatWasMissUnderstood.length() != 0)
112                         whatWasMissUnderstood.append(", ");
113                     whatWasMissUnderstood.append(badQName.toString());
114                     SOAPHeaderElement newHeader = new
115                             SOAPHeaderElement(Constants.URI_SOAP12_ENV,
116                                     Constants.ELEM_NOTUNDERSTOOD);
117                     newHeader.addAttribute(null,
118                             Constants.ATTR_QNAME,
119                             badQName);
120                     fault.addHeader(newHeader);
121                 }
122             }
123             fault.setFaultString(Messages.getMessage("noUnderstand00",
124                     whatWasMissUnderstood.toString()));
125             throw fault;
126         }
127     }
128 }
129
Popular Tags