]> 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/AddRepository.java
Fix leaking session
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / AddRepository.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 java.net.URI;
19 import java.util.Hashtable;
20
21 import javax.jcr.Node;
22 import javax.jcr.Property;
23 import javax.jcr.Repository;
24 import javax.jcr.RepositoryFactory;
25 import javax.jcr.Session;
26 import javax.jcr.SimpleCredentials;
27 import javax.jcr.nodetype.NodeType;
28
29 import org.argeo.ArgeoException;
30 import org.argeo.eclipse.ui.ErrorFeedback;
31 import org.argeo.jcr.ArgeoJcrConstants;
32 import org.argeo.jcr.ArgeoJcrUtils;
33 import org.argeo.jcr.ArgeoNames;
34 import org.argeo.jcr.ArgeoTypes;
35 import org.argeo.jcr.JcrUtils;
36 import org.argeo.jcr.UserJcrUtils;
37 import org.argeo.slc.jcr.SlcNames;
38 import org.argeo.slc.repo.RepoConstants;
39 import org.argeo.util.security.Keyring;
40 import org.eclipse.core.commands.AbstractHandler;
41 import org.eclipse.core.commands.ExecutionEvent;
42 import org.eclipse.core.commands.ExecutionException;
43 import org.eclipse.jface.dialogs.Dialog;
44 import org.eclipse.jface.dialogs.IMessageProvider;
45 import org.eclipse.jface.dialogs.MessageDialog;
46 import org.eclipse.jface.dialogs.TitleAreaDialog;
47 import org.eclipse.swt.SWT;
48 import org.eclipse.swt.events.SelectionAdapter;
49 import org.eclipse.swt.events.SelectionEvent;
50 import org.eclipse.swt.graphics.Point;
51 import org.eclipse.swt.layout.GridData;
52 import org.eclipse.swt.layout.GridLayout;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Composite;
55 import org.eclipse.swt.widgets.Control;
56 import org.eclipse.swt.widgets.Display;
57 import org.eclipse.swt.widgets.Label;
58 import org.eclipse.swt.widgets.Shell;
59 import org.eclipse.swt.widgets.Text;
60
61 /**
62 * Connect to a remote repository.
63 */
64 public class AddRepository extends AbstractHandler implements ArgeoNames,
65 SlcNames {
66
67 private RepositoryFactory repositoryFactory;
68 private Repository nodeRepository;
69 private Keyring keyring;
70
71 public Object execute(ExecutionEvent event) throws ExecutionException {
72 RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
73 Display.getDefault().getActiveShell());
74 if (dlg.open() == Dialog.OK) {
75 }
76 return null;
77 }
78
79 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
80 this.repositoryFactory = repositoryFactory;
81 }
82
83 public void setKeyring(Keyring keyring) {
84 this.keyring = keyring;
85 }
86
87 public void setNodeRepository(Repository nodeRepository) {
88 this.nodeRepository = nodeRepository;
89 }
90
91 class RemoteRepositoryLoginDialog extends TitleAreaDialog {
92 private Text name;
93 private Text uri;
94 private Text username;
95 private Text password;
96 private Button saveInKeyring;
97
98 public RemoteRepositoryLoginDialog(Shell parentShell) {
99 super(parentShell);
100 }
101
102 protected Point getInitialSize() {
103 return new Point(600, 400);
104 }
105
106 protected Control createDialogArea(Composite parent) {
107 Composite dialogarea = (Composite) super.createDialogArea(parent);
108 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
109 true));
110 Composite composite = new Composite(dialogarea, SWT.NONE);
111 composite.setLayout(new GridLayout(2, false));
112 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
113 false));
114 setMessage("Login to remote repository", IMessageProvider.NONE);
115 name = createLT(composite, "Name", "Example Java Repository");
116 uri = createLT(composite, "URI",
117 "https://example.com/data/jcr/java");
118 username = createLT(composite, "User", "");
119 password = createLP(composite, "Password");
120
121 saveInKeyring = createLC(composite, "Remember password", false);
122 parent.pack();
123 return composite;
124 }
125
126 @Override
127 protected void createButtonsForButtonBar(Composite parent) {
128 super.createButtonsForButtonBar(parent);
129 Button test = createButton(parent, 2, "Test", false);
130 test.addSelectionListener(new SelectionAdapter() {
131 public void widgetSelected(SelectionEvent arg0) {
132 testConnection();
133 }
134 });
135 }
136
137 void testConnection() {
138 Session session = null;
139 try {
140 if (uri.getText().startsWith("http")) {// http, https
141 URI checkedUri = new URI(uri.getText());
142 String checkedUriStr = checkedUri.toString();
143 Hashtable<String, String> params = new Hashtable<String, String>();
144 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI,
145 checkedUriStr);
146 Repository repository = ArgeoJcrUtils.getRepositoryByUri(
147 repositoryFactory, checkedUriStr);
148 if (username.getText().trim().equals("")) {// anonymous
149 session = repository.login();
150 } else {
151 // FIXME use getTextChars() when upgrading to 3.7
152 // see
153 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
154 char[] pwd = password.getText().toCharArray();
155 SimpleCredentials sc = new SimpleCredentials(
156 username.getText(), pwd);
157 session = repository.login(sc);
158 }
159 } else {// alias
160 Repository repository = ArgeoJcrUtils.getRepositoryByAlias(
161 repositoryFactory, uri.getText());
162 session = repository.login();
163 }
164 MessageDialog.openInformation(getParentShell(), "Success",
165 "Connection to '" + uri.getText() + "' successful");
166 } catch (Exception e) {
167 ErrorFeedback.show(
168 "Connection test failed for " + uri.getText(), e);
169 } finally {
170 JcrUtils.logoutQuietly(session);
171 }
172 }
173
174 @Override
175 protected void okPressed() {
176 Session nodeSession = null;
177 try {
178 nodeSession = nodeRepository.login();
179 String reposPath = UserJcrUtils.getUserHome(nodeSession)
180 .getPath() + RepoConstants.REPOSITORIES_BASE_PATH;
181
182 Node repos = nodeSession.getNode(reposPath);
183 String repoNodeName = JcrUtils.replaceInvalidChars(name
184 .getText());
185 if (repos.hasNode(repoNodeName))
186 throw new ArgeoException(
187 "There is already a remote repository named "
188 + repoNodeName);
189 Node repoNode = repos.addNode(repoNodeName,
190 ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
191 repoNode.setProperty(ARGEO_URI, uri.getText());
192 repoNode.setProperty(ARGEO_USER_ID, username.getText());
193 repoNode.addMixin(NodeType.MIX_TITLE);
194 repoNode.setProperty(Property.JCR_TITLE, name.getText());
195 nodeSession.save();
196 if (saveInKeyring.getSelection()) {
197 String pwdPath = repoNode.getPath() + '/' + ARGEO_PASSWORD;
198 keyring.set(pwdPath, password.getText().toCharArray());
199 nodeSession.save();
200 }
201 MessageDialog.openInformation(getParentShell(),
202 "Repository Added",
203 "Remote repository " + uri.getText() + "' added");
204
205 super.okPressed();
206 } catch (Exception e) {
207 ErrorFeedback.show("Cannot add remote repository", e);
208 } finally {
209 JcrUtils.logoutQuietly(nodeSession);
210 }
211 }
212
213 /** Creates label and text. */
214 protected Text createLT(Composite parent, String label, String initial) {
215 new Label(parent, SWT.NONE).setText(label);
216 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
217 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
218 text.setText(initial);
219 return text;
220 }
221
222 /** Creates label and check. */
223 protected Button createLC(Composite parent, String label,
224 Boolean initial) {
225 new Label(parent, SWT.NONE).setText(label);
226 Button check = new Button(parent, SWT.CHECK);
227 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
228 check.setSelection(initial);
229 return check;
230 }
231
232 protected Text createLP(Composite parent, String label) {
233 new Label(parent, SWT.NONE).setText(label);
234 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
235 | SWT.PASSWORD);
236 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
237 return text;
238 }
239 }
240 }