aslcore/src/org/gcube/application/framework/core/content/impl/DigitalObject.java

335 lines
12 KiB
Java

package org.gcube.application.framework.core.content.impl;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.axis.message.addressing.Address;
import org.apache.axis.message.addressing.EndpointReferenceType;
import org.gcube.application.framework.core.cache.CachesManager;
import org.gcube.application.framework.core.cache.HarvestersManager;
import org.gcube.application.framework.core.commons.model.CollectionInfo;
import org.gcube.application.framework.core.content.DigitalObjectInfoI;
import org.gcube.application.framework.core.session.D4ScienceSession;
import org.gcube.application.framework.core.util.CacheEntryConstants;
import org.gcube.application.framework.core.util.FindInfo;
import org.gcube.application.framework.core.util.PortalSecurityManager;
import org.gcube.application.framework.core.util.QueryString;
import org.gcube.application.framework.core.util.SessionConstants;
import org.gcube.common.core.contexts.GCUBERemotePortTypeContext;
import org.gcube.common.core.security.GCUBESecurityManager;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.CMSPortType1PortType;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.DocumentDescription;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.DocumentPropertyDescription;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.GetDocumentParameters;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.Hint;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.ReferencedObjectDescription;
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.service.CMSPortType1ServiceAddressingLocator;
import org.gcube.contentmanagement.layerindependent.descriptions.BasicInfoObjectDescription;
import org.gcube.contentmanagement.layerindependent.descriptions.BasicStorageHints;
import org.gcube.metadatamanagement.mmlibrary.model.BaseMetadata;
public class DigitalObject implements DigitalObjectInfoI {
/**
* the D4Science session
*/
protected D4ScienceSession session;
/**
* the object identifier to the object for which you want to retrieve info
*/
protected String oid;
/**
*
*/
protected String colID;
/**
*
*/
protected CollectionInfo colInfo;
/**
*
*/
private final static int chunkSize = 1048576;
/**
*
*/
protected static AtomicInteger cmsId = new AtomicInteger(0);
/**
* @param session the D4Science session to be used for retrieving information needed
* @param oid the object identifier
* @param colID the collection identifier
*/
public DigitalObject(D4ScienceSession session, String oid, String colID) {
super();
this.session = session;
this.oid = oid;
this.colID = colID;
List<CollectionInfo>[] collections = (List<CollectionInfo>[]) session.getAttribute(SessionConstants.Collections);
colInfo = FindInfo.findCollectionInfo(colID, collections);
}
/* (non-Javadoc)
* @see org.gcube.application.framework.DigitalObjectInfoI#getAvailableSchemata()
*/
public List<String> getAvailableSchemata() {
List<String> schemata = new ArrayList<String>();
for(int i=0; i<colInfo.getMetadataSize(); i++)
{
schemata.add(colInfo.getSchema(i));
}
return schemata;
}
/* (non-Javadoc)
* @see org.gcube.application.framework.DigitalObjectInfoI#getLength()
*/
public long getLength() {
try {
QueryString query = new QueryString();
query.addParameter(CacheEntryConstants.dl, session.getScopeName());
query.addParameter(CacheEntryConstants.oid, oid);
DocumentDescription docDescription = (DocumentDescription) CachesManager.getInstance().getContentCache().get(query).getValue();
DocumentPropertyDescription[] properties = docDescription.getProperties();
for(int j = 0 ; j < properties.length; j++)
{
if(properties[j].getName().indexOf("LengthOfRawContent") != -1)
{
return Long.parseLong(properties[j].getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
//TODO: edw tha prepei na mpei kai i glwssa gia na einai unique...
/* (non-Javadoc)
* @see org.gcube.application.framework.DigitalObjectInfoI#getMetadata(java.lang.String)
*/
public BaseMetadata getMetadata(String schema) {
try {
QueryString query = new QueryString();
query.addParameter(CacheEntryConstants.dl, session.getScopeName());
query.addParameter(CacheEntryConstants.oid, oid);
query.addParameter(CacheEntryConstants.metadataColID, colInfo.getMetadataID(colInfo.getIndexOfSchema(schema)));
return (BaseMetadata) CachesManager.getInstance().getMetadataCache().get(query).getValue();
}
catch(Exception e)
{
}
return null;
}
/* (non-Javadoc)
* @see org.gcube.application.framework.DigitalObjectInfoI#getMimeType()
*/
public String getMimeType() {
try {
QueryString query = new QueryString();
query.addParameter(CacheEntryConstants.dl, session.getScopeName());
query.addParameter(CacheEntryConstants.oid, oid);
DocumentDescription docDescription = (DocumentDescription) CachesManager.getInstance().getContentCache().get(query).getValue();
return docDescription.getMimeType();
} catch (Exception e) {
e.printStackTrace();
}
return "unknown/unknown";
}
/* (non-Javadoc)
* @see org.gcube.application.framework.DigitalObjectInfoI#getObject()
*/
public OutputStream getObject() {
try {
String [] cmsURIs = HarvestersManager.getInstance().getISInfo(session.getScope()).getEndPointForCMSService("");
boolean excep = true;
String cmsAddress;
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i = 0; i < cmsURIs.length && excep; i++) {
excep = false;
cmsAddress = cmsURIs[cmsId.getAndIncrement()% cmsURIs.length];
try {
System.out.println("CID=" + oid + " cmsAddress=" + cmsAddress);
CMSPortType1PortType cms;
EndpointReferenceType endpoint = new EndpointReferenceType();
endpoint.setAddress(new Address(cmsAddress));
CMSPortType1ServiceAddressingLocator cmslocator = new CMSPortType1ServiceAddressingLocator();
cms = cmslocator.getCMSPortType1PortTypePort(endpoint);
GCUBESecurityManager secManager = new PortalSecurityManager();
if(secManager.isSecurityEnabled())
{
try {
secManager.useCredentials(session.getCredential());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
GCUBERemotePortTypeContext.getProxy(cms, session.getScope(), secManager);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int count = 0;; count++) {
// Retrieving content in chunks :-)
Hint[] hints = new Hint[2];
hints[0] = new Hint();
hints[0].setName(BasicStorageHints.HINT_NAME_READING_START_OFFSET);
hints[0].setValue("" + count * chunkSize);
hints[1] = new Hint();
hints[1].setName(BasicStorageHints.HINT_NAME_LIMIT_CONTENT_LENGTH_READ);
hints[1].setValue("" + chunkSize);
GetDocumentParameters getDocumentParams = new GetDocumentParameters();
getDocumentParams.setDocumentID(oid);
getDocumentParams.setTargetFileLocation(BasicInfoObjectDescription.RAW_CONTENT_IN_MESSAGE);
getDocumentParams.setHints(hints);
// inmessage://
try {
DocumentDescription document = cms.getDocument(getDocumentParams);
byte[] buffer = document.getRawContent();
if (buffer == null)
break;
out.write(buffer);
out.flush();
if (buffer.length < chunkSize)
break;
buffer = null;
if (count % 5 == 0)
System.gc();
} catch (RemoteException e) {
count--;
e.printStackTrace();
} catch (Throwable e1) {
e1.printStackTrace();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
excep = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/* (non-Javadoc)
* @see org.gcube.application.framework.DigitalObjectInfoI#getObjectByName(java.lang.String, java.lang.String)
*/
public FileOutputStream getObjectByName(String name, String collectionName) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.gcube.application.framework.DigitalObjectInfoI#getPrimaryRoles()
*/
public HashMap<String, String> getPrimaryRoles() {
HashMap<String, String> primRoles = new HashMap<String, String>();
try {
QueryString query = new QueryString();
query.addParameter(CacheEntryConstants.dl, session.getScopeName());
query.addParameter(CacheEntryConstants.oid, oid);
DocumentDescription docDescription = (DocumentDescription) CachesManager.getInstance().getContentCache().get(query).getValue();
ReferencedObjectDescription[] references = docDescription.getReferences();
for(int j = 0 ; j < references.length; j++)
{
primRoles.put(references[j].getRole(), references[j].getTargetObjectID());
}
} catch (Exception e) {
e.printStackTrace();
}
return primRoles;
}
/* (non-Javadoc)
* @see org.gcube.application.framework.DigitalObjectInfoI#getSecondaryRoles()
*/
public HashMap<String, String> getSecondaryRoles() {
HashMap<String, String> secRoles = new HashMap<String, String>();
try {
QueryString query = new QueryString();
query.addParameter(CacheEntryConstants.dl, session.getScopeName());
query.addParameter(CacheEntryConstants.oid, oid);
DocumentDescription docDescription = (DocumentDescription) CachesManager.getInstance().getContentCache().get(query).getValue();
ReferencedObjectDescription[] references = docDescription.getReferences();
for(int j = 0 ; j < references.length; j++)
{
secRoles.put(references[j].getSecondaryRole(), references[j].getTargetObjectID());
}
} catch (Exception e) {
e.printStackTrace();
}
return secRoles;
}
/* (non-Javadoc)
* @see org.gcube.application.framework.DigitalObjectInfoI#getThumbnail(int, int)
*/
public byte[] getThumbnail(int width, int height) {
try {
QueryString query = new QueryString();
query.addParameter(CacheEntryConstants.dl, session.getScopeName());
query.addParameter(CacheEntryConstants.oid, oid);
query.addParameter(CacheEntryConstants.width, "" + width);
query.addParameter(CacheEntryConstants.height, "" + height);
return (byte[]) CachesManager.getInstance().getThumbnailCache().get(query).getValue();
}
catch (Exception e) {
}
return null;
}
/* (non-Javadoc)
* @see org.gcube.application.framework.DigitalObjectInfoI#updateMetadata(org.gcube.metadatamanagement.mmlibrary.model.BaseMetadata)
*/
public void updateMetadata(BaseMetadata metadata) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.gcube.application.framework.DigitalObjectInfoI#getName()
*/
public String getName() {
try {
QueryString query = new QueryString();
query.addParameter(CacheEntryConstants.dl, session.getScopeName());
query.addParameter(CacheEntryConstants.oid, oid);
DocumentDescription docDescription = (DocumentDescription) CachesManager.getInstance().getContentCache().get(query).getValue();
return docDescription.getName();
} catch (Exception e) {
e.printStackTrace();
}
return "unknown";
}
}