All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cxf.binding.soap.tcp.ChannelService Maven / Gradle / Ivy

There is a newer version: 2.7.18
Show newest version
/**
 * 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 org.apache.cxf.binding.soap.tcp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.cxf.binding.soap.tcp.frames.SoapTcpMessage;
import org.apache.cxf.staxutils.StaxUtils;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

public final class ChannelService {
    private ChannelService() {
        
    }
    
    public static void service(IoSession session, SoapTcpMessage message) {
        XMLStreamReader xmlReader = null;
        try {
            xmlReader
                = StaxUtils.createXMLStreamReader(message.getContentAsStream(), "UTF-8");
            while (xmlReader.hasNext()) {
                xmlReader.next();
                if (xmlReader.getEventType() == XMLStreamReader.START_ELEMENT) {
                    if (xmlReader.getLocalName().equals("initiateSession")) {
                        initiateSession(session);
                    } else if (xmlReader.getLocalName().equals("openChannel")) {
                        String targetWSURI = null;
                        List negotiatedMimeTypes = new ArrayList();
                        List negotiatedParams = new ArrayList();
                        while (xmlReader.hasNext()) {
                            xmlReader.next();
                            if (xmlReader.getEventType() == XMLStreamReader.START_ELEMENT) {
                                if (xmlReader.getLocalName().equals("targetWSURI")) {
                                    targetWSURI = xmlReader.getElementText();
                                } else if (xmlReader.getLocalName().equals("negotiatedMimeTypes")) {
                                    negotiatedMimeTypes.add(xmlReader.getElementText());
                                }  else if (xmlReader.getLocalName().equals("negotiatedParams")) {
                                    negotiatedParams.add(xmlReader.getElementText());
                                }
                            }
                        }
                        openChannel(session, targetWSURI, negotiatedMimeTypes, negotiatedParams);
                    } else  if (xmlReader.getLocalName().equals("closeChannel")) {
                        int channelId = -1;
                        while (xmlReader.hasNext()) {
                            if (xmlReader.getEventType() == XMLStreamReader.START_ELEMENT
                                && xmlReader.getLocalName().equals("channelId")) {
                                channelId = Integer.parseInt(xmlReader.getElementText());
                            }
                        }
                        closeChannel(session, channelId);
                    }
                    
                }
            }
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } finally {
            StaxUtils.close(xmlReader);
        }
    }
    
    private static void initiateSession(IoSession session) {
        System.out.println("initiateSession service");
        String response = ""
            + "";
        SoapTcpMessage soapTcpMessage = SoapTcpMessage.createSoapTcpMessage(response, 0);
        IoBuffer buffer = IoBuffer.allocate(512);
        buffer.setAutoExpand(true);
        try {
            SoapTcpUtils.writeSoapTcpMessage(buffer.asOutputStream(), soapTcpMessage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        buffer.flip();
        session.write(buffer);
    }
    
    @SuppressWarnings("unchecked")
    private static void openChannel(IoSession session, String targetWSURI, List negotiatedMimeTypes,
                                    List negotiatedParams) {
        System.out.println("openChannel service");
        List channels = (List)session.getAttribute("channels");
        int max = 0;
        for (SoapTcpChannel channel : channels) {
            if (channel.getChannelId() > max) {
                max = channel.getChannelId();
            }
        }
        channels.add(new SoapTcpChannel(max + 1, targetWSURI));
        
        String response = ""
            + ""
            + (max + 1)
            + ""
            + "application/soap+xmlcharsetSOAPActionaction";
        SoapTcpMessage soapTcpMessage = SoapTcpMessage.createSoapTcpMessage(response, 0);
        IoBuffer buffer = IoBuffer.allocate(512);
        buffer.setAutoExpand(true);
        try {
            SoapTcpUtils.writeSoapTcpMessage(buffer.asOutputStream(), soapTcpMessage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        buffer.flip();
        session.write(buffer);
    }
    
    @SuppressWarnings("unchecked")
    private static void closeChannel(IoSession session, int channelId) {
        System.out.println("closeChannel service");
        List channels = (List)session.getAttribute("channels");
        for (SoapTcpChannel channel : channels) {
            if (channel.getChannelId() == channelId) {
                channels.remove(channel);
                break;
            }
        }
        
        String response = ""
            + "";
        SoapTcpMessage soapTcpMessage = SoapTcpMessage.createSoapTcpMessage(response, 0);
        IoBuffer buffer = IoBuffer.allocate(512);
        buffer.setAutoExpand(true);
        try {
            SoapTcpUtils.writeSoapTcpMessage(buffer.asOutputStream(), soapTcpMessage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        buffer.flip();
        session.write(buffer);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy