Cloud Software Group, Inc. EBX®
Digital Asset Manager Add-on Documentation > Developer Guide > Alternative storage locations
Navigation modeDigital Asset Manager Add-on Documentation > Developer Guide > Alternative storage locations

Example Implementation

Overview

This section provides a sample configuration by connecting to a Google Drive. Note these requirements are specific to Google Drive at the time of publishing and your requirements will vary depending on how you plan to implement the API.

Sample external manager class

As shown below, you can begin with a manager class that implements the ExternalManager interface. The sample includes search functionality, asset display, and other features. Keep in mind that although you can code most add-on features including download, upload and delete, administrators can still define permissions on these actions when creating the Drive and D.A.C in the UI.

Attention

The samples below have several references to a helper class (not shown) that has been defined to meet requirements specific to making Google Drive API calls. The contents of your helper class will depend on implementation requirements.

The sections below provide the following samples:

Class implementation

import java.io.*;
import java.math.*;
import java.net.*;
import java.util.*;

import javax.activation.*;

import com.onwbp.base.text.bean.*;
import com.orchestranetworks.addon.dama.*;
import com.orchestranetworks.addon.dama.common.*;
import com.orchestranetworks.addon.dama.ext.bean.*;
import com.orchestranetworks.addon.dama.ext.exception.*;
import com.orchestranetworks.addon.dama.ext.exception.DAMException;
import com.orchestranetworks.addon.dama.ext.resource.*;
import com.orchestranetworks.addon.dama.externalmanagement.*;
import com.orchestranetworks.addon.dama.externalmanagement.bean.*;
import com.orchestranetworks.addon.dama.externalmanagement.request.*;
import com.orchestranetworks.addon.dama.externalmanagement.response.*;
import com.orchestranetworks.addon.dama.models.*;
import com.orchestranetworks.addon.utils.*;

/**
 */
public class ExternalGoogleManager implements ExternalManager
{
	//Instance variable to access the helper class for the Google Drive
	private Drive googleDrive;

	public ExternalGoogleManager()
	{
		try
		{
			this.googleDrive = GoogleDriveAPIHelper.getInstance().getDriveService();
		}
		catch (Exception ex)
		{
			throw new RuntimeException(ex);
		}
	}
	
	//Sample method used to pass asset URL information to other methods
	private String getUrl(String externalId, String fileName, String extension, boolean isDownload)
		throws UnsupportedEncodingException
	{
		//The network location where EBX is running
		String host = "localhost:8080";

		String providerLink = "http://" + host
			+ "/<your module name>/<your service dispatcher>?service=<your service name>";
		StringBuilder urlDownload = new StringBuilder(providerLink);
		urlDownload.append("&external_id=").append(URLEncoder.encode(externalId, "UTF-8"));
		urlDownload.append("&fileName=")
			.append(URLEncoder.encode(this.getFileName(fileName, extension), "UTF-8"));
		urlDownload.append("&extension=").append(extension);
		if (isDownload)
		{
			urlDownload.append("&download=").append(Boolean.TRUE.toString());
		}

		return urlDownload.toString();
	}

	private LabelDescription getLabelDescription(String name, List<Locale> locales)
	{
		if (AddonStringUtils.isEmpty(name))
		{
			name = DAMConstants.EMPTY_STRING;
		}

		List<LabelDescriptionForLocale> labelDescriptionForLocales = new ArrayList<>();
		for (Locale locale : locales)
		{
			LabelDescriptionForLocale labelDesLocale = new LabelDescriptionForLocale();
			labelDesLocale.setLocale(locale);
			labelDesLocale.setLabel(name);
			labelDesLocale.setDescription(name);
			labelDescriptionForLocales.add(labelDesLocale);
		}

		LabelDescription labelDescription = new LabelDescription();
		labelDescription.setLocalizedDocumentations(labelDescriptionForLocales);

		return labelDescription;
	}
}

Search filter implementation

