158 lines
4.0 KiB
Java
158 lines
4.0 KiB
Java
package eu.dnetlib.organizations.controller;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import eu.dnetlib.common.controller.AbstractDnetController;
|
|
import eu.dnetlib.organizations.utils.DatabaseUtils;
|
|
|
|
@RestController
|
|
@RequestMapping("/oa_api")
|
|
public class OpenaireInternalApiController extends AbstractDnetController {
|
|
|
|
@Autowired
|
|
private DatabaseUtils databaseUtils;
|
|
|
|
@Value("${openaire.api.https.proxy}")
|
|
private String httpsProxy;
|
|
|
|
private static final Log log = LogFactory.getLog(OpenaireInternalApiController.class);
|
|
|
|
private final ImportExecution lastExecution = new ImportExecution();
|
|
|
|
@GetMapping("/import/dedupEvents")
|
|
public ImportExecution importDedupEvents(final HttpServletRequest req) {
|
|
if (req.getRemoteAddr().equals(httpsProxy)) {
|
|
log.warn("Call received by blaklisted ip (https proxy): " + req.getRemoteAddr());
|
|
throw new RuntimeException("Call received by blaklisted ip (https proxy): " + req.getRemoteAddr());
|
|
}
|
|
|
|
synchronized (lastExecution) {
|
|
if (lastExecution.getStatus() != ImportStatus.RUNNING) {
|
|
lastExecution.startNew();
|
|
new Thread(() -> {
|
|
try {
|
|
databaseUtils.importDedupEvents();
|
|
lastExecution.complete();
|
|
} catch (final Throwable e) {
|
|
lastExecution.fail(e);
|
|
log.error("Error importing conflicts and duplicates", e);
|
|
}
|
|
}).start();
|
|
} else {
|
|
throw new RuntimeException("An import is already running");
|
|
}
|
|
|
|
}
|
|
return lastExecution;
|
|
}
|
|
|
|
@GetMapping("/import/dedupEvents/status")
|
|
public final ImportExecution statusDedupEvents(final HttpServletRequest req) {
|
|
if (req.getRemoteAddr().equals(httpsProxy)) {
|
|
log.warn("Call received by blaklisted ip (https proxy): " + req.getRemoteAddr());
|
|
throw new RuntimeException("Call received by blaklisted ip (https proxy): " + req.getRemoteAddr());
|
|
}
|
|
return lastExecution;
|
|
}
|
|
|
|
@GetMapping("/refresh/fulltextIndex")
|
|
public final List<String> updateFulltextIndex(final HttpServletRequest req) {
|
|
if (req.getRemoteAddr().equals(httpsProxy)) {
|
|
log.warn("Call received by blaklisted ip (https proxy): " + req.getRemoteAddr());
|
|
throw new RuntimeException("Call received by blaklisted ip (https proxy): " + req.getRemoteAddr());
|
|
}
|
|
new Thread(databaseUtils::updateFulltextIndex).start();
|
|
|
|
return Arrays.asList("Updating ...");
|
|
}
|
|
|
|
class ImportExecution {
|
|
|
|
private String id;
|
|
private Long dateStart;
|
|
private Long dateEnd;
|
|
private ImportStatus status = ImportStatus.NOT_LAUNCHED;
|
|
private String error;
|
|
|
|
public String getId() {
|
|
return id;
|
|
}
|
|
|
|
public void setId(final String id) {
|
|
this.id = id;
|
|
}
|
|
|
|
public Long getDateStart() {
|
|
return dateStart;
|
|
}
|
|
|
|
public void setDateStart(final Long dateStart) {
|
|
this.dateStart = dateStart;
|
|
}
|
|
|
|
public Long getDateEnd() {
|
|
return dateEnd;
|
|
}
|
|
|
|
public void setDateEnd(final Long dateEnd) {
|
|
this.dateEnd = dateEnd;
|
|
}
|
|
|
|
public ImportStatus getStatus() {
|
|
return status;
|
|
}
|
|
|
|
public void setStatus(final ImportStatus status) {
|
|
this.status = status;
|
|
}
|
|
|
|
public String getError() {
|
|
return error;
|
|
}
|
|
|
|
public void setError(final String error) {
|
|
this.error = error;
|
|
}
|
|
|
|
public void startNew() {
|
|
setId("import-" + UUID.randomUUID());
|
|
setDateStart(new Date().getTime());
|
|
setDateEnd(null);
|
|
setStatus(ImportStatus.RUNNING);
|
|
setError(null);
|
|
}
|
|
|
|
public void complete() {
|
|
setDateEnd(new Date().getTime());
|
|
setStatus(ImportStatus.SUCCESS);
|
|
}
|
|
|
|
public void fail(final Throwable e) {
|
|
setDateEnd(new Date().getTime());
|
|
setStatus(ImportStatus.FAILED);
|
|
setError(e.getMessage());
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
enum ImportStatus {
|
|
SUCCESS,
|
|
FAILED,
|
|
RUNNING,
|
|
NOT_LAUNCHED
|
|
}
|