使用Orbeon发布表单上的REST API

时间:2016-03-09 19:22:00

标签: orbeon

我正在浏览文档,了解如何处理来自Orbeon表单的提交,我收集了一些数据,然后通过REST提交给另一个应用程序。我没有看到任何显示如何做到这一点。 Orbeon是否提供了这样做的功能,还是我需要在背面编写一些JSP或其他东西来处理它?

1 个答案:

答案 0 :(得分:1)

我的理解是,您必须自己提供/实施REST服务。您不限于在Java中执行此操作,但如果这是您的首选语言,那么这是一个非常简单的servlet的样子。在这种情况下,REST服务将表单保存在临时目录中的文件中。

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Optional;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FormDumpServlet extends HttpServlet {
    private static final Logger logger = Logger.getLogger(FormDumpServlet.class.getName());
    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS");


    protected Optional<String> makeTempDir() {
        final String dir = System.getProperty("java.io.tmpdir");
        logger.info(String.format("java.io.tmpdir=%s", dir));
        if (dir == null) {
            logger.severe("java.io.tmpdir is null, can't create temp directory");
            return Optional.empty();
        }
        final File f = new File(dir,"form-dumps");
        if (f.exists() && f.isDirectory() && f.canWrite()) {
            return Optional.of(f.getAbsolutePath());
        }
        if (f.mkdir()) {
            return Optional.of(f.getAbsolutePath());
        }
        logger.severe(String.format("failed to create temp dir <%s>", f.getAbsolutePath()));
        return Optional.empty();
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getPathInfo();
        if (!path.equalsIgnoreCase("/accept-form")) {
            resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        Enumeration<String> parameterNames = req.getParameterNames();
        while(parameterNames.hasMoreElements()) {
            final String name = parameterNames.nextElement();
            final String value = req.getParameter(name);
            logger.info(String.format("parameter: name=<%s>, value=<%s>", name, value));
        }
        Optional<String> tempPath = makeTempDir();
        if (tempPath.isPresent()) {
            String fn = String.format("%s.xml", FORMAT.format(new Date()));
            File f = new File(new File(tempPath.get()), fn);
            logger.info(String.format("saving form to file <%s>", f.getAbsolutePath()));
            try(PrintWriter pw = new PrintWriter(new FileWriter(f))) {
                req.getReader().lines().forEach((l) -> pw.println(l));
            }
        }
        resp.setStatus(HttpServletResponse.SC_OK);
    }
}

您还必须在properties-local.xml中配置一个属性,该属性将表单的发送操作(应用程序my_form中名称为my_application的表单)连接到REST端点。此属性可能如下所示:

<property
        as="xs:string"
        name="oxf.fr.detail.process.send.my_application.my_form"
        >
        require-valid
        then save-final
        then send(uri = "http://localhost:8080/my-form-dump-servlet/accept-form")
        then success-message(message = "Success: the form was transferred to the REST service") 
    </property>