public ExternalSearchAssetsResult searchAssets(ExternalSearchRequest searchRequest)
{
	ExternalSearchAssetsResult result = new ExternalSearchAssetsResult();
	SearchFilter searchFilter = searchRequest.getSearchFilter();
	String keyword = (searchFilter == null) ? DAMConstants.EMPTY_STRING
		: searchFilter.getKeyword();
	try
	{
		FileList fileList = this.googleDrive.files().list().setFields("*").execute();
		List<File> allFiles = fileList.getFiles();
		List<File> filteredFiles = new ArrayList<>();

		for (File file : allFiles)
		{
			if (AddonStringUtils.isEmpty(keyword))
			{
				filteredFiles.add(file);
			}
			else
			{
				if (file.getName().contains(keyword)
					|| file.getOriginalFilename().contains(keyword))
				{
					filteredFiles.add(file);
				}
			}
		}
		int size = filteredFiles.size();
		int pageIndex = (searchRequest.getPageIndex() < 1) ? 1 : searchRequest.getPageIndex();
		int pageSize = (searchRequest.getPageSize() < 10) ? 10 : searchRequest.getPageSize();
		int startIndex = (pageIndex - 1) * pageSize;
		int endIndex = startIndex + pageSize - 1;
		if (endIndex > size - 1)
		{
			endIndex = size - 1;
		}

		List<ExternalDigitalAsset> externalAssets = new ArrayList<>();
		for (int i = startIndex; i <= endIndex; i++)
		{
			File googleFile = filteredFiles.get(i);
			String downloadUrl = this.getUrl(
				googleFile.getId(),
				googleFile.getName(),
				googleFile.getFileExtension(),
				true);
			String previewUrl = this.getUrl(
				googleFile.getId(),
				googleFile.getName(),
				googleFile.getFileExtension(),
				false);
			ExternalDigitalAsset externalAsset = new ExternalDigitalAsset();
			externalAsset.setLabelDescription(
				this.getLabelDescription(
					googleFile.getName(),
					searchRequest.getHome().getRepository().getLocales()));
			externalAsset.setURL(previewUrl);
			externalAsset.setThumbnailURL(googleFile.getThumbnailLink());
			externalAsset.setDownloadURL(downloadUrl);
			externalAsset.setExtension(googleFile.getFileExtension());
			externalAsset.setAssetType(googleFile.getFileExtension());
			externalAsset.setId(googleFile.getId());
			externalAsset.setPhysicalName(googleFile.getName());
			externalAsset.setCreationDate(new Date(googleFile.getCreatedTime().getValue()));
			externalAsset.setLastUpdatedDate(new Date(googleFile.getModifiedTime().getValue()));
			externalAsset.setFileSize(new BigDecimal(googleFile.getSize().longValue()));
			Map<String, String> metaDataMap = new HashMap<>();
			metaDataMap.put("mKey1", "mValue1");
			metaDataMap.put("mKey2", "mValue2");
			externalAsset.setMetaDatas(metaDataMap);
			externalAsset.setWidth(new Integer(300));
			externalAsset.setHeight(new Integer(200));

			externalAssets.add(externalAsset);
		}
        // Specifies the total digital assets of search results.
        result.setTotal(1000)
		result.setAssets(externalAssets);
	}
	catch (Exception ex)
	{
		throw new RuntimeException(ex);
	}

	return result;
}

Asset creation

public ExternalDigitalAsset createAsset(ExternalUploadAssetRequest request)
{
	ExternalDigitalAsset asset = new ExternalDigitalAsset();
	File googleFile = new File();
	FileResource fileResource = request.getFileResource();
	String fileName = fileResource.getFile().getName();
	googleFile.setName(fileName);
	FileContent mediaContent = new FileContent(
		new MimetypesFileTypeMap().getContentType(fileName),
		fileResource.getFile());
	try
	{
		File googleFileUploaded = this.googleDrive.files()
			.create(googleFile, mediaContent)
			.setFields("*")
			.execute();
		asset.setId(googleFileUploaded.getId());
		asset.setPhysicalName(googleFileUploaded.getOriginalFilename());
		asset.setThumbnailURL(googleFileUploaded.getThumbnailLink());
		asset.setLabelDescription(
			this.getLabelDescription(
				googleFileUploaded.getName(),
				request.getHome().getRepository().getLocales()));
	}
	catch (IOException ex)
	{
		throw new RuntimeException(ex);
	}

	return asset;
}

Asset deletion

public OperationExecutionStatus deleteAsset(
			ExternalSingularRequest request,
			boolean isPhysicalDelete)
{
	OperationExecutionStatus status = new OperationExecutionStatus();
	try
	{
		this.googleDrive.files().delete(request.getExternalId()).execute();
	}
	catch (IOException ex)
	{
		status = new OperationExecutionStatus(new DAMException(ex.getMessage()));
	}

	return status;
}

Asset retrieval

