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 org.apache.commons.io.IOUtils;
022import org.nuxeo.ecm.core.api.NuxeoException;
023import org.nuxeo.ecm.core.work.api.WorkManager;
024import org.nuxeo.ecm.platform.importer.executor.AbstractImporterExecutor;
025import org.nuxeo.ecm.platform.importer.log.BufferredLogger;
026import org.nuxeo.ecm.platform.importer.log.ImporterLogger;
027import org.nuxeo.runtime.api.Framework;
028import org.nuxeo.runtime.transaction.TransactionHelper;
029
030import java.io.IOException;
031import java.io.InputStream;
032import java.util.concurrent.TimeUnit;
033
034import javax.ws.rs.GET;
035import javax.ws.rs.Path;
036import javax.ws.rs.Produces;
037import javax.ws.rs.QueryParam;
038import javax.ws.rs.core.Response;
039
040@Produces("text/plain; charset=UTF-8")
041public abstract class AbstractJaxRSImporterExecutor extends AbstractImporterExecutor {
042
043    @Override
044    public ImporterLogger getLogger() {
045        if (log == null) {
046            log = new BufferredLogger(getJavaLogger());
047        }
048        return log;
049    }
050
051    @GET
052    @Produces("text/html; charset=UTF-8")
053    public String index() throws IOException {
054        try (InputStream stream = this.getClass().getResource("/static/importForm.html").openStream()) {
055            return IOUtils.toString(stream, "UTF-8");
056        }
057    }
058
059    @GET
060    @Path("log")
061    public String getLogAsString() {
062        return getLogger().getLoggerBuffer();
063    }
064
065    @GET
066    @Path("logActivate")
067    public String enableLogging() {
068        getLogger().setBufferActive(true);
069        return "Logging activated";
070    }
071
072    @GET
073    @Path("logDesactivate")
074    public String disableLogging() {
075        getLogger().setBufferActive(false);
076        return "Logging desactivated";
077    }
078
079    @GET
080    @Path("status")
081    public String getStatus() {
082        return super.getStatus();
083    }
084
085    @GET
086    @Path("running")
087    public String running() {
088        return Boolean.toString(super.isRunning());
089    }
090
091    @GET
092    @Path("kill")
093    public String kill() {
094        return super.kill();
095    }
096
097    @GET
098    @Path("waitForAsyncJobs")
099    public Response waitForAsyncJobs(@QueryParam("timeoutInSeconds") Integer timeoutInSeconds) {
100        // do not maintain a tx for this
101        TransactionHelper.commitOrRollbackTransaction();
102        WorkManager workManager = Framework.getService(WorkManager.class);
103        if (timeoutInSeconds == null) {
104            timeoutInSeconds = 120;
105        }
106        try {
107            if (workManager.awaitCompletion(timeoutInSeconds, TimeUnit.SECONDS)) {
108                return Response.ok().build();
109            }
110        } catch (InterruptedException e) {
111            Thread.currentThread().interrupt();
112            throw new NuxeoException(e);
113        }
114        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Timeout").build();
115    }
116
117}