Package com.ericsson.ssa.sip.dns

Source Code of com.ericsson.ssa.sip.dns.Translator$MappingTuple

/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License").  You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code.  If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license."  If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above.  However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.ericsson.ssa.sip.dns;

import com.ericsson.ssa.sip.UriUtil;

import org.glassfish.comms.api.telurl.PhoneContextHandler;
import org.glassfish.comms.api.uriutils.UriAliasHandler;

import org.jvnet.glassfish.comms.util.LogUtil;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import javax.servlet.sip.URI;


/**
* This class implements number normalization and URI canonicalization.
* It makes use of plug-ins provided by the application which it loads at start-up.
* <p>
* A specific plug-in is selected based on regular expressions as configured to the Translator.
*
*/
class Translator {
    private static final Logger logger = LogUtil.SIP_LOGGER.getLogger();
    private Map<String, PatternMapping<PhoneContextHandler>> phoneContextHandlerMappings =
        new TreeMap<String, PatternMapping<PhoneContextHandler>>();
    private Map<String, PatternMapping<UriAliasHandler>> uriAliasHandlerMappings =
        new TreeMap<String, PatternMapping<UriAliasHandler>>();

    /**
     * Creates the object.
     */
    Translator() {
    }

    /**
     * Normalizes the specified local number to a global number based on the specified phone-context.
     * It does this by matching the phone-context against regular expressions to select a plug-in class
     * and requesting that plug-in to do the normalization.
     * @param localNumber the local number to normalize
     * @param phoneContext the phone-context
     * @return the normalized number
     */
    String normalize(String localNumber, String phoneContext) {
        PhoneContextHandler handler = null;

        Map<String, PatternMapping<PhoneContextHandler>> mappings;

        synchronized (this) {
            mappings = phoneContextHandlerMappings;
        }

        for (PatternMapping<PhoneContextHandler> mapping : mappings.values()) {
            if (mapping.regexpPattern.matcher(phoneContext).find()) {
                handler = mapping.mappedObject;

                break;
            }
        }

        String normalizedNumber = (handler != null)
            ? handler.normalize(localNumber, phoneContext) : null;

        if ((normalizedNumber != null) && (normalizedNumber.charAt(0) != '+')) {
            // Be nice and make it correct :-)
            normalizedNumber = "+" + normalizedNumber;

            if (logger.isLoggable(Level.WARNING)) {
                logger.log(Level.WARNING,
                    "The result of phone number normalization using: \"" +
                    handler.getClass() +
                    "\", was not a number prefixed with '+'. This has been fixed by the container.");
            }
        }

        return UriUtil.cleanupPhonenumber(normalizedNumber);
    }

    /**
     * Canonicalizes the specified URI that might be an alias for a canonical URI.
     * It does this by matching the URI against regular expressions to select a plug-in class
     * and requesting that plug-in to do the canonicalization.
     * @param uri the URI to canonicalize
     * @return the canonicalized URI
     */
    URI canonicalize(URI uri) {
        UriAliasHandler handler = null;

        Map<String, PatternMapping<UriAliasHandler>> mappings;

        synchronized (this) {
            mappings = uriAliasHandlerMappings;
        }

        for (PatternMapping<UriAliasHandler> mapping : mappings.values()) {
            if (mapping.regexpPattern.matcher(uri.toString()).find()) {
                handler = mapping.mappedObject;

                break;
            }
        }

        return (handler != null) ? handler.canonicalize(uri) : uri;
    }

    void loadUriAliasHandlers(List<MappingTuple> handlers) {
        Map<String, PatternMapping<UriAliasHandler>> oldMappings;

        if (logger.isLoggable(Level.INFO)) {
            synchronized (this) {
                oldMappings = new TreeMap<String, PatternMapping<UriAliasHandler>>(uriAliasHandlerMappings);
            }

            if (oldMappings.size() > 0) {
                StringBuilder sb = new StringBuilder(
                        "Removing the following mappings: ");

                for (Map.Entry<String, PatternMapping<UriAliasHandler>> mappingEntry : oldMappings.entrySet()) {
                    sb.append("(").append(mappingEntry.getKey()).append(":")
                      .append(mappingEntry.getValue().mappedObject.getClass())
                      .append(";").append(mappingEntry.getValue().regexpPattern)
                      .append("),");
                }

                sb.deleteCharAt(sb.length() - 1);
                logger.log(Level.INFO, sb.toString());
            }
        }

        Map<String, PatternMapping<UriAliasHandler>> newMappings = new TreeMap<String, PatternMapping<UriAliasHandler>>();

        for (MappingTuple mappingTuple : handlers) {
            loadPlugin(mappingTuple.mappingName, mappingTuple.handlerClassName,
                mappingTuple.pattern, "URI-alias handler", newMappings);
        }

        synchronized (this) {
            uriAliasHandlerMappings = newMappings;
        }
    }

