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

org.mobicents.servlet.sip.address.ParameterableHeaderImpl Maven / Gradle / Ivy

There is a newer version: 4.0.91
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2011, Red Hat, Inc. and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.mobicents.servlet.sip.address;

import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import javax.sip.header.Header;
import javax.sip.header.Parameters;

import org.mobicents.servlet.sip.address.AddressImpl.ModifiableRule;

/**
 * This class is impl of Parameterable object returned
 * 
 * @author baranowb
 * 
 */
public class ParameterableHeaderImpl extends ParameterableImpl {
	private static final long serialVersionUID = 1L;
	protected String value = null;
	
	public ParameterableHeaderImpl() {
		super();
	}

	public ParameterableHeaderImpl(Header header, String value, Map params, ModifiableRule isModifiable) {
		// General form of parametrable header
		super(header, params, isModifiable);
		this.value = value;
	}

	@Override
	public Object clone() {
		ParameterableHeaderImpl cloned = new ParameterableHeaderImpl();
		cloned.parameters = cloneParameters(super.parameters);
		cloned.value = this.value;
		cloned.header = (Parameters)((Header)super.header).clone();
		return cloned;
	}

	public String getValue() {
		return this.value;
	}

	public void setValue(String value) {
		if(value == null) {
			throw new NullPointerException("value is null ! ");
		}
		if(isModifiable == ModifiableRule.NotModifiable) {
			throw new IllegalStateException("it is forbidden for an application to set the From Header");
		}		
		if(isModifiable == ModifiableRule.From || isModifiable == ModifiableRule.To) {
			throw new IllegalStateException("it is forbidden for an application to set the From or To Header");
		}
		this.value = value;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ParameterableHeaderImpl other = (ParameterableHeaderImpl) obj;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		String retVal = value;
		if(retVal.trim().startsWith("<") && !retVal.trim().endsWith(">")) {
			retVal = retVal.concat(">");
		}
		if(!super.toString().trim().equals("")) {
			retVal = retVal.concat(";" + super.toString());
		}
		return retVal;
	}

	public static final Map cloneParameters(Map parametersToClone) {
		Map params = new ConcurrentHashMap();
		for(Entry param : parametersToClone.entrySet()) {			
			params.put(param.getKey(), param.getValue());
		}
		return params;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy