SoapInInterceptor使用

package com.infomaster2.ws.gateway.interceptor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.cxf.binding.soap.Soap11;
import org.apache.cxf.binding.soap.Soap12;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.binding.soap.interceptor.EndpointSelectionInterceptor;
import org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor;
import org.apache.cxf.binding.soap.model.SoapOperationInfo;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.service.model.BindingOperationInfo;
import org.apache.cxf.service.model.OperationInfo;

public class SoapActionInInterceptor extends AbstractSoapInterceptor {

    public SoapActionInInterceptor() {
        super(Phase.READ);
        addAfter(ReadHeadersInterceptor.class.getName());
        addAfter(EndpointSelectionInterceptor.class.getName());
    }

    public void handleMessage(SoapMessage message) throws Fault {
        if (message.getVersion() instanceof Soap11) {
            Map<String, List<String>> headers = CastUtils.cast((Map)message.get(Message.PROTOCOL_HEADERS));
            List<String> soapaction =  headers.get("SOAPAction");
            
            if (soapaction != null && soapaction.size() > 0) {
        			System.out.println("Request Orginal SoapAction:" +soapaction.get(0) );
            }
            
            headers.remove("SOAPAction");
            
            soapaction = new ArrayList<String>();
            //add server action
            soapaction.add("your action");
            headers.put("SOAPAction", soapaction);
            
//            if (headers != null) {
//            	List<String> sa = headers.get("SOAPAction");
//            	if (sa != null && sa.size() > 0) {
//            		for (String string : sa) {
//            			System.out.println(string);
//            		}
//            		String action = sa.get(0);
//            		if (action.startsWith("\"")) {
//            			action = action.substring(1, action.length() - 1);
//            		}
//            		System.out.println("action:"+action);
//            		getAndSetOperation(message, action);
//            	}
//            }
        } else if (message.getVersion() instanceof Soap12) {
        	
        }
    }

    private void getAndSetOperation(SoapMessage message, String action) {
        if ("".equals(action)) {
            return;
        }

        Exchange ex = message.getExchange();
        Endpoint ep = ex.get(Endpoint.class);

        BindingOperationInfo bindingOp = null;

        Collection<BindingOperationInfo> bops = ep.getBinding().getBindingInfo().getOperations();
        for (BindingOperationInfo boi : bops) {
            SoapOperationInfo soi = (SoapOperationInfo) boi.getExtensor(SoapOperationInfo.class);
            if (soi != null && soi.getAction().equals(action)) {
                if (bindingOp != null) {
                    //more than one op with the same action, will need to parse normally
                    return;
                }
                bindingOp = boi;
            }
        }
        if (bindingOp != null) {
            ex.put(BindingOperationInfo.class, bindingOp);
            ex.put(OperationInfo.class, bindingOp.getOperationInfo());
        }
    }

}

 发布方式

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.infomaster2.ws.gateway.https;

import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.ws.Endpoint;


import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.endpoint.EndpointImpl;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;

import com.infomaster2.ws.gateway.TransactionImpl;
import com.infomaster2.ws.gateway.TransactionService;
import com.infomaster2.ws.gateway.interceptor.MessageInterceptor;
import com.infomaster2.ws.gateway.interceptor.SoapActionInInterceptor;

public class Transaction_HTTPS_Server {

	private static final LoggingInInterceptor IN = new LoggingInInterceptor();
    private static final LoggingOutInterceptor OUT = new LoggingOutInterceptor();
    protected Transaction_HTTPS_Server(String ip ,String port) throws Exception {
        System.out.println("Starting Server");
       
        
    	
    	
        SpringBusFactory bf = new SpringBusFactory();
        URL busFile = Transaction_HTTPS_Server.class.getClassLoader().getResource("ServerConfig.xml");
        
        Bus bus = bf.createBus(busFile.toString());
        bus.getInInterceptors().add(IN);
        bus.getInFaultInterceptors().add(IN);
//        bus.getInInterceptors().add(new MessageInterceptor(Phase.RECEIVE));
        bus.getInInterceptors().add(new SoapActionInInterceptor());
        
        bus.getOutInterceptors().add(OUT);
        bus.getOutInterceptors().add(new SoapActionInInterceptor());
        bus.getOutFaultInterceptors().add(OUT);
        
        
        BusFactory.setDefaultBus(bus);

        
        Object implementor = new TransactionImpl();
        String address = "https://"+ip+":"+port+"/SOAPGWApp/TransactionService";
        
        Endpoint.publish(address, implementor);
    }

    public static void main(String args[]) throws Exception {
        System.out.println("The server's security configuration will be taken "
                           + "from server.xml using the bean name : "
                           + "\"{http://apache.org/hello_world_soap_http}"
                           + "GreeterImplPort.http-destination\".");
        System.out.println();
        new Transaction_HTTPS_Server(args[0],args[1]);
        System.out.println("Server ready...");

//        Thread.sleep(5 * 60 * 1000);
//        System.out.println("Server exiting");
//        System.exit(0);
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值