/*
* 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;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
import javax.servlet.sip.Address;
import javax.servlet.sip.ServletParseException;
import javax.servlet.sip.URI;
public class AddressPolicyProxy extends AddressImpl {
/**
*
*/
private static final long serialVersionUID = -7115615935577947939L;
private static final String ILLEGAL_TO_CHANGE_FROM_TO_TAG = "Illegal to change 'tag' on To/From Header";
private static final String ILLEGAL_TO_CHANGE_SYSTEM_HEADER = "Illegal to change System Header";
private transient AddressImpl delegate;
private boolean isDelegateReadonly = true;
private String header;
private DialogFragment df;
private SipSessionImplBase session;
public AddressPolicyProxy(String headerName, AddressImpl address,
SipSessionBase session, DialogFragment df) throws ServletParseException {
super(address.toString());
header = headerName;
delegate = address;
isDelegateReadonly = delegate.isReadOnly();
this.session = (SipSessionImplBase) session;
this.df = df;
}
public AddressImpl getDelegate() {
if (delegate == null) {
return this;
} else {
return delegate;
}
}
// Getters
public String getDisplayName() {
return getDelegate().getDisplayName();
}
public int getExpires() {
return getDelegate().getExpires();
}
public float getQ() {
return getDelegate().getQ();
}
public boolean isWildcard() {
return getDelegate().isWildcard();
}
public String getParameter(String key) {
return getDelegate().getParameter(key);
}
public Iterator<String> getParameterNames() {
return getDelegate().getParameterNames();
}
public Set<Entry<String, String>> getParameters() {
return getDelegate().getParameters();
}
public String getValue() {
return getDelegate().getValue();
}
// Setters
public void setDisplayName(String displayName) {
getDelegate().setReadOnly(false);
getDelegate().setDisplayName(displayName);
getDelegate().setReadOnly(isDelegateReadonly);
updateSession();
}
public void setExpires(int expires) {
getDelegate().setReadOnly(false);
getDelegate().setExpires(expires);
getDelegate().setReadOnly(isDelegateReadonly);
updateSession();
}
public void setQ(float q) {
getDelegate().setReadOnly(false);
getDelegate().setQ(q);
getDelegate().setReadOnly(isDelegateReadonly);
updateSession();
}
public void setURI(URI uri) {
getDelegate().setReadOnly(false);
if ( uri instanceof SipUriPolicyProxy ) {
getDelegate().setURI( ((SipUriPolicyProxy) uri).getDelegate());
} else {
getDelegate().setURI(uri);
}
getDelegate().setReadOnly(isDelegateReadonly);
updateSession();
}
public void removeParameter(String name) {
validateAccess(name);
getDelegate().setReadOnly(false);
getDelegate().removeParameter(name);
getDelegate().setReadOnly(isDelegateReadonly);
updateSession();
}
public void setParameter(String name, String value) {
validateAccess(name);
getDelegate().setReadOnly(false);
getDelegate().setParameter(name, value);
getDelegate().setReadOnly(isDelegateReadonly);
updateSession();
}
public void setValue(String value) {
getDelegate().setReadOnly(false);
getDelegate().setValue(value);
getDelegate().setReadOnly(isDelegateReadonly);
updateSession();
}
public URI getURI() {
return getDelegate().getURI();
// @TODO. We will need to enable the following code.
// It is not any more restrictive than today.
/*
URI uri = getDelegate().getURI();
// SipUriPolicyProxy is a wrapper class that
// controls setting of uri data for system
// heders. From/To uri can freely be changed
boolean isFromTo = Header.FROM.equals(header) ||
Header.TO.equals(header);
if ( isDelegateReadoly == true && !isFromTo ) {
// System Header
if (uri.isSipURI()) {
SipURIImpl sipUri = (SipURIImpl) uri;
boolean secure = sipUri.isSecure();
String user = sipUri.getUser();
String host = sipUri.getHost();
return new SipUriPolicyProxy(header,sipUri,secure,user,host);
}
}
return uri;
*/
}
public Object clone() {
return getDelegate().clone();
}
public boolean equals( Object o ) {
return getDelegate().equals(o);
}
private void updateSession() {
if (session.isValid()) {
DialogSet ds = session.getDS();
if (ds != null ) {
if (matchTag(ds.getFrom(), this.getDelegate())) {
ds.setFrom(this.getDelegate());
}
Iterator<DialogFragment> fragments = ds.getDialogs();
while (fragments.hasNext()) {
DialogFragment d = fragments.next();
if (d.isValid()) {
if (matchTag(d.getFrom(), this.getDelegate())) {
d.setFrom(this.getDelegate());
}
}
}
}
if (df != null && matchTag(df.getFrom(), this.getDelegate())) {
df.setFrom(this.getDelegate());
}
Address to = session.getPFieldTo();
if (matchTag(to, this.getDelegate())) {
session.setPFieldTo(this.getDelegate());
}
}
}
private boolean matchTag(Address source, Address dest) {
String sourceTag = source.getParameter("tag");
String destTag = dest.getParameter("tag");
return sourceTag != null ? sourceTag.equals(destTag) : destTag == null;
}
private void validateAccess(String parameter) {
//Tag cannot be changed.
if ("tag".equals(parameter)) {
throw new IllegalStateException(ILLEGAL_TO_CHANGE_FROM_TO_TAG);
}
}
}