• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

28.87
/exist-core/src/main/java/org/exist/protocolhandler/xmldb/XmldbURL.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 *
24
 * NOTE: Parts of this file contain code from 'The eXist-db Authors'.
25
 *       The original license header is included below.
26
 *
27
 * =====================================================================
28
 *
29
 * eXist-db Open Source Native XML Database
30
 * Copyright (C) 2001 The eXist-db Authors
31
 *
32
 * info@exist-db.org
33
 * http://www.exist-db.org
34
 *
35
 * This library is free software; you can redistribute it and/or
36
 * modify it under the terms of the GNU Lesser General Public
37
 * License as published by the Free Software Foundation; either
38
 * version 2.1 of the License, or (at your option) any later version.
39
 *
40
 * This library is distributed in the hope that it will be useful,
41
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43
 * Lesser General Public License for more details.
44
 *
45
 * You should have received a copy of the GNU Lesser General Public
46
 * License along with this library; if not, write to the Free Software
47
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
48
 */
49
package org.exist.protocolhandler.xmldb;
50

51
import java.net.MalformedURLException;
52
import java.net.URI;
53
import java.net.URL;
54

55
import org.exist.xmldb.XmldbURI;
56

57
/**
58
 *  A utility class for xmldb URLs. Since, java.net.URL is final this class
59
 * acts as a wrapper, convenience methods have been added.<BR>
60
 * <BR>
61
 * Example:<BR>
62
 * <I>xmldb:exist://username:password@hostname:8080/exist/xmlrpc/db/collection/document.xml</I><BR>
63
 * <BR>
64
 * Note: A collection URL ends with a "/":<BR>
65
 * <I>xmldb:exist://hostname:8080/exist/xmlrpc/db/collection/</I>
66
 *
67
 * @see java.net.URI
68
 * @see java.net.URL
69
 * @see org.exist.xmldb.XmldbURI
70
 *
71
 * @author Dannes Wessels
72
 */
