protected void doPost()

in vault-rcp/src/main/java/org/apache/jackrabbit/vault/rcp/impl/RcpServlet.java [159:352]


    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {
        TypedMapWrapper data;
        try {
            data = new TypedMapWrapper(new JSONParser(request.getInputStream()).getParsed());
        } catch (IOException e) {
            log.error("Error while reading json", e);
            response.setStatus(500);
            return;
        }
        String cmd = data.optString(PARAM_CMD, "");
        RcpTask task;
        final String id = data.optString(PARAM_ID, null);;
        try {
            // --------------------------------------------------------------------------------------------< create >---
            boolean isEdit = "edit".equals(cmd);
            if (isEdit || "create".equals(cmd)) {
                if (isEdit) {
                    if (id == null || id.length() == 0) {
                        throw new IllegalArgumentException("Need task id.");
                    }
                }
                String src = data.optString(PARAM_SRC, "");
                if (isEdit && (src == null || src.length() == 0)) {
                    throw new IllegalArgumentException("Need src.");
                }
                String dst = data.optString(PARAM_DST, "");
                String srcCreds = data.optString(PARAM_SRC_CREDS, null);

                RepositoryAddress address = new RepositoryAddress(src);
                Credentials creds = address.getCredentials();
                if (creds != null) {
                    // remove creds from repository address to prevent logging
                    URI uri = address.getURI();
                    address = new RepositoryAddress(
                            new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment())
                    );
                }
                if (srcCreds != null && srcCreds.length() > 0) {
                    creds = createCredentials(srcCreds);
                }
                Boolean recursive = null;
                if (data.containsKey(PARAM_RECURSIVE)) {
                    recursive = data.optBoolean(PARAM_RECURSIVE, false);
                }

                ConnectionOptions.Builder connectionOptionsBuilder = ConnectionOptions.builder();
                connectionOptionsBuilder.useSystemProperties(data.optBoolean(PARAM_USE_SYSTEM_PROPERTIES, false));
                connectionOptionsBuilder.allowSelfSignedCertificates(data.optBoolean(PARAM_ALLOW_SELF_SIGNED_CERTIFICATE, false));
                connectionOptionsBuilder.disableHostnameVerification(data.optBoolean(PARAM_DISABLE_HOSTNAME_VERIFICATION, false));
                int connectionTimeoutMs = data.optInt(PARAM_CONNECTION_TIMEOUT_MS, -1);
                connectionOptionsBuilder.connectionTimeoutMs(connectionTimeoutMs);
                int requestTimeoutMs = data.optInt(PARAM_REQUEST_TIMEOUT_MS, -1);
                connectionOptionsBuilder.requestTimeoutMs(requestTimeoutMs);
                int socketTimeoutMs = data.optInt(PARAM_SOCKET_TIMEOUT_MS, -1);
                connectionOptionsBuilder.socketTimeoutMs(socketTimeoutMs);

                if (data.containsKey(PARAM_PROXY_HOST)) {
                    connectionOptionsBuilder.proxyHost(data.getString(PARAM_PROXY_HOST));
                    if (data.containsKey(PARAM_PROXY_PORT)) {
                        connectionOptionsBuilder.proxyPort(data.getInt(PARAM_PROXY_PORT));
                    }
                    if (data.containsKey(PARAM_PROXY_PROTOCOL)) {
                        connectionOptionsBuilder.proxyProtocol(data.getString(PARAM_PROXY_PROTOCOL));
                    }
                    if (data.containsKey(PARAM_PROXY_USERNAME)) {
                        connectionOptionsBuilder.proxyUsername(data.getString(PARAM_PROXY_USERNAME));
                        if (data.containsKey(PARAM_PROXY_PASSWORD)) {
                            connectionOptionsBuilder.proxyPassword(data.getString(PARAM_PROXY_PASSWORD));
                        }
                    }
                }
                if (data.containsKey(PARAM_EXCLUDES)) {
                    List<String> excludeList = data.getStringList(PARAM_EXCLUDES);
                    if (isEdit) {
                        task = taskMgr.editTask(id, address, connectionOptionsBuilder.build(), creds, dst, excludeList, null, recursive);
                    } else {
                        task = taskMgr.addTask(address, connectionOptionsBuilder.build(), creds, dst, id, excludeList, recursive);
                    }
                } else {
                    final WorkspaceFilter filter;
                    if (data.containsKey(PARAM_FILTER)) {
                        DefaultWorkspaceFilter filterImpl = new DefaultWorkspaceFilter();
                        filterImpl.load(IOUtils.toInputStream(data.getString(PARAM_FILTER), StandardCharsets.UTF_8));
                        filter = filterImpl;
                    } else {
                        filter = null;
                    }
                    if (isEdit) {
                        task = taskMgr.editTask(id, address, connectionOptionsBuilder.build(), creds, dst, null, filter, recursive);
                    } else {
                        task = taskMgr.addTask(address, connectionOptionsBuilder.build(), creds, dst, id, filter, recursive);
                    }
                }

                // add additional data
                if (data.containsKey(PARAM_BATCHSIZE)) {
                    task.getRcp().setBatchSize((int) data.getLong(PARAM_BATCHSIZE));
                }
                if (data.containsKey(PARAM_UPDATE)) {
                    task.getRcp().setUpdate(data.optBoolean(PARAM_UPDATE, false));
                }
                if (data.containsKey(PARAM_ONLY_NEWER)) {
                    task.getRcp().setOnlyNewer(data.optBoolean(PARAM_ONLY_NEWER, false));
                }
                if (data.containsKey(PARAM_NO_ORDERING)) {
                    task.getRcp().setNoOrdering(data.optBoolean(PARAM_NO_ORDERING, false));
                }
                if (data.containsKey(PARAM_THROTTLE)) {
                    task.getRcp().setThrottle(data.getLong(PARAM_THROTTLE));
                }
                if (data.containsKey(PARAM_RESUME_FROM)) {
                    task.getRcp().setResumeFrom(data.getString(PARAM_RESUME_FROM));
                }
                if (isEdit) {
                    response.setStatus(HttpServletResponse.SC_OK);
                } else {
                    response.setStatus(HttpServletResponse.SC_CREATED);
                }
                String path = SERVLET_PATH + "/" + task.getId();
                response.setHeader("Location", path);

            // ---------------------------------------------------------------------------------------------< start >---
            } else if ("start".equals(cmd)) {
                if (id == null || id.length() == 0) {
                    throw new IllegalArgumentException("Need task id.");
                }
                task = taskMgr.getTasks().get(id);
                if (task == null) {
                    throw new IllegalArgumentException("No such task with id='" + id + "'");
                }
                task.start(request.getResourceResolver().adaptTo(Session.class));

            // ----------------------------------------------------------------------------------------------< stop >---
            } else if ("stop".equals(cmd)) {
                if (id == null || id.length() == 0) {
                    throw new IllegalArgumentException("Need task id.");
                }
                task = taskMgr.getTasks().get(id);
                if (task == null) {
                    throw new IllegalArgumentException("No such task with id='" + id + "'");
                }
                task.stop();

            // --------------------------------------------------------------------------------------------< remove >---
            } else if ("remove".equals(cmd)) {
                if (id == null || id.length() == 0) {
                    throw new IllegalArgumentException("Need task id.");
                }
                if (!taskMgr.removeTask(id)) {
                    throw new IllegalArgumentException("No such task with id='" + id + "'");
                }
            // --------------------------------------------------------------------------------------------< remove >---
            } else if ("set-credentials".equals(cmd)) {
                // only add the credentials for a certain task id
                if (id == null || id.length() == 0) {
                    throw new IllegalArgumentException("Need task id.");
                }
                String srcCreds = data.optString(PARAM_SRC_CREDS, "");
                final Credentials credentials;
                if (srcCreds.isEmpty()) {
                    credentials = null;
                } else {
                    credentials = createCredentials(srcCreds);
                }
                taskMgr.setSourceCredentials(id, credentials);
            } else {
                throw new IllegalArgumentException("Invalid command.");
            }
            // default response
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            JSONWriter w = new JSONWriter(response.getWriter());
            w.object();
            w.key("status").value("ok");
            w.key("id").value(id);
            w.endObject();

        } catch (Exception e) {
            log.error("Error while executing command {}", cmd, e);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.setStatus(500);
            JSONWriter w = new JSONWriter(response.getWriter());
            try {
                w.object();
                w.key("status").value("error");
                w.key("message").value("Error while executing '" + cmd + "': " + e.getMessage());
                w.endObject();
            } catch (IOException e1) {
                // ignore
            }
        }
    }