Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 21202362eef381f8f2bbab7cc1a48b8092691d65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsi.internal.core.profile.validator.impl.envelope;

import java.util.List;

import javax.wsdl.Binding;
import javax.wsdl.BindingOperation;
import javax.wsdl.Message;
import javax.wsdl.Part;

import org.eclipse.wst.wsi.internal.core.WSIConstants;
import org.eclipse.wst.wsi.internal.core.WSIException;
import org.eclipse.wst.wsi.internal.core.analyzer.AssertionFailException;
import org.eclipse.wst.wsi.internal.core.analyzer.AssertionNotApplicableException;
import org.eclipse.wst.wsi.internal.core.analyzer.AssertionPassException;
import org.eclipse.wst.wsi.internal.core.log.MessageEntry;
import org.eclipse.wst.wsi.internal.core.profile.TestAssertion;
import org.eclipse.wst.wsi.internal.core.profile.validator.EntryContext;
import org.eclipse.wst.wsi.internal.core.profile.validator.impl.AssertionProcess;
import org.eclipse.wst.wsi.internal.core.profile.validator.impl.BaseMessageValidator;
import org.eclipse.wst.wsi.internal.core.report.AssertionResult;
import org.eclipse.wst.wsi.internal.core.util.OperationSignature;
import org.eclipse.wst.wsi.internal.core.util.TypesRegistry;
import org.eclipse.wst.wsi.internal.core.xml.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


/**
 * BP1008.
 * Message has part accessor elements for parameters and return value, in no namespaces, 
 * but the children of these are namespace qualified with the same targetNamespace with which 
 * their types are defined.
 */
public class BP1008 extends AssertionProcess
{
  private final BaseMessageValidator validator;