public ExternalDigitalAsset getAsset(ExternalSingularRequest request) throws DAMException
{
	ExternalDigitalAsset digitalAsset = new ExternalDigitalAsset();
	try
	{
		File file = this.googleDrive.files()
			.get(request.getExternalId())
			.setFields("*")
			.execute();
		String urlDownload = this
			.getUrl(file.getId(), file.getName(), file.getFileExtension(), true);
		String previewUrl = this
			.getUrl(file.getId(), file.getName(), file.getFileExtension(), false);
		digitalAsset.setId(file.getId());
		digitalAsset.setPhysicalName(file.getOriginalFilename());
		digitalAsset.setLabelDescription(
			this.getLabelDescription(
				file.getName(),
				request.getHome().getRepository().getLocales()));
		digitalAsset.setURL(previewUrl);

		boolean isImage = DigitalAssetExtension.parseExtension(file.getFileExtension())
			.getDigitalAssetContentType()
			.isImage();
		if (isImage)
		{
			digitalAsset.setThumbnailURL((previewUrl));
		}
		else
		{
			digitalAsset.setThumbnailURL(file.getThumbnailLink());
		}
		digitalAsset.setDownloadURL(urlDownload);
		digitalAsset.setAssetType(file.getFileExtension());
		digitalAsset.setExtension(file.getFileExtension());
		digitalAsset.setCreationDate(new Date(file.getCreatedTime().getValue()));
		digitalAsset.setLastUpdatedDate(new Date(file.getModifiedTime().getValue()));
		digitalAsset.setFileSize(new BigDecimal(file.getSize().longValue()));
		Map<String, String> metaDataMap = new HashMap<>();
		metaDataMap.put("mKey1", "mValue1");
		metaDataMap.put("mKey2", "mValue2");
		digitalAsset.setMetaDatas(metaDataMap);
		digitalAsset.setWidth(new Integer(300));
		digitalAsset.setHeight(new Integer(200));
	}
	catch (IOException ex)
	{
		throw new DAMException(ex);
	}

	return digitalAsset;
}

public List<ExternalDigitalAsset> getAssets(ExternalPluralRequest externalRequest)
{
	List<ExternalDigitalAsset> externalAssets = new ArrayList<>();
	for (String externalId : externalRequest.getExternalIds())
	{
		try
		{
			File file;
			try
			{
				file = this.googleDrive.files().get(externalId).setFields("*").execute();
			}
			catch (Exception ex)
			{
				continue;
			}

			ExternalDigitalAsset externalDigitalAsset = new ExternalDigitalAsset();
			externalDigitalAsset.setId(file.getId());
			externalDigitalAsset.setPhysicalName(file.getOriginalFilename());
			externalDigitalAsset.setCreationDate(new Date(file.getCreatedTime().getValue()));
			externalDigitalAsset
				.setLastUpdatedDate(new Date(file.getModifiedTime().getValue()));
			externalDigitalAsset.setLabelDescription(
				this.getLabelDescription(
					file.getName(),
					externalRequest.getHome().getRepository().getLocales()));
			String urlDownload = this
				.getUrl(file.getId(), file.getName(), file.getFileExtension(), true);
			String previewUrl = this
				.getUrl(file.getId(), file.getName(), file.getFileExtension(), false);
			externalDigitalAsset.setURL(previewUrl);
			externalDigitalAsset.setThumbnailURL(file.getThumbnailLink());
			externalDigitalAsset.setWidth(new Integer(300));
			externalDigitalAsset.setHeight(new Integer(200));
			externalDigitalAsset.setDownloadURL(urlDownload);
			externalDigitalAsset.setAssetType(file.getFileExtension());
			externalDigitalAsset.setExtension(file.getFileExtension());

			Map<String, String> metaDatas = new HashMap<>();
			metaDatas.put("metadata1", "metadata1");
			metaDatas.put("metadata2", "metadata2");
			externalDigitalAsset.setMetaDatas(metaDatas);

			externalDigitalAsset.setTags(Arrays.asList("tag1", "tag2"));

			externalAssets.add(externalDigitalAsset);
		}
		catch (IOException ex)
		{
			throw new RuntimeException(ex);
		}
	}

	return externalAssets;
}

Asset tags

public ExternalTagResult getTags(ExternalCommonRequest request)
{
	ExternalTagResult externalTagResult = new ExternalTagResult();

	Tag tag1 = new Tag();
	tag1.setCode("tag1");
	tag1.setFontSize("14");
	externalTagResult.getTags().add(tag1);

	Tag tag2 = new Tag();
	tag2.setCode("tag2");
	tag2.setFontSize("16");
	externalTagResult.getTags().add(tag2);

	return externalTagResult;
}

Sample definition class

The following code sample shows how to instantiate the class you implemented above. Additionally, note that you can use the allowUpload() to determine whether users can upload to the external storage location.

import com.orchestranetworks.addon.dama.externalmanagement.*;

public final class ExternalGoogleManagerDefinition extends ExternalManagerDefinition
{
	// Creates a new ExternalManager based on your implementation
	public ExternalManager getExternalManager()
	{
		return new ExternalGoogleManager();
	}
	
	// Determines whether upload to the external storage location is enabled
	public boolean allowUpload()
	{
		return true;
	}
}

Registering your manager class

You can register your custom class either on repository startup or by running a service in the UI. For information on how to declare a module (for startup registration) or declare a user service, please see the TIBCO EBX® Developer Guide.

Including third-party resources

Any resources required by a third-party should be added to the same location as your ebx.jar file. As shown below the required libraries for the Google Drive API are deployed in Tomcat along with EBX®.

/third-party-libraries.png