-
Notifications
You must be signed in to change notification settings - Fork 234
/
BaseCredentialsCLICommand.java
108 lines (101 loc) · 4.06 KB
/
BaseCredentialsCLICommand.java
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
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc..
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.plugins.credentials.cli;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.common.IdCredentials;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.util.XMLUtils;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.xml.XppDriver;
import hudson.cli.CLICommand;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.lang.StringUtils;
import org.xml.sax.SAXException;
/**
* Base class for the Credentials CLI commands.
*
* @since 2.1.1
*/
public abstract class BaseCredentialsCLICommand extends CLICommand {
protected static Credentials getCredentialsById(CredentialsStore store, Domain domain, String id) {
List<Credentials> credentialsList = store.getCredentials(domain);
Set<String> ids = new HashSet<String>(credentialsList.size());
Credentials existing = null;
int index = 0;
for (Credentials c : credentialsList) {
String cid;
if (c instanceof IdCredentials) {
cid = ((IdCredentials) c).getId();
} else {
while (ids.contains("index-" + index)) {
index++;
}
cid = "index-" + index;
index++;
}
if (id.equals(cid)) {
existing = c;
break;
}
ids.add(cid);
}
return existing;
}
protected static Domain getDomainByName(CredentialsStore store, String domain) {
if (StringUtils.equals("_", domain) || StringUtils.isBlank(domain) || "(global)".equals(domain)) {
return Domain.global();
} else {
for (Domain d : store.getDomains()) {
if (domain.equals(d.getName())) {
return d;
}
}
}
return null;
}
protected static HierarchicalStreamReader safeXmlStreamReader(InputStream stream) throws IOException {
return safeXmlStreamReader(new StreamSource(stream));
}
protected static HierarchicalStreamReader safeXmlStreamReader(Source source) throws IOException {
final StringWriter out = new StringWriter();
try {
XMLUtils.safeTransform(source, new StreamResult(out));
out.close();
} catch (TransformerException | SAXException e) {
throw new IOException("Failed to parse", e);
}
return new XppDriver().createReader(new StringReader(out.toString()));
}
}