Example usage for org.jdom2.output XMLOutputter XMLOutputter

List of usage examples for org.jdom2.output XMLOutputter XMLOutputter

Introduction

In this page you can find the example usage for org.jdom2.output XMLOutputter XMLOutputter.

Prototype

public XMLOutputter(XMLOutputProcessor processor) 

Source Link

Document

This will create an XMLOutputter with the specified XMLOutputProcessor.

Usage

From source file:org.fiware.cybercaptor.server.rest.RestJsonAPI.java

License:Open Source License

/**
 * Get the attack paths list/*  ww  w  .ja va 2  s.c  o  m*/
 *
 * @param request the HTTP Request
 * @return the HTTP Response
 */
@GET
@Path("attack_path/list")
@Produces(MediaType.APPLICATION_JSON)
public Response getList(@Context HttpServletRequest request) {
    Monitoring monitoring = ((Monitoring) request.getSession(true).getAttribute("monitoring"));

    if (monitoring == null) {
        return RestApplication.returnErrorMessage(request,
                "The monitoring object is empty. Did you forget to " + "initialize it ?");
    }

    Element attackPathsXML = AttackPathManagement.getAttackPathsXML(monitoring);
    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
    return RestApplication.returnJsonObject(request, XML.toJSONObject(output.outputString(attackPathsXML)));

}

From source file:org.fiware.cybercaptor.server.rest.RestJsonAPI.java

License:Open Source License

/**
 * Get one attack path (id starting from 0)
 *
 * @param request the HTTP Request// w  w  w .j a  v  a 2s  . c  o m
 * @param id      the id of the attack path to get
 * @return the HTTP Response
 */
@GET
@Path("attack_path/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getAttackPath(@Context HttpServletRequest request, @PathParam("id") int id) {
    Monitoring monitoring = ((Monitoring) request.getSession(true).getAttribute("monitoring"));

    if (monitoring == null) {
        return RestApplication.returnErrorMessage(request,
                "The monitoring object is empty. Did you forget to " + "initialize it ?");
    }

    int numberAttackPaths = monitoring.getAttackPathList().size();

    if (id >= numberAttackPaths) {
        return RestApplication.returnErrorMessage(request,
                "The attack path " + id + " does not exist. There are only" + numberAttackPaths
                        + " attack paths (0 to " + (numberAttackPaths - 1) + ")");
    }

    Element attackPathXML = AttackPathManagement.getAttackPathXML(monitoring, id);
    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());

    return RestApplication.returnJsonObject(request, XML.toJSONObject(output.outputString(attackPathXML)));
}

From source file:org.fiware.cybercaptor.server.rest.RestJsonAPI.java

License:Open Source License

/**
 * Compute and return the remediations for an attack path
 *
 * @param request the HTTP Request/*w w  w  . jav a2 s . co  m*/
 * @param id      the identifier of the attack path for which the remediations will be computed
 * @return the HTTP Response
 */
@GET
@Path("attack_path/{id}/remediations")
@Produces(MediaType.APPLICATION_JSON)
public Response getAttackPathRemediations(@Context HttpServletRequest request, @PathParam("id") int id) {
    Monitoring monitoring = ((Monitoring) request.getSession(true).getAttribute("monitoring"));
    Database db = ((Database) request.getSession(true).getAttribute("database"));

    if (monitoring == null) {
        return RestApplication.returnErrorMessage(request,
                "The monitoring object is empty. Did you forget to " + "initialize it ?");
    }

    if (db == null) {
        return RestApplication.returnErrorMessage(request,
                "The database object is empty. Did you forget to " + "initialize it ?");
    }

    int numberAttackPaths = monitoring.getAttackPathList().size();

    if (id >= numberAttackPaths) {
        return RestApplication.returnErrorMessage(request,
                "The attack path " + id + " does not exist. There are only" + numberAttackPaths
                        + " attack paths (0 to " + (numberAttackPaths - 1) + ")");
    }

    Element remediationXML = AttackPathManagement.getRemediationXML(monitoring, id, db);
    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());

    return RestApplication.returnJsonObject(request, XML.toJSONObject(output.outputString(remediationXML)));
}

From source file:org.fiware.cybercaptor.server.rest.RestJsonAPI.java

License:Open Source License

