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
.cms
.e4
.jcr
.handlers
;
19 import java
.util
.Hashtable
;
21 import javax
.inject
.Inject
;
22 import javax
.inject
.Named
;
23 import javax
.jcr
.Node
;
24 import javax
.jcr
.Repository
;
25 import javax
.jcr
.RepositoryFactory
;
26 import javax
.jcr
.Session
;
27 import javax
.jcr
.SimpleCredentials
;
29 import org
.argeo
.cms
.ArgeoNames
;
30 import org
.argeo
.cms
.ArgeoTypes
;
31 import org
.argeo
.cms
.e4
.jcr
.JcrBrowserView
;
32 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
33 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
34 import org
.argeo
.jcr
.JcrUtils
;
35 import org
.argeo
.node
.NodeConstants
;
36 import org
.argeo
.node
.NodeUtils
;
37 import org
.argeo
.node
.security
.Keyring
;
38 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
39 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
40 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
41 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
42 import org
.eclipse
.jface
.dialogs
.Dialog
;
43 import org
.eclipse
.jface
.dialogs
.IMessageProvider
;
44 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
45 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
46 import org
.eclipse
.swt
.SWT
;
47 import org
.eclipse
.swt
.events
.SelectionAdapter
;
48 import org
.eclipse
.swt
.events
.SelectionEvent
;
49 import org
.eclipse
.swt
.graphics
.Point
;
50 import org
.eclipse
.swt
.layout
.GridData
;
51 import org
.eclipse
.swt
.layout
.GridLayout
;
52 import org
.eclipse
.swt
.widgets
.Button
;
53 import org
.eclipse
.swt
.widgets
.Composite
;
54 import org
.eclipse
.swt
.widgets
.Control
;
55 import org
.eclipse
.swt
.widgets
.Display
;
56 import org
.eclipse
.swt
.widgets
.Label
;
57 import org
.eclipse
.swt
.widgets
.Shell
;
58 import org
.eclipse
.swt
.widgets
.Text
;
61 * Connect to a remote repository and, if successful publish it as an OSGi
64 public class AddRemoteRepository
{
67 private RepositoryFactory repositoryFactory
;
69 private Repository nodeRepository
;
72 private Keyring keyring
;
75 public void execute(@Named(IServiceConstants
.ACTIVE_PART
) MPart part
) {
76 JcrBrowserView view
= (JcrBrowserView
) part
.getObject();
77 RemoteRepositoryLoginDialog dlg
= new RemoteRepositoryLoginDialog(Display
.getDefault().getActiveShell());
78 if (dlg
.open() == Dialog
.OK
) {
83 // public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
84 // this.repositoryFactory = repositoryFactory;
87 // public void setKeyring(Keyring keyring) {
88 // this.keyring = keyring;
91 // public void setNodeRepository(Repository nodeRepository) {
92 // this.nodeRepository = nodeRepository;
95 class RemoteRepositoryLoginDialog
extends TitleAreaDialog
{
96 private static final long serialVersionUID
= 2234006887750103399L;
99 private Text username
;
100 private Text password
;
101 private Button saveInKeyring
;
103 public RemoteRepositoryLoginDialog(Shell parentShell
) {
107 protected Point
getInitialSize() {
108 return new Point(600, 400);
111 protected Control
createDialogArea(Composite parent
) {
112 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
113 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
114 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
115 composite
.setLayout(new GridLayout(2, false));
116 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
117 setMessage("Login to remote repository", IMessageProvider
.NONE
);
118 name
= createLT(composite
, "Name", "remoteRepository");
119 uri
= createLT(composite
, "URI", "http://localhost:7070/jcr/node");
120 username
= createLT(composite
, "User", "");
121 password
= createLP(composite
, "Password");
123 saveInKeyring
= createLC(composite
, "Remember password", false);
129 protected void createButtonsForButtonBar(Composite parent
) {
130 super.createButtonsForButtonBar(parent
);
131 Button test
= createButton(parent
, 2, "Test", false);
132 test
.addSelectionListener(new SelectionAdapter() {
133 private static final long serialVersionUID
= -1829962269440419560L;
135 public void widgetSelected(SelectionEvent arg0
) {
141 void testConnection() {
142 Session session
= null;
144 URI checkedUri
= new URI(uri
.getText());
145 String checkedUriStr
= checkedUri
.toString();
147 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
148 params
.put(NodeConstants
.LABELED_URI
, checkedUriStr
);
149 Repository repository
= repositoryFactory
.getRepository(params
);
150 if (username
.getText().trim().equals("")) {// anonymous
151 // FIXME make it more generic
152 session
= repository
.login("main");
154 // FIXME use getTextChars() when upgrading to 3.7
155 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
156 char[] pwd
= password
.getText().toCharArray();
157 SimpleCredentials sc
= new SimpleCredentials(username
.getText(), pwd
);
158 session
= repository
.login(sc
, "main");
159 MessageDialog
.openInformation(getParentShell(), "Success",
160 "Connection to '" + uri
.getText() + "' successful");
162 } catch (Exception e
) {
163 ErrorFeedback
.show("Connection test failed for " + uri
.getText(), e
);
165 JcrUtils
.logoutQuietly(session
);
170 protected void okPressed() {
171 Session nodeSession
= null;
173 nodeSession
= nodeRepository
.login();
174 Node home
= NodeUtils
.getUserHome(nodeSession
);
176 Node remote
= home
.hasNode(ArgeoNames
.ARGEO_REMOTE
) ? home
.getNode(ArgeoNames
.ARGEO_REMOTE
)
177 : home
.addNode(ArgeoNames
.ARGEO_REMOTE
);
178 if (remote
.hasNode(name
.getText()))
179 throw new EclipseUiException("There is already a remote repository named " + name
.getText());
180 Node remoteRepository
= remote
.addNode(name
.getText(), ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
);
181 remoteRepository
.setProperty(ArgeoNames
.ARGEO_URI
, uri
.getText());
182 remoteRepository
.setProperty(ArgeoNames
.ARGEO_USER_ID
, username
.getText());
184 if (saveInKeyring
.getSelection()) {
185 String pwdPath
= remoteRepository
.getPath() + '/' + ArgeoNames
.ARGEO_PASSWORD
;
186 keyring
.set(pwdPath
, password
.getText().toCharArray());
189 MessageDialog
.openInformation(getParentShell(), "Repository Added",
190 "Remote repository '" + username
.getText() + "@" + uri
.getText() + "' added");
193 } catch (Exception e
) {
194 ErrorFeedback
.show("Cannot add remote repository", e
);
196 JcrUtils
.logoutQuietly(nodeSession
);
200 /** Creates label and text. */
201 protected Text
createLT(Composite parent
, String label
, String initial
) {
202 new Label(parent
, SWT
.NONE
).setText(label
);
203 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
);
204 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
205 text
.setText(initial
);
209 /** Creates label and check. */
210 protected Button
createLC(Composite parent
, String label
, Boolean initial
) {
211 new Label(parent
, SWT
.NONE
).setText(label
);
212 Button check
= new Button(parent
, SWT
.CHECK
);
213 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
214 check
.setSelection(initial
);
218 protected Text
createLP(Composite parent
, String label
) {
219 new Label(parent
, SWT
.NONE
).setText(label
);
220 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
| SWT
.PASSWORD
);
221 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));