  /**
   * @param BaseMessageValidator
   */
  public BP1008(BaseMessageValidator impl)
  {
    super(impl);
    this.validator = impl;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.profile.validator.impl.BaseValidatorImpl.AssertionProcess#validate(org.wsi.test.profile.TestAssertion, org.wsi.test.profile.validator.EntryContext)
   */
  public AssertionResult validate(
    TestAssertion testAssertion,
    EntryContext entryContext)
    throws WSIException
  {
    try
    {

      if (this.validator.isOneWayResponse(entryContext))
        throw new AssertionFailException(
          AssertionResult.RESULT_NOT_APPLICABLE);

      // Parse message
      Document doc = entryContext.getMessageEntryDocument();

      // If there is no message, then throw fail exception
      if (doc == null)
        throw new AssertionFailException();

      if (this.validator.isFault(doc))
      {
        throw new AssertionFailException(
          AssertionResult.RESULT_NOT_APPLICABLE);
      }

      // Check if there is a soap body element
      if (!this.validator.containsSoapBodyWithChild(doc))
      {
        throw new AssertionNotApplicableException();
      }

      // Parse request message
      Document docRequest = entryContext.getRequestDocument();

      // get SOAPAction
      String action =
        validator.getSoapAction(entryContext.getRequest().getHTTPHeaders());

      Binding binding = validator.analyzerContext.getCandidateInfo().getBindings()[0];
      //Definition definition = entryContext.getAnalyzerContext().getCandidateInfo().getDefinitions()[0];
      TypesRegistry registry =
        new TypesRegistry(
          this.validator.getWSDLDocument().getDefinitions(),
          validator);
      OperationSignature.OperationMatch match =
        OperationSignature.matchOperation(
          docRequest,
          action,
          binding,
          registry);
      if (match == null)
        throw new AssertionNotApplicableException();

      BindingOperation bindingOperation = match.getOperation();

      // Is rpc style?
      if (!WSIConstants
        .ATTRVAL_SOAP_BIND_STYLE_RPC
        .equals(match.getOperationStyle()))
        throw new AssertionFailException(
          AssertionResult.RESULT_NOT_APPLICABLE);

      // Gets body
      NodeList soapBodyList =
        doc.getElementsByTagNameNS(
          WSIConstants.NS_URI_SOAP,
          XMLUtils.SOAP_ELEM_BODY);
      if (soapBodyList.getLength() == 0 || soapBodyList.getLength() > 1)
      {
        // There is not a body or more than one bodies in the envlope.
        throw new AssertionFailException();
      }
      Element soapBodyElem = (Element) soapBodyList.item(0);

      result = null;

      // Find operation in the body
      Element soapOperation = XMLUtils.getFirstChild(soapBodyElem);
      if (soapOperation == null)
      {
        throw new AssertionFailException();
      }

      // Find operation message and ext. elements in the binding
      Message operationMessage = null;
      List extElements = null;
      if (MessageEntry
        .TYPE_REQUEST
        .equals(entryContext.getMessageEntry().getType()))
      {
        operationMessage =
          bindingOperation.getOperation().getInput().getMessage();
        if (bindingOperation.getBindingInput() != null)
          extElements =
            bindingOperation.getBindingInput().getExtensibilityElements();
      }
      else
      {
        if (MessageEntry
          .TYPE_RESPONSE
          .equals(entryContext.getMessageEntry().getType()))
        {
          operationMessage =
            bindingOperation.getOperation().getOutput().getMessage();
          if (bindingOperation.getBindingOutput() != null)
            extElements =
              bindingOperation.getBindingOutput().getExtensibilityElements();
        }
      }

      // Is message RPC-literal?
      if (!validator.isLiteral(extElements))
        throw new AssertionFailException(
          AssertionResult.RESULT_NOT_APPLICABLE);

      // gets first child of message
      Element soapMessagePart = XMLUtils.getFirstChild(soapOperation);
      if (soapMessagePart == null)
      {
        throw new AssertionPassException();
      }

      while (soapMessagePart != null)
      {
        // check whether part accessor or return are in no namespace		    		

        if (soapMessagePart.getNamespaceURI() != null
          || "".equals(soapMessagePart.getNamespaceURI()))
        {
          throw new AssertionFailException();
        }

        //    check whether children are namespace qualified 
        //  with the same targetNamespace with which their types are defined
        String typesTargetNS =
          getTypeNS(operationMessage, soapMessagePart.getLocalName());

        if (typesTargetNS == null)
          throw new AssertionFailException();

        if (!checkChildrenNamespace(soapMessagePart, typesTargetNS))
        {
          throw new AssertionFailException();
        }

        soapMessagePart = XMLUtils.getNextSibling(soapMessagePart);
      }

      result = AssertionResult.RESULT_PASSED;

    }
    catch (AssertionFailException e)
    {
      if (e.getMessage() != null && e.getMessage().length() > 0)
        result = e.getMessage();
      else
        result = AssertionResult.RESULT_FAILED;
    }
    catch (AssertionPassException e)
    {
      result = AssertionResult.RESULT_PASSED;
    }
    catch (Exception e)
    {
      result = AssertionResult.RESULT_NOT_APPLICABLE;
    }

    if (result == AssertionResult.RESULT_FAILED)
    {
      failureDetail = this.validator.createFailureDetail(null, entryContext);
    }

    // Return assertion result
    return validator.createAssertionResult(testAssertion, result, failureDetail);
  }

  /**
   * Gets part type NS.
   * @param message
   * @param partName
   * @return String
   */
  private String getTypeNS(Message message, String partName)
  {
    Part part = message.getPart(partName);
    if (part == null)
      return null;
    else
      return part.getTypeName().getNamespaceURI();
  }

  /**
   * Checks children elements' namespace.
   * @param node
   * @param typesTargetNS
   * @return boolean
   */

  private boolean checkChildrenNamespace(Element node, String typesTargetNS)
  {
    NodeList parts = node.getElementsByTagName("*");
    for (int i = 0; i < parts.getLength(); i++)
    {
      Element part = (Element) parts.item(i);
      if (!typesTargetNS.equals(part.getNamespaceURI()))
      {
        return false;
      }
      if (!checkChildrenNamespace(part, typesTargetNS))
      {
        return false;
      }
    }
    return true;
  }

}

Back to the top