Skip to main content
summaryrefslogtreecommitdiffstats
blob: e85297608cb19ae2238dcc3f59836be90e18670f (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
/*******************************************************************************
 * 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.message;

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.log.MimePart;
import org.eclipse.wst.wsi.internal.core.log.MimeParts;
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.MIMEConstants;
import org.eclipse.wst.wsi.internal.core.util.MIMEUtils;
import org.eclipse.wst.wsi.internal.core.util.Utils;

/**
 * AP1915
 *
 * <context>For a candidate root-part of a multipart/related message</context>
 * <assertionDescription>The entity body of the root-part of a
 * multipart/related message is serialized using either UTF-8 or UTF-16
 * character encoding.</assertionDescription>
 */
public class AP1915 extends AssertionProcess
{
  private final BaseMessageValidator validator;

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

  /* Validates the test assertion.
  * @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
  {
    // get MIME parts
    if (!entryContext.getMessageEntry().isMimeContent())
    {
      result = AssertionResult.RESULT_NOT_APPLICABLE;
    }
    else
    {
      MimeParts mimeParts = entryContext.getMessageEntry().getMimeParts();    	
      MimePart part = mimeParts.getRootPart();
      if (part == null)
      {
        result = AssertionResult.RESULT_NOT_APPLICABLE;
      }
      else
      {
        String xmlEncoding = null;
        String charset = MIMEUtils.getMimeHeaderSubAttribute(
          part.getHeaders(),
          MIMEConstants.HEADER_CONTENT_TYPE, 
          "charset");
        try
        {
          // The HTTP Content-Type header's charset value is either UTF-8 or UTF-16. 
          if((charset != null) && (charset.equalsIgnoreCase("utf-8") ||
              charset.equalsIgnoreCase("utf-16")))
          {
            // Looking at the messageContent element of the logged message, either 
            // (1) it has a BOM attribute which maps the charset value in the Content-Type header, or 
            int bom = 0;
            if ((bom = entryContext.getMessageEntry().getBOM()) != 0)
            {
              if ((bom == WSIConstants.BOM_UTF8
                && !charset.equalsIgnoreCase("utf-8"))
                || ((bom == WSIConstants.BOM_UTF16
                  && !charset.equalsIgnoreCase("utf-16")))
                || ((bom == WSIConstants.BOM_UTF16_BIG_ENDIAN
                  && !charset.equalsIgnoreCase("utf-16"))))
              {
                throw new AssertionFailException("The BOM (" + bom + 
                    ") and charset value (" + charset + ")do not match.");
              }
            }
            // (2) it has an XML declaration which matches the charset value in the Content-Type header, or 
            else if (((xmlEncoding = 
              Utils.getXMLEncoding(part.getContent())) != null) &&
              !xmlEncoding.equals(""))  
            {
              if(!xmlEncoding.equalsIgnoreCase(charset))
              {
                throw new AssertionFailException("The XML declaration encoding (" + 
                    xmlEncoding + ") and charset value (" + charset + 
                    ") do not match.");
              }
            
            }
            // (3) there is no BOM attribute and no XML declaration, and the charset value is UTF-8.
            else if(!charset.equalsIgnoreCase("utf-8"))
            {
              throw new AssertionFailException("The no BOM attribute and no XML "+
                  "declaration, and the charset value is (" + charset + ")");
            }
          }
          // header do not found or incorrect charset value
          else 
          {
            throw new AssertionFailException("Either the Content-Type header is not "+
                "present in the Root Part or a charset value is invalid.");
          }
        }
        catch (AssertionFailException e) 
        {
          result = AssertionResult.RESULT_FAILED;
          failureDetail = validator.createFailureDetail(e.getMessage(), entryContext);
        }
      }
    }
    // Return assertion result
    return validator.createAssertionResult(
      testAssertion, result, failureDetail);
  }
}

Back to the top