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    @Override
080    @GET
081    @Path("status")
082    public String getStatus() {
083        return super.getStatus();
084    }
085
086    @GET
087    @Path("running")
088    public String running() {
089        return Boolean.toString(super.isRunning());
090    }
091
092    @Override
093    @GET
094    @Path("kill")
095    public String kill() {
096        return super.kill();
097    }
098
099    @GET
100    @Path("waitForAsyncJobs")
101    public Response waitForAsyncJobs(@QueryParam("timeoutInSeconds") Integer timeoutInSeconds) {
102        // do not maintain a tx for this
103        TransactionHelper.commitOrRollbackTransaction();
104        WorkManager workManager = Framework.getService(WorkManager.class);
105        if (timeoutInSeconds == null) {
106            timeoutInSeconds = 120;
107        }
108        try {
109            if (workManager.awaitCompletion(timeoutInSeconds, TimeUnit.SECONDS)) {
110                return Response.ok().build();
111            }
112        } catch (InterruptedException e) {
113            Thread.currentThread().interrupt();
114            throw new NuxeoException(e);
115        }
116        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Timeout").build();
117    }
118
119}