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

Apache CXF example source code file (ServletTransportFactory.java)

This example Apache CXF source code file (ServletTransportFactory.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

abstracthttptransportfactory, concurrenthashmap, concurrenthashmap, io, ioexception, iso-8859-1, list, map, net, network, resource, servletdestination, servletdestination, servlettransportfactory, servlettransportfactory, string, string, threading, threads, util

The Apache CXF ServletTransportFactory.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.transport.servlet;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;

import org.apache.cxf.Bus;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.service.model.EndpointInfo;
import org.apache.cxf.transport.Destination;
import org.apache.cxf.transport.DestinationFactory;
import org.apache.cxf.transport.http.AbstractHTTPTransportFactory;
import org.apache.cxf.wsdl.http.AddressType;

public class ServletTransportFactory extends AbstractHTTPTransportFactory
    implements DestinationFactory {
    
    private Map<String, ServletDestination> destinations = 
        new ConcurrentHashMap<String, ServletDestination>();
    private Map<String, ServletDestination> decodedDestinations = 
        new ConcurrentHashMap<String, ServletDestination>();
    
    private ServletController controller;
    
    public ServletTransportFactory(Bus b) {
        super.setBus(b);
        List<String> ids = Arrays.asList(new String[] {
            "http://cxf.apache.org/transports/http",
            "http://cxf.apache.org/transports/http/configuration",
            "http://schemas.xmlsoap.org/wsdl/http",
            "http://schemas.xmlsoap.org/wsdl/http/",
        });
        this.setTransportIds(ids);
    }

    public ServletTransportFactory() {
    }
   
    public void setServletController(ServletController c) {
        controller = c;
    }

    @Resource(name = "cxf")
    public void setBus(Bus b) {
        super.setBus(b);
    }
    
    public void removeDestination(String path) throws IOException {
        destinations.remove(path);
        decodedDestinations.remove(URLDecoder.decode(path, "ISO-8859-1"));
    }
    
    public Destination getDestination(EndpointInfo endpointInfo)
        throws IOException {
        ServletDestination d = getDestinationForPath(endpointInfo.getAddress());
        if (d == null) { 
            String path = getTrimmedPath(endpointInfo.getAddress());
            d = new ServletDestination(getBus(), endpointInfo, this, path);
            destinations.put(path, d);
            decodedDestinations.put(URLDecoder.decode(path, "ISO-8859-1"), d);
            
            if (controller != null
                && !StringUtils.isEmpty(controller.getLastBaseURL())) {
                String ad = d.getEndpointInfo().getAddress();
                if (ad != null 
                    && (ad.equals(path)
                    || ad.equals(controller.getLastBaseURL() + path))) {
                    d.getEndpointInfo().setAddress(controller.getLastBaseURL() + path);
                    if (d.getEndpointInfo().getExtensor(AddressType.class) != null) {
                        d.getEndpointInfo().getExtensor(AddressType.class)
                            .setLocation(controller.getLastBaseURL() + path);
                    }
                }
            }
        }
        return d;
    }
    
    public ServletDestination getDestinationForPath(String path) {
        return getDestinationForPath(path, false);
    }
    public ServletDestination getDestinationForPath(String path, boolean tryDecoding) {
        // to use the url context match  
        String m = getTrimmedPath(path);
        ServletDestination s = destinations.get(m);
        if (s == null) {
            s = decodedDestinations.get(m);
        }
        return s;
    }

    static String getTrimmedPath(String path) {
        if (path == null) {
            return "/";
        }
        final String lh = "http://localhost/";
        final String lhs = "https://localhost/";
        
        if (path.startsWith(lh)) {
            path = path.substring(lh.length());
        } else if (path.startsWith(lhs)) {
            path = path.substring(lhs.length());
        }
        if (!path.startsWith("/")) {
            path = "/" + path;
            
        }
        return path;
    }
    
    public Collection<ServletDestination> getDestinations() {
        return Collections.unmodifiableCollection(destinations.values());        
    }
    public Set<String> getDestinationsPaths() {
        return Collections.unmodifiableSet(destinations.keySet());        
    }

    
}

Other Apache CXF examples (source code examples)

Here is a short list of links related to this Apache CXF ServletTransportFactory.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.