update scope parsing with ScopeBean

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/common-encryption@68959 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Roberto Cirillo 2013-02-07 11:14:44 +00:00
parent 8588c2111b
commit 7f5f52b173
5 changed files with 40 additions and 107 deletions

View File

@ -29,7 +29,7 @@
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-scope</artifactId>
<version>1.0.0</version>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
</dependency>
</dependencies>
<build>

View File

@ -12,6 +12,8 @@ import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type;
final class SymmetricKey {
@ -43,7 +45,7 @@ final class SymmetricKey {
private static void load() throws InvalidKeyException {
byte[] rawKey;
try {
InputStream is =SymmetricKey.class.getResourceAsStream("/"+getLocalKey());
InputStream is =SymmetricKey.class.getResourceAsStream("/"+getKeyFileName());
rawKey = getBytesFromStream(is);
} catch (Exception e) {
System.out.println("Unable to load the Key from the classpath");
@ -71,24 +73,40 @@ final class SymmetricKey {
return rawKey;
}
protected static String getLocalKey() throws InvalidKeyException{
String scopeReference=null;
//file that contains the key
// protected static String getLocalKey() throws InvalidKeyException{
// String scopeReference=null;
// //file that contains the key
// String keyFile=null;
// String scope=ScopeProvider.instance.get();
// System.out.println("scope founded "+scope);
// if((scope !=null) && (scope.contains("/"))){
// String[] vos=scope.split("/");
// // if the scope is a VO or VRE scope
// if((vos != null) && (vos.length > 2)){
// scopeReference=vos[2];
// // if the scope is a infrastructure scope
// }else if((vos != null) && (vos.length == 2)){
// scopeReference=vos[1];
// }else{
// throw new InvalidKeyException(" invalid key for scope: "+scope);
// }
// keyFile=scopeReference+".gcubekey";
// }else{
// throw new InvalidKeyException(" invalid key for scope: "+scope);
// }
// return keyFile;
// }
protected static String getKeyFileName() throws InvalidKeyException{
String keyFile=null;
String scope=ScopeProvider.instance.get();
System.out.println("scope founded "+scope);
if((scope !=null) && (scope.contains("/"))){
String[] vos=scope.split("/");
// if the scope is a VO or VRE scope
if((vos != null) && (vos.length > 2)){
scopeReference=vos[2];
// if the scope is a infrastructure scope
}else if((vos != null) && (vos.length == 2)){
scopeReference=vos[1];
}else{
throw new InvalidKeyException(" invalid key for scope: "+scope);
}
keyFile=scopeReference+".gcubekey";
String scope = ScopeProvider.instance.get();
if(scope!=null){
ScopeBean bean = new ScopeBean(scope);
if(bean.is(Type.VRE))
bean = bean.enclosingScope();
String name = bean.name();
//build keyfile name with name
keyFile=name+".gcubekey";
}else{
throw new InvalidKeyException(" invalid key for scope: "+scope);
}

View File

@ -17,7 +17,7 @@ public class LocalKeyTest {
@Test
public void test() throws InvalidKeyException {
String key=SymmetricKey.getLocalKey();
String key=SymmetricKey.getKeyFileName();
System.out.println("file key founded: "+key);
}

View File

@ -2,6 +2,7 @@ package org.gcube.common.utils.encryption;
import static org.junit.Assert.*;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.utils.encryption.StringEncrypter;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@ -15,6 +16,7 @@ public class StringEncrypterTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ScopeProvider.instance.set("/gcube/devsec");
key = SymmetricKey.getKey();
}

View File

@ -1,87 +0,0 @@
package org.gcube.common.utils.encryption;
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
/**
* Various helpers for XML document serialization
* @author Manuele Simi (CNR)
*
*/
public class XMLDocumentUtils {
/**
* Loads a document from the given file
* @param fileName the absolute path of the file
* @return the document
* @throws Exception if the deserialization fails or the document is not well-formed
*/
protected static Document loadFromFile(String fileName) throws Exception {
File encryptionFile = new File(fileName);
javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(encryptionFile);
System.out.println("Encryption document loaded from "
+ encryptionFile.toURI().toURL().toString());
return document;
}
/**
* Serializes the document to the given file
* @param doc the document to serialize
* @param fileName the file in which the document is persisted
* @throws Exception if the serialization fails or the document is not well-formed
*/
protected static void sendToFile(Document doc, String fileName) throws Exception {
File encryptionFile = new File(fileName);
FileOutputStream f = new FileOutputStream(encryptionFile);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(f);
transformer.transform(source, result);
f.close();
System.out.println("Wrote document containing encrypted data to "
+ encryptionFile.toURI().toURL().toString());
}
/**
* Generates a string serialization of the document
* @param doc the document to serialize
* @return the serialized string
* @throws Exception if the serialization fails or the document is not well-formed
*/
public static String serialize(Document doc) throws Exception {
StringWriter stw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(doc), new StreamResult(stw));
return stw.toString();
}
/**
* Loads a document from its string serialization
* @param serializeddoc
* @return
* @throws Exception if the deserialization fails or the document is not well-formed
*/
public static Document deserialize(String serializeddoc) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(serializeddoc)));
}
}