Skip to content
This repository was archived by the owner on Jul 18, 2022. It is now read-only.

Commit af49a68

Browse files
committed
Do not remove leading or trailing slashes on path
Extracted property for service url Renamed some properties
1 parent fabeece commit af49a68

1 file changed

Lines changed: 61 additions & 39 deletions

File tree

backmeup-storage-client/src/main/java/org/backmeup/storage/client/StorageConnectionStringBuilder.java

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package org.backmeup.storage.client;
22

3-
import java.net.MalformedURLException;
43
import java.net.URL;
54
import java.util.HashMap;
65
import java.util.Map.Entry;
76

87

98
/*
10-
* [service-name]:[[protocoll://]serverName[:portNumber][/basePath]][;property=value]*
9+
* [service-name];[[protocoll://]serverName[:portNumber][/basePath]][;property=value]*
1110
*
12-
* backmeup-storage:[[protocoll://]serverName[:portNumber][/basePath]][;property=value]*
11+
* backmeup-storage;[[protocoll://]serverName[:portNumber][/basePath]][;property=value]*
1312
*
1413
* backmeup-storage;http://localhost:8080/backmeup-storage-service/;Token=abc123
1514
*
@@ -30,6 +29,11 @@ public StorageConnectionStringBuilder() {
3029
properties = new HashMap<>();
3130
}
3231

32+
public StorageConnectionStringBuilder(String connectionString) {
33+
this();
34+
parse(connectionString);
35+
}
36+
3337
// Properties -------------------------------------------------------------
3438

3539
public String getProtocol() {
@@ -69,16 +73,41 @@ public String getPath() {
6973
}
7074

7175
public void setPath(String basePath) {
72-
url.put(PATH, removeSlashes(basePath));
76+
url.put(PATH, basePath);
7377
}
7478

75-
public void add(String key, String value) {
79+
public void addProperty(String key, String value) {
7680
if (key == null || value == null) {
7781
throw new IllegalArgumentException("Arguments must not be null");
7882
}
7983
properties.put(key, value);
8084
}
8185

86+
public String getProperty(String key) {
87+
if (key == null || key == "") {
88+
throw new IllegalArgumentException("Key must not be null");
89+
}
90+
91+
return properties.get(key);
92+
}
93+
94+
public String getUrl() {
95+
StringBuffer sb = new StringBuffer();
96+
if (url.containsKey(PROTOCOL)) {
97+
sb.append(url.get(PROTOCOL));
98+
sb.append("://");
99+
}
100+
sb.append(url.get(HOST));
101+
if (url.containsKey(PORT)) {
102+
sb.append(":");
103+
sb.append(url.get(PORT));
104+
}
105+
if (url.containsKey(PATH)) {
106+
sb.append(url.get(PATH));
107+
}
108+
return sb.toString();
109+
}
110+
82111
public void parse(String connectionString) {
83112
if (connectionString == null || connectionString.length() == 0) {
84113
throw new IllegalArgumentException("Invalid Connection String");
@@ -99,14 +128,16 @@ public void parse(String connectionString) {
99128

100129
setProtocol(serverUrl.getProtocol());
101130
setHost(serverUrl.getHost());
102-
setPort(serverUrl.getPort());
131+
if (serverUrl.getPort() != -1) {
132+
setPort(serverUrl.getPort());
133+
}
103134
setPath(serverUrl.getPath());
104135

105-
} catch (MalformedURLException e) {
106-
throw new IllegalArgumentException("Invalid Connection String");
136+
} catch (Exception e) {
137+
throw new IllegalArgumentException("Invalid Connection String");
107138
}
108139

109-
// 2: for each remaining pair parse into appropriate map entries
140+
// 4: for each remaining pair parse into appropriate map entries
110141
for (int i = 2; i < valuePairs.length; i++) {
111142
final int equalDex = valuePairs[i].indexOf("=");
112143
if (equalDex < 1) {
@@ -123,49 +154,21 @@ public void parse(String connectionString) {
123154
throw new IllegalArgumentException("Invalid Connection String");
124155
}
125156

126-
add(key, value);
157+
addProperty(key, value);
127158
}
128159
}
129160

130161
public String build() {
131162
return this.toString();
132163
}
133164

134-
private String removeSlashes(String string) {
135-
// return string.replaceAll("/$", "");
136-
137-
String retVal = string;
138-
// remove beginning slash
139-
if (retVal.startsWith("/")) {
140-
retVal = retVal.substring(1, retVal.length());
141-
}
142-
143-
// remove trailing slash
144-
if (retVal.endsWith("/")) {
145-
retVal = retVal.substring(0, retVal.length() - 1);
146-
}
147-
148-
return retVal;
149-
}
150165

151166
@Override
152167
public String toString() {
153168
StringBuilder sb = new StringBuilder();
154169
sb.append(PREFIX);
155170
sb.append(";");
156-
if (url.containsKey(PROTOCOL)) {
157-
sb.append(url.get(PROTOCOL));
158-
sb.append("://");
159-
}
160-
sb.append(url.get(HOST));
161-
if (url.containsKey(PORT)) {
162-
sb.append(":");
163-
sb.append(url.get(PORT));
164-
}
165-
if (url.containsKey(PATH)) {
166-
sb.append("/");
167-
sb.append(url.get(PATH));
168-
}
171+
sb.append(getUrl());
169172
if (!properties.isEmpty()) {
170173
for (Entry<String, String> entry : properties.entrySet()) {
171174
sb.append(";");
@@ -176,4 +179,23 @@ public String toString() {
176179
}
177180
return sb.toString();
178181
}
182+
183+
// Private methods --------------------------------------------------------
184+
185+
private String removeSlashes(String string) {
186+
// return string.replaceAll("/$", "");
187+
188+
String retVal = string;
189+
// remove leading slash
190+
if (retVal.startsWith("/")) {
191+
retVal = retVal.substring(1, retVal.length());
192+
}
193+
194+
// remove trailing slash
195+
if (retVal.endsWith("/")) {
196+
retVal = retVal.substring(0, retVal.length() - 1);
197+
}
198+
199+
return retVal;
200+
}
179201
}

0 commit comments

Comments
 (0)