Exemplo n.º 1
0
 protected SyntaxChecker getCheckerBPMN(Document document) {
   BPMNRDFImporter importer = new BPMNRDFImporter(document);
   BPMNDiagram diagram = importer.loadBPMN();
   return diagram.getSyntaxChecker();
 }
  protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {

    res.setContentType("text/html");
    try {
      if (config == null) {
        config = new PropertiesConfiguration("pnengine.properties");
      }
    } catch (ConfigurationException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    String postVariable = config.getString("pnengine.post_variable");
    String engineURL = config.getString("pnengine.url") + "/petrinets";
    String engineURL_host = config.getString("pnengine.url");
    String modelURL = config.getString("pnengine.default_model_url");
    String formURL = null;
    String bindingsURL = null;

    String rdf = req.getParameter("data");
    String diagramTitle = req.getParameter("title");

    DocumentBuilder builder;
    BPMNDiagram diagram;
    try {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      builder = factory.newDocumentBuilder();
      Document document = builder.parse(new ByteArrayInputStream(rdf.getBytes()));
      BPMNRDFImporter importer = new BPMNRDFImporter(document);
      diagram = (BPMNDiagram) importer.loadBPMN();

      String basefilename = String.valueOf(System.currentTimeMillis());
      String tmpPNMLFile =
          this.getServletContext().getRealPath("/")
              + "tmp"
              + File.separator
              + basefilename
              + ".pnml";
      BufferedWriter out1 = new BufferedWriter(new FileWriter(tmpPNMLFile));

      // URL only for testing purposes...
      ExecConverter converter =
          new ExecConverter(diagram, modelURL, this.getServletContext().getRealPath("/"));
      converter.setBaseFileName(
          this.getServletContext().getRealPath("/") + "tmp" + File.separator + basefilename);
      PetriNet net = converter.convert();
      ExecPetriNet execnet = (ExecPetriNet) net;
      Document pnmlDoc = builder.newDocument();

      ExecPNPNMLExporter exp = new ExecPNPNMLExporter();
      execnet.setName(diagramTitle);
      exp.savePetriNet(pnmlDoc, execnet);

      OutputFormat format = new OutputFormat(pnmlDoc);
      XMLSerializer serial = new XMLSerializer(out1, format);
      serial.asDOMSerializer();
      serial.serialize(pnmlDoc.getDocumentElement());
      out1.close();

      StringWriter stringOut = new StringWriter();
      XMLSerializer serial2 = new XMLSerializer(stringOut, format);
      serial2.asDOMSerializer();

      serial2.serialize(pnmlDoc.getDocumentElement());

      URL url_engine = new URL(engineURL);
      HttpURLConnection connection_engine = (HttpURLConnection) url_engine.openConnection();
      connection_engine.setRequestMethod("POST");

      String encoding = new sun.misc.BASE64Encoder().encode("testuser:"******"Authorization", "Basic " + encoding);

      connection_engine.setUseCaches(false);
      connection_engine.setDoInput(true);
      connection_engine.setDoOutput(true);

      String escaped_content =
          postVariable + "=" + URLEncoder.encode(stringOut.toString(), "UTF-8");

      connection_engine.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      connection_engine.setRequestProperty(
          "Accept",
          "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");

      // connection_engine.setRequestProperty("Content-Length",
      // ""+escaped_content.getBytes().length);
      String errorMessage = null;
      try {
        connection_engine.getOutputStream().write(escaped_content.getBytes());
        connection_engine.connect();
      } catch (ConnectException e) {
        errorMessage = e.getMessage();
      }

      // output link address
      res.getWriter().print("tmp/" + basefilename + ".pnml" + "\">View PNML</a><br/><br/>");
      if (errorMessage == null && connection_engine.getResponseCode() == 200) {
        res.getWriter()
            .println(
                "Deployment to Engine <a href=\""
                    + engineURL_host
                    + "/worklist\" target=\"_blank\">"
                    + engineURL_host
                    + "</a><br/><b>successful</b>!");
      } else {
        res.getWriter()
            .println(
                "Deployment to Engine <a href=\""
                    + engineURL
                    + "\" target=\"_blank\">"
                    + engineURL
                    + "</a><br/><b>failed</b> with message: \n"
                    + errorMessage
                    + "!");
      }

    } catch (ParserConfigurationException e1) {
      res.getWriter().println(e1.getMessage());
    } catch (SAXException e1) {
      res.getWriter().println(e1.getMessage());
    }
  }