/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat, Inc. and/or its affiliates, and individual
* contributors as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a full listing
* of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU General Public License, v. 2.0.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* v. 2.0 along with this distribution; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.mobicents.diameter.stack.functional.cxdx.base;
import org.jdiameter.api.Answer;
import org.jdiameter.api.Avp;
import org.jdiameter.api.AvpSet;
import org.jdiameter.api.IllegalDiameterStateException;
import org.jdiameter.api.InternalException;
import org.jdiameter.api.NetworkReqListener;
import org.jdiameter.api.OverloadException;
import org.jdiameter.api.Request;
import org.jdiameter.api.RouteException;
import org.jdiameter.api.cxdx.ServerCxDxSession;
import org.jdiameter.api.cxdx.events.JUserAuthorizationAnswer;
import org.jdiameter.api.cxdx.events.JUserAuthorizationRequest;
import org.jdiameter.common.impl.app.cxdx.JUserAuthorizationAnswerImpl;
import org.mobicents.diameter.stack.functional.Utils;
import org.mobicents.diameter.stack.functional.cxdx.AbstractServer;
/**
*
* @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
* @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
*/
public class ServerUAR extends AbstractServer {
protected boolean receivedUserAuthorization;
protected boolean sentUserAuthorization;
protected JUserAuthorizationRequest request;
/**
*
*/
public ServerUAR() {
}
public void sendUserAuthorization() throws Exception {
if (!receivedUserAuthorization || request == null) {
fail("Did not receive UAR or answer already sent.", null);
throw new Exception("Did not receive UAR or answer already sent. Request: " + this.request);
}
JUserAuthorizationAnswer answer = new JUserAuthorizationAnswerImpl((Request) this.request.getMessage(), 2001);
AvpSet reqSet = request.getMessage().getAvps();
AvpSet set = answer.getMessage().getAvps();
set.removeAvp(Avp.DESTINATION_HOST);
set.removeAvp(Avp.DESTINATION_REALM);
set.addAvp(reqSet.getAvp(Avp.CC_REQUEST_TYPE), reqSet.getAvp(Avp.CC_REQUEST_NUMBER), reqSet.getAvp(Avp.AUTH_APPLICATION_ID));
request = null;
//
// < User-Authorization-Answer> ::= < Diameter Header: 300, PXY, 16777216 >
// < Session-Id >
// { Vendor-Specific-Application-Id }
if (set.getAvp(Avp.VENDOR_SPECIFIC_APPLICATION_ID) == null) {
AvpSet vendorSpecificApplicationId = set.addGroupedAvp(Avp.VENDOR_SPECIFIC_APPLICATION_ID, 0, false, false);
// 1* [ Vendor-Id ]
vendorSpecificApplicationId.addAvp(Avp.VENDOR_ID, getApplicationId().getVendorId(), true);
// 0*1{ Auth-Application-Id }
vendorSpecificApplicationId.addAvp(Avp.AUTH_APPLICATION_ID, getApplicationId().getAuthAppId(), true);
}
// [ Result-Code ]
// [ Experimental-Result ]
// { Auth-Session-State }
if (set.getAvp(Avp.AUTH_SESSION_STATE) == null) {
set.addAvp(Avp.AUTH_SESSION_STATE, 1);
}
// { Origin-Host }
// { Origin-Realm }
// *[ Supported-Features ]
// [ Server-Name ]
// [ Server-Capabilities ]
// [ Wildcarded-IMPU ]
// *[ AVP ]
// *[ Failed-AVP ]
// *[ Proxy-Info ]
// *[ Route-Record ]
this.serverCxDxSession.sendUserAuthorizationAnswer(answer);
Utils.printMessage(log, super.stack.getDictionary(), answer.getMessage(), true);
this.request = null;
this.sentUserAuthorization = true;
}
@Override
public Answer processRequest(Request request) {
int code = request.getCommandCode();
if (code != JUserAuthorizationRequest.code) {
fail("Received Request with code not used by CxDx!. Code[" + request.getCommandCode() + "]", null);
return null;
}
if (super.serverCxDxSession != null) {
// do fail?
fail("Received Request in base listener, not in app specific!" + code, null);
}
else {
try {
super.serverCxDxSession = this.sessionFactory.getNewAppSession(request.getSessionId(), getApplicationId(), ServerCxDxSession.class, (Object) null);
((NetworkReqListener) this.serverCxDxSession).processRequest(request);
}
catch (Exception e) {
e.printStackTrace();
fail(null, e);
}
}
return null;
}
@Override
public void doUserAuthorizationRequest(ServerCxDxSession session, JUserAuthorizationRequest request)
throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
if (this.receivedUserAuthorization) {
fail("Received UAR more than once", null);
return;
}
this.receivedUserAuthorization = true;
this.request = request;
}
public boolean isReceivedUserAuthorization() {
return receivedUserAuthorization;
}
public boolean isSentUserAuthorization() {
return sentUserAuthorization;
}
}