Roberto Cirillo 4 years ago
commit
4d184c64ba
  1. 37
      .classpath
  2. 23
      .project
  3. 6
      .settings/org.eclipse.core.resources.prefs
  4. 12
      .settings/org.eclipse.jdt.core.prefs
  5. 5
      .settings/org.eclipse.m2e.core.prefs
  6. 16
      distro/CHANGELOG
  7. 2
      distro/LICENSE
  8. 1
      distro/MAINTAINERS
  9. BIN
      distro/README
  10. 47
      distro/changelog.xml
  11. 32
      distro/descriptor.xml
  12. 25
      distro/profile.xml
  13. 1
      distro/svnpath.txt
  14. 130
      pom.xml
  15. 15
      src/main/java/log4j.properties
  16. 40
      src/main/java/org/gcube/common/scope/impl/ServiceMapScannerMediator.java
  17. 155
      src/main/java/org/gcube/contentmanager/storageclient/model/protocol/URIs.java
  18. 39
      src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/Handler.java
  19. 60
      src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPConnection.java
  20. 47
      src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnection.java
  21. 115
      src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionById.java
  22. 118
      src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionFactory.java
  23. 147
      src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionOld.java
  24. 137
      src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPUrl.java
  25. 118
      src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/StringDecrypter.java
  26. 120
      src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/StringEncrypter.java
  27. 164
      src/main/java/org/gcube/contentmanager/storageclient/protocol/utils/Utils.java
  28. 14
      src/main/java/org/gcube/contentmanager/storageclient/wrapper/AccessType.java
  29. 464
      src/main/java/org/gcube/contentmanager/storageclient/wrapper/Configuration.java
  30. 234
      src/main/java/org/gcube/contentmanager/storageclient/wrapper/ISClientConnector.java
  31. 12
      src/main/java/org/gcube/contentmanager/storageclient/wrapper/MemoryType.java
  32. 406
      src/main/java/org/gcube/contentmanager/storageclient/wrapper/StorageClient.java
  33. 219
      src/test/java/org/gcube/contentmanager/storageclient/test/CompressTest.java
  34. 140
      src/test/java/org/gcube/contentmanager/storageclient/test/CopyDirTest.java
  35. 224
      src/test/java/org/gcube/contentmanager/storageclient/test/CopyTest.java
  36. 75
      src/test/java/org/gcube/contentmanager/storageclient/test/CountingTest.java
  37. 191
      src/test/java/org/gcube/contentmanager/storageclient/test/DownloadsTest.java
  38. 159
      src/test/java/org/gcube/contentmanager/storageclient/test/DuplicateTest.java
  39. 108
      src/test/java/org/gcube/contentmanager/storageclient/test/FwsQueryTest.java
  40. 117
      src/test/java/org/gcube/contentmanager/storageclient/test/GetMetaFileTest.java
  41. 140
      src/test/java/org/gcube/contentmanager/storageclient/test/HLcheckTest.java
  42. 152
      src/test/java/org/gcube/contentmanager/storageclient/test/LinkTest.java
  43. 110
      src/test/java/org/gcube/contentmanager/storageclient/test/LockTest.java
  44. 118
      src/test/java/org/gcube/contentmanager/storageclient/test/MoveDirTest.java
  45. 108
      src/test/java/org/gcube/contentmanager/storageclient/test/MoveTest.java
  46. 129
      src/test/java/org/gcube/contentmanager/storageclient/test/PropertiesTest.java
  47. 102
      src/test/java/org/gcube/contentmanager/storageclient/test/RemoveTest.java
  48. 80
      src/test/java/org/gcube/contentmanager/storageclient/test/RemoveVolatileArea.java
  49. 153
      src/test/java/org/gcube/contentmanager/storageclient/test/ReplaceFileTest.java
  50. 320
      src/test/java/org/gcube/contentmanager/storageclient/test/SoftCopyTest.java
  51. 60
      src/test/java/org/gcube/contentmanager/storageclient/test/StringEncrypterTest.java
  52. 145
      src/test/java/org/gcube/contentmanager/storageclient/test/UploadsTest.java
  53. 191
      src/test/java/org/gcube/contentmanager/storageclient/test/UrlResolverByIdTest.java
  54. 73
      src/test/java/org/gcube/contentmanager/storageclient/test/UrlResolverTest.java
  55. 26
      src/test/java/org/gcube/contentmanager/storageclient/test/scopeValidationTest.java
  56. 20
      src/test/java/org/gcube/contentmanager/storageclient/test/utils/Costants.java
  57. BIN
      src/test/resources/CostaRica.jpg
  58. BIN
      src/test/resources/CostaRica1.jpg
  59. BIN
      src/test/resources/dog.jpg
  60. BIN
      src/test/resources/download.png

37
.classpath

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

23
.project

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>storage-manager-wrapper</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

6
.settings/org.eclipse.core.resources.prefs

@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8

12
.settings/org.eclipse.jdt.core.prefs

@ -0,0 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7

5
.settings/org.eclipse.m2e.core.prefs

@ -0,0 +1,5 @@
#Wed May 02 14:50:59 CEST 2012
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

16
distro/CHANGELOG

