001/*
002 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.ecm.platform.importer.executor.jaxrs;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.Path;
023import javax.ws.rs.Produces;
024import javax.ws.rs.QueryParam;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.platform.importer.base.ImporterRunner;
029import org.nuxeo.ecm.platform.importer.service.DefaultImporterService;
030import org.nuxeo.runtime.api.Framework;
031
032@Path("fileImporter")
033public class HttpFileImporterExecutor extends AbstractJaxRSImporterExecutor {
034
035    private static final Log log = LogFactory.getLog(HttpFileImporterExecutor.class);
036
037    @Override
038    protected Log getJavaLogger() {
039        return log;
040    }
041
042    @GET
043    @Path("run")
044    @Produces("text/plain; charset=UTF-8")
045    public String run(@QueryParam("leafType") String leafType, @QueryParam("folderishType") String folderishType,
046            @QueryParam("inputPath") String inputPath, @QueryParam("targetPath") String targetPath,
047            @QueryParam("skipRootContainerCreation") Boolean skipRootContainerCreation,
048            @QueryParam("batchSize") Integer batchSize, @QueryParam("nbThreads") Integer nbThreads,
049            @QueryParam("interactive") Boolean interactive,
050            @QueryParam("transactionTimeout") Integer transactionTimeout) {
051
052        if (inputPath == null || targetPath == null) {
053            return "Can not import, missing " + (inputPath == null ? "inputPath" : "targetPath");
054        }
055        if (skipRootContainerCreation == null) {
056            skipRootContainerCreation = false;
057        }
058        if (batchSize == null) {
059            batchSize = 5;
060        }
061        if (nbThreads == null) {
062            nbThreads = 5;
063        }
064        if (interactive == null) {
065            interactive = false;
066        }
067        if (transactionTimeout == null) {
068            transactionTimeout = 0;
069        }
070
071        DefaultImporterService defaultImporterService = Framework.getService(DefaultImporterService.class);
072
073        defaultImporterService.setTransactionTimeout(transactionTimeout);
074
075        if (leafType != null || folderishType != null) {
076            log.info("Importing with the specified doc types");
077            return defaultImporterService.importDocuments(this, leafType, folderishType, targetPath, inputPath,
078                    skipRootContainerCreation, batchSize, nbThreads, interactive);
079        } else {
080            log.info("Importing with the deafult doc types");
081            return defaultImporterService.importDocuments(this, targetPath, inputPath, skipRootContainerCreation,
082                    batchSize, nbThreads, interactive);
083        }
084
085    }
086
087    @Override
088    public String run(ImporterRunner runner, Boolean interactive) {
089        return doRun(runner, interactive);
090    }
091
092}