2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.jcr
.ui
.explorer
.commands
;
19 import java
.util
.Hashtable
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.Repository
;
23 import javax
.jcr
.RepositoryFactory
;
24 import javax
.jcr
.Session
;
25 import javax
.jcr
.SimpleCredentials
;
27 import org
.argeo
.ArgeoException
;
28 import org
.argeo
.eclipse
.ui
.ErrorFeedback
;
29 import org
.argeo
.jcr
.ArgeoJcrConstants
;
30 import org
.argeo
.jcr
.ArgeoJcrUtils
;
31 import org
.argeo
.jcr
.ArgeoNames
;
32 import org
.argeo
.jcr
.ArgeoTypes
;
33 import org
.argeo
.jcr
.JcrUtils
;
34 import org
.argeo
.jcr
.security
.JcrKeyring
;
35 import org
.argeo
.jcr
.security
.SecurityJcrUtils
;
36 import org
.argeo
.jcr
.ui
.explorer
.JcrExplorerConstants
;
37 import org
.eclipse
.core
.commands
.AbstractHandler
;
38 import org
.eclipse
.core
.commands
.ExecutionEvent
;
39 import org
.eclipse
.core
.commands
.ExecutionException
;
40 import org
.eclipse
.jface
.dialogs
.Dialog
;
41 import org
.eclipse
.jface
.dialogs
.IMessageProvider
;
42 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
43 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
44 import org
.eclipse
.swt
.SWT
;
45 import org
.eclipse
.swt
.events
.SelectionAdapter
;
46 import org
.eclipse
.swt
.events
.SelectionEvent
;
47 import org
.eclipse
.swt
.graphics
.Point
;
48 import org
.eclipse
.swt
.layout
.GridData
;
49 import org
.eclipse
.swt
.layout
.GridLayout
;
50 import org
.eclipse
.swt
.widgets
.Button
;
51 import org
.eclipse
.swt
.widgets
.Composite
;
52 import org
.eclipse
.swt
.widgets
.Control
;
53 import org
.eclipse
.swt
.widgets
.Display
;
54 import org
.eclipse
.swt
.widgets
.Label
;
55 import org
.eclipse
.swt
.widgets
.Shell
;
56 import org
.eclipse
.swt
.widgets
.Text
;
59 * Connect to a remote repository and, if successful publish it as an OSGi
62 public class AddRemoteRepository
extends AbstractHandler
implements
63 JcrExplorerConstants
, ArgeoNames
{
65 private RepositoryFactory repositoryFactory
;
66 private JcrKeyring keyring
;
68 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
69 RemoteRepositoryLoginDialog dlg
= new RemoteRepositoryLoginDialog(
70 Display
.getDefault().getActiveShell());
71 if (dlg
.open() == Dialog
.OK
) {
76 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
77 this.repositoryFactory
= repositoryFactory
;
80 public void setKeyring(JcrKeyring keyring
) {
81 this.keyring
= keyring
;
84 class RemoteRepositoryLoginDialog
extends TitleAreaDialog
{
87 private Text username
;
88 private Text password
;
89 private Button saveInKeyring
;
91 public RemoteRepositoryLoginDialog(Shell parentShell
) {
95 protected Point
getInitialSize() {
96 return new Point(600, 400);
99 protected Control
createDialogArea(Composite parent
) {
100 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
101 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
103 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
104 composite
.setLayout(new GridLayout(2, false));
105 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
107 setMessage("Login to remote repository", IMessageProvider
.NONE
);
108 name
= createLT(composite
, "Name", "remoteRepository");
109 uri
= createLT(composite
, "URI",
110 "http://localhost:7070/org.argeo.jcr.webapp/remoting/node");
111 username
= createLT(composite
, "User", "");
112 password
= createLP(composite
, "Password");
114 saveInKeyring
= createLC(composite
, "Remember password", false);
120 protected void createButtonsForButtonBar(Composite parent
) {
121 super.createButtonsForButtonBar(parent
);
122 Button test
= createButton(parent
, 2, "Test", false);
123 test
.addSelectionListener(new SelectionAdapter() {
124 public void widgetSelected(SelectionEvent arg0
) {
130 void testConnection() {
131 Session session
= null;
133 URI checkedUri
= new URI(uri
.getText());
134 String checkedUriStr
= checkedUri
.toString();
136 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
137 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_URI
, checkedUriStr
);
138 Repository repository
= repositoryFactory
.getRepository(params
);
139 if (username
.getText().trim().equals("")) {// anonymous
140 session
= repository
.login();
142 // FIXME use getTextChars() when upgrading to 3.7
143 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
144 char[] pwd
= password
.getText().toCharArray();
145 SimpleCredentials sc
= new SimpleCredentials(
146 username
.getText(), pwd
);
147 session
= repository
.login(sc
);
148 MessageDialog
.openInformation(getParentShell(), "Success",
149 "Connection to '" + uri
.getText() + "' successful");
151 } catch (Exception e
) {
153 "Connection test failed for " + uri
.getText(), e
);
155 JcrUtils
.logoutQuietly(session
);
160 protected void okPressed() {
162 Session nodeSession
= keyring
.getSession();
163 Node home
= ArgeoJcrUtils
.getUserHome(nodeSession
);
165 // FIXME better deal with non existing home dir
167 home
= SecurityJcrUtils
.createUserHomeIfNeeded(nodeSession
,
168 nodeSession
.getUserID());
170 Node remote
= home
.hasNode(ARGEO_REMOTE
) ? home
171 .getNode(ARGEO_REMOTE
) : home
.addNode(ARGEO_REMOTE
);
172 if (remote
.hasNode(name
.getText()))
173 throw new ArgeoException(
174 "There is already a remote repository named "
176 Node remoteRepository
= remote
.addNode(name
.getText(),
177 ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
);
178 remoteRepository
.setProperty(ARGEO_URI
, uri
.getText());
179 remoteRepository
.setProperty(ARGEO_USER_ID
, username
.getText());
180 Node pwd
= remoteRepository
.addNode(ARGEO_PASSWORD
);
181 pwd
.getSession().save();
182 if (saveInKeyring
.getSelection())
183 keyring
.set(pwd
.getPath(), password
.getText().toCharArray());
184 keyring
.getSession().save();
185 MessageDialog
.openInformation(
188 "Remote repository '" + username
.getText() + "@"
189 + uri
.getText() + "' added");
192 } catch (Exception e
) {
193 ErrorFeedback
.show("Cannot add remote repository", e
);
197 /** Creates label and text. */
198 protected Text
createLT(Composite parent
, String label
, String initial
) {
199 new Label(parent
, SWT
.NONE
).setText(label
);
200 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
);
201 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
202 text
.setText(initial
);
206 /** Creates label and check. */
207 protected Button
createLC(Composite parent
, String label
,
209 new Label(parent
, SWT
.NONE
).setText(label
);
210 Button check
= new Button(parent
, SWT
.CHECK
);
211 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
212 check
.setSelection(initial
);
216 protected Text
createLP(Composite parent
, String label
) {
217 new Label(parent
, SWT
.NONE
).setText(label
);
218 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
220 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));