use of io.jenkins.blueocean.rest.model.CreateResponse in project blueocean-plugin by jenkinsci.
the class CredentialApi method create.
@POST
@WebMethod(name = "")
public CreateResponse create(@JsonBody JSONObject body, StaplerRequest request) throws IOException {
User authenticatedUser = User.current();
if (authenticatedUser == null) {
throw new ServiceException.UnauthorizedException("No authenticated user found");
}
JSONObject jsonObject = body.getJSONObject("credentials");
final IdCredentials credentials = request.bindJSON(IdCredentials.class, jsonObject);
String domainName = DOMAIN_NAME;
if (jsonObject.get("domain") != null && jsonObject.get("domain") instanceof String) {
domainName = (String) jsonObject.get("domain");
}
CredentialsUtils.createCredentialsInUserStore(credentials, authenticatedUser, domainName, Collections.singletonList(new BlueOceanDomainSpecification()));
CredentialsStoreAction.DomainWrapper domainWrapper = credentialStoreAction.getDomain(domainName);
if (domainWrapper != null) {
CredentialsStoreAction.CredentialsWrapper credentialsWrapper = domainWrapper.getCredential(credentials.getId());
if (credentialsWrapper != null) {
return new CreateResponse(new CredentialApi.Credential(credentialsWrapper, getLink().rel("domains").rel(domainName).rel("credentials")));
}
}
// this should never happen
throw new ServiceException.UnexpectedErrorException("Unexpected error, failed to create credential");
}
Aggregations