ECMSActivityFileStoragePlugin extends ActivityFileStoragePlugin { @Override public void storeAttachments(ExoSocialActivity activity, Identity streamOwner, ActivityFile... attachments) throws Exception { if (attachments == null || attachments.length == 0) { return; } for (ActivityFile activityFile : attachments) { UploadResource uploadedResource = uploadService.getUploadResource(activityFile.getUploadId()); if (uploadedResource == null) { throw new IllegalStateException("Cannot attach uploaded file " + activityFile.getUploadId() + ", it may not exist"); } Node parentNode; SessionProvider sessionProvider = sessionProviderService.getSystemSessionProvider(null); ManageableRepository currentRepository = repositoryService.getCurrentRepository(); String workspaceName = currentRepository.getConfiguration().getDefaultWorkspaceName(); Session session = sessionProvider.getSession(workspaceName, currentRepository); if (streamOwner.getProviderId().equals(SpaceIdentityProvider.NAME)) { Space space = spaceService.getSpaceByPrettyName(streamOwner.getRemoteId()); String groupPath = nodeHierarchyCreator.getJcrPath("groupsPath"); String spaceParentPath = groupPath + space.getGroupId() + "/Documents"; if (!session.itemExists(spaceParentPath)) { throw new IllegalStateException("Root node of space '" + spaceParentPath + "' doesn't exist"); } parentNode = (Node) session.getItem(spaceParentPath); if (!parentNode.hasNode(ACTIVITY_FOLDER_UPLOAD_NAME)) { parentNode.addNode(ACTIVITY_FOLDER_UPLOAD_NAME); session.save(); } } else { String remoteUser = ConversationState.getCurrent().getIdentity().getUserId(); if (org.apache.commons.lang.StringUtils.isBlank(remoteUser)) { throw new IllegalStateException("Remote user is empty"); } Node userNode = nodeHierarchyCreator.getUserNode(sessionProvider, remoteUser); String publicPath = nodeHierarchyCreator.getJcrPath(BasePath.CMS_USER_PUBLIC_ALIAS); if (userNode == null || !userNode.hasNode(publicPath)) { throw new IllegalStateException("User '" + remoteUser + "' hasn't public folder"); } parentNode = userNode.getNode(publicPath); if (!parentNode.hasNode(ACTIVITY_FOLDER_UPLOAD_NAME)) { parentNode.addNode(ACTIVITY_FOLDER_UPLOAD_NAME); session.save(); } } Node parentUploadNode = null; if (StringUtils.isNotBlank(activityFile.getDestinationFolder())) { SessionProvider sessionProviderUser = sessionProviderService.getSessionProvider(null); session = sessionProviderUser.getSession(workspaceName, currentRepository); StringBuilder folderExpression = new StringBuilder(); folderExpression.append("repository").append(":").append(workspaceName).append(":").append(activityFile.getDestinationFolder()); parentUploadNode = NodeLocation.getNodeByExpression(folderExpression.toString()); }else { parentUploadNode = parentNode.getNode(ACTIVITY_FOLDER_UPLOAD_NAME); } String nodeName = Utils.cleanName(uploadedResource.getFileName()); if (!parentUploadNode.getDefinition().allowsSameNameSiblings()) { nodeName = getFileName(parentUploadNode, nodeName, nodeName, 1); } Node node = null; try { node = parentUploadNode.addNode(nodeName, NodetypeConstant.NT_FILE); } catch (ItemExistsException e) { nodeName = getFileName(parentUploadNode, nodeName, nodeName, 1); node = parentUploadNode.addNode(nodeName, NodetypeConstant.NT_FILE); } node.setProperty("exo:title", nodeName); Node resourceNode = node.addNode("jcr:content", "nt:resource"); resourceNode.setProperty("jcr:mimeType", uploadedResource.getMimeType()); resourceNode.setProperty("jcr:lastModified", Calendar.getInstance()); String fileDiskLocation = uploadedResource.getStoreLocation(); try (InputStream inputStream = new FileInputStream(fileDiskLocation)) { resourceNode.setProperty("jcr:data", inputStream); session.save(); node = (Node) session.getItem(node.getPath()); } if (activity.getTemplateParams() == null) { activity.setTemplateParams(new HashMap<>()); } concatenateParam(activity.getTemplateParams(), "REPOSITORY", "repository"); concatenateParam(activity.getTemplateParams(), "WORKSPACE", "collaboration"); concatenateParam(activity.getTemplateParams(), "DOCPATH", node.getPath()); concatenateParam(activity.getTemplateParams(), "mimeType", resourceNode.getProperty("jcr:mimeType").getString()); concatenateParam(activity.getTemplateParams(), "id", node.isNodeType("mix:referenceable") ? node.getUUID() : ""); uploadService.removeUploadResource(activityFile.getUploadId()); } } ECMSActivityFileStoragePlugin(SpaceService spaceService,
                                       NodeHierarchyCreator nodeHierarchyCreator,
                                       RepositoryService repositoryService,
                                       UploadService uploadService,
                                       SessionProviderService sessionProviderService,
                                       InitParams initParams); @Override void storeAttachments(ExoSocialActivity activity, Identity streamOwner, ActivityFile... attachments); void attachExistingFile(ExoSocialActivity activity, Identity streamOwner, ActivityFile attachment);  }
