Skip to main content
summaryrefslogtreecommitdiffstats
blob: 63acd565ab2c290fddcf9052c08f5d33dd22db6c (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
/*******************************************************************************
 * 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 org.eclipse.wst.wsi.internal.core.WSIException;
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.xml.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
   * BP1302.
   * The soap:faultcode value in the soap:Fault element of the response 
   * message is not custom, and is one of: VersionMismatch, MustUnderstand, Client, Server.
   */
public class BP1302 extends AssertionProcess
{
  private final BaseMessageValidator validator;

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

  //SOAP fault code values should not be custom: only standard code qualifiers 
  //are used: VersionMismatch, MustUnderstand, Client, Server
  public AssertionResult validate(
    TestAssertion testAssertion,
    EntryContext entryContext)
    throws WSIException
  {

    //String requestMessage = null;
    String responseMessage = null;

    if (this.validator.isOneWayResponse(entryContext))
    {
      result = AssertionResult.RESULT_NOT_APPLICABLE;
    }
    else
    {
      //Get the response message check the faultcode.
      MessageEntry logEntryResponse = entryContext.getResponse();
      if (logEntryResponse != null)
      {
        responseMessage = logEntryResponse.getMessage();

        Document targetDoc = XMLUtils.parseXML(responseMessage);
        NodeList faultCodes = targetDoc.getElementsByTagName("faultcode");
        //                    targetDoc.getElementsByTagNameNS(WSIConstants.NS_URI_SOAP, "faultcode");

        if (faultCodes.getLength() <= 0)
          result = AssertionResult.RESULT_NOT_APPLICABLE;
        //No faultcode	

        for (int i = 0; i < faultCodes.getLength(); i++)
        {
          Node faultCodeElem = faultCodes.item(i);
          //If the faultcode was MustUnderstand, check the value of 
          //the mustUnderstand attribute in the request.
          if (faultCodeElem == null)
            result = AssertionResult.RESULT_NOT_APPLICABLE;
          else if (faultCodeElem.getNodeType() == Node.ELEMENT_NODE)
          {
            if (isValidFaultCode((Element) faultCodeElem))
            {
              result = AssertionResult.RESULT_PASSED;
            }
            else
            {
              result = AssertionResult.RESULT_WARNING;
              failureDetail =
                this.validator.createFailureDetail(responseMessage, entryContext);
              //failureDetail =
              //    "The faultcode value was : "
              //        + faultCodeElem.getFirstChild().getNodeValue().trim();
            } //End if (faultCode.equals...)
          }
          else
          {
            result = AssertionResult.RESULT_NOT_APPLICABLE;
          }
        } //End for	
      } //End if logEntryResponse!= null
    }

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

  /**
   * Check faultCode.
   * @param faultCodeElem a faultcode.
   * @return true if faultcode is valid.
   */
  public boolean isValidFaultCode(Element faultCodeElem)
  {
    //A faultcode may or maynot have a prefix, if it does its namespace should be 
    //http://schemas.xmlsoap.org/soap/envelope which is the same as the faultcode element.
    //If a prefix is present and it corresponds to a different namespace or is null 
    //then this returns false.  The local part of the faultcode is then check to see
    //if it is one of the permitted values.
    if (faultCodeElem == null)
      return true;

    String faultCode = faultCodeElem.getFirstChild().getNodeValue().trim();
    if (faultCode == null)
      return false;

    // FIX: This is not correct, since this element can not be namespace qualified
    String faultCodeElemPrefix = faultCodeElem.getParentNode().getPrefix();

    // If this is not a QName then return false
    int index;
    String faultCodePrefix = null;
    String faultCodeName = faultCode;
    if ((index = faultCode.indexOf(':')) != -1)
    {
      faultCodePrefix = faultCode.substring(0, index);
      faultCodeName = faultCode.substring(index + 1, faultCode.length());
    }

    if ((faultCodeElemPrefix != null && faultCodePrefix != null))
    {
      if (faultCodePrefix.equals(faultCodeElemPrefix))
      {
        if ((faultCodeName.equals("MustUnderstand")
          || faultCodeName.equals("VersionMismatch")
          || faultCodeName.equals("Client")
          || faultCodeName.equals("Server")))
          /* REMOVE:
          || faultCodeName.startsWith("MustUnderstand.")
          || faultCodeName.startsWith("VersionMismatch.")
          || faultCodeName.startsWith("Client.")
          || faultCodeName.startsWith("Server.")))
          */
          return true;
        else
          return false;
      }
      else
      {
        return true;
      }
    }
    else
    {
      if (faultCodeName.equals("MustUnderstand")
        || faultCodeName.equals("VersionMismatch")
        || faultCodeName.equals("Client")
        || faultCodeName.equals("Server")
        || faultCodeName.startsWith("MustUnderstand.")
        || faultCodeName.startsWith("VersionMismatch.")
        || faultCodeName.startsWith("Client.")
        || faultCodeName.startsWith("Server."))
        return true;
      else
        return false;
    }
  }
}

Back to the top