@ -0,0 +1,16 @@
v 2.2.0 (21-04-2015)
* change smp protocol
v 2.1.0 (01-04-2014)
* support MongoDB backend with authentication
v 2.0.1 (29-05-2013)
* bug fix incident: connection pending #606
v 2.0.0 (19-05-2013)
* added move, copy and link operations
*FWS integration
v 1.0.2 (15-01-2013)
* Integration with new gCube release 2.12.0 version system
v 1.0.1 (11-10-2012)
* added VOLATILE area
v. 1.0.0 (04-05-2012)
* First release

2
distro/LICENSE

@ -0,0 +1,2 @@
${gcube.license}

1
distro/MAINTAINERS

@ -0,0 +1 @@
* Roberto Cirillo (roberto.cirillo@isti.cnr.it), CNR Pisa, Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo"

BIN
distro/README

Binary file not shown.

47
distro/changelog.xml

@ -0,0 +1,47 @@
<ReleaseNotes>
<Changeset component="org.gcube.content-management.storage-manager-wrapper.2-5-0" date="2017-10-15">
<Change>Added new JUnit Test classes</Change>
<Change>update JUnit Test Classes</Change>
<Change>delete old method for retrieving remote phrase used by old smp protocol classes</Change>
<Change></Change>
</Changeset>
<Changeset component="org.gcube.content-management.storage-manager-wrapper.2-3-1" date="2016-01-14">
<Change>dinamically load "write concern"" and ""read preference" options</Change>
<Change>add method for decrypt an id</Change>
</Changeset>
<Changeset component="org.gcube.content-management.storage-manager-wrapper.2-3-0" date="2016-01-14">
<Change>dinamically load smpConnection handler</Change>
<Change>add method for decrypt an id</Change>
</Changeset>
<Changeset component="org.gcube.content-management.storage-manager-wrapper.2-2-1" date="2015-07-01">
<Change>update JUnit test for smp uri without payload</Change>
</Changeset>
<Changeset component="org.gcube.content-management.storage-manager-wrapper.2-2-0" date="2015-04-21">
<Change>change smp protocol</Change>
</Changeset>
<Changeset component="org.gcube.content-management.storage-manager-wrapper.2-1-0" date="2014-04-01">
<Change>support MongoDB backend with authentication</Change>
</Changeset>
<Changeset component="org.gcube.content-management.storage-manager-wrapper.2-0-3" date="2014-02-12">
<Change>Integration with new gCube release 3.0.0 version system</Change>
<Change>Java7 refactoring</Change>
</Changeset>
<Changeset component="${build.finalName}" date="2013-05-29">
<Change>Integration with new gCube release 2.15.0 version system</Change>
<Change>bugfix on pending connections</Change>
</Changeset>
<Changeset component="${build.finalName}" date="2013-04-29">
<Change>Integration with new gCube release 2.14.0 version system</Change>
<Change>FWS integration</Change>
<Change>added move, copy and link operations</Change>
</Changeset>
<Changeset component="${build.finalName}" date="2013-01-15">
<Change>Integration with new gCube release 2.12.0 version system</Change>
</Changeset>
<Changeset component="org.gcube.content-management.storage-manager-wrapper.1-0-1" date="2012-10-11">
<Change>Added VOLATILE area</Change>
</Changeset>
<Changeset component="${build.finalName}" date="2012-05-04">
<Change>First Release</Change>
</Changeset>
</ReleaseNotes>

32
distro/descriptor.xml

@ -0,0 +1,32 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>servicearchive</id>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<directory>${distroDirectory}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>README</include>
<include>LICENSE</include>
<include>changelog.xml</include>
<include>profile.xml</include>
</includes>
<fileMode>755</fileMode>
<filtered>true</filtered>
</fileSet>
</fileSets>
<files>
<file>
<source>target/${build.finalName}.${project.packaging}</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
</files>
</assembly>

25
distro/profile.xml

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ID />
<Type>Service</Type>
<Profile>
<Description>${description}</Description>
<Class>ContentManagement</Class>
<Name>${artifactId}</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Name>${artifactId}</Name>
<Version>${version}</Version>
<MavenCoordinates>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</MavenCoordinates>
<Files>
<File>${build.finalName}.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

1
distro/svnpath.txt

@ -0,0 +1 @@
${scm.url}

130
pom.xml

@ -0,0 +1,130 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>maven-parent</artifactId>
<groupId>org.gcube.tools</groupId>
<version>1.0.0</version>
<relativePath />
</parent>
<groupId>org.gcube.contentmanagement</groupId>
<artifactId>storage-manager-wrapper</artifactId>
<version>2.5.0-SNAPSHOT</version>
<scm>
<connection>scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/content-management/${project.artifactId}</connection>
<developerConnection>scm:svn:https://svn.d4science.research-infrastructures.eu/gcube/trunk/content-management/${project.artifactId}</developerConnection>
<url>http://svn.d4science.research-infrastructures.eu/public/d4science/gcube/trunk/content-management/${project.artifactId}</url>
</scm>
<properties>
<distroDirectory>${project.basedir}/distro</distroDirectory>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>LATEST</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.contentmanagement</groupId>
<artifactId>storage-manager-core</artifactId>
<version>[2.2.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>common-gcore-resources</artifactId>
<!-- <version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version> -->
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-gcore-stubs</artifactId>
<!-- <version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version> -->
</dependency>
<dependency>
<groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId>
<!-- <version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version> -->
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId>
<!-- <version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version> -->
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-scope-maps</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-profile</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target</outputDirectory>
<resources>
<resource>
<directory>${distroDirectory}</directory>
<filtering>true</filtering>
<includes>
<include>profile.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>servicearchive</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

15
src/main/java/log4j.properties

@ -0,0 +1,15 @@
log4j.rootLogger=DEBUG, A1, stdout
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=log.txt
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# ***** Max file size is set to 100KB
log4j.appender.A1.MaxFileSize=100MB
# ***** Keep one backup file
log4j.appender.A1.MaxBackupIndex=1
#CONSOLE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=INFO
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%t] %-5p %c %d{dd MMM yyyy ;HH:mm:ss.SSS} - %m%n

