Skip to content

Commit cb0441b

Browse files
committed
Simplify code
1 parent 24dd95b commit cb0441b

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ associated documentation files (the "Software"), to deal in the Software without
4040
import javax.crypto.spec.SecretKeySpec;
4141
import java.io.IOException;
4242
import java.io.PrintWriter;
43-
import java.io.UnsupportedEncodingException;
4443
import java.net.URLDecoder;
4544
import java.nio.charset.Charset;
4645
import java.util.Arrays;
47-
import java.util.LinkedHashMap;
4846
import java.util.List;
4947
import java.util.Map;
5048
import java.util.logging.Logger;
49+
import java.util.regex.Pattern;
50+
import java.util.stream.Collectors;
5151

5252
import static com.google.common.base.Preconditions.checkNotNull;
53+
import static com.google.common.base.Strings.isNullOrEmpty;
5354

5455
/**
5556
* @author Alexander Verhaar
@@ -114,15 +115,15 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
114115

115116
// Get X-Gogs-Delivery header with deliveryID
116117
String gogsDelivery = req.getHeader("X-Gogs-Delivery");
117-
if (gogsDelivery == null || gogsDelivery.isEmpty()) {
118+
if (isNullOrEmpty(gogsDelivery)) {
118119
gogsDelivery = "Triggered by Jenkins-Gogs-Plugin. Delivery ID unknown.";
119120
} else {
120121
gogsDelivery = "Gogs-ID: " + gogsDelivery;
121122
}
122123

123124
// Get X-Gogs-Signature
124125
String gogsSignature = req.getHeader("X-Gogs-Signature");
125-
if (gogsSignature == null || gogsSignature.isEmpty()) {
126+
if (isNullOrEmpty(gogsSignature)) {
126127
gogsSignature = null;
127128
}
128129

@@ -137,14 +138,11 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
137138
exitWebHook(result, rsp);
138139
return;
139140
}
140-
Object jobObject = queryStringMap.get("job");
141-
String jobName;
142-
if (jobObject == null) {
141+
String jobName = queryStringMap.get("job").toString();
142+
if (isNullOrEmpty(jobName)) {
143143
result.setStatus(404, "No value assigned to parameter 'job'");
144144
exitWebHook(result, rsp);
145145
return;
146-
} else {
147-
jobName = jobObject.toString();
148146
}
149147

150148
// Get the POST stream
@@ -272,20 +270,10 @@ private void exitWebHook(GogsResults result, StaplerResponse resp) throws IOExce
272270
* @param qs Querystring
273271
* @return returns map from querystring
274272
*/
275-
private static Map<String, String> splitQuery(String qs) throws UnsupportedEncodingException {
276-
final Map<String, String> query_pairs = new LinkedHashMap<>();
277-
final String[] pairs = qs.split("&");
278-
for (String pair : pairs) {
279-
final int idx = pair.indexOf("=");
280-
final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), DEFAULT_CHARSET) : pair;
281-
final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), DEFAULT_CHARSET) : null;
282-
query_pairs.put(key, value);
283-
}
284-
return query_pairs;
285-
}
286-
287-
private boolean isNullOrEmpty(String s) {
288-
return s == null || s.trim().isEmpty();
273+
private static Map<String, String> splitQuery(String qs) {
274+
return Pattern.compile("&").splitAsStream(qs)
275+
.map(p -> p.split("="))
276+
.collect(Collectors.toMap(a -> a[0], a -> a.length > 1 ? a[1] : ""));
289277
}
290278
}
291279

0 commit comments

Comments
 (0)