Package de.netsysit.model

Source Code of de.netsysit.model.XMLPolicyDBWriter

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package de.netsysit.model;

import de.netsysit.policymanager.Enums.ApplicationBase;
import de.netsysit.policymanager.PolicyManager;
import de.netsysit.policymanager.PolicyUtilities;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import javax.swing.JOptionPane;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.Namespace;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

/**
*
* @author Ducksoul
*/
public class XMLPolicyDBWriter {
    private DataBaseModel dbModel;
    private ResourceBundle mb;
    private LevelModel low;
    private LevelModel normal;
    private LevelModel high;
   
    public XMLPolicyDBWriter() {
        mb = PolicyUtilities.getMessageBundle();
        dbModel = PolicyManager.getPolicyModel();
       
        low = dbModel.getBaseLow();
        normal = dbModel.getBaseNormal();
        high = dbModel.getBaseHigh();
    }

    public void savePolicyDB(String path) throws Exception {
            // Model auf Gültigkeit prüfen
        String message = checkModelB4Save();
        if (message != null) {
            PolicyUtilities.showMessageDialog(message, JOptionPane.WARNING_MESSAGE);
        }
       
        List<ApplicationModel> apps = dbModel.getApplications();
        List<ReceiverListModel> lists = dbModel.getReceiverLists();
        List<PolicyModel> apolicies = dbModel.getAPolicies();
        List<PolicyModel> cpolicies = dbModel.getCPolicies();
        List<PolicyModel> ipolicies = dbModel.getIPolicies();
       
        String filePath = path;
    File unsignedXmlFile;
    FileInputStream unsignedXMLFileInputStream;
    byte[] input;

        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        XMLEventWriter eventWriter = null;
        if(filePath == null) {
            filePath = PolicyUtilities.showSaveDialog();

      if(filePath == null)
                return;
        }

        eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(filePath));
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent end = eventFactory.createDTD("\n");
        XMLEvent tab = eventFactory.createDTD("\t");
        XMLEvent tab2 = eventFactory.createDTD("\t\t");
        XMLEvent tab3 = eventFactory.createDTD("\t\t\t");
        XMLEvent tab4 = eventFactory.createDTD("\t\t\t\t");
        XMLEvent tab5 = eventFactory.createDTD("\t\t\t\t\t");
        XMLEvent tab6 = eventFactory.createDTD("\t\t\t\t\t\t");

        StartDocument startDocument = eventFactory.createStartDocument();
        eventWriter.add(startDocument);
        eventWriter.add(end);

        // Create config open tag
        Namespace ns = eventFactory.createNamespace("n0", "urn:nsit");
        Namespace ns2 = eventFactory.createNamespace("urn:nsit");
        Set<Namespace> nspaces = new HashSet();
        nspaces.add(ns);
        nspaces.add(ns2);
        StartElement dbse = eventFactory.createStartElement("", "urn:nsit", "policyDB", null, nspaces.iterator());
        eventWriter.add(tab);
        eventWriter.add(dbse);
        eventWriter.add(end);
        // Lists
        eventWriter.add(tab2);
        eventWriter.add(eventFactory.createStartElement("", "", "lists"));
        eventWriter.add(end);
        for (int i = 0; i < lists.size(); i++) {
            ReceiverListModel list = lists.get(i);
            eventWriter.add(tab3);
            eventWriter.add(eventFactory.createStartElement("", "", "list"));
            eventWriter.add(end);
            createNode(eventWriter, "id", new Integer(list.getId()).toString(), 4);
            createNode(eventWriter, "name", list.getName(), 4);
            eventWriter.add(tab4);
            eventWriter.add(eventFactory.createStartElement("", "", "receivers"));
            eventWriter.add(end);
            List<ReceiverModel> receivers = list.getReceivers();

      for (int j = 0; j < receivers.size(); j++) {
                ReceiverModel receiver = receivers.get(j);
                eventWriter.add(tab5);
                eventWriter.add(eventFactory.createStartElement("", "", "receiver"));
                eventWriter.add(end);
                createNode(eventWriter, "id", new Integer(receiver.getId()).toString(), 6);
                createNode(eventWriter, "name", receiver.getName(), 6);
                eventWriter.add(tab5);
                eventWriter.add(eventFactory.createEndElement("", "", "receiver"));
                eventWriter.add(end);
            }

      eventWriter.add(tab4);
            eventWriter.add(eventFactory.createEndElement("", "", "receivers"));
            eventWriter.add(end);
            eventWriter.add(tab3);
            eventWriter.add(eventFactory.createEndElement("", "", "list"));
            eventWriter.add(end);
        }