40
src/main/java/org/gcube/common/scope/impl/ServiceMapScannerMediator.java

@ -0,0 +1,40 @@
package org.gcube.common.scope.impl;
import java.util.Set;
import org.gcube.common.scope.api.ScopeProvider;
/**
* Check the validity of a given scope
* @author Roberto Cirillo (ISTI-CNR)
*
*/
public class ServiceMapScannerMediator {
/**
* The validation has been removed
* @param scope
* @return
*/
@Deprecated
public static boolean isValid(String scope){
// log.debug("validating scope "+scope);
String currentScope=ScopeProvider.instance.get();
ScopeProvider.instance.set(scope);
try{
new ScopedServiceMap().scope();
}catch(Exception e){
return false;
}finally{
ScopeProvider.instance.set(currentScope);
}
return true;
}
public static Set<String> getScopeKeySet(){
return ServiceMapScanner.maps().keySet();
}
}

155
src/main/java/org/gcube/contentmanager/storageclient/model/protocol/URIs.java

@ -0,0 +1,155 @@
package org.gcube.contentmanager.storageclient.model.protocol;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
//import org.gcube.common.core.scope.GCUBEScope;
//import org.gcube.common.core.scope.GCUBEScopeManager;
import org.gcube.contentmanager.storageclient.model.protocol.smp.Handler;
/**
* Utility methods for content URI creation and manipulation.
* @author Fabio Simeoni (University of Strathclyde)
* @author Roberto Cirillo (ISTI-CNR)
*
*/
public class URIs {
static {
Handler.activateProtocol();
}
/**Scheme of cms URIs.*/
public static final String PROTOCOL="smp";
/**
* Indicates whether a URI is a valid content URI.
* @param uri the URI.
* @throws URISyntaxException if the URI fails validation.
*/
public static void validate(URI uri) throws URISyntaxException {
if (!PROTOCOL.equals(uri.getScheme()) ||
uri.getAuthority()==null ||
uri.getPath()==null ||
uri.getPath().length()<2)
throw new IllegalArgumentException(new URISyntaxException(uri.toString(),"uri is not a well-formed content URI"));
}
/**
* Constructs a content URI from a collection identifiers and one or more node identifiers.
* @param collectionID the collection identifier.
* @param identifiers the node identifiers.
* @return the URI.
* @throws IllegalArgumentException if the input is <code>null</code> or empty.
*/
public static URI make(String collectionID, String ... identifiers) throws IllegalArgumentException {
if (collectionID==null || identifiers==null || identifiers.length==0)
throw new IllegalArgumentException("null or empty input");
StringBuilder path = new StringBuilder();
for (Object id : identifiers)
path.append("/"+id.toString());
URI uri = null;
try {
uri = new URI(PROTOCOL,collectionID,path.toString(),null);
}
catch(URISyntaxException e) {
throw new RuntimeException("error in generation uri with "+PROTOCOL+","+collectionID+","+path,e);
}
return uri;
}
/**
* Returns the collection identifier in a <code>sm</code> URI.
* @param uri the URI.
* @return the identifier.
* @throws URISyntaxException if the URI is not a content URI.
*/
public static String collectionID(URI uri) throws URISyntaxException {
validate(uri);
return uri.getAuthority();
}
/**
* Returns the document identifier in a content URI.
* @param uri the URI.
* @return the identifier.
* @throws URISyntaxException if the URI is not a content URI.
*/
public static String documentID(URI uri) throws URISyntaxException {
validate(uri);
String p = uri.getPath().substring(1);
if (p.endsWith("/"))
p = p.substring(0,p.length()-1);
int index = p.indexOf("/");
return p.substring(0,index>0?index:p.length());
}
/**
* Returns the identifier of the node identified by a content URI.
* @param uri the URI.
* @return the identifier.
* @throws URISyntaxException if the URI is not a content URI.
*/
public static String nodeID(URI uri) throws URISyntaxException {
validate(uri);
return uri.getPath().substring(uri.getPath().lastIndexOf("/")+1);
}
/**
* Returns the identifiers in a content URI.
* @param uri the URI.
* @return the identifiers.
* @throws URISyntaxException if the URI is not a content URI.
*/
public static String[] nodeIDs(URI uri) throws URISyntaxException {
validate(uri);
List<String> ids = new ArrayList<String>();
for (String s : uri.getPath().substring(1).split("/")) //will be validated here
ids.add(s);
return ids.toArray(new String[0]);
}
/**
* Returns a content URI for the parent of the node identified by another content URI.
* @param uri the input URI.
* @return the parent URI.
* @throws URISyntaxException if the input URI is not a content URL.
*/
public static URI parentURI(URI uri) throws URISyntaxException {
validate(uri);
String u = uri.getPath();
return make(uri.getAuthority(),u.substring(1,u.lastIndexOf("/")).split("/"));
}
/**
* Returns a content URI for the document of the node identified by another content URI.
* @param uri the input URI.
* @return the document URI.
* @throws URISyntaxException if the input URI is not a content URI.
*/
public static URI documentURI(URI uri) throws URISyntaxException {
validate(uri);
return make(uri.getAuthority(),documentID(uri));
}
/**
* Returns a URL connection in a given scope.
* @param uri a content URI.
* @param scope the scope.
* @return the connection.
* @throws IOException if the connections could not be established.
* @throws URISyntaxException if the URI is not a content URI or if the protocol handler for the <code>smp</code> scheme is not active.
* @deprecated since 2.3.1. Use {@link URLConnection} normally in current scope.
*/
public static URLConnection connection(URI uri, String scope) throws IOException, URISyntaxException {
validate(uri);
URLConnection connection = uri.toURL().openConnection();
return connection;
}
}

