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
.ui
.model
.application
.ui
.basic
.MPart
;
40 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
41 import org
.eclipse
.jface
.dialogs
.Dialog
;
42 import org
.eclipse
.jface
.dialogs
.IMessageProvider
;
43 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
44 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
45 import org
.eclipse
.swt
.SWT
;
46 import org
.eclipse
.swt
.events
.SelectionAdapter
;
47 import org
.eclipse
.swt
.events
.SelectionEvent
;
48 import org
.eclipse
.swt
.graphics
.Point
;
49 import org
.eclipse
.swt
.layout
.GridData
;
50 import org
.eclipse
.swt
.layout
.GridLayout
;
51 import org
.eclipse
.swt
.widgets
.Button
;
52 import org
.eclipse
.swt
.widgets
.Composite
;
53 import org
.eclipse
.swt
.widgets
.Control
;
54 import org
.eclipse
.swt
.widgets
.Display
;
55 import org
.eclipse
.swt
.widgets
.Label
;
56 import org
.eclipse
.swt
.widgets
.Shell
;
57 import org
.eclipse
.swt
.widgets
.Text
;
60 * Connect to a remote repository and, if successful publish it as an OSGi
63 public class AddRemoteRepository
implements ArgeoNames
{
66 private RepositoryFactory repositoryFactory
;
68 private Repository nodeRepository
;
70 private Keyring keyring
;
73 public void execute(@Named(IServiceConstants
.ACTIVE_PART
) MPart part
) {
74 JcrBrowserView view
= (JcrBrowserView
) part
.getObject();
75 RemoteRepositoryLoginDialog dlg
= new RemoteRepositoryLoginDialog(Display
.getDefault().getActiveShell());
76 if (dlg
.open() == Dialog
.OK
) {
81 // public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
82 // this.repositoryFactory = repositoryFactory;
85 // public void setKeyring(Keyring keyring) {
86 // this.keyring = keyring;
89 // public void setNodeRepository(Repository nodeRepository) {
90 // this.nodeRepository = nodeRepository;
93 class RemoteRepositoryLoginDialog
extends TitleAreaDialog
{
94 private static final long serialVersionUID
= 2234006887750103399L;
97 private Text username
;
98 private Text password
;
99 private Button saveInKeyring
;
101 public RemoteRepositoryLoginDialog(Shell parentShell
) {
105 protected Point
getInitialSize() {
106 return new Point(600, 400);
109 protected Control
createDialogArea(Composite parent
) {
110 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
111 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
112 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
113 composite
.setLayout(new GridLayout(2, false));
114 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
115 setMessage("Login to remote repository", IMessageProvider
.NONE
);
116 name
= createLT(composite
, "Name", "remoteRepository");
117 uri
= createLT(composite
, "URI", "http://localhost:7070/jcr/node");
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 private static final long serialVersionUID
= -1829962269440419560L;
133 public void widgetSelected(SelectionEvent arg0
) {
139 void testConnection() {
140 Session session
= null;
142 URI checkedUri
= new URI(uri
.getText());
143 String checkedUriStr
= checkedUri
.toString();
145 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
146 params
.put(NodeConstants
.LABELED_URI
, checkedUriStr
);
147 Repository repository
= repositoryFactory
.getRepository(params
);
148 if (username
.getText().trim().equals("")) {// anonymous
149 // FIXME make it more generic
150 session
= repository
.login("main");
152 // FIXME use getTextChars() when upgrading to 3.7
153 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
154 char[] pwd
= password
.getText().toCharArray();
155 SimpleCredentials sc
= new SimpleCredentials(username
.getText(), pwd
);
156 session
= repository
.login(sc
, "main");
157 MessageDialog
.openInformation(getParentShell(), "Success",
158 "Connection to '" + uri
.getText() + "' successful");
160 } catch (Exception e
) {
161 ErrorFeedback
.show("Connection test failed for " + uri
.getText(), e
);
163 JcrUtils
.logoutQuietly(session
);
168 protected void okPressed() {
169 Session nodeSession
= null;
171 nodeSession
= nodeRepository
.login();
172 Node home
= NodeUtils
.getUserHome(nodeSession
);
174 Node remote
= home
.hasNode(ARGEO_REMOTE
) ? home
.getNode(ARGEO_REMOTE
) : home
.addNode(ARGEO_REMOTE
);
175 if (remote
.hasNode(name
.getText()))
176 throw new EclipseUiException("There is already a remote repository named " + name
.getText());
177 Node remoteRepository
= remote
.addNode(name
.getText(), ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
);
178 remoteRepository
.setProperty(ARGEO_URI
, uri
.getText());
179 remoteRepository
.setProperty(ARGEO_USER_ID
, username
.getText());
181 if (saveInKeyring
.getSelection()) {
182 String pwdPath
= remoteRepository
.getPath() + '/' + ARGEO_PASSWORD
;
183 keyring
.set(pwdPath
, password
.getText().toCharArray());
186 MessageDialog
.openInformation(getParentShell(), "Repository Added",
187 "Remote repository '" + username
.getText() + "@" + uri
.getText() + "' added");
190 } catch (Exception e
) {
191 ErrorFeedback
.show("Cannot add remote repository", e
);
193 JcrUtils
.logoutQuietly(nodeSession
);
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
, Boolean initial
) {
208 new Label(parent
, SWT
.NONE
).setText(label
);
209 Button check
= new Button(parent
, SWT
.CHECK
);
210 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
211 check
.setSelection(initial
);
215 protected Text
createLP(Composite parent
, String label
) {
216 new Label(parent
, SWT
.NONE
).setText(label
);
217 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
| SWT
.PASSWORD
);
218 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));