    eventWriter.add(tab2);
        eventWriter.add(eventFactory.createEndElement("", "", "lists"));
        eventWriter.add(end);

        // Apps
        eventWriter.add(tab2);
        eventWriter.add(eventFactory.createStartElement("", "", "apps"));
        eventWriter.add(end);
        for (int i = 0; i < apps.size(); i++) {
            ApplicationModel app = apps.get(i);
            eventWriter.add(tab3);
            eventWriter.add(eventFactory.createStartElement("", "", "app"));
            eventWriter.add(end);
            createNode(eventWriter, "id", new Integer(app.getId()).toString(), 4);
            createNode(eventWriter, "name", app.getName(), 4);
            createNode(eventWriter, "expectRequestEncrypted", new Integer(PolicyUtilities.boolToInt(app.isExpectRequestEncrypted())).toString(), 4);
            createNode(eventWriter, "a-level", new Integer(ApplicationBaseToInt(app.getBaseA())).toString(), 4);
            createNode(eventWriter, "c-level", new Integer(ApplicationBaseToInt(app.getBaseC())).toString(), 4);
            createNode(eventWriter, "i-level", new Integer(ApplicationBaseToInt(app.getBaseI())).toString(), 4);
            eventWriter.add(tab4);
            eventWriter.add(eventFactory.createStartElement("", "", "applists"));
            eventWriter.add(end);
            List<ReceiverListModel> applists = app.getReceiverLists();
            for (int j = 0; j < applists.size(); j++) {
                createNode(eventWriter, "listID", new Integer(applists.get(j).getId()).toString(), 5);
            }
            eventWriter.add(tab4);
            eventWriter.add(eventFactory.createEndElement("", "", "applists"));
            eventWriter.add(end);
            eventWriter.add(tab3);
            eventWriter.add(eventFactory.createEndElement("", "", "app"));
            eventWriter.add(end);
        }
        eventWriter.add(tab2);
        eventWriter.add(eventFactory.createEndElement("", "", "apps"));
        eventWriter.add(end);
        // Mapping
        eventWriter.add(tab2);
        eventWriter.add(eventFactory.createStartElement("", "", "level2policyMapping"));
        eventWriter.add(end);

        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createStartElement("", "", "level1"));
        eventWriter.add(end);
        if((low != null) && (low.getABasePolicy() != null))
            createNode(eventWriter, "a", new Integer(low.getABasePolicy().getId()).toString(), 4);
        else createNode(eventWriter, "a", "-1", 4);

        if((low != null) && (low.getCBasePolicy() != null))
            createNode(eventWriter, "c", new Integer(low.getCBasePolicy().getId()).toString(), 4);
        else createNode(eventWriter, "c", "-1", 4);

        if((low != null) && (low.getIBasePolicy() != null))
            createNode(eventWriter, "i", new Integer(low.getIBasePolicy().getId()).toString(), 4);
        else createNode(eventWriter, "i", "-1", 4);
        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createEndElement("", "", "level1"));
        eventWriter.add(end);      



        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createStartElement("", "", "level2"));
        eventWriter.add(end);
        if((normal != null) && (normal.getABasePolicy() != null))
            createNode(eventWriter, "a", new Integer(normal.getABasePolicy().getId()).toString(), 4);
        else createNode(eventWriter, "a", "-1", 4);

        if((normal != null) && (normal.getCBasePolicy() != null))
            createNode(eventWriter, "c", new Integer(normal.getCBasePolicy().getId()).toString(), 4);
        else createNode(eventWriter, "c", "-1", 4);

        if((normal != null) && (normal.getIBasePolicy() != null))
            createNode(eventWriter, "i", new Integer(normal.getIBasePolicy().getId()).toString(), 4);
        else createNode(eventWriter, "i", "-1", 4);
        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createEndElement("", "", "level2"));
        eventWriter.add(end);

        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createStartElement("", "", "level3"));
        eventWriter.add(end);
        if((high != null) && (high.getABasePolicy() != null))
            createNode(eventWriter, "a", new Integer(high.getABasePolicy().getId()).toString(), 4);
        else createNode(eventWriter, "a", "-1", 4);

        if((high != null) && (high.getCBasePolicy() != null))
            createNode(eventWriter, "c", new Integer(high.getCBasePolicy().getId()).toString(), 4);
        else createNode(eventWriter, "c", "-1", 4);

        if((high != null) && (high.getIBasePolicy() != null))
            createNode(eventWriter, "i", new Integer(high.getIBasePolicy().getId()).toString(), 4);
        else createNode(eventWriter, "i", "-1", 4);
        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createEndElement("", "", "level3"));
        eventWriter.add(end);

        eventWriter.add(tab2);
        eventWriter.add(eventFactory.createEndElement("", "", "level2policyMapping"));
        eventWriter.add(end);

        // Policies
        eventWriter.add(tab2);
        eventWriter.add(eventFactory.createStartElement("", "", "policies"));
        eventWriter.add(end);

        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createStartElement("", "", "a-policies"));
        eventWriter.add(end);
        for (int i = 0; i < apolicies.size(); i++) {
            PolicyModel policy = apolicies.get(i);
            eventWriter.add(tab4);
            eventWriter.add(eventFactory.createStartElement("", "", "policy"));
            eventWriter.add(end);
            createNode(eventWriter, "name", policy.getName(), 5);
            createNode(eventWriter, "id", new Integer(policy.getId()).toString(), 5);

            if(policy.getFallback() != null)
                createNode(eventWriter, "fallback", new Integer(policy.getFallback().getId()).toString(), 5);
            else createNode(eventWriter, "fallback", "-1", 5);
            if(policy.getAction() != null)
                createNode(eventWriter, "action", policy.getAction().getId(), 5);
            else
                createNode(eventWriter, "action", "-1", 5);
            createNode(eventWriter, "useToken", new Integer(PolicyUtilities.boolToInt(policy.isUseToken())).toString(), 5);
            createNode(eventWriter, "rank", new Integer(policy.getRank()).toString(), 5);
            eventWriter.add(tab4);
            eventWriter.add(eventFactory.createEndElement("", "", "policy"));
            eventWriter.add(end);
        }
        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createEndElement("", "", "a-policies"));
        eventWriter.add(end);

        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createStartElement("", "", "c-policies"));
        eventWriter.add(end);
        for (int i = 0; i < cpolicies.size(); i++) {
            PolicyModel policy = cpolicies.get(i);
            eventWriter.add(tab4);
            eventWriter.add(eventFactory.createStartElement("", "", "policy"));
            eventWriter.add(end);
            createNode(eventWriter, "name", policy.getName(), 5);
            createNode(eventWriter, "id", new Integer(policy.getId()).toString(), 5);

            if(policy.getFallback() != null)
                createNode(eventWriter, "fallback", new Integer(policy.getFallback().getId()).toString(), 5);
            else createNode(eventWriter, "fallback", "-1", 5);
            createNode(eventWriter, "action", policy.getAction().getId(), 5);
            createNode(eventWriter, "useToken", new Integer(PolicyUtilities.boolToInt(policy.isUseToken())).toString(), 5);
            createNode(eventWriter, "rank", new Integer(policy.getRank()).toString(), 5);
            eventWriter.add(tab4);
            eventWriter.add(eventFactory.createEndElement("", "", "policy"));
            eventWriter.add(end);
        }
        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createEndElement("", "", "c-policies"));
        eventWriter.add(end);

        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createStartElement("", "", "i-policies"));
        eventWriter.add(end);
        for (int i = 0; i < ipolicies.size(); i++) {
            PolicyModel policy = ipolicies.get(i);
            eventWriter.add(tab4);
            eventWriter.add(eventFactory.createStartElement("", "", "policy"));
            eventWriter.add(end);
            createNode(eventWriter, "name", policy.getName(), 5);
            createNode(eventWriter, "id", new Integer(policy.getId()).toString(), 5);

            if(policy.getFallback() != null)
                createNode(eventWriter, "fallback", new Integer(policy.getFallback().getId()).toString(), 5);
            else createNode(eventWriter, "fallback", "-1", 5);
            createNode(eventWriter, "action", policy.getAction().getId(), 5);
            createNode(eventWriter, "useToken", new Integer(PolicyUtilities.boolToInt(policy.isUseToken())).toString(), 5);
            createNode(eventWriter, "rank", new Integer(policy.getRank()).toString(), 5);
            eventWriter.add(tab4);
            eventWriter.add(eventFactory.createEndElement("", "", "policy"));
            eventWriter.add(end);
        }
        eventWriter.add(tab3);
        eventWriter.add(eventFactory.createEndElement("", "", "i-policies"));
        eventWriter.add(end);
        eventWriter.add(tab2);
        eventWriter.add(eventFactory.createEndElement("", "", "policies"));
        eventWriter.add(end);

        eventWriter.add(tab);
        eventWriter.add(eventFactory.createEndElement("", "", "policyDB"));
        eventWriter.add(end);
        eventWriter.add(eventFactory.createEndDocument());
        eventWriter.close();

    unsignedXmlFile = new File(filePath);
    unsignedXMLFileInputStream = new FileInputStream(unsignedXmlFile);
    input = new byte[(int) unsignedXmlFile.length()];
    unsignedXMLFileInputStream.read(input);
   
    }

    private void createNode(XMLEventWriter eventWriter, String name,
            String value, int tabs) throws XMLStreamException {

        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent end = eventFactory.createDTD("\n");
        XMLEvent tab = eventFactory.createDTD("\t");
        // Create Start node
        StartElement sElement = eventFactory.createStartElement("", "", name);
        for (int i = 0; i < tabs; i++) {
            eventWriter.add(tab);
        }
        eventWriter.add(sElement);
        // Create Content
        Characters characters = eventFactory.createCharacters(value);
        eventWriter.add(characters);
        // Create End node
        EndElement eElement = eventFactory.createEndElement("", "", name);
        eventWriter.add(eElement);
        eventWriter.add(end);
    }

    private int ApplicationBaseToInt(ApplicationBase base) {
        switch (base) {
            case LOW:
                return 1;
            case NORMAL:
                return 2;
            case HIGH:
                return 3;
            default:
                return 0;
        }
    }
   
    private boolean checkMapping() {
            // prüfen, ob PolicyModel vollständig
        if((low == null) || (normal == null) || (high == null)) {
            return false;
        } else {
                // PolicyMapping niedrig nicht vollständig
            if ((low.getABasePolicy() == null) ||
                (low.getCBasePolicy() == null) ||
                (low.getIBasePolicy() == null)) {
               
                return false;
            } else
                // PolicyMapping mittel nicht vollständig
            if ((normal.getABasePolicy() == null) ||
                (normal.getCBasePolicy() == null) ||
                (normal.getIBasePolicy() == null)) {
               
                return false;
            } else
                // PolicyMapping hoch nicht vollständig
            if ((high.getABasePolicy() == null) ||
                (high.getCBasePolicy() == null) ||
                (high.getIBasePolicy() == null)) {
               
                return false;
            }
        }
       
        return true;
    }
   
    private String checkModelB4Save() {
        mb = PolicyUtilities.getMessageBundle();
        String message = "";
       
        if(!checkMapping()) {
           
            message += mb.getString("mappingincomplete");
        }
       
        if(message.equals("")) {
            message = null;
        } else {
            message = PolicyUtilities.replacePlaceHolder(message, mb.getString("savewarning"));
        }
       
        return message;
    }
}
TOP

Related Classes of de.netsysit.model.XMLPolicyDBWriter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.