2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
19 import java
.util
.Hashtable
;
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
;
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
;
62 * Connect to a remote repository.
64 public class AddRepository
extends AbstractHandler
implements ArgeoNames
,
67 private RepositoryFactory repositoryFactory
;
68 private Repository nodeRepository
;
69 private Keyring keyring
;
71 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
72 RemoteRepositoryLoginDialog dlg
= new RemoteRepositoryLoginDialog(
73 Display
.getDefault().getActiveShell());
74 if (dlg
.open() == Dialog
.OK
) {
79 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
80 this.repositoryFactory
= repositoryFactory
;
83 public void setKeyring(Keyring keyring
) {
84 this.keyring
= keyring
;
87 public void setNodeRepository(Repository nodeRepository
) {
88 this.nodeRepository
= nodeRepository
;
91 class RemoteRepositoryLoginDialog
extends TitleAreaDialog
{
94 private Text username
;
95 private Text password
;
96 private Button saveInKeyring
;
98 public RemoteRepositoryLoginDialog(Shell parentShell
) {
102 protected Point
getInitialSize() {
103 return new Point(600, 400);
106 protected Control
createDialogArea(Composite parent
) {
107 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
108 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, 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,
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");
121 saveInKeyring
= createLC(composite
, "Remember password", false);
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
) {
137 void testConnection() {
138 Session session
= null;
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
,
146 Repository repository
= ArgeoJcrUtils
.getRepositoryByUri(
147 repositoryFactory
, checkedUriStr
);
148 if (username
.getText().trim().equals("")) {// anonymous
149 session
= repository
.login();
151 // FIXME use getTextChars() when upgrading to 3.7
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
);
160 Repository repository
= ArgeoJcrUtils
.getRepositoryByAlias(
161 repositoryFactory
, uri
.getText());
162 session
= repository
.login();
164 MessageDialog
.openInformation(getParentShell(), "Success",
165 "Connection to '" + uri
.getText() + "' successful");
166 } catch (Exception e
) {
168 "Connection test failed for " + uri
.getText(), e
);
170 JcrUtils
.logoutQuietly(session
);
175 protected void okPressed() {
176 Session nodeSession
= null;
178 nodeSession
= nodeRepository
.login();
179 String reposPath
= UserJcrUtils
.getUserHome(nodeSession
)
180 .getPath() + RepoConstants
.REPOSITORIES_BASE_PATH
;
182 Node repos
= nodeSession
.getNode(reposPath
);
183 String repoNodeName
= JcrUtils
.replaceInvalidChars(name
185 if (repos
.hasNode(repoNodeName
))
186 throw new ArgeoException(
187 "There is already a remote repository named "
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());
196 if (saveInKeyring
.getSelection()) {
197 String pwdPath
= repoNode
.getPath() + '/' + ARGEO_PASSWORD
;
198 keyring
.set(pwdPath
, password
.getText().toCharArray());
201 MessageDialog
.openInformation(getParentShell(),
203 "Remote repository " + uri
.getText() + "' added");
206 } catch (Exception e
) {
207 ErrorFeedback
.show("Cannot add remote repository", e
);
209 JcrUtils
.logoutQuietly(nodeSession
);
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
);
222 /** Creates label and check. */
223 protected Button
createLC(Composite parent
, String label
,
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
);
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
236 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));