39
src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/Handler.java

@ -0,0 +1,39 @@
package org.gcube.contentmanager.storageclient.model.protocol.smp;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
/**
* A handler for the <code>smp</code> protocol.
* @author Fabio Simeoni (University of Strathclyde)
* @author Roberto Cirillo (ISTI-CNR)
*
*/
public class Handler extends URLStreamHandler {
/**{@inheritDoc}*/
@Override
protected URLConnection openConnection(URL u) throws IOException {
return SMPURLConnectionFactory.getSmp(u);
}
/**
* Registers the protocol with the JVM.
*/
public static void activateProtocol() {
String pkgs_name="java.protocol.handler.pkgs";
String pkgs = System.getProperty(pkgs_name);
String pkg = "org.gcube.contentmanager.storageclient.model.protocol";
if (pkgs==null)
pkgs = pkg ;
else if (!pkgs.contains(pkg))
pkgs = pkgs+"|"+pkg;
System.setProperty(pkgs_name, pkgs);
}
}

60
src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPConnection.java

@ -0,0 +1,60 @@
package org.gcube.contentmanager.storageclient.model.protocol.smp;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.contentmanager.storageclient.wrapper.ISClientConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An extension of URLConnection used for smp url
* @author Roberto Cirillo (ISTI-CNR)
*
*/
public abstract class SMPConnection extends URLConnection {
Logger logger= LoggerFactory.getLogger(SMPURLConnection.class);
public SMPConnection(URL url) {
super(url);
}
public abstract URLConnection init(URL url);
/**{@inheritDoc}*/
@Override
public synchronized void connect() throws IOException {
connected=true;
}
@Override
public abstract InputStream getInputStream() throws IOException;
protected abstract InputStream storageClient(String url) throws Exception;
/**
* This method has been moved in Configuration class
* @param rootScope
* @return
* @throws Exception
* @see org.gcube.contentmanager.storageclient.wrapper.Configuration
*
*/
@Deprecated
protected String retrieveEncryptionPhrase() throws Exception {
String currentScope=ScopeProvider.instance.get();
logger.debug("retrieve encryption prhase on scope: "+currentScope);
String encryptedKey=null;
ISClientConnector isclient=new ISClientConnector();
encryptedKey=isclient.retrievePropertyValue("PassPhrase", currentScope);
String decryptString=org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(encryptedKey);
return decryptString;
}
}

47
src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnection.java

@ -0,0 +1,47 @@
package org.gcube.contentmanager.storageclient.model.protocol.smp;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* This is invoked by the platform with a URL of the right protocol.
* @author Fabio Simeoni (University of Strathclyde), @author Roberto Cirillo (ISTI-CNR)
* @deprecated this class will be deleted and will be changed with the factory class invocation: SMPURLConnectionFactory
* @see SMPURLConnectionFactory
*/
@Deprecated
public class SMPURLConnection extends URLConnection {
SMPConnection smp;
Logger logger= LoggerFactory.getLogger(SMPURLConnection.class);
/**
* Constructs a new instance for a given <code>sm</code> URL.
* @deprecated reason this method is deprecated </br>
* {will be removed in next version} </br>
* use {@link #SMPURLConnectionFactory.getSmp(url)}
* @param url the URL.
*/
@Deprecated
public SMPURLConnection(URL url) {
super(url);
this.smp=SMPURLConnectionFactory.getSmp(url);
}
/**{@inheritDoc}*/
@Override
public synchronized void connect() throws IOException {
connected=true;
}
/**{@inheritDoc}*/
@Override
public synchronized InputStream getInputStream() throws IOException {
return smp.getInputStream();
}
}

115
src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionById.java

