/* * 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.sh.base; import java.io.InputStream; import org.jdiameter.api.Answer; import org.jdiameter.api.ApplicationId; 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.app.AppAnswerEvent; import org.jdiameter.api.app.AppRequestEvent; import org.jdiameter.api.app.AppSession; import org.jdiameter.api.sh.ClientShSession; import org.jdiameter.api.sh.ServerShSession; import org.jdiameter.api.sh.events.ProfileUpdateAnswer; import org.jdiameter.api.sh.events.ProfileUpdateRequest; import org.jdiameter.api.sh.events.PushNotificationAnswer; import org.jdiameter.api.sh.events.PushNotificationRequest; import org.jdiameter.api.sh.events.SubscribeNotificationsAnswer; import org.jdiameter.api.sh.events.SubscribeNotificationsRequest; import org.jdiameter.api.sh.events.UserDataAnswer; import org.jdiameter.api.sh.events.UserDataRequest; import org.jdiameter.common.impl.app.sh.PushNotificationAnswerImpl; import org.jdiameter.common.impl.app.sh.ShSessionFactoryImpl; import org.mobicents.diameter.stack.functional.Utils; import org.mobicents.diameter.stack.functional.sh.AbstractClient; /** * Base implementation of Client * * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a> * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a> */ public class ClientPNR extends AbstractClient { protected boolean sentSubscribeNotifications; protected boolean sentProfileUpdate; protected boolean sentUserData; protected boolean sentPushNotification; protected boolean receiveSubscribeNotifications; protected boolean receiveProfileUpdate; protected boolean receiveUserData; protected boolean receivePushNotification; protected PushNotificationRequest request; /** * */ public ClientPNR() { } // override init, so we dont create session @Override public void init(InputStream configStream, String clientID) throws Exception { try { super.init(configStream, clientID, ApplicationId.createByAuthAppId(10415, 16777217)); ShSessionFactoryImpl shSessionFactory = new ShSessionFactoryImpl(this.sessionFactory); sessionFactory.registerAppFacory(ServerShSession.class, shSessionFactory); sessionFactory.registerAppFacory(ClientShSession.class, shSessionFactory); shSessionFactory.setClientShSessionListener(this); } finally { try { configStream.close(); } catch (Exception e) { e.printStackTrace(); } } } public void sendPushNotification() throws Exception { if (!this.receivePushNotification || this.request == null) { fail("Did not receive NOTIFICATION or answer already sent.", null); throw new Exception("Did not receive NOTIFICATION or answer already sent. Request: " + this.request); } PushNotificationAnswer answer = new PushNotificationAnswerImpl((Request) 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; Utils.printMessage(log, super.stack.getDictionary(), answer.getMessage(), true); // < Push-Notification-Answer > ::=< Diameter Header: 309, PXY, 16777217 > // < 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 } // { Origin-Host } // { Origin-Realm } // *[ Supported-Features ] // *[ AVP ] // *[ Failed-AVP ] // *[ Proxy-Info ] // *[ Route-Record ] Utils.printMessage(log, super.stack.getDictionary(), answer.getMessage(), true); super.clientShSession.sendPushNotificationAnswer(answer); this.sentPushNotification = true; } // ------------ event handlers; @Override public void doSubscribeNotificationsAnswerEvent(ClientShSession session, SubscribeNotificationsRequest request, SubscribeNotificationsAnswer answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException { receiveSubscribeNotifications = true; fail("Received \"SNR\" event, request[" + request + "], answer[" + answer + "], on session[" + session + "]", null); } @Override public void doProfileUpdateAnswerEvent(ClientShSession session, ProfileUpdateRequest request, ProfileUpdateAnswer answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException { receiveProfileUpdate = true; fail("Received \"PUR\" event, request[" + request + "], answer[" + answer + "], on session[" + session + "]", null); } @Override public void doPushNotificationRequestEvent(ClientShSession session, PushNotificationRequest request) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException { receivePushNotification = true; this.request = request; } @Override public void doUserDataAnswerEvent(ClientShSession session, UserDataRequest request, UserDataAnswer answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException { receiveUserData = true; fail("Received \"UDR\" event, request[" + request + "], answer[" + answer + "], on session[" + session + "]", null); } @Override public void doOtherEvent(AppSession session, AppRequestEvent request, AppAnswerEvent answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException { fail("Received \"Other\" event, request[" + request + "], answer[" + answer + "], on session[" + session + "]", null); } /* * (non-Javadoc) * * @see org.jdiameter.api.NetworkReqListener#processRequest(org.jdiameter.api.Request) */ @Override public Answer processRequest(Request request) { int code = request.getCommandCode(); if (code != PushNotificationRequest.code) { fail("Received Request with code not used by Sh!. Code[" + request.getCommandCode() + "]", null); return null; } if (super.clientShSession != null) { // do fail? fail("Received Request in base listener, not in app specific!" + code, null); } else { try { super.clientShSession = this.sessionFactory.getNewAppSession(request.getSessionId(), getApplicationId(), ClientShSession.class, (Object) null); ((NetworkReqListener) this.clientShSession).processRequest(request); } catch (Exception e) { e.printStackTrace(); fail(null, e); } } return null; } @Override protected String getClientURI() { return clientURI; } public boolean isSentSubscribeNotifications() { return sentSubscribeNotifications; } public boolean isSentProfileUpdate() { return sentProfileUpdate; } public boolean isSentUserData() { return sentUserData; } public boolean isSentPushNotification() { return sentPushNotification; } public boolean isReceiveSubscribeNotifications() { return receiveSubscribeNotifications; } public boolean isReceiveProfileUpdate() { return receiveProfileUpdate; } public boolean isReceiveUserData() { return receiveUserData; } public boolean isReceivePushNotification() { return receivePushNotification; } }