alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Apache CXF example source code file (ChannelService.java)

This example Apache CXF source code file (ChannelService.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Apache CXF tags/keywords

arraylist, channelservice, io, iobuffer, iobuffer, ioexception, list, list, soaptcpchannel, soaptcpmessage, soaptcpmessage, string, string, suppresswarnings, util, xmlschema

The Apache CXF ChannelService.java source code

/**
 * 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) {
        try {
            XMLStreamReader 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<String> negotiatedMimeTypes = new ArrayList();
                        List<String> 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();
        }
    }
    
    private static void initiateSession(IoSession session) {
        System.out.println("initiateSession service");
        String response = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            + "<s:Body>";
        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<String> negotiatedMimeTypes,
                                    List<String> negotiatedParams) {
        System.out.println("openChannel service");
        List<SoapTcpChannel> 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 = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            + "<openChannelResponse xmlns=\"http://servicechannel.tcp.transport.ws.xml.sun.com/\""
            + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org"
            + "/2001/XMLSchema\"><channelId xmlns=\"\">"
            + (max + 1)
            + "</channelId>"
            + "application/soap+xml</negotiatedMimeTypes>charsetSOAPAction";
        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<SoapTcpChannel> channels = (List)session.getAttribute("channels");
        for (SoapTcpChannel channel : channels) {
            if (channel.getChannelId() == channelId) {
                channels.remove(channel);
                break;
            }
        }
        
        String response = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            + "<s:Body>";
        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);
    }
}

Other Apache CXF examples (source code examples)

Here is a short list of links related to this Apache CXF ChannelService.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.