2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.subversion.ui.repository;
43
import java.net.MalformedURLException;
44
import org.netbeans.modules.subversion.config.Scrambler;
45
import org.netbeans.modules.subversion.util.SvnUtils;
46
import org.openide.ErrorManager;
47
import org.openide.util.NbBundle;
48
import org.tigris.subversion.svnclientadapter.SVNRevision;
49
import org.tigris.subversion.svnclientadapter.SVNUrl;
53
* @author Tomas Stupka
55
public class RepositoryConnection {
57
private static final String RC_DELIMITER = "~=~";
60
private String username;
61
private String password;
62
private String externalCommand;
63
private boolean savePassword;
64
private SVNUrl svnUrl;
65
private SVNRevision svnRevision;
67
public RepositoryConnection(RepositoryConnection rc) {
68
this(rc.url, rc.username, rc.password, rc.externalCommand, rc.savePassword);
71
public RepositoryConnection(String url) {
72
this(url, null, null, null, false);
75
public RepositoryConnection(String url, String username, String password, String externalCommand, boolean savePassword) {
77
this.setUsername(username);
78
this.setPassword(password);
79
this.setExternalCommand(externalCommand);
80
this.savePassword = savePassword;
83
public String getUrl() {
87
public String getUsername() {
88
return username == null ? "" : username;
91
public String getPassword() {
92
return password == null ? "" : password;
95
public String getExternalCommand() {
96
return externalCommand == null ? "" : externalCommand;
99
public boolean getSavePassword() {
103
public SVNUrl getSvnUrl() throws MalformedURLException {
110
public SVNRevision getSvnRevision() throws MalformedURLException {
111
if(svnRevision == null) {
117
public boolean equals(Object o) {
121
if (getClass() != o.getClass()) {
125
final RepositoryConnection test = (RepositoryConnection) o;
127
if (this.url != test.url && this.url != null && !this.url.equals(test.url)) {
133
public int hashCode() {
135
hash = 61 * hash + (this.url != null ? this.url.hashCode() : 0);
139
void setUrl(String url) {
145
void setUsername(String username) {
146
this.username = username;
149
void setPassword(String password) {
150
this.password = password;
153
void setExternalCommand(String externalCommand) {
154
this.externalCommand = externalCommand;
157
void setSavePassword(boolean savePassword) {
158
this.savePassword = savePassword;
161
public String toString() {
165
private void parseUrlString(String urlString) throws MalformedURLException {
166
int idx = urlString.lastIndexOf('@');
167
int hostIdx = urlString.indexOf("://"); // NOI18N
168
int firstSlashIdx = urlString.indexOf("/", hostIdx + 3); // NOI18N
169
if(idx < 0 || firstSlashIdx < 0 || idx < firstSlashIdx) {
170
svnRevision = SVNRevision.HEAD;
171
} else /*if (acceptRevision)*/ {
172
if( idx + 1 < urlString.length()) {
173
String revisionString = ""; // NOI18N
175
revisionString = urlString.substring(idx + 1);
176
svnRevision = SvnUtils.getSVNRevision(revisionString);
177
} catch (NumberFormatException ex) {
178
throw new MalformedURLException(NbBundle.getMessage(Repository.class, "MSG_Repository_WrongRevision", revisionString)); // NOI18N
181
svnRevision = SVNRevision.HEAD;
183
urlString = urlString.substring(0, idx);
185
svnUrl = removeEmptyPathSegments(new SVNUrl(urlString));
189
private SVNUrl removeEmptyPathSegments(SVNUrl url) throws MalformedURLException {
190
String[] pathSegments = url.getPathSegments();
191
StringBuffer urlString = new StringBuffer();
192
urlString.append(url.getProtocol());
193
urlString.append("://"); // NOI18N
194
urlString.append(SvnUtils.ripUserFromHost(url.getHost()));
195
if(url.getPort() > 0) {
196
urlString.append(":"); // NOI18N
197
urlString.append(url.getPort());
199
boolean gotSegments = false;
200
for (int i = 0; i < pathSegments.length; i++) {
201
if(!pathSegments[i].trim().equals("")) { // NOI18N
203
urlString.append("/"); // NOI18N
204
urlString.append(pathSegments[i]);
209
return new SVNUrl(urlString.toString());
213
} catch (MalformedURLException ex) {
218
public static String getString(RepositoryConnection rc) {
221
url = rc.getSvnUrl();
222
} catch (MalformedURLException mue) {
224
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, mue);
227
StringBuffer sb = new StringBuffer();
228
sb.append(url.toString());
229
sb.append(RC_DELIMITER);
230
if(rc.getSavePassword()) sb.append(rc.getUsername());
231
sb.append(RC_DELIMITER);
232
if(rc.getSavePassword()) sb.append(Scrambler.getInstance().scramble(rc.getPassword()));
233
sb.append(RC_DELIMITER);
234
sb.append(rc.getExternalCommand());
235
sb.append(RC_DELIMITER);
236
sb.append(rc.getSavePassword());
237
sb.append(RC_DELIMITER);
238
return sb.toString();
241
public static RepositoryConnection parse(String str) {
242
String[] fields = str.split(RC_DELIMITER);
243
int l = fields.length;
244
String url = fields[0];
245
String username = l > 1 && !fields[1].equals("") ? fields[1] : null;
246
String password = l > 2 && !fields[2].equals("") ? Scrambler.getInstance().descramble(fields[2]) : null;
247
String extCmd = l > 3 && !fields[3].equals("") ? fields[3] : null;
248
boolean save = l > 4 && !fields[4].equals("") ? Boolean.parseBoolean(fields[4]) : true;
249
return new RepositoryConnection(url, username, password, extCmd, save);