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