-
-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathUrlUtils.java
More file actions
156 lines (126 loc) · 4.92 KB
/
UrlUtils.java
File metadata and controls
156 lines (126 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (c) 2013 Cosmin Stejerean, Karl Heinz Marbaise, and contributors.
*
* Distributed under the MIT license: http://opensource.org/licenses/MIT
*/
package com.offbytwo.jenkins.client.util;
import com.offbytwo.jenkins.model.FolderJob;
import java.net.URI;
/**
* Utility class for manipulating API paths.
* @author Dell Green
*/
public final class UrlUtils {
/**
* The default size to use for string buffers.
*/
private static final int DEFAULT_BUFFER_SIZE = 64;
/**
* Utility Class.
*/
private UrlUtils() {
//do nothing
}
/**
* Helper to create a base url in case a folder is given
* @param folder the folder or {@code null}
* @return The created base url.
*/
public static String toBaseUrl(final FolderJob folder) {
return folder == null ? "/" : folder.getUrl();
}
/**
* Helper to create the base url for a job, with or without a given folder
* @param folder the folder or {@code null}
* @param jobName the name of the job.
* @return converted base url.
*/
public static String toJobBaseUrl(final FolderJob folder, final String jobName) {
final StringBuilder sb = new StringBuilder(DEFAULT_BUFFER_SIZE);
sb.append(UrlUtils.toBaseUrl(folder));
if (sb.charAt(sb.length() - 1) != '/') sb.append('/');
sb.append("job/");
final String[] jobNameParts = jobName.split("/");
for (int i = 0; i < jobNameParts.length; i++) {
sb.append(EncodingUtils.encode(jobNameParts[i]));
if (i != jobNameParts.length - 1) sb.append('/');
}
return sb.toString().replace("+","%20");
}
/**
* Helper to create the base url for a view, with or without a given folder
* @param folder the folder or {@code null}
* @param name the of the view.
* @return converted view url.
*/
public static String toViewBaseUrl(final FolderJob folder, final String name) {
final StringBuilder sb = new StringBuilder(DEFAULT_BUFFER_SIZE);
final String base = UrlUtils.toBaseUrl(folder);
sb.append(base);
if (!base.endsWith("/")) sb.append('/');
sb.append("view/")
.append(EncodingUtils.encode(name));
return sb.toString().replace("+","%20");
}
/**
* Parses the provided job name for folders to get the full path for the job.
* @param jobName the fullName of the job.
* @return the path of the job including folders if present.
*/
public static String toFullJobPath(final String jobName) {
final String[] parts = jobName.split("/");
if (parts.length == 1) return parts[0];
final StringBuilder sb = new StringBuilder(DEFAULT_BUFFER_SIZE);
for (int i = 0; i < parts.length; i++) {
sb.append(parts[i]);
if (i != parts.length -1) sb.append("/job/");
}
return sb.toString().replace(" ","%20");
}
/**
* Join two paths together taking into account leading/trailing slashes.
* @param path1 the first path
* @param path2 the second path
* @return the joins path
*/
public static String join(final String path1, final String path2) {
if (path1.isEmpty() && path2.isEmpty()) return "";
if (path1.isEmpty() && !path2.isEmpty()) return path2;
if (path2.isEmpty() && !path1.isEmpty()) return path1;
final StringBuilder sb = new StringBuilder(DEFAULT_BUFFER_SIZE);
sb.append(path1);
if (sb.charAt(sb.length() - 1) == '/') sb.setLength(sb.length() - 1);
if (path2.charAt(0) != '/') sb.append('/');
sb.append(path2);
return sb.toString();
}
/**
* Create a JSON URI from the supplied parameters.
* @param uri the server URI
* @param context the server context if any
* @param path the specific API path
* @return new full URI instance
*/
public static URI toJsonApiUri(final URI uri, final String context, final String path) {
String p = path;
if (!p.matches("(?i)https?://.*")) p = join(context, p);
if (!p.contains("?")) {
p = join(p, "api/json");
} else {
final String[] components = p.split("\\?", 2);
p = join(components[0], "api/json") + "?" + components[1];
}
return uri.resolve("/").resolve(p.replace(" ", "%20"));
}
/**
* Create a URI from the supplied parameters.
* @param uri the server URI
* @param context the server context if any
* @param path the specific API path
* @return new full URI instance
*/
public static URI toNoApiUri(final URI uri, final String context, final String path) {
final String p = path.matches("(?i)https?://.*") ? path : join(context, path);
return uri.resolve("/").resolve(p.replace(" ", "%20"));
}
}