001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.soap.interceptor;
019:
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.cxf.binding.soap.Soap11;
026: import org.apache.cxf.binding.soap.Soap12;
027: import org.apache.cxf.binding.soap.SoapConstants;
028: import org.apache.cxf.binding.soap.SoapMessage;
029: import org.apache.cxf.binding.soap.model.SoapOperationInfo;
030: import org.apache.cxf.helpers.CastUtils;
031: import org.apache.cxf.interceptor.Fault;
032: import org.apache.cxf.message.Message;
033: import org.apache.cxf.phase.Phase;
034: import org.apache.cxf.service.model.BindingOperationInfo;
035:
036: public class SoapActionOutInterceptor extends AbstractSoapInterceptor {
037:
038: public SoapActionOutInterceptor() {
039: super (Phase.POST_LOGICAL);
040: }
041:
042: public void handleMessage(SoapMessage message) throws Fault {
043: if (!(message == message.getExchange().getInMessage())) {
044: setSoapAction(message);
045: }
046: }
047:
048: private void setSoapAction(SoapMessage message) {
049: BindingOperationInfo boi = message.getExchange().get(
050: BindingOperationInfo.class);
051:
052: // The soap action is set on the wrapped operation.
053: if (boi != null && boi.isUnwrapped()) {
054: boi = boi.getWrappedOperation();
055: }
056:
057: String action = getSoapAction(message, boi);
058:
059: if (message.getVersion() instanceof Soap11) {
060: Map<String, List<String>> reqHeaders = CastUtils
061: .cast((Map) message.get(Message.PROTOCOL_HEADERS));
062: if (reqHeaders == null) {
063: reqHeaders = new HashMap<String, List<String>>();
064: }
065:
066: if (reqHeaders.size() == 0) {
067: message.put(Message.PROTOCOL_HEADERS, reqHeaders);
068: }
069:
070: if (!reqHeaders.containsKey("SOAPAction")) {
071: reqHeaders.put("SOAPAction", Collections
072: .singletonList(action));
073: }
074: } else if (message.getVersion() instanceof Soap12
075: && !"\"\"".equals(action)) {
076: String ct = (String) message.get(Message.CONTENT_TYPE);
077:
078: if (ct.indexOf("action=\"") == -1) {
079: ct = new StringBuilder().append(ct).append("; action=")
080: .append(action).toString();
081: message.put(Message.CONTENT_TYPE, ct);
082: }
083: }
084: }
085:
086: private String getSoapAction(SoapMessage message,
087: BindingOperationInfo boi) {
088: // allow an interceptor to override the SOAPAction if need be
089: String action = (String) message.get(SoapConstants.SOAP_ACTION);
090:
091: // Fall back on the SOAPAction in the operation info
092: if (action == null) {
093: if (boi == null) {
094: action = "\"\"";
095: } else {
096: SoapOperationInfo soi = (SoapOperationInfo) boi
097: .getExtensor(SoapOperationInfo.class);
098: action = soi == null ? "\"\""
099: : soi.getAction() == null ? "\"\"" : soi
100: .getAction();
101: if (!action.startsWith("\"")) {
102: action = new StringBuffer().append("\"").append(
103: action).append("\"").toString();
104: }
105: }
106: }
107:
108: return action;
109: }
110:
111: }
|