net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskXMsgRqstHandler.CFAsteriskXMsgRqstLogOutHandler.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskXMsgRqstHandler.CFAsteriskXMsgRqstLogOutHandler.java

Source

// Description: Java 8 XML Message SAX Request Log Out Element Handler

/*
 *   Code Factory Asterisk 11 Configuration Model
 *
 *   Copyright (c) 2014-2015 Mark Sobkow
 *   
 *   This program is available as free software under the GNU GPL v3, or
 *   under a commercial license from Mark Sobkow.  For commercial licensing
 *   details, please contact msobkow@sasktel.net.
 *   
 *   Under the terms of the GPL:
 *   
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *     
 *      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
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 */

package net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskXMsgRqstHandler;

import java.math.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import org.apache.commons.codec.binary.Base64;
import org.xml.sax.*;
import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.*;
import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurity.*;
import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternet.*;
import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsterisk.*;
import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurityObj.*;
import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternetObj.*;
import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*;
import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskXMsg.*;

/*
 *   CFAsteriskXMsgRqstLogOutHandler XML SAX Element Handler implementation
 */
public class CFAsteriskXMsgRqstLogOutHandler extends CFLibXmlCoreElementHandler {
    public CFAsteriskXMsgRqstLogOutHandler(CFAsteriskXMsgRqstHandler xmsgRqstHandler) {
        super(xmsgRqstHandler);
    }

    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
        CFAsteriskXMsgSchemaMessageFormatter schemaFormatter = null;
        try {
            // Common XML Attributes
            String attrId = null;
            // Request Attributes
            String attrSecSessionId = null;
            // Attribute Extraction
            String attrLocalName;
            int numAttrs;
            int idxAttr;
            final String S_ProcName = "startElement";
            final String S_LocalName = "LocalName";

            assert qName.equals("RqstLogOut");

            CFAsteriskXMsgRqstHandler xmsgRqstHandler = (CFAsteriskXMsgRqstHandler) getParser();
            if (xmsgRqstHandler == null) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "getParser()");
            }

            schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter();

            ICFAsteriskSchemaObj schemaObj = xmsgRqstHandler.getSchemaObj();
            if (schemaObj == null) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "getParser().getSchemaObj()");
            }

            // Extract Attributes
            numAttrs = attrs.getLength();
            for (idxAttr = 0; idxAttr < numAttrs; idxAttr++) {
                attrLocalName = attrs.getLocalName(idxAttr);
                if (attrLocalName.equals("Id")) {
                    if (attrId != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrId = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("SecSessionId")) {
                    if (attrSecSessionId != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrSecSessionId = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("schemaLocation")) {
                    // ignored
                } else {
                    throw CFLib.getDefaultExceptionFactory().newUnrecognizedAttributeException(getClass(),
                            S_ProcName, getParser().getLocationInfo(), attrLocalName);
                }
            }

            // Ensure that required attributes have values
            if ((attrSecSessionId == null) || (attrSecSessionId.length() <= 0)) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "SecSessionId");
            }

            UUID secSessionId = UUID.fromString(attrSecSessionId);
            if (secSessionId == null) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "converted-secSessionId");
            }
            if (schemaObj.getAuthorization() == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Already disconnected from the database");
            }
            ICFSecuritySecSessionObj secSession = schemaObj.getSecSession();
            if (secSession == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Security session does not exist for this schema");
            }
            if (!secSessionId.equals(secSession.getRequiredSecSessionId())) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Security session id does not match the one established by this schema");
            }
            if (secSession.getOptionalFinish() == null) {
                schemaObj.rollback();
                schemaObj.beginTransaction();
                ICFSecuritySecSessionEditObj editSecSession = secSession.beginEdit();
                editSecSession.setOptionalFinish(Calendar.getInstance());
                editSecSession.update();
                editSecSession.endEdit();
                schemaObj.commit();
            }
            schemaObj.disconnect(false);
            schemaObj.setAuthorization(null);
            String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t"
                    + CFAsteriskXMsgSchemaMessageFormatter.formatRspnLoggedOut("\n\t\t\t", secSessionId) + "\n"
                    + schemaFormatter.formatRspnXmlPostamble();
            ((CFAsteriskXMsgRqstHandler) getParser()).appendResponse(response);
        } catch (RuntimeException e) {
            CFAsteriskXMsgRqstHandler xmsgRqstHandler = ((CFAsteriskXMsgRqstHandler) getParser());
            schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter();
            String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t"
                    + CFAsteriskXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n"
                    + schemaFormatter.formatRspnXmlPostamble();
            xmsgRqstHandler.resetResponse();
            xmsgRqstHandler.appendResponse(response);
            xmsgRqstHandler.setCaughtException(true);
        } catch (Error e) {
            CFAsteriskXMsgRqstHandler xmsgRqstHandler = ((CFAsteriskXMsgRqstHandler) getParser());
            schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter();
            String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t"
                    + CFAsteriskXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n"
                    + schemaFormatter.formatRspnXmlPostamble();
            xmsgRqstHandler.resetResponse();
            xmsgRqstHandler.appendResponse(response);
            xmsgRqstHandler.setCaughtException(true);
        }
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
    }
}