@ -0,0 +1,115 @@
package org.gcube.contentmanager.storageclient.model.protocol.smp;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanagement.blobstorage.service.operation.GetHttpUrl;
import org.gcube.contentmanagement.blobstorage.service.operation.GetUrl;
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
import org.gcube.contentmanager.storageclient.wrapper.Configuration;
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
import org.gcube.contentmanager.storageclient.wrapper.ISClientConnector;
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This is invoked by the platform with a URL of the smp protocol with new format (>= 2.2.0 version) .
* @author Roberto Cirillo (ISTI-CNR)
*
* Example: smp://data.gcube.org?uhdnfounhcfnshfnrhbvyaeegytf6dfawiuawgcyg
*/
public class SMPURLConnectionById extends SMPConnection {
private Logger logger= LoggerFactory.getLogger(SMPURLConnectionOld.class);
private String serviceClass="Storage-manager";
private String serviceName="resolver-uri";
private String owner="storage-manager";
/**
* Constructs a new instance for a given <code>sm</code> URL.
* @param url the URL.
*/
public SMPURLConnectionById(URL url) {
super(url);
}
public SMPConnection init(URL url){
return new SMPURLConnectionById(url);
}
/**{@inheritDoc}
* internal handler implementation
* */
@Override
public synchronized InputStream getInputStream() throws IOException {
if (!connected)
this.connect();
try {
return storageClient(this.url.toString());
}
catch(Exception e) {
IOException ioe = new IOException();
ioe.initCause(e);
throw ioe;
}
}
/**
* Get a StorageManager instance from url
*/
protected InputStream storageClient(String url) throws Exception {
logger.info("url :" + url);
String [] urlParam=url.split(GetUrl.URL_SEPARATOR);
String protocol=urlParam[0];
protocol=protocol.substring(0, protocol.length()-1);
logger.debug("protocol is "+protocol);
if(ScopeProvider.instance.get() == null){
throw new RuntimeException("Scope not set");
}
String encrypted=retrieveStringEncrypted(urlParam);
MemoryType memory=null;
if(encrypted.contains(GetHttpUrl.VOLATILE_URL_IDENTIFICATOR)){
memory=MemoryType.VOLATILE;
encrypted=encrypted.replace(GetHttpUrl.VOLATILE_URL_IDENTIFICATOR, "");
}
logger.debug("String encrypted "+encrypted);
String phrase=retrieveEncryptionPhrase();
String location=null;
if(Base64.isBase64(encrypted) && (protocol.equalsIgnoreCase("http"))){
byte[] valueDecoded= Base64.decodeBase64(encrypted);
String encryptedID = new String(valueDecoded);
location=new StringDecrypter("DES", phrase).decrypt(encryptedID);
}else{
location=new StringDecrypter("DES", phrase).decrypt(encrypted);
}
IClient client=null;
if(memory!=null)
client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, memory).getClient();
else
client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED).getClient();
InputStream is=null;
is=client.get().RFileAsInputStream(location);
return is;
}
private String retrieveStringEncrypted(String[] urlParam) {
String encrypted=urlParam[3];
int i=4;
while(i < urlParam.length){
encrypted=encrypted+GetUrl.URL_SEPARATOR+urlParam[i];
i++;
}
return encrypted;
}
}

118
src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionFactory.java