@Test public void storeAttachments() throws Exception { ConversationState.setCurrent(new ConversationState(new org.exoplatform.services.security.Identity("root"))); InitParams initParams = new InitParams(); ValueParam datasource = new ValueParam(); datasource.setName("datasource"); datasource.setValue("jcr"); ValueParam priority = new ValueParam(); priority.setName("priority"); priority.setValue("1"); initParams.addParameter(datasource); initParams.addParameter(priority); ECMSActivityFileStoragePlugin ecmsActivityFileStoragePlugin = new ECMSActivityFileStoragePlugin(spaceService,nodeHierarchyCreator,repositoryService,uploadService,sessionProviderService,initParams); ExoSocialActivity activity = new ExoSocialActivityImpl(); activity.setTitle("test"); activity.setStreamOwner("root"); Identity streamOwner = new Identity(OrganizationIdentityProvider.NAME,"root"); streamOwner.setId("root"); ActivityFile activityFile = new ActivityFile(); activityFile.setUploadId("1234"); activityFile.setName("testFileUpload"); UploadResource uploadResource = new UploadResource("1234"); uploadResource.setFileName("testFileUpload"); File file = File.createTempFile("testFileUpload", ".xml"); uploadResource.setStoreLocation(file.getPath()); uploadResource.setMimeType(TEXT_PLAIN); when(sessionProviderService.getSystemSessionProvider(any())).thenReturn(sessionProvider); when(sessionProviderService.getSessionProvider(any())).thenReturn(sessionProvider); when(repositoryService.getCurrentRepository()).thenReturn(repository); when(repository.getConfiguration()).thenReturn(repositoryEntry); when(repositoryEntry.getName()).thenReturn("workspace"); when(repositoryEntry.getDefaultWorkspaceName()).thenReturn("collaboration"); when(sessionProvider.getSession(any(), any())).thenReturn(session); Node userNode = mock(Node.class); Node parentNode = mock(Node.class); Node parentUploadNode = mock(Node.class); Node node = mock(Node.class); Node resourceNode = mock(Node.class); NodeDefinition nodeDefinition = mock(NodeDefinition.class); Property property = mock(Property.class); when(nodeHierarchyCreator.getUserNode(any(), any())).thenReturn(userNode); when(nodeHierarchyCreator.getJcrPath(any())).thenReturn("/Users"); when(userNode.hasNode(any())).thenReturn(true); when(userNode.getNode(any())).thenReturn(parentNode); when(parentNode.hasNode(any())).thenReturn(true); when(parentNode.getNode(any())).thenReturn(parentUploadNode); when(parentUploadNode.getDefinition()).thenReturn(nodeDefinition); when(parentUploadNode.addNode(any(),any())).thenReturn(node); when(node.addNode(any(),any())).thenReturn(resourceNode); when(uploadService.getUploadResource(any())).thenReturn(uploadResource); when(session.getItem(any())).thenReturn(node); when(node.getPath()).thenReturn("/test/node_B/node_1"); when(resourceNode.getProperty(any())).thenReturn(property); when(property.getString()).thenReturn("testProperty"); when(node.isNodeType(any())).thenReturn(false); ecmsActivityFileStoragePlugin.storeAttachments(activity,streamOwner,activityFile); verify(uploadService, times(1)).removeUploadResource(any()); verify(uploadService, times(1)).getUploadResource(any()); verify(session, times(1)).save(); }