View Javadoc

1   /*
2    * Copyright 2008 the original author or authors.
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.springframework.ws.soap.support;
18  
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  import org.springframework.util.Assert;
23  import org.springframework.util.StringUtils;
24  import org.springframework.ws.transport.TransportConstants;
25  
26  /**
27   * Contains various utility methods for handling SOAP messages.
28   *
29   * @author Arjen Poutsma
30   * @since 1.5.5
31   */
32  public abstract class SoapUtils {
33  
34      private static final Pattern ACTION_PATTERN = Pattern.compile("action\\s*=\\s*([^;]+)");
35  
36      private SoapUtils() {
37      }
38  
39      /** Escapes the given SOAP action to be surrounded by quotes. */
40      public static String escapeAction(String soapAction) {
41          if (!StringUtils.hasLength(soapAction)) {
42              soapAction = "\"\"";
43          }
44          if (!soapAction.startsWith("\"")) {
45              soapAction = "\"" + soapAction;
46          }
47          if (!soapAction.endsWith("\"")) {
48              soapAction = soapAction + "\"";
49          }
50          return soapAction;
51      }
52  
53      /**
54       * Returns the value of the action parameter in the given SOAP 1.2 content type.
55       *
56       * @param contentType the SOAP 1.2 content type
57       * @return the action
58       */
59      public static String extractActionFromContentType(String contentType) {
60          if (contentType != null) {
61              Matcher matcher = ACTION_PATTERN.matcher(contentType);
62              if (matcher.find() && matcher.groupCount() == 1) {
63                  return matcher.group(1).trim();
64              }
65          }
66          return TransportConstants.EMPTY_SOAP_ACTION;
67      }
68  
69      /**
70       * Replaces or adds the value of the action parameter in the given SOAP 1.2 content type.
71       *
72       * @param contentType the SOAP 1.2 content type
73       * @param action      the action
74       * @return the new content type
75       */
76      public static String setActionInContentType(String contentType, String action) {
77          Assert.hasLength(contentType, "'contentType' must not be empty");
78          if (StringUtils.hasText(action)) {
79              Matcher matcher = ACTION_PATTERN.matcher(contentType);
80              if (matcher.find() && matcher.groupCount() == 1) {
81                  StringBuffer buffer = new StringBuffer();
82                  matcher.appendReplacement(buffer, "action=" + action);
83                  matcher.appendTail(buffer);
84                  return buffer.toString();
85              }
86              else {
87                  return contentType + "; action=" + action;
88              }
89          }
90          else {
91              return contentType;
92          }
93      }
94  
95  
96  }