@ -0,0 +1,118 @@
package org.gcube.contentmanager.storageclient.model.protocol.smp;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type;
import org.gcube.contentmanagement.blobstorage.service.operation.GetUrl;
import org.gcube.contentmanager.storageclient.protocol.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Factory class used to determine the right smp url protocol: old format(<2.2.0 version), new format (>= 2.2.0-SNAPSHOT)
* @author Roberto Cirillo (ISTI-CNR)
*
*/
public class SMPURLConnectionFactory {
static Logger logger=LoggerFactory.getLogger(SMPURLConnectionFactory.class);
public static SMPConnection getSmp(URL url) {
String urlString=url.toString();
Utils.checkScopeProvider();
if(isNewSmpFormat(urlString)|| isIntermediateType(urlString)){
logger.info("detected new smp format "+url);
SMPConnection connection=load(url);
if (connection!= null)
return connection;
return new SMPURLConnectionById(url);
}else{
logger.info("detected old smp format "+url);
return new SMPURLConnectionOld(url);
}
}
/**
* patch method for a particular smp uri: smp://data.gcube.org/8TFLhJ991DF1M/Ae8rGamC3CgCjAXlnVGmbP5+HKCzc=
* @param urlString
* @return
*/
private static boolean isIntermediateType(String urlString) {
String [] urlParam=urlString.split(GetUrl.URL_SEPARATOR);
String infraHost=urlParam[2];
if(Utils.isScopeProviderMatch(infraHost))
return true;
String infra=Utils.getInfraFromResolverHost(infraHost);
String rootScope="/"+infra;
ScopeBean scope=new ScopeBean(rootScope);
if(scope.is(Type.INFRASTRUCTURE)){
return Utils.validationScope2(rootScope);
}else{
return false;
}
}
private static boolean isNewSmpFormat(String urlString) {
logger.debug("check format: "+urlString);
String httpString=urlString;
httpString=httpString.replace("smp://", "http://");
logger.debug("httpUrl conversion: "+httpString);
URL httpUrl=null;
try {
httpUrl=new URL(httpString);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<ServiceEndpoint> services=Utils.queryServiceEndpoint(Utils.URI_RESOLVER_RESOURCE_CATEGORY, Utils.URI_RESOLVER_RESOURCE_NAME);
String host=null;
if(services != null && services.size()>0){
host=Utils.getResolverHost(services.get(0));
}
logger.debug("uri-resolver host: "+host+" in scope: "+ScopeProvider.instance.get());
if((host!=null) && (host.equals(httpUrl.getHost()))){
return true;
}else{
return false;
}
}
/**
* Load a backend driver from classpath
* @param url
* @return
*/
private static SMPConnection load(URL url){
ServiceLoader<SMPConnection> loader = ServiceLoader.load(SMPConnection.class);
Iterator<SMPConnection> iterator = loader.iterator();
List<SMPConnection> impls = new ArrayList<SMPConnection>();
while(iterator.hasNext())
impls.add(iterator.next());
int implementationCounted=impls.size();
// System.out.println("size: "+implementationCounted);
if(implementationCounted==0){
logger.info(" 0 implementation found. Load default implementation of SMPConnection");
return null;
}else if(implementationCounted>0){
SMPConnection connection = impls.get(0);
logger.info("1 implementation of TransportManager found. ");
connection.init(url);
return connection;
}else{
return null;
}
}
}

147
src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionOld.java

@ -0,0 +1,147 @@
package org.gcube.contentmanager.storageclient.model.protocol.smp;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
import org.gcube.contentmanager.storageclient.wrapper.Configuration;
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* This is invoked by the platform with a URL of the right protocol.
* @author Fabio Simeoni (University of Strathclyde), @author Roberto Cirillo (ISTI-CNR)
*
*/
public class SMPURLConnectionOld extends SMPConnection{
Logger logger= LoggerFactory.getLogger(SMPURLConnectionOld.class);
/**
* Constructs a new instance for a given <code>sm</code> URL.
* @param url the URL.
*/
public SMPURLConnectionOld(URL url) {
super(url);
}
public SMPConnection init(URL url){
return new SMPURLConnectionOld(url);
}
/**{@inheritDoc}*/
@Override
public synchronized InputStream getInputStream() throws IOException {
if (!connected)
this.connect();
try {
return storageClient(this.url.toString());
}
catch(Exception e) {
IOException ioe = new IOException();
ioe.initCause(e);
throw ioe;
}
}
protected InputStream storageClient(String url) throws Exception {
logger.info("url :" + url);
String [] urlParam=url.split("\\?");
logger.info("String encrypted "+urlParam[1]);
String param=new StringDecrypter("DES", retrieveEncryptionPhrase()).decrypt(urlParam[1]);
logger.info("String decrypted: "+param);
String [] getParam=param.split("\\&");
String serviceClass=null;
String serviceName=null;
String owner=null;
String accessType=null;
String memoryType=null;
String scopeType=null;
AccessType type = null;
MemoryType mType = null;
String server= null;
String [] par1;
for(String par : getParam){
if(par.contains("ServiceClass")){
par1=par.split("=");
serviceClass=par1[1];
}else if(par.contains("ServiceName")){
par1=par.split("=");
serviceName=par1[1];
}else if(par.contains("owner")){
par1=par.split("=");
owner=par1[1];
}else if(par.contains("scope")){
par1=par.split("=");
scopeType=par1[1];
}else if(par.contains("server")){
par1=par.split("=");
server=par1[1];
}else if(par.contains("AccessType")){
par1=par.split("=");
accessType=par1[1];
if(accessType.equalsIgnoreCase("public")){
type=AccessType.PUBLIC;
}else if(accessType.equalsIgnoreCase("shared")){
type=AccessType.SHARED;
}
}else if(par.contains("MemoryType")){
par1=par.split("=");
memoryType=par1[1];
if(memoryType.equalsIgnoreCase("VOLATILE")){
mType=MemoryType.VOLATILE;
}else{
mType=MemoryType.PERSISTENT;
}
}
}
// throw an exception if one or more uri parameters are invalid
if((serviceName==null) || (serviceClass==null) || (owner == null) || (scopeType==null) || (type == null)){
logger.error("Bad Parameter in URI");
if (type == null){
logger.error("URI generated from a private file");
throw new MalformedURLException("The uri is generated from a private file. It cannot be accessed");
}
if (serviceName==null) throw new MalformedURLException("The uri generated has an invalid serviceName");
if (serviceClass==null) throw new MalformedURLException("The uri generated has an invalid serviceClass");
if (owner==null) throw new MalformedURLException("The uri generated has an invalid owner");
if (scopeType==null) throw new MalformedURLException("The uri is generated has an invalid scopeType.");
}
String location=extractLocation(urlParam[0]);
logger.info("Parameters from URI "+serviceClass+" "+serviceName+" "+owner+" "+type+" "+mType +" location: "+urlParam[0]+" scope: "+scopeType);
IClient client=null;
String currentScope=ScopeProvider.instance.get();
logger.info("current scope used: "+currentScope+". scope found on url is: "+ scopeType);
// ScopeProvider.instance.set(scopeType);
if(mType != null){
if(server!=null)
client=new StorageClient(serviceClass, serviceName, owner, type, mType, server).getClient();
else
client=new StorageClient(serviceClass, serviceName, owner, type, mType).getClient();
}else{
if (server != null)
client=new StorageClient(serviceClass, serviceName, owner, server, type ).getClient();
else
client=new StorageClient(serviceClass, serviceName, owner, type).getClient();
}
InputStream is=null;
is=client.get().RFileAsInputStream(location);
// ScopeProvider.instance.set(currentScope);
return is;
}
private String extractLocation(String url) {
String [] loc=url.split("//");
logger.info("url extracted: "+loc[1]);
return loc[1];
}
}

137
src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPUrl.java

@ -0,0 +1,137 @@
package org.gcube.contentmanager.storageclient.model.protocol.smp;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URLConnection;
import org.gcube.contentmanager.storageclient.model.protocol.smp.SMPURLConnectionFactory;
/**
* Allow to the end user to manage a smp url as an url with a configured stream handler
* @author Roberto Cirillo (ISTI-CNR)
*
*/
@Deprecated
public class SMPUrl extends java.net.URLStreamHandler{
private java.net.URL url;
/**
* map a url string as a normal url
* @param u url string
* @throws MalformedURLException
*/
public SMPUrl(String u) throws MalformedURLException{
this.url=new java.net.URL(null, u, this);
}
public URLConnection openConnection() throws IOException{
return this.openConnection(url);
}
@Override
protected URLConnection openConnection(java.net.URL u) throws IOException {
if(u.getProtocol().equalsIgnoreCase("smp")){
return SMPURLConnectionFactory.getSmp(u);
}else{
return new java.net.URL(null, u.toString()).openConnection();
}
}
public String getHost(){
return this.url.getHost();
}
/**
* Gets the path part of this <code>URL</code>.
*
* @return the path part of this <code>URL</code>, or an
* empty string if one does not exist
* @since 1.3
*/
public String getPath() {
return url.getPath();
}
/**
* Gets the userInfo part of this <code>URL</code>.
*
* @return the userInfo part of this <code>URL</code>, or
* <CODE>null</CODE> if one does not exist
* @since 1.3
*/
public String getUserInfo() {
return url.getUserInfo();
}
/**
* Gets the authority part of this <code>URL</code>.
*
* @return the authority part of this <code>URL</code>
* @since 1.3
*/
public String getAuthority() {
return url.getAuthority();
}
/**
* Gets the port number of this <code>URL</code>.
*
* @return the port number, or -1 if the port is not set
*/
public int getPort() {
return url.getPort();
}
/**
* Gets the default port number of the protocol associated
* with this <code>URL</code>. If the URL scheme or the URLStreamHandler
* for the URL do not define a default port number,
* then -1 is returned.
*
* @return the port number
* @since 1.4
*/
public int getDefaultPort() {
return url.getDefaultPort();
}
/**
* Gets the protocol name of this <code>URL</code>.
*
* @return the protocol of this <code>URL</code>.
*/
public String getProtocol() {
return url.getProtocol();
}
/**
* Gets the file name of this <code>URL</code>.
* The returned file portion will be
* the same as <CODE>getPath()</CODE>, plus the concatenation of
* the value of <CODE>getQuery()</CODE>, if any. If there is
* no query portion, this method and <CODE>getPath()</CODE> will
* return identical results.
*
* @return the file name of this <code>URL</code>,
* or an empty string if one does not exist
*/
public String getFile() {
return url.getFile();
}
/**
* Gets the anchor (also known as the "reference") of this
* <code>URL</code>.
*
* @return the anchor (also known as the "reference") of this
* <code>URL</code>, or <CODE>null</CODE> if one does not exist
*/
public String getRef() {
return url.getRef();
}
}

118
src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/StringDecrypter.java

@ -0,0 +1,118 @@
package org.gcube.contentmanager.storageclient.model.protocol.smp;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class can be used to encrypt and decrypt using DES and a given key
*
* @author Javid Jamae @author Roberto Cirillo
*
*
*/
public class StringDecrypter {
public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
public static final String DES_ENCRYPTION_SCHEME = "DES";
private KeySpec keySpec;
private SecretKeyFactory keyFactory;
private Cipher cipher;
private static final String UNICODE_FORMAT = "UTF8";
private static final Logger logger = LoggerFactory.getLogger(StringDecrypter.class);
public StringDecrypter( String encryptionScheme , String phrase) throws EncryptionException{
if(phrase!=null)
buildEncryption(encryptionScheme, phrase);
else
throw new RuntimeException("No valid encripted phrase discovered on IS");
// buildEncryption(encryptionScheme, DEFAULT_ENCRYPTION_KEY);
}
private void buildEncryption(String encryptionScheme, String encryptionKey)
throws EncryptionException {
if ( encryptionKey == null )
throw new IllegalArgumentException( "encryption key was null" );
if ( encryptionKey.trim().length() < 24 )
throw new IllegalArgumentException(
"encryption key was less than 24 characters" );
try
{
byte[] keyAsBytes = encryptionKey.getBytes( UNICODE_FORMAT );
if ( encryptionScheme.equals( DESEDE_ENCRYPTION_SCHEME) )
{
keySpec = new DESedeKeySpec( keyAsBytes );
}
else if ( encryptionScheme.equals( DES_ENCRYPTION_SCHEME ) )
{
keySpec = new DESKeySpec( keyAsBytes );
}
else
{
throw new IllegalArgumentException( "Encryption scheme not supported: "
+ encryptionScheme );
}
keyFactory = SecretKeyFactory.getInstance( encryptionScheme );
cipher = Cipher.getInstance( encryptionScheme );
}
catch (InvalidKeyException e)
{
throw new EncryptionException( e );
}
catch (UnsupportedEncodingException e)
{
throw new EncryptionException( e );
}
catch (NoSuchAlgorithmException e)
{
throw new EncryptionException( e );
}
catch (NoSuchPaddingException e)
{
throw new EncryptionException( e );
}
}
public String decrypt( String encryptedString ) throws EncryptionException
{
logger.trace("decrypting string...");
if ( encryptedString == null || encryptedString.trim().length() <= 0 )
throw new IllegalArgumentException( "encrypted string was null or empty" );
try
{
SecretKey key = keyFactory.generateSecret( keySpec );
cipher.init( Cipher.DECRYPT_MODE, key );
return org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(encryptedString, key);
}
catch (Exception e)
{
throw new EncryptionException( e );
}
}
@SuppressWarnings("serial")
public static class EncryptionException extends Exception
{
public EncryptionException( Throwable t )
{
super( t );
}
}
}

120
src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/StringEncrypter.java

@ -0,0 +1,120 @@
package org.gcube.contentmanager.storageclient.model.protocol.smp;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class can be used to encrypt and decrypt using DES and a given key
*
* @author Javid Jamae @author Roberto Cirillo
*
*
*/
@Deprecated
public class StringEncrypter {
public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
public static final String DES_ENCRYPTION_SCHEME = "DES";
private KeySpec keySpec;
private SecretKeyFactory keyFactory;
private Cipher cipher;
private static final String UNICODE_FORMAT = "UTF8";
private static final Logger logger = LoggerFactory.getLogger(StringDecrypter.class);
public StringEncrypter( String encryptionScheme , String phrase) throws EncryptionException{
buildEncryption(encryptionScheme, phrase);
}
public StringEncrypter( String encryptionScheme) throws EncryptionException{
buildEncryption(encryptionScheme, null);
}
private void buildEncryption(String encryptionScheme, String encryptionKey)
throws EncryptionException {
if ( encryptionKey == null )
throw new IllegalArgumentException( "encryption key was null" );
if ( encryptionKey.trim().length() < 24 )
throw new IllegalArgumentException(
"encryption key was less than 24 characters" );
try
{
byte[] keyAsBytes = encryptionKey.getBytes( UNICODE_FORMAT );
if ( encryptionScheme.equals( DESEDE_ENCRYPTION_SCHEME) )
{
keySpec = new DESedeKeySpec( keyAsBytes );
}
else if ( encryptionScheme.equals( DES_ENCRYPTION_SCHEME ) )
{
keySpec = new DESKeySpec( keyAsBytes );
}
else
{
throw new IllegalArgumentException( "Encryption scheme not supported: "
+ encryptionScheme );
}
keyFactory = SecretKeyFactory.getInstance( encryptionScheme );
cipher = Cipher.getInstance( encryptionScheme );
}
catch (InvalidKeyException e)
{
throw new EncryptionException( e );
}
catch (UnsupportedEncodingException e)
{
throw new EncryptionException( e );
}
catch (NoSuchAlgorithmException e)
{
throw new EncryptionException( e );
}
catch (NoSuchPaddingException e)
{
throw new EncryptionException( e );
}
}
public String decrypt( String unencryptedString ) throws EncryptionException
{
logger.trace("decrypting string...");
if ( unencryptedString == null || unencryptedString.trim().length() <= 0 )
throw new IllegalArgumentException( "encrypted string was null or empty" );
try
{
SecretKey key = keyFactory.generateSecret( keySpec );
cipher.init( Cipher.ENCRYPT_MODE, key );
return org.gcube.common.encryption.StringEncrypter.getEncrypter().encrypt(unencryptedString, key);
}
catch (Exception e)
{
throw new EncryptionException( e );
}
}
@SuppressWarnings("serial")
public static class EncryptionException extends Exception
{
public EncryptionException( Throwable t )
{
super( t );
}
}
}

164
src/main/java/org/gcube/contentmanager/storageclient/protocol/utils/Utils.java

@ -0,0 +1,164 @@
package org.gcube.contentmanager.storageclient.protocol.utils;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type;
import org.gcube.common.scope.impl.ServiceMapScannerMediator;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* Utility class for scope identity
*
* @author Roberto Cirillo (ISTI-CNR)
*/
public class Utils {
// Temporary CONSTANTS
private static final String GCUBE_RESOLVER_HOST = "data.gcube.org";
private static final String D4SCIENCE_RESOLVER_HOST = "data.d4science.org";
private static final String GCUBE_INFRA = "gcube";
private static final String D4SCIENCE_INFRA = "d4science.research-infrastructures.eu";
public static final String INFRASTRUCTURE_ENV_VARIABLE_NAME="infrastructure";
public static final String URI_RESOLVER_RESOURCE_CATEGORY="Service";
public static final String URI_RESOLVER_RESOURCE_NAME="HTTP-URI-Resolver";
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
public static boolean validationScope2(String scope){
ScopeBean scopeBean=new ScopeBean(scope);
if((scopeBean.is(Type.VRE)))
scope=scopeBean.enclosingScope().toString();
return ServiceMapScannerMediator.isValid(scope);
}
public static ArrayList<String> getVOScopes(String scope){
ArrayList<String> vos=new ArrayList<String>();
ScopeBean scopeBean=new ScopeBean(scope);
//retrieve INFRA scope
while(!scopeBean.is(Type.INFRASTRUCTURE)){
logger.debug("the scope "+scope+" is not an INFRA scope ");
scopeBean=new ScopeBean(scopeBean.enclosingScope().toString());
}
scope=scopeBean.toString();
if(scopeBean.is(Type.INFRASTRUCTURE)){
Set<String> scopeSet=ServiceMapScannerMediator.getScopeKeySet();
for(String scopeItem : scopeSet){
//retrieve all Vo scopes
logger.debug("scope scanned: "+scopeItem);
if(scopeItem.contains(scope) && (new ScopeBean(scopeItem).is(Type.VO))){
logger.debug("found vo scope: "+scopeItem);
vos.add(scopeItem);
}
}
}
return vos;
}
public static String extractInfrastructureNewVersion(String urlParam){
logger.debug("urlParam: "+urlParam);
String infra= checkVarEnv(INFRASTRUCTURE_ENV_VARIABLE_NAME);
if (infra != null)
return infra;
else
//only for test return the infra from the uri. In prod will be throws a runtime exception
return getInfraFromResolverHost(urlParam);
}
public static String getInfraFromResolverHost(String resolverHost)