/**
 * Simulate the remediation id_remediation of the path id, and compute the new attack graph
 *
 * @param request        the HTTP Request
 * @param id             the identifier of the attack path for which the remediations will be computed
 * @param id_remediation the identifier of the remediation to simulate
 * @return the HTTP Response//from   ww w.  j a  va  2  s . c o m
 */
@GET
@Path("attack_path/{id}/remediation/{id-remediation}")
@Produces(MediaType.APPLICATION_JSON)
public Response simulateRemediationInAttackGraph(@Context HttpServletRequest request, @PathParam("id") int id,
        @PathParam("id-remediation") int id_remediation) throws Exception {
    Monitoring monitoring = ((Monitoring) request.getSession(true).getAttribute("monitoring"));
    Database db = ((Database) request.getSession(true).getAttribute("database"));

    if (monitoring == null) {
        return RestApplication.returnErrorMessage(request,
                "The monitoring object is empty. Did you forget to " + "initialize it ?");
    }

    if (db == null) {
        return RestApplication.returnErrorMessage(request,
                "The database object is empty. Did you forget to " + "initialize it ?");
    }

    int numberAttackPaths = monitoring.getAttackPathList().size();

    if (id >= numberAttackPaths) {
        return RestApplication.returnErrorMessage(request,
                "The attack path " + id + " does not exist. There are only" + numberAttackPaths
                        + " attack paths (0 to " + (numberAttackPaths - 1) + ")");
    }

    List<DeployableRemediation> remediations = monitoring.getAttackPathList().get(id).getDeployableRemediations(
            monitoring.getInformationSystem(), db.getConn(), monitoring.getPathToCostParametersFolder());

    int numberRemediations = remediations.size();

    if (id_remediation >= numberRemediations) {
        return RestApplication.returnErrorMessage(request,
                "The remediation " + id_remediation + " does not exist. There are only" + numberRemediations
                        + " remediations (0 to " + (numberRemediations - 1) + ")");
    }
    DeployableRemediation deployableRemediation = remediations.get(id_remediation);

    AttackGraph simulatedAttackGraph;

    try {
        simulatedAttackGraph = monitoring.getAttackGraph().clone();

        for (int i = 0; i < deployableRemediation.getActions().size(); i++) {
            Vertex vertexToDelete = deployableRemediation.getActions().get(i).getRemediationAction()
                    .getRelatedVertex();
            simulatedAttackGraph.deleteVertex(simulatedAttackGraph.vertices.get(vertexToDelete.id));
        }

        AttackPathManagement.scoreAttackPaths(simulatedAttackGraph,
                monitoring.getAttackGraph().getNumberOfVertices());

        Element attackGraphXML = simulatedAttackGraph.toDomElement();
        XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
        return RestApplication.returnJsonObject(request, XML.toJSONObject(output.outputString(attackGraphXML)));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return RestApplication.returnErrorMessage(request, "Error during the simulation of the remediation.");

}

From source file:org.fiware.cybercaptor.server.rest.RestJsonAPI.java

License:Open Source License

/**
 * Get the whole attack graph/* w  w w .java  2s  . c o  m*/
 *
 * @param request the HTTP Request
 * @return the HTTP Response
 */
@GET
@Path("attack_graph")
@Produces(MediaType.APPLICATION_JSON)
public Response getAttackGraph(@Context HttpServletRequest request) {
    Monitoring monitoring = ((Monitoring) request.getSession(true).getAttribute("monitoring"));

    if (monitoring == null) {
        return RestApplication.returnErrorMessage(request,
                "The monitoring object is empty. Did you forget to " + "initialize it ?");
    }

    Element attackGraphXML = monitoring.getAttackGraph().toDomElement();
    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
    return RestApplication.returnJsonObject(request, XML.toJSONObject(output.outputString(attackGraphXML)));
}

From source file:org.fiware.cybercaptor.server.rest.RestJsonConfiguration.java

License:Open Source License

/**
 * Get the global cost parameters//  www . j av  a  2  s . c o  m
 *
 * @param request the HTTP Request
 * @return the HTTP Response
 */
@GET
@Path("/remediation-cost-parameters/global")
@Produces(MediaType.APPLICATION_JSON)
public Response getGlobalCostParameters(@Context HttpServletRequest request) {
    String costParametersFolderPath = ProjectProperties.getProperty("cost-parameters-path");
    GlobalParameters globalParameters = new GlobalParameters();
    try {
        globalParameters.loadFromXMLFile(costParametersFolderPath + "/" + GlobalParameters.FILE_NAME);
    } catch (Exception e) {
        return RestApplication.returnErrorMessage(request,
                "The global parameters " + "can not be load: " + e.getMessage());
    }

    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
    return RestApplication.returnJsonObject(request,
            XML.toJSONObject(output.outputString(globalParameters.toDomElement())));
}

From source file:org.fiware.cybercaptor.server.rest.RestJsonConfiguration.java

License:Open Source License

/**
 * Generic function to build the HTTP Reponse for operational cost parameters (snort rule, firewall rule, patch...)
 *
 * @param request               the HTTP Request
 * @param costParameterFileName the filename of the file to get
 * @return the HTTP response/*  w  w w .j av a2  s  . co  m*/
 */
private Response buildResponseForOperationalCostParameters(HttpServletRequest request,
        String costParameterFileName) {
    String costParametersFolderPath = ProjectProperties.getProperty("cost-parameters-path");
    OperationalCostParameters operationalCostParameters = new OperationalCostParameters();
    try {
        operationalCostParameters.loadFromXMLFile(costParametersFolderPath + "/" + costParameterFileName);
    } catch (Exception e) {
        return RestApplication.returnErrorMessage(request,
                "The operational cost parameters " + "can not be load: " + e.getMessage());
    }

    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
    return RestApplication.returnJsonObject(request,
            XML.toJSONObject(output.outputString(operationalCostParameters.toDomElement())));
}

From source file:org.fiware.cybercaptor.server.scoring.gui.Launch.java

License:Open Source License

/**
 * Save the list of attack paths into a XML file
 *
 * @param filePath    the paths where the list XML of attack paths can be written
 * @param AttackPaths list of attack paths
 * @throws Exception/*www . j a  v a2  s  .co  m*/
 */
protected static void saveToXmlFile(String filePath, Graph[] AttackPaths) throws Exception {
    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
    Element MainRoot = new Element("attack_paths");
    Element root;
    if (AttackPaths != null) {
        for (Graph AttackPathBuffer : AttackPaths) {
            Arc[] AttackPathArcs = AttackPathBuffer.getArcs();
            root = new Element("attack_path");
            Element scoringElement = new Element("scoring");
            scoringElement.setText(String.valueOf(formulas.MinMax(formulas.globalScore(AttackPathBuffer),
                    AttackPathBuffer.getVertices().length)));
            root.addContent(scoringElement);
            if (AttackPathArcs != null) {
                Element arcsElement = new Element("arcs");
                for (Arc AttackPathArc : AttackPathArcs) {
                    Element arcElement = new Element("arc");
                    arcsElement.addContent(arcElement);
                    Element srcElement = new Element("src");
                    srcElement.setText(String.valueOf(AttackPathArc.getSource()));
                    arcElement.addContent(srcElement);
                    Element dstElement = new Element("dst");
                    dstElement.setText(String.valueOf(AttackPathArc.getDestination()));
                    arcElement.addContent(dstElement);
                }
                root.addContent(arcsElement);
                MainRoot.addContent(root);
            }
        }
    }
    output.output(MainRoot, new FileOutputStream(filePath));
}

From source file:org.fnppl.opensdx.keyserverfe.Helper.java

License:Open Source License

public static String elementToString(Element e) {
    if (e == null)
        return null;

    try {//ww w  . j a v a  2 s .  co  m
        org.jdom2.output.Format f = org.jdom2.output.Format.getPrettyFormat();
        f.setEncoding(XML_OUTPUTTER_CHARSET);
        XMLOutputter xmlOutputter = new XMLOutputter(f);

        ByteArrayOutputStream baous = new ByteArrayOutputStream();
        xmlOutputter.output(e, baous);
        return baous.toString();
    } catch (Exception x) {
        x.printStackTrace();
        return null;
    }
}

From source file:org.fnppl.opensdx.keyserverfe.Helper.java

License:Open Source License

public static String elementToString(Document doc) {
    if (doc == null)
        return null;

    try {//from w  w  w . java 2s.  c  o m
        org.jdom2.output.Format f = org.jdom2.output.Format.getPrettyFormat();
        f.setEncoding(XML_OUTPUTTER_CHARSET);
        XMLOutputter xmlOutputter = new XMLOutputter(f);

        ByteArrayOutputStream baous = new ByteArrayOutputStream();

        return baous.toString();
    } catch (Exception x) {
        x.printStackTrace();
        return null;
    }
}