Advertisement
Guest User

AzureContentStore.java

a guest
May 24th, 2018
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. package com.eisenvault.azure;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Calendar;
  5. import java.util.Collections;
  6. import java.util.GregorianCalendar;
  7. import java.util.Map;
  8.  
  9. import org.alfresco.repo.content.AbstractContentStore;
  10. import org.alfresco.repo.content.ContentStore;
  11. import org.alfresco.repo.content.ContentStoreCreatedEvent;
  12. import org.alfresco.repo.content.UnsupportedContentUrlException;
  13. import org.alfresco.repo.content.filestore.FileContentStore;
  14. import org.alfresco.service.cmr.repository.ContentReader;
  15. import org.alfresco.service.cmr.repository.ContentWriter;
  16. import org.alfresco.util.GUID;
  17. import org.alfresco.util.Pair;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.springframework.beans.BeansException;
  22. import org.springframework.context.ApplicationContext;
  23. import org.springframework.context.ApplicationContextAware;
  24. import org.springframework.context.ApplicationEvent;
  25. import org.springframework.context.ApplicationListener;
  26. import org.springframework.context.event.ContextRefreshedEvent;
  27.  
  28.  
  29. //import com.amazonaws.services.s3.transfer.TransferManager;
  30. import com.microsoft.azure.storage.CloudStorageAccount;
  31. import com.microsoft.azure.storage.blob.CloudBlobClient;
  32. import com.microsoft.azure.storage.blob.CloudBlobContainer;
  33. import com.microsoft.azure.storage.blob.CloudBlockBlob;
  34.  
  35. public class AzureContentStore extends AbstractContentStore implements
  36. ApplicationContextAware, ApplicationListener<ApplicationEvent> {
  37.  
  38. private static final Log logger = LogFactory
  39. .getLog(AzureContentStore.class);
  40. private ApplicationContext applicationContext;
  41.  
  42. //private TransferManager transferManager;
  43.  
  44. private String accessKey;
  45. private String accountName;
  46. private String containerName;
  47. private String rootDirectory;
  48. //private String pathToObject;
  49.  
  50. private CloudStorageAccount storageAccount;
  51. private CloudBlobClient blobClient;
  52. private CloudBlobContainer container;
  53.  
  54. @Override
  55. public boolean isWriteSupported() {
  56. return true;
  57. }
  58.  
  59. @Override
  60. public ContentReader getReader(String contentUrl) {
  61.  
  62. String pathToObject = makePathToObject(contentUrl);
  63. return new AzureContentReader(pathToObject, contentUrl, container);
  64.  
  65. }
  66.  
  67. public void init() {
  68. try {
  69. // check if access key is specified
  70. if (StringUtils.isNotBlank(this.accessKey)) {
  71.  
  72. logger.debug("Found Azure Access Key in properties file");
  73.  
  74. } else {
  75. throw new Exception(
  76. "Azure Access Key not specified in properties");
  77. }
  78. // check if account name is specified
  79. if (StringUtils.isNotBlank(this.accountName)) {
  80.  
  81. logger.debug("Found Azure Account Name in properties file");
  82.  
  83. } else {
  84. throw new Exception(
  85. "Azure Account Name not specified in properties");
  86. }
  87. // check if container name is specified
  88. if (StringUtils.isNotBlank(this.containerName)) {
  89.  
  90. logger.debug("Found Azure Container Name in properties file");
  91.  
  92. } else {
  93. throw new Exception(
  94. "Azure Container Name not specified in properties");
  95. }
  96. // Build Azure connection string
  97. // Connection method is always assumed to be HTTPS
  98. String storageConnectionString = "DefaultEndpointsProtocol=https;"
  99. + "AccountName=" + this.accountName + ";AccountKey="
  100. + this.accessKey;
  101.  
  102. logger.debug("Azure Connection String: " + storageConnectionString);
  103.  
  104. //Initialize Azure connection
  105. this.storageAccount = CloudStorageAccount.parse(storageConnectionString);
  106. this.blobClient = storageAccount.createCloudBlobClient();
  107. this.container = blobClient.getContainerReference(containerName);
  108.  
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. }
  112.  
  113. }
  114.  
  115. public void setAccessKey(String accessKey) {
  116. this.accessKey = accessKey;
  117. }
  118.  
  119.  
  120. public void setAccountName(String accountName) {
  121. this.accountName = accountName;
  122. }
  123.  
  124. public void setContainerName(String containerName) {
  125. this.containerName = containerName;
  126. }
  127.  
  128. public void setRootDirectory(String rootDirectory) {
  129.  
  130. String dir = rootDirectory;
  131. if (dir.startsWith("/")) {
  132. dir = dir.substring(1);
  133. }
  134.  
  135. this.rootDirectory = dir;
  136. }
  137.  
  138. @Override
  139. public void setApplicationContext(ApplicationContext applicationContext)
  140. throws BeansException {
  141. this.applicationContext = applicationContext;
  142. }
  143.  
  144. @Override
  145. protected ContentWriter getWriterInternal(
  146. ContentReader existingContentReader, String newContentUrl) {
  147.  
  148. String contentUrl = newContentUrl;
  149.  
  150. if (StringUtils.isBlank(contentUrl)) {
  151. contentUrl = createNewUrl();
  152. }
  153.  
  154. String key = makePathToObject(contentUrl);
  155.  
  156. return new AzureContentWriter(key, contentUrl,
  157. existingContentReader, this.container);
  158.  
  159. }
  160.  
  161. public static String createNewUrl() {
  162.  
  163. Calendar calendar = new GregorianCalendar();
  164. int year = calendar.get(Calendar.YEAR);
  165. int month = calendar.get(Calendar.MONTH) + 1; // 0-based
  166. int day = calendar.get(Calendar.DAY_OF_MONTH);
  167. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  168. int minute = calendar.get(Calendar.MINUTE);
  169. // create the URL
  170. StringBuilder sb = new StringBuilder(20);
  171. sb.append(FileContentStore.STORE_PROTOCOL)
  172. .append(ContentStore.PROTOCOL_DELIMITER).append(year)
  173. .append('/').append(month).append('/').append(day).append('/')
  174. .append(hour).append('/').append(minute).append('/')
  175. .append(GUID.generate()).append(".bin");
  176. String newContentUrl = sb.toString();
  177. // done
  178. return newContentUrl;
  179.  
  180. }
  181.  
  182. private String makePathToObject(String contentUrl) {
  183. // take just the part after the protocol
  184. Pair<String, String> urlParts = super.getContentUrlParts(contentUrl);
  185. String protocol = urlParts.getFirst();
  186. String relativePath = urlParts.getSecond();
  187. // Check the protocol
  188. if (!protocol.equals(FileContentStore.STORE_PROTOCOL)) {
  189. throw new UnsupportedContentUrlException(this, protocol
  190. + PROTOCOL_DELIMITER + relativePath);
  191. }
  192.  
  193. return rootDirectory + "/" + relativePath;
  194.  
  195. }
  196.  
  197. @Override
  198. public boolean delete(String contentUrl) {
  199.  
  200. try {
  201. String pathToObject = makePathToObject(contentUrl);
  202. logger.debug("Deleting object from Azure with url: " + contentUrl
  203. + ", pathToObject: " + pathToObject);
  204.  
  205. CloudBlockBlob blob = container.getBlockBlobReference(pathToObject);
  206. blob.deleteIfExists();
  207.  
  208. return true;
  209. } catch (Exception e) {
  210. logger.error("Error deleting Azure Object", e);
  211. }
  212.  
  213. return false;
  214.  
  215. }
  216.  
  217. /**
  218. * Publishes an event to the application context that will notify any
  219. * interested parties of the existence of this content store.
  220. *
  221. * @param context
  222. * the application context
  223. * @param extendedEventParams
  224. */
  225. private void publishEvent(ApplicationContext context,
  226. Map<String, Serializable> extendedEventParams) {
  227. context.publishEvent(new ContentStoreCreatedEvent(this,
  228. extendedEventParams));
  229. }
  230.  
  231. public void onApplicationEvent(ApplicationEvent event) {
  232. // Once the context has been refreshed, we tell other interested beans
  233. // about the existence of this content store
  234. // (e.g. for monitoring purposes)
  235. if (event instanceof ContextRefreshedEvent
  236. && event.getSource() == this.applicationContext) {
  237. publishEvent(
  238. ((ContextRefreshedEvent) event).getApplicationContext(),
  239. Collections.<String, Serializable> emptyMap());
  240. }
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement