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/StorageClientProvider.java

67 lines
2.1 KiB
Java
Raw Normal View History

2020-11-19 17:38:56 +01:00
package org.gcube.application.geoportal.service.engine;
2020-11-19 18:50:20 +01:00
import java.io.FileInputStream;
2020-11-19 17:38:56 +01:00
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
2020-11-19 18:50:20 +01:00
import java.time.Duration;
import java.time.temporal.ChronoUnit;
2020-11-19 17:38:56 +01:00
2020-11-19 18:50:20 +01:00
import org.eclipse.persistence.internal.sessions.remote.SequencingFunctionCall.GetNextValue;
2020-11-19 17:38:56 +01:00
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.model.fault.ConfigurationException;
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
2020-11-19 18:50:20 +01:00
import lombok.extern.slf4j.Slf4j;
@Slf4j
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) {
try {
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
}