Commit 392d6f8c authored by Matija Obreza's avatar Matija Obreza

Merge branch 'amphibian' into 'master'

Amphibian

See merge request genesys-pgr/genesys-server!406
parents 296c34d6 9dfe37a0
......@@ -642,6 +642,11 @@
<artifactId>validator-collection</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.genesys-pgr</groupId>
<artifactId>amphibian-client-resttemplate</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
......
......@@ -15,8 +15,8 @@
*/
package org.genesys.catalog.service.impl;
import static org.genesys.catalog.model.dataset.QDataset.dataset;
import static org.genesys.catalog.model.dataset.QDatasetCreator.datasetCreator;
import static org.genesys.catalog.model.dataset.QDataset.*;
import static org.genesys.catalog.model.dataset.QDatasetCreator.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
......@@ -624,6 +624,7 @@ public class DatasetServiceImpl implements DatasetService {
final RepositoryFile repositoryFile = repositoryService.addFile(getDatasetRepositoryFolder(dataset), file.getOriginalFilename(), file.getContentType(), file.getBytes(),
null);
dataset.getRepositoryFiles().add(repositoryFile);
return lazyLoad(datasetRepository.save(dataset));
}
......
......@@ -26,6 +26,8 @@ import org.genesys.filerepository.FileRepositoryException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.service.AmphibianService.AmphibianException;
import org.genesys2.server.service.AmphibianService.AmphibianNotAvailableException;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -153,6 +155,22 @@ public class ApiExceptionHandler {
return new ApiError<>(e);
}
/**
* Handle invalid api usage.
*
* @param e the e
* @param request the request
* @return the api error
*/
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
@ExceptionHandler({ AmphibianNotAvailableException.class, AmphibianException.class })
@ResponseBody
public ApiError<Exception> handleAmphibianExceptions(final Exception e, final HttpServletRequest request) {
LOG.warn("Amphibian: {} for {} {}", e.getMessage(), request.getMethod(), request.getRequestURL());
return new ApiError<>(e);
}
/**
* Handle invalid api usage.
*
......
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.api.v1;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.genesys.amphibian.client.model.Document;
import org.genesys.amphibian.client.model.Preview;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.service.AmphibianService;
import org.genesys2.server.service.AmphibianService.AmphibianException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@RestController
@PreAuthorize("isAuthenticated()")
@RequestMapping(AmphibianController.API_BASE)
public class AmphibianController {
/** The Constant API_BASE. */
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/amphibian";
@Autowired
private AmphibianService amphibianService;
@Autowired
private RepositoryService repositoryService;
/**
* Gets the preview.
*
* @param uuid the repositoryFile UUID
* @return the preview
* @throws NoSuchRepositoryFileException the no such repository file exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws AmphibianException
*/
@GetMapping(value = "/preview/{uuid:\\w{8}\\-\\w{4}\\-.{22}}")
public Preview getPreview(@PathVariable(name = "uuid", required = true) UUID uuid) throws NoSuchRepositoryFileException, IOException, AmphibianException {
RepositoryFile repositoryFile = repositoryService.getFile(uuid);
try {
return amphibianService.loadPreview(uuid);
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
byte[] contents = repositoryService.getFileBytes(repositoryFile);
return amphibianService.makePreview(uuid, contents, repositoryFile.getContentType());
} else {
throw e;
}
}
}
@GetMapping(path = { "/preview/{uuid:\\w{8}\\-\\w{4}\\-.{22}}/{sheet}/{startRow}" })
@ApiOperation(value = "Get the overview of the parsed dataset", notes = "Use the same reference UUID as provided when ingesting a dataset")
public List<Document> getData(@ApiParam(value = "Your reference UUID", required = true) @PathVariable UUID uuid, // uuid
@ApiParam(value = "Sheet index", required = true) @PathVariable long sheet, // sheet index
@ApiParam(value = "Index of the first row", required = true) @PathVariable long startRow, // start row
@ApiParam(value = "Number of rows to return", required = false) @RequestParam(name = "count", required = false, defaultValue = "50") Optional<Integer> count)
throws NoSuchRepositoryFileException, AmphibianException {
repositoryService.getFile(uuid);
List<Document> data = amphibianService.getPreviewData(uuid, sheet, startRow, count.orElse(50));
if (data == null || data.isEmpty()) {
throw new NotFoundElement("No data in this dataset for sheet=" + sheet + " startRow=" + startRow);
} else {
return data;
}
}
}
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.service;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import org.genesys.amphibian.client.model.Document;
import org.genesys.amphibian.client.model.Preview;
import org.genesys2.server.exception.InvalidApiUsageException;
public interface AmphibianService {
public static class AmphibianNotAvailableException extends InvalidApiUsageException {
private static final long serialVersionUID = 1L;
public AmphibianNotAvailableException() {
super("Amphibian not available");
}
}
public static class AmphibianException extends Exception {
private static final long serialVersionUID = 1L;
public AmphibianException() {
}
public AmphibianException(String message) {
super(message);
}
public AmphibianException(String message, Throwable cause) {
super(message, cause);
}
}
/**
* Make a preview of the uploaded spreadsheet file.
*
* @param uuid the uuid
* @param contents the file contents
* @param contentType the content type
* @return the preview
* @throws IOException
* @throws AmphibianException
*/
Preview makePreview(UUID uuid, byte[] contents, String contentType) throws IOException, AmphibianException;
/**
* Load preview.
*
* @param uuid the uuid
* @return the preview
* @throws AmphibianException
*/
Preview loadPreview(UUID uuid);
/**
* Gets the preview data.
*
* @param uuid the uuid
* @param sheet the sheet
* @param startRow the start row
* @param count the count
* @return the preview data
* @throws AmphibianException
*/
List<Document> getPreviewData(UUID uuid, long sheet, long startRow, int count) throws AmphibianException;
}
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.service.impl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.UUID;
import org.genesys.amphibian.client.api.InfoApi;
import org.genesys.amphibian.client.api.PreviewApi;
import org.genesys.amphibian.client.invoker.ApiClient;
import org.genesys.amphibian.client.model.Document;
import org.genesys.amphibian.client.model.Preview;
import org.genesys2.server.service.AmphibianService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AmphibianServiceImpl implements AmphibianService, InitializingBean {
public static final Logger LOG = LoggerFactory.getLogger(AmphibianServiceImpl.class);
@Autowired(required = false)
ApiClient amphibianClient;
private InfoApi infoApi;
private PreviewApi previewApi;
@Override
public void afterPropertiesSet() throws Exception {
if (amphibianClient != null) {
infoApi = new InfoApi(amphibianClient);
previewApi = new PreviewApi(amphibianClient);
try {
LOG.info("Amphibian {} at {}", infoApi.versionUsingGET(), amphibianClient.getBasePath());
} catch (Throwable e) {
LOG.warn("Amphibian at {} not reachable: {}", amphibianClient.getBasePath(), e.getMessage());
}
} else {
LOG.info("Not using Amphibian.");
}
}
@Override
public Preview makePreview(UUID uuid, byte[] contents, String contentType) throws IOException, AmphibianException {
if (amphibianClient == null) {
throw new AmphibianNotAvailableException();
}
if (contents == null) {
throw new AmphibianException("File contents not available");
}
File localFile = Files.createTempFile("amph", "tmp").toFile();
try (FileOutputStream fos = new FileOutputStream(localFile)) {
fos.write(contents);
fos.flush();
}
try {
return previewApi.ingestUsingPOST(uuid, localFile, null, null, contentType);
} catch (Throwable e) {
throw new AmphibianException("Error ingesting file", e);
} finally {
localFile.delete();
}
}
@Override
public Preview loadPreview(UUID uuid) {
if (amphibianClient == null) {
throw new AmphibianNotAvailableException();
}
return previewApi.getUsingGET(uuid);
}
@Override
public List<Document> getPreviewData(UUID uuid, long sheet, long startRow, int count) throws AmphibianException {
if (amphibianClient == null) {
throw new AmphibianNotAvailableException();
}
try {
return previewApi.getDataUsingGET(sheet, startRow, uuid, count);
} catch (Throwable e) {
throw new AmphibianException("Error getting preview data", e);
}
}
}
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.spring.config;
import org.apache.commons.lang3.StringUtils;
import org.genesys.amphibian.client.invoker.ApiClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AmphibianConfig {
@Value("${amphibian.url}")
private String amphibianUrl;
@Bean
public ApiClient amphibianClient() {
if (StringUtils.isNotBlank(amphibianUrl)) {
ApiClient client = new ApiClient();
client.setDebugging("true".equals(System.getenv("AMPHIBIAN_DEBUG")));
client.setBasePath(amphibianUrl);
System.err.println("Amphibian at " + client.getBasePath());
return client;
} else {
return null;
}
}
}
......@@ -51,7 +51,7 @@ import org.springframework.validation.beanvalidation.MethodValidationPostProcess
@ComponentScan(basePackages= { "org.genesys2.server.security", "org.genesys2.server.service", "org.genesys2.server.component", "org.genesys2.brapi.service", "org.genesys.catalog.service" })
@Import({ HazelcastConfig.class, CommonConfig.class, SchedulerConfig.class, DatabaseConfig.class, TemplatingConfig.class, MailConfig.class, OAuth2ServerConfig.class, SecurityConfig.class, CacheConfig.class,
ElasticsearchConfig.class, FileRepositoryConfig.class, WebSecurityConfig.class, WebConfiguration.class, AuditConfig.class, GLISConfig.class, SwaggerConfig.class, AccessionListenersConfig.class })
ElasticsearchConfig.class, FileRepositoryConfig.class, WebSecurityConfig.class, WebConfiguration.class, AuditConfig.class, GLISConfig.class, SwaggerConfig.class, AccessionListenersConfig.class, AmphibianConfig.class })
@EnableAspectJAutoProxy
@EnableWebSecurity
......
......@@ -185,7 +185,7 @@ public class WebConfiguration extends WebMvcConfigurerAdapter {
public void addCorsMappings(final CorsRegistry registry) {
registry.addMapping("/api/**")
// .allowedOrigins(uiOrigin)
.allowedMethods("PUT", "POST", "GET", "DELETE");
.allowedMethods("PUT", "POST", "GET", "DELETE").maxAge(60*60*24);
// .allowedHeaders("header1", "header2", "header3")
// .exposedHeaders("header1", "header2")
// .allowCredentials(false).maxAge(3600);
......
......@@ -224,3 +224,6 @@ partner.primary.uuid=39d3022b-dfca-45d8-98f1-3eeaa6c3e605
# Genesys Catalog URL
genesys.catalog.url=http://localhost:3000
# Amphibian
amphibian.url=
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment