1 package org
.argeo
.cms
.e4
.jcr
.handlers
;
4 import java
.util
.Hashtable
;
6 import javax
.inject
.Inject
;
7 import javax
.inject
.Named
;
9 import javax
.jcr
.Repository
;
10 import javax
.jcr
.RepositoryFactory
;
11 import javax
.jcr
.Session
;
12 import javax
.jcr
.SimpleCredentials
;
14 import org
.argeo
.api
.cms
.CmsConstants
;
15 import org
.argeo
.cms
.ArgeoNames
;
16 import org
.argeo
.cms
.ArgeoTypes
;
17 import org
.argeo
.cms
.e4
.jcr
.JcrBrowserView
;
18 import org
.argeo
.cms
.jcr
.CmsJcrUtils
;
19 import org
.argeo
.cms
.security
.Keyring
;
20 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
21 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
22 import org
.argeo
.jcr
.JcrUtils
;
23 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
24 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
25 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
26 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
27 import org
.eclipse
.jface
.dialogs
.Dialog
;
28 import org
.eclipse
.jface
.dialogs
.IMessageProvider
;
29 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
30 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
31 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.swt
.events
.SelectionAdapter
;
33 import org
.eclipse
.swt
.events
.SelectionEvent
;
34 import org
.eclipse
.swt
.graphics
.Point
;
35 import org
.eclipse
.swt
.layout
.GridData
;
36 import org
.eclipse
.swt
.layout
.GridLayout
;
37 import org
.eclipse
.swt
.widgets
.Button
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Control
;
40 import org
.eclipse
.swt
.widgets
.Display
;
41 import org
.eclipse
.swt
.widgets
.Label
;
42 import org
.eclipse
.swt
.widgets
.Shell
;
43 import org
.eclipse
.swt
.widgets
.Text
;
46 * Connect to a remote repository and, if successful publish it as an OSGi
49 public class AddRemoteRepository
{
52 private RepositoryFactory repositoryFactory
;
54 private Repository nodeRepository
;
57 private Keyring keyring
;
60 public void execute(@Named(IServiceConstants
.ACTIVE_PART
) MPart part
) {
61 JcrBrowserView view
= (JcrBrowserView
) part
.getObject();
62 RemoteRepositoryLoginDialog dlg
= new RemoteRepositoryLoginDialog(Display
.getDefault().getActiveShell());
63 if (dlg
.open() == Dialog
.OK
) {
68 // public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
69 // this.repositoryFactory = repositoryFactory;
72 // public void setKeyring(Keyring keyring) {
73 // this.keyring = keyring;
76 // public void setNodeRepository(Repository nodeRepository) {
77 // this.nodeRepository = nodeRepository;
80 class RemoteRepositoryLoginDialog
extends TitleAreaDialog
{
81 private static final long serialVersionUID
= 2234006887750103399L;
84 private Text username
;
85 private Text password
;
86 private Button saveInKeyring
;
88 public RemoteRepositoryLoginDialog(Shell parentShell
) {
92 protected Point
getInitialSize() {
93 return new Point(600, 400);
96 protected Control
createDialogArea(Composite parent
) {
97 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
98 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
99 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
100 composite
.setLayout(new GridLayout(2, false));
101 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
102 setMessage("Login to remote repository", IMessageProvider
.NONE
);
103 name
= createLT(composite
, "Name", "remoteRepository");
104 uri
= createLT(composite
, "URI", "http://localhost:7070/jcr/node");
105 username
= createLT(composite
, "User", "");
106 password
= createLP(composite
, "Password");
108 saveInKeyring
= createLC(composite
, "Remember password", false);
114 protected void createButtonsForButtonBar(Composite parent
) {
115 super.createButtonsForButtonBar(parent
);
116 Button test
= createButton(parent
, 2, "Test", false);
117 test
.addSelectionListener(new SelectionAdapter() {
118 private static final long serialVersionUID
= -1829962269440419560L;
120 public void widgetSelected(SelectionEvent arg0
) {
126 void testConnection() {
127 Session session
= null;
129 URI checkedUri
= new URI(uri
.getText());
130 String checkedUriStr
= checkedUri
.toString();
132 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
133 params
.put(CmsConstants
.LABELED_URI
, checkedUriStr
);
134 Repository repository
= repositoryFactory
.getRepository(params
);
135 if (username
.getText().trim().equals("")) {// anonymous
136 // FIXME make it more generic
137 session
= repository
.login(CmsConstants
.SYS_WORKSPACE
);
139 // FIXME use getTextChars() when upgrading to 3.7
140 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
141 char[] pwd
= password
.getText().toCharArray();
142 SimpleCredentials sc
= new SimpleCredentials(username
.getText(), pwd
);
143 session
= repository
.login(sc
, "main");
144 MessageDialog
.openInformation(getParentShell(), "Success",
145 "Connection to '" + uri
.getText() + "' successful");
147 } catch (Exception e
) {
148 ErrorFeedback
.show("Connection test failed for " + uri
.getText(), e
);
150 JcrUtils
.logoutQuietly(session
);
155 protected void okPressed() {
156 Session nodeSession
= null;
158 nodeSession
= nodeRepository
.login();
159 Node home
= CmsJcrUtils
.getUserHome(nodeSession
);
161 Node remote
= home
.hasNode(ArgeoNames
.ARGEO_REMOTE
) ? home
.getNode(ArgeoNames
.ARGEO_REMOTE
)
162 : home
.addNode(ArgeoNames
.ARGEO_REMOTE
);
163 if (remote
.hasNode(name
.getText()))
164 throw new EclipseUiException("There is already a remote repository named " + name
.getText());
165 Node remoteRepository
= remote
.addNode(name
.getText(), ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
);
166 remoteRepository
.setProperty(ArgeoNames
.ARGEO_URI
, uri
.getText());
167 remoteRepository
.setProperty(ArgeoNames
.ARGEO_USER_ID
, username
.getText());
169 if (saveInKeyring
.getSelection()) {
170 String pwdPath
= remoteRepository
.getPath() + '/' + ArgeoNames
.ARGEO_PASSWORD
;
171 keyring
.set(pwdPath
, password
.getText().toCharArray());
174 MessageDialog
.openInformation(getParentShell(), "Repository Added",
175 "Remote repository '" + username
.getText() + "@" + uri
.getText() + "' added");
178 } catch (Exception e
) {
179 ErrorFeedback
.show("Cannot add remote repository", e
);
181 JcrUtils
.logoutQuietly(nodeSession
);
185 /** Creates label and text. */
186 protected Text
createLT(Composite parent
, String label
, String initial
) {
187 new Label(parent
, SWT
.NONE
).setText(label
);
188 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
);
189 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
190 text
.setText(initial
);
194 /** Creates label and check. */
195 protected Button
createLC(Composite parent
, String label
, Boolean initial
) {
196 new Label(parent
, SWT
.NONE
).setText(label
);
197 Button check
= new Button(parent
, SWT
.CHECK
);
198 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
199 check
.setSelection(initial
);
203 protected Text
createLP(Composite parent
, String label
) {
204 new Label(parent
, SWT
.NONE
).setText(label
);
205 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
| SWT
.PASSWORD
);
206 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));