73
public class XmldbURL {
74
    
75
    private static final int USERNAME=1;
76
    private static final int PASSWORD=2;
77
    
78
    private URL myUrl;
79
    
80
    /**
81
     * Creates a new instance of XmldbURL using an XmldbURI object.
82
     *
83
     * @param xmldbURI Resource location.
84
     * @throws java.net.MalformedURLException URL is not correct.
85
     */
86
    public XmldbURL(XmldbURI xmldbURI) throws MalformedURLException {
87
        this(xmldbURI.toURL());
×
88
    }
×
89
    
90
    /**
91
     * Creates a new instance of XmldbURL using an URL object.
92
     * @param url Resource location.
93
     * @throws java.net.MalformedURLException URL is not correct.
94
     */
95
    public XmldbURL(URL url) throws MalformedURLException  {
1✔
96
        // check protocol
97
        if("xmldb".equals(url.getProtocol())){
1!
98
            myUrl = url;
1✔
99
        } else {
1✔
100
            throw new MalformedURLException("URL is not an \"xmldb:\" URL: "+url.toString() );
×
101
        }
102
    }
1✔
103
    
104
    /**
105
     * Creates a new instance of XmldbURL using an URI object.
106
     *
107
     * @param uri Resource location.
108
     * @throws java.net.MalformedURLException URL is not correct.
109
     */
110
    public XmldbURL(URI uri) throws MalformedURLException  {
111
        this(uri.toURL());
×
112
    }
×
113
    
114
    /**
115
     * Creates a new instance of XmldbURL using an String.
116
     * @param txt Resource location.
117
     * @throws java.net.MalformedURLException URL is not correct.
118
     */
119
    public XmldbURL(String txt) throws MalformedURLException {
120
        this(new URL(txt));
1✔
121
    }
1✔
122
    
123
    /**
124
     * xmldb:exist://<B>username:password</B>@hostname:8080/exist/xmlrpc/db/collection/document.xml
125
     * @see java.net.URL#getUserInfo
126
     *
127
     * @return username:password
128
     */
129
    public String getUserInfo() {
130
        return myUrl.getUserInfo();
×
131
    }
132
    
133
    /**
134
     * xmldb:exist://<B>username</B>:password@hostname:8080/exist/xmlrpc/db/collection/document.xml
135
     * @return username
136
     */
137
    public String getUsername(){
138
        return extractCredentials(USERNAME);
×
139
    }
140
    
141
    /**
142
     * xmldb:exist://username:<B>password</B>@hostname:8080/exist/xmlrpc/db/collection/document.xml
143
     * @return password
144
     */
145
    public String getPassword(){
146
        return extractCredentials(PASSWORD);
×
147
    }
148
    
149
    /**
150
     * @return URL representation of location.
151
     */
152
    public URL getURL(){
153
        return myUrl;
×
154
    }
155
    
156
    /**
157
     * xmldb:exist://<B>username:password@hostname:8080/exist/xmlrpc/db/collection/document.xml</B>?query#fragment
158
     * @see java.net.URL#getAuthority
159
     * @return authority
160
     */
161
    public String getAuthority() {
162
        return myUrl.getAuthority();
×
163
    }
164
    
165
    /**
166
     * xmldb:exist://username:password@hostname:8080<B>/exist/xmlrpc</B>/db/collection/document.xml?query#fragment
167
     * @return context, null if not available.
168
     */
169
    public String getContext() {
170
        final String path = myUrl.getPath();
×
171
        final int dbPosition=path.indexOf("/db");
×
172
        String context=null;
×
173
        
174
        if(dbPosition!=-1){
×
175
            // since all paths begin with this pattern..
176
            context=path.substring(0,dbPosition);
×
177
        } 
178
        
179
        if(context!=null && context.isEmpty()){
×
180
            context=null;
×
181
        }
182
        
183
        return context;
×
184
    }
185
    
186
    // /exist/xmlrpc/db/shakespeare/plays/macbeth.xml
187
    // /exist/xmlrpc/db/shakespeare/plays/
188
    // /db/shakespeare/plays/macbeth.xml
189
    // /db/shakespeare/plays/
190
    
191
    
192
    /**
193
     * xmldb:exist://username:password@hostname:8080/exist/xmlrpc<B>/db/collection</B>/document.xml
194
     * @return collection
195
     */
196
    public String getCollection(){
197
        
198
        String path=myUrl.getPath();
1✔
199
        String collectionName=null;
1✔
200
        
201
        final int dbLocation=path.indexOf("/db");
1✔
202
        
203
        if(dbLocation!=-1){
1!
204
            // found pattern "/db"
205
            if(path.endsWith("/")){
1!
206
                // -1 removes the slash
207
                collectionName=path.substring(dbLocation, (path.length()-1) );
×
208
            } else {
×
209
                final int lastSep=path.lastIndexOf('/');
1✔
210
                if(lastSep==0){
1!
211
                    collectionName="/";
×
212
                    
213
                } else if(lastSep!=-1){
1!
214
                    collectionName=path.substring(dbLocation, lastSep);
1✔
215
                    
216
                } else {
1✔
217
                    collectionName=path;
×
218
                }
219
            }
220
            
221
        } else {  // TODO not very well tested
×
222
            // pattern not found, taking full path
223
            if(path.endsWith("/")){
×
224
                // -1 removes the slash
225
                collectionName=path.substring(0, (path.length()-1) );
×
226
            } else {
×
227
                final int lastSep=path.lastIndexOf('/');
×
228
                if(lastSep!=-1){
×
229
                    collectionName=path.substring(0, lastSep);
×
230
                } else {
×
231
                    collectionName="/";
×
232
                }
233
            }
234
        }
235
        
236
        return collectionName;
1✔
237
    }
238
    
239
    /**
240
     * xmldb:exist://username:password@hostname:8080/exist/xmlrpc/db/collection/<B>document.xml</B>
241
     * @return collection
242
     */
243
    public String getDocumentName(){
244
        String serverPath=myUrl.getPath();
1✔
245
        String documentName=null;
1✔
246
        if(!serverPath.endsWith("/")){
1!
247
            final int lastSep=serverPath.lastIndexOf('/');
1✔
248
            if(lastSep==-1){
1!
249
                documentName=serverPath;
×
250
            } else {
×
251
                documentName=serverPath.substring(lastSep+1);
1✔
252
            }
253
        }
254
        return documentName;
1✔
255
    }
256
    
257
    // Get username or password
258
    private String extractCredentials(int part) {
259
        
260
        String userInfo = myUrl.getUserInfo();
×
261
        String username = null;
×
262
        String password = null;
×
263
        
264
        if(userInfo!=null){
×
265
            final int separator = userInfo.indexOf(':');
×
266
            if(separator==-1){
×
267
                username=userInfo;
×
268
                password=null;
×
269
            } else {
×
270
                username=userInfo.substring(0,separator);
×
271
                password=userInfo.substring(separator+1);
×
272
            }
273
        }
274
        
275
        // Fix credentials. If not found (empty string) fill NULL
276
        if(username!=null && username.isEmpty()){
×
277
            username=null;
×
278
        }
279
        
280
        // Fix credentials. If not found (empty string) fill NULL
281
        if(password!=null && password.isEmpty()){
×
282
            password=null;
×
283
        }
284
        
285
        if(part==USERNAME){
×
286
            return username;
×
287
        } else if(part==PASSWORD){
×
288
            return password;
×
289
        }
290
        return null;
×
291
    }
292
    
293
    /**
294
     * <B>xmldb</B>:exist://username:password@hostname:8080/exist/xmlrpc/db/collection/document.xml
295
     * @see java.net.URL#getProtocol
296
     * @return protocol
297
     */
298
    public String getProtocol(){
299
        return myUrl.getProtocol();
×
300
    }
301
    
302
    /**
303
     * xmldb:exist://username:password@<B>hostname</B>:8080/exist/xmlrpc/db/collection/document.xml
304
     * @see java.net.URL#getProtocol
305
     * @return protocol
306
     */
307
    public String getHost(){
308
        final String hostname=myUrl.getHost();
1✔
309
        if(hostname != null && hostname.isEmpty()){
1!
310
            return null;
1✔
311
        } else {
312
            return hostname;
×
313
        }
314
    }
315
    
316
    /**
317
     * xmldb:exist://username:password@hostname:<B>8080</B>/exist/xmlrpc/db/collection/document.xml
318
     * @see java.net.URL#getPort
319
     * @return port
320
     */
321
    public int getPort(){
322
        return myUrl.getPort();
×
323
    }
324
    
325
    /**
326
     * xmldb:exist://username:password@hostname:8080:<B>/exist/xmlrpc/db/collection/document.xml</B>
327
     * @see java.net.URL#getPath
328
     * @return port
329
     */
330
    public String getPath(){
331
        return myUrl.getPath();
1✔
332
    }
333
    
334
    /**
335
     * xmldb:exist://username:password@hostname:8080/exist/xmlrpc/db/collection/document.xml?<B>query</B>#fragment
336
     * @see java.net.URL#getQuery
337
     * @return query
338
     */
339
    public String getQuery(){
340
        return myUrl.getQuery();
×
341
    }
342
    
343
    /**
344
     * xmldb:exist://username:password@hostname:8080:/exist/xmlrpc<B>/db/collection/document.xml</B>
345
     * @return collectionpath
346
     */
347
    public String getCollectionPath(){
348
        return myUrl.getPath().substring(13);
×
349
    }
350
    
351
    /**
352
     * Get http:// URL from xmldb:exist:// URL
353
     * xmldb:exist://username:password@hostname:8080:/exist/xmlrpc/db/collection/document.xml
354
     * @return http://username:password@hostname:8080:/exist/xmlrpc/db/collection/document.xml
355
     */
356
    public String getXmlRpcURL(){
357
        return "http://" + myUrl.getAuthority() + getContext();
×
358
    }
359
    
360
    /**
361
     * Does the URL have at least a username?
362
     * @return TRUE when URL contains username
363
     */
364
    public boolean hasUserInfo(){
365
        return (getUserInfo()!=null && getUsername()!=null);
×
366
    }
367
    
368
    /**
369
     * Get instance name.
370
     *
371
     * @return instance name, at this moment fixed to 'exist'
372
     */
373
    public String getInstanceName() {
374
        return "exist";  // No other choice
1✔
375
    }
376
    
377
    /**
378
     * Get textual representation of URL.
379
     *
380
     * @see java.net.URL#toString
381
     * @return Text representation of URL.
382
     */
383
    public String toString(){
384
        return myUrl.toString();
×
385
    }
386
    
387
    /**
388
     * Get information whether URL is an embedded URL.
389
     *
390
     * @return TRUE when URL refers to an embedded resource
391
     */
392
    public boolean isEmbedded(){
393
        return (getHost()==null);
1!
394
    }
395
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc