This repository has been archived on 2021-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/StorageClientProvider.java

65 lines
2.1 KiB
Java
Raw Normal View History

2021-08-04 16:22:29 +02:00
package org.gcube.application.geoportal.service.engine.providers;
2020-11-19 17:38:56 +01:00
import lombok.extern.slf4j.Slf4j;
2020-11-19 17:38:56 +01:00
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
2020-11-19 17:38:56 +01:00
import org.gcube.application.geoportal.service.utils.ContextUtils;
import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
2020-11-19 18:50:20 +01:00
import org.gcube.data.transfer.library.utils.Utils;
2020-11-19 17:38:56 +01:00
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
2020-11-19 18:50:20 +01:00
@Slf4j
2021-08-04 16:22:29 +02:00
public class StorageClientProvider extends AbstractScopedMap<IClient> {
2020-11-19 17:38:56 +01:00
2020-11-19 18:50:20 +01:00
public StorageClientProvider() {
super("Storage client cache");
setTTL(Duration.of(10, ChronoUnit.MINUTES));
}
2020-11-19 17:38:56 +01:00
@Override
2020-11-19 18:50:20 +01:00
protected IClient retrieveObject() throws ConfigurationException {
2020-11-19 17:38:56 +01:00
return new StorageClient(InterfaceConstants.SERVICE_CLASS, InterfaceConstants.SERVICE_NAME, ContextUtils.getCurrentCaller(), AccessType.SHARED, MemoryType.VOLATILE).getClient();
}
@Override
2020-11-19 18:50:20 +01:00
protected void dispose(IClient toDispose) {
2021-08-04 16:22:29 +02:00
try {
2021-08-06 16:17:24 +02:00
//TODO ASK
2021-08-04 16:22:29 +02:00
// toDispose.close();
2021-01-04 16:58:40 +01:00
}catch (NullPointerException e) {
// expected if closed without uploading
2020-11-19 18:50:20 +01:00
}catch(Throwable t) {
log.warn(" unable to dispose "+toDispose,t);
}
2020-11-19 17:38:56 +01:00
}
2020-11-19 18:50:20 +01:00
2020-11-19 17:38:56 +01:00
@Override
public void init() {
}
//wrapping methods
public InputStream open(String id) throws MalformedURLException, RemoteBackendException, IOException, ConfigurationException {
return new URL(getObject().getHttpsUrl().RFileById(id)).openConnection().getInputStream();
}
2020-11-19 18:50:20 +01:00
public String store(InputStream is) throws RemoteBackendException, ConfigurationException {
return getObject().put(true).LFile(is).RFile(Utils.getUniqueString());
}
2020-11-19 17:38:56 +01:00
}