]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/PublishWorkspace.java
Fix single sourcing issue on file download
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / PublishWorkspace.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.client.ui.dist.commands;
17
18 import javax.jcr.Credentials;
19 import javax.jcr.Node;
20 import javax.jcr.Repository;
21 import javax.jcr.RepositoryException;
22 import javax.jcr.RepositoryFactory;
23 import javax.jcr.Session;
24 import javax.jcr.security.Privilege;
25
26 import org.argeo.ArgeoException;
27 import org.argeo.jcr.JcrUtils;
28 import org.argeo.slc.SlcConstants;
29 import org.argeo.slc.client.ui.dist.DistPlugin;
30 import org.argeo.slc.repo.RepoUtils;
31 import org.argeo.util.security.Keyring;
32 import org.eclipse.core.commands.AbstractHandler;
33 import org.eclipse.core.commands.ExecutionEvent;
34 import org.eclipse.core.commands.ExecutionException;
35 import org.eclipse.jface.dialogs.MessageDialog;
36 import org.eclipse.jface.resource.ImageDescriptor;
37
38 /**
39 * Publish the current workspace by giving READ_ONLY rights to anonymous.
40 */
41 public class PublishWorkspace extends AbstractHandler {
42 // private static final Log log = LogFactory.getLog(PublishWorkspace.class);
43
44 public final static String ID = DistPlugin.ID + ".publishWorkspace";
45 public final static String DEFAULT_LABEL = "Make Public";
46 public final static ImageDescriptor DEFAULT_ICON = DistPlugin
47 .getImageDescriptor("icons/publish.gif");
48
49 public final static String PARAM_WORKSPACE_NAME = "workspaceName";
50 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
51
52 // DEPENDENCY INJECTION
53 private RepositoryFactory repositoryFactory;
54 private Keyring keyring;
55 private Repository nodeRepository;
56
57 private String publicRole = SlcConstants.USER_ANONYMOUS;
58
59 public Object execute(ExecutionEvent event) throws ExecutionException {
60 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
61 String workspaceName = event.getParameter(PARAM_WORKSPACE_NAME);
62
63 Session nodeSession = null;
64 Session session = null;
65
66 try {
67 nodeSession = nodeRepository.login();
68 Node repoNode = nodeSession.getNode(targetRepoPath);
69 Repository repository = RepoUtils.getRepository(repositoryFactory,
70 keyring, repoNode);
71 Credentials credentials = RepoUtils.getRepositoryCredentials(
72 keyring, repoNode);
73
74 String msg = "Are you sure you want to publish this distribution: "
75 + workspaceName + " ?";
76 boolean result = MessageDialog.openConfirm(DistPlugin.getDefault()
77 .getWorkbench().getDisplay().getActiveShell(),
78 "Confirm publication", msg);
79
80 if (result) {
81 session = repository.login(credentials, workspaceName);
82 JcrUtils.addPrivilege(session, "/", publicRole,
83 Privilege.JCR_READ);
84 session.save();
85 JcrUtils.logoutQuietly(session);
86 // CommandHelpers.callCommand(RefreshDistributionsView.ID);
87 }
88 } catch (RepositoryException re) {
89 throw new ArgeoException(
90 "Unexpected error while publishing workspace "
91 + workspaceName, re);
92 } finally {
93 JcrUtils.logoutQuietly(session);
94 JcrUtils.logoutQuietly(nodeSession);
95 }
96 return null;
97 }
98
99 /* DEPENDENCY INJECTION */
100 public void setNodeRepository(Repository nodeRepository) {
101 this.nodeRepository = nodeRepository;
102 }
103
104 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
105 this.repositoryFactory = repositoryFactory;
106 }
107
108 public void setKeyring(Keyring keyring) {
109 this.keyring = keyring;
110 }
111 }