    void loadPhoneContextHandlers(List<MappingTuple> handlers) {
        Map<String, PatternMapping<PhoneContextHandler>> oldMappings;

        if (logger.isLoggable(Level.INFO)) {
            synchronized (this) {
                oldMappings = new TreeMap<String, PatternMapping<PhoneContextHandler>>(phoneContextHandlerMappings);
            }

            if (oldMappings.size() > 0) {
                StringBuilder sb = new StringBuilder(
                        "Removing the following mappings: ");

                for (Map.Entry<String, PatternMapping<PhoneContextHandler>> mappingEntry : oldMappings.entrySet()) {
                    sb.append("(").append(mappingEntry.getKey()).append(":")
                      .append(mappingEntry.getValue().mappedObject.getClass())
                      .append(";").append(mappingEntry.getValue().regexpPattern)
                      .append("),");
                }

                sb.deleteCharAt(sb.length() - 1);
                logger.log(Level.INFO, sb.toString());
            }
        }

        Map<String, PatternMapping<PhoneContextHandler>> newMappings = new TreeMap<String, PatternMapping<PhoneContextHandler>>();

        for (MappingTuple mappingTuple : handlers) {
            loadPlugin(mappingTuple.mappingName, mappingTuple.handlerClassName,
                mappingTuple.pattern, "Phone context handler", newMappings);
        }

        synchronized (this) {
            phoneContextHandlerMappings = newMappings;
        }
    }

    @SuppressWarnings("unchecked")
    private <T> void loadPlugin(String patternName, String handlerClassName,
        String regexp, String pluginType,
        Map<String, PatternMapping<T>> mappings) {
        try {
            Class<?> handlerClass = Class.forName(handlerClassName);
            Constructor<?> constructor = handlerClass.getConstructor((Class<?>[]) null);
            Object handlerObject = constructor.newInstance((Object[]) null);
            Pattern pattern = Pattern.compile(regexp);

            try {
                mappings.put(patternName,
                    new PatternMapping<T>((T) handlerObject, pattern));

                if (logger.isLoggable(Level.INFO)) {
                    logger.log(Level.INFO,
                        "Added " + pluginType + ": name=" + patternName + ", plugin-class=" +
                        handlerClassName + " for regular expression: " +
                        regexp);
                }
            } catch (ClassCastException e) {
                if (logger.isLoggable(Level.WARNING)) {
                    logger.log(Level.WARNING,
                        "Error when loading the " + pluginType + "; class: " +
                        e);
                }
            }
        } catch (ClassNotFoundException e) {
            if (logger.isLoggable(Level.WARNING)) {
                logger.log(Level.WARNING,
                    "Exception: " + e + " when loading the " + pluginType +
                    "; class: " + handlerClassName);
            }
        } catch (SecurityException e) {
            if (logger.isLoggable(Level.WARNING)) {
                logger.log(Level.WARNING,
                    "Exception: " + e + " when loading the " + pluginType +
                    "; class: " + handlerClassName);
            }
        } catch (NoSuchMethodException e) {
            if (logger.isLoggable(Level.WARNING)) {
                logger.log(Level.WARNING,
                    "Exception: " + e + " when loading the " + pluginType +
                    "; class: " + handlerClassName);
            }
        } catch (IllegalArgumentException e) {
            if (logger.isLoggable(Level.WARNING)) {
                logger.log(Level.WARNING,
                    "Exception: " + e + " when loading the " + pluginType +
                    "; class: " + handlerClassName);
            }
        } catch (InstantiationException e) {
            if (logger.isLoggable(Level.WARNING)) {
                logger.log(Level.WARNING,
                    "Exception: " + e + " when loading the " + pluginType +
                    "; class: " + handlerClassName);
            }
        } catch (IllegalAccessException e) {
            if (logger.isLoggable(Level.WARNING)) {
                logger.log(Level.WARNING,
                    "Exception: " + e + " when loading the " + pluginType +
                    "; class: " + handlerClassName);
            }
        } catch (InvocationTargetException e) {
            if (logger.isLoggable(Level.WARNING)) {
                logger.log(Level.WARNING,
                    "Exception: " + e + " when loading the " + pluginType +
                    "; class: " + handlerClassName);
            }
        }
    }

    static class MappingTuple {
        String mappingName;
        String handlerClassName;
        String pattern;

        public MappingTuple(String mappingName, String handlerClassName,
            String pattern) {
            this.handlerClassName = handlerClassName;
            this.mappingName = mappingName;
            this.pattern = pattern;
        }
    }

    private static class PatternMapping<T> {
        Pattern regexpPattern;
        T mappedObject;

        PatternMapping(T mappedObject, Pattern regexp) {
            this.mappedObject = mappedObject;
            this.regexpPattern = regexp;
        }
    }
}
TOP

Related Classes of com.ericsson.ssa.sip.dns.Translator$MappingTuple

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.