Example usage for org.jdom2.output Format getPrettyFormat

List of usage examples for org.jdom2.output Format getPrettyFormat

Introduction

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

Prototype

public static Format getPrettyFormat() 

Source Link

Document

Returns a new Format object that performs whitespace beautification with 2-space indents, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy.

Usage

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

License:Open Source License

@GET
@Path("{id}")
@Produces(MediaType.TEXT_XML)/*from  w w w .  ja  v a2 s  .co  m*/
public Response getAttackPath(@Context HttpServletRequest request, @PathParam("id") int id) {
    Monitoring monitoring = ((Monitoring) request.getSession(true).getAttribute("monitoring"));

    if (monitoring == null)
        return Response.status(Status.NO_CONTENT).build();

    Element attackPathXML = org.fiware.cybercaptor.server.api.AttackPathManagement.getAttackPathXML(monitoring,
            id);
    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());

    return Response.ok(output.outputString(attackPathXML)).build();
}

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

License:Open Source License

@GET
@Path("{id}/remediations")
@Produces(MediaType.TEXT_XML)//from w w  w . j  ava2 s . c  o  m
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 || db == null)
        return Response.status(Status.NO_CONTENT).build();

    Element remediationXML = org.fiware.cybercaptor.server.api.AttackPathManagement
            .getRemediationXML(monitoring, id, db);
    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());

    if (remediationXML == null)
        return Response.status(Status.PRECONDITION_FAILED).build();
    return Response.ok(output.outputString(remediationXML)).build();
}

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

License:Open Source License

@GET
@Path("attack_graph")
@Produces(MediaType.TEXT_XML)/*from   w w w  .  j ava 2  s . com*/
public Response getAttackGraph(@Context HttpServletRequest request) {
    Monitoring monitoring = ((Monitoring) request.getSession(true).getAttribute("monitoring"));

    if (monitoring == null)
        return Response.status(Status.NO_CONTENT).build();

    Element attackGraphXML = monitoring.getAttackGraph().toDomElement();
    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
    return Response.ok(output.outputString(attackGraphXML)).build();
}

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

License:Open Source License

/**
 * Get the XML topology//ww  w  .  j  a v a  2  s .com
 *
 * @param request the HTTP Request
 * @return the HTTP Response
 */
@GET
@Path("/topology")
@Produces(MediaType.APPLICATION_XML)
public Response getTopology(@Context HttpServletRequest request) {
    Monitoring monitoring = ((Monitoring) request.getSession(true).getAttribute("monitoring"));

    if (monitoring == null) {
        return Response.ok("The monitoring object is empty. Did you forget to " + "initialize it ?").build();
    }
    return Response.ok(new XMLOutputter(Format.getPrettyFormat())
            .outputString(monitoring.getInformationSystem().toDomXMLElement())).build();
}

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

License:Open Source License

/**
 * Get the attack paths list//w  w w.j av a2 s  .  com
 *
 * @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//from   w  w  w.j a  va  2  s. co 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//  ww w .jav a  2  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//w ww.  ja va2  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//from www.  j  av a2 s  .co 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/*from  w w  w.j a va  2s  .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())));
}