300 lines
10 KiB
Java
300 lines
10 KiB
Java
package org.gcube.application.framework.core.cache.factories;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
import javax.xml.rpc.ServiceException;
|
|
|
|
import org.apache.axis.message.addressing.Address;
|
|
import org.apache.axis.message.addressing.EndpointReference;
|
|
import org.apache.axis.message.addressing.EndpointReferenceType;
|
|
import org.apache.axis.types.URI.MalformedURIException;
|
|
import org.gcube.application.framework.core.cache.CachesManager;
|
|
import org.gcube.application.framework.core.cache.RIsManager;
|
|
import org.gcube.application.framework.core.security.ServiceContextManager;
|
|
import org.gcube.application.framework.core.util.CacheEntryConstants;
|
|
import org.gcube.application.framework.core.util.QueryString;
|
|
import org.gcube.application.framework.core.util.ThumbnailConstants;
|
|
import org.gcube.common.core.scope.GCUBEScope;
|
|
import org.gcube.common.core.scope.GCUBEScope.MalformedScopeExpressionException;
|
|
import org.gcube.common.core.utils.logging.GCUBELog;
|
|
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.AlternativeRepresentationDescription;
|
|
import org.gcube.contentmanagement.contentlayer.contentmanagementservice.stubs.ArrayOfAlternativeRepresentation;
|
|
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.service.CMSPortType1ServiceAddressingLocator;
|
|
import org.gcube.contentmanagement.layerindependent.descriptions.BasicInfoObjectDescription;
|
|
import org.gcube.informationsystem.cache.SrvType;
|
|
import org.gcube.thumbnailer.stubs.ThumbnailerPortType;
|
|
import org.gcube.thumbnailer.stubs.TransformContent;
|
|
import org.gcube.thumbnailer.stubs.service.ThumbnailServiceAddressingLocator;
|
|
import org.ietf.jgss.GSSCredential;
|
|
|
|
import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
|
|
|
|
/**
|
|
* @author Valia Tsagkalidou (NKUA)
|
|
*
|
|
*/
|
|
public class ThumbnailCacheEntryFactory implements CacheEntryFactory {
|
|
|
|
/** Object logger. */
|
|
protected final GCUBELog logger = new GCUBELog(this);
|
|
|
|
protected static AtomicInteger thumbId = new AtomicInteger(0);
|
|
|
|
/**
|
|
* @param key a QueryString representing pairs of keys and values.
|
|
* @return a Stream containing the thumbnail
|
|
*/
|
|
public Object createEntry(Object key) throws Exception {
|
|
|
|
QueryString query = (QueryString) key;
|
|
String vre = query.get(CacheEntryConstants.vre);
|
|
String oid = query.get(CacheEntryConstants.oid);
|
|
int width = Integer.parseInt(query.get(CacheEntryConstants.width));
|
|
int height = Integer.parseInt(query.get(CacheEntryConstants.height));
|
|
String[] thumbOptions = query.get(CacheEntryConstants.thumbOptions).split(",");
|
|
|
|
ArrayOfAlternativeRepresentation altRes;
|
|
List<String[]> thumbs = new ArrayList<String[]>();
|
|
CMSPortType1PortType cms = null;
|
|
int k =0;
|
|
while(true)
|
|
{
|
|
cms = getCMS(vre);
|
|
try
|
|
{
|
|
altRes = cms.getAlternativeRepresentations(oid);
|
|
}
|
|
catch (Exception e) {
|
|
k++;
|
|
if(k == 5)
|
|
break;
|
|
else
|
|
continue;
|
|
}
|
|
AlternativeRepresentationDescription[] docRes = altRes.getDocumentRepresentations();
|
|
if(docRes != null)
|
|
{
|
|
logger.debug("Found " + docRes.length + " alternative representations!!!");
|
|
for(int i=0; i < docRes.length; i++)
|
|
{
|
|
String toid = docRes[i].getRepresentationID();
|
|
if(docRes[i].getRepresentationRole().equals("thumbnail"))
|
|
{
|
|
logger.debug("Thumbnail Representation found: " + toid + "for object: " + oid + " description: " +docRes[i].getRepresenationDescription() + " role:" + docRes[i].getRepresentationRole());
|
|
|
|
QueryString newQuery = new QueryString();
|
|
newQuery.addParameter(CacheEntryConstants.vre, vre);
|
|
newQuery.addParameter(CacheEntryConstants.oid, toid);
|
|
DocumentDescription docDescription = (DocumentDescription) CachesManager.getInstance().getContentCache().get(newQuery).getValue();
|
|
DocumentPropertyDescription[] props = docDescription.getProperties();
|
|
logger.debug("Properties available: " + props.length);
|
|
String[] resolution = getResolution(props);
|
|
if(resolution != null)
|
|
thumbs.add(resolution);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
String thoid;
|
|
String[] thumbObj = findThumbnail(thumbs, thumbOptions, height, width);
|
|
if(thumbObj == null && !containOption(thumbOptions, ThumbnailConstants.FORCE_CREATE))
|
|
return null;
|
|
else if(thumbObj == null)
|
|
{
|
|
|
|
while(true)
|
|
{
|
|
ThumbnailerPortType stub = getThumb(vre);
|
|
TransformContent params = new TransformContent();
|
|
params.setOID(oid);
|
|
params.setHeight(width);
|
|
params.setWidth(height);
|
|
try
|
|
{
|
|
String tOid = stub.transformContent(params);
|
|
logger.debug("Object with id: " + oid + " was transformed to object " + tOid + "!");
|
|
thoid = tOid;
|
|
}
|
|
catch (Exception e) {
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
|
|
}
|
|
else
|
|
thoid = thumbObj[2];
|
|
|
|
// The thumbnail existed or just created! Going to retrieve it from cms now!!!
|
|
GetDocumentParameters params = new GetDocumentParameters();
|
|
params.setDocumentID(thoid);
|
|
params.setTargetFileLocation(BasicInfoObjectDescription.RAW_CONTENT_IN_MESSAGE);
|
|
params.setUnrollLevels(3);
|
|
|
|
logger.debug("Calling cms to get the object");
|
|
DocumentDescription docDescr = cms.getDocument(params );
|
|
logger.debug("CMS call ended");
|
|
return docDescr.getRawContent();
|
|
|
|
}
|
|
|
|
private static ThumbnailerPortType getThumb(String vre) throws MalformedURIException,
|
|
ServiceException {
|
|
EndpointReference[] thumbURIs = null;
|
|
try {
|
|
|
|
thumbURIs = RIsManager.getInstance().getISCache(GCUBEScope.getScope(vre)).getEPRsFor("Portal","Thumbnailer-service",SrvType.SIMPLE.name());
|
|
} catch (Exception e2) {
|
|
// TODO Auto-generated catch block
|
|
e2.printStackTrace();
|
|
}
|
|
String thumbAddress;
|
|
thumbAddress = thumbURIs[thumbId.getAndIncrement() % thumbURIs.length].getAddress().toString();
|
|
ThumbnailerPortType thumb;
|
|
EndpointReferenceType endpoint = new EndpointReferenceType();
|
|
endpoint.setAddress(new Address(thumbAddress));
|
|
ThumbnailServiceAddressingLocator thumblocator = new ThumbnailServiceAddressingLocator();
|
|
thumb = thumblocator.getThumbnailerPortTypePort(endpoint);
|
|
|
|
GSSCredential cred = ApplicationCredentials.getInstance().getCredential(vre);
|
|
try {
|
|
thumb = ServiceContextManager.applySecurity(thumb, GCUBEScope.getScope(vre), cred);
|
|
} catch (MalformedScopeExpressionException e) {
|
|
return null;
|
|
} catch (Exception e) {
|
|
return null;
|
|
}
|
|
|
|
return thumb;
|
|
}
|
|
|
|
private String[] findThumbnail(List<String[]> thumbs, String[] thumbOptions, int height, int width) {
|
|
if(thumbs == null || thumbs.size() == 0)
|
|
return null;
|
|
|
|
String[] res = null;
|
|
for(int i = 0; i < thumbs.size(); i++)
|
|
{
|
|
String[] thumbnail = thumbs.get(i);
|
|
int wThumb = Integer.parseInt(thumbnail[0]);
|
|
int hThumb = Integer.parseInt(thumbnail[1]);
|
|
if(containOption(thumbOptions, ThumbnailConstants.EQUAL))
|
|
{
|
|
if((height == hThumb && width >=wThumb)|| (height >= hThumb && width == wThumb))
|
|
return thumbnail;
|
|
}
|
|
else if(containOption(thumbOptions, ThumbnailConstants.FLOOR))
|
|
{
|
|
if(height >= hThumb && width >=wThumb)
|
|
{
|
|
if(res != null)
|
|
{
|
|
int w = Integer.parseInt(res[0]);
|
|
int h = Integer.parseInt(res[1]);
|
|
if(h < hThumb || w < wThumb)
|
|
res = thumbnail;
|
|
}
|
|
else
|
|
res = thumbnail;
|
|
}
|
|
}
|
|
else if(containOption(thumbOptions, ThumbnailConstants.CEIL))
|
|
{
|
|
logger.debug("hThumb= " + hThumb + " wThumb= " + wThumb);
|
|
if(height <= hThumb && width <=wThumb)
|
|
{
|
|
if(res != null)
|
|
{
|
|
int w = Integer.parseInt(res[0]);
|
|
int h = Integer.parseInt(res[1]);
|
|
logger.debug("h= " + h + " w= " + w);
|
|
if(h > hThumb || w > wThumb)
|
|
res = thumbnail;
|
|
}
|
|
else
|
|
res = thumbnail;
|
|
}
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
private boolean containOption(String[] thumbOptions, String option) {
|
|
for(int i=0; i < thumbOptions.length; i++)
|
|
{
|
|
if(thumbOptions[i].equals(option))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private String[] getResolution(DocumentPropertyDescription[] props) {
|
|
String[] resolution = new String[3];
|
|
int x = 0;
|
|
for(int i=0; i< props.length && x < 2; i++)
|
|
{
|
|
logger.debug("Property: " + props[i].getName() + " with value: " + props[i].getValue());
|
|
if(props[i].getName().equals("width"))
|
|
{
|
|
resolution[0] = props[i].getValue();
|
|
x++;
|
|
}
|
|
else if(props[i].getName().equals("height"))
|
|
{
|
|
resolution[1] = props[i].getValue();
|
|
x++;
|
|
}
|
|
}
|
|
resolution[2] = props[0].getObjectID();
|
|
if(x < 2)
|
|
return null;
|
|
else
|
|
return resolution;
|
|
}
|
|
|
|
protected static CMSPortType1PortType getCMS(String vre) throws MalformedURIException,
|
|
ServiceException {
|
|
|
|
EndpointReference[] cmsURIs = null;
|
|
try {
|
|
|
|
cmsURIs = RIsManager.getInstance().getISCache(GCUBEScope.getScope(vre)).getEPRsFor("ContentManagement","ContentManagementService",SrvType.SIMPLE.name());
|
|
} catch (Exception e2) {
|
|
// TODO Auto-generated catch block
|
|
e2.printStackTrace();
|
|
}
|
|
boolean excep = true;
|
|
String cmsAddress;
|
|
Random random = new Random();
|
|
cmsAddress = cmsURIs[random.nextInt(cmsURIs.length)].getAddress().toString();
|
|
CMSPortType1PortType cms;
|
|
EndpointReferenceType endpoint = new EndpointReferenceType();
|
|
endpoint.setAddress(new Address(cmsAddress));
|
|
CMSPortType1ServiceAddressingLocator cmslocator = new CMSPortType1ServiceAddressingLocator();
|
|
cms = cmslocator.getCMSPortType1PortTypePort(endpoint);
|
|
try {
|
|
cms = ServiceContextManager.applySecurity(cms, GCUBEScope.getScope(vre), ApplicationCredentials.getInstance().getCredential(vre));
|
|
} catch (MalformedScopeExpressionException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
} catch (Exception e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
return cms;
|
|
}
|
|
|
|
}
|