use of net.sf.json.JSONException in project blueocean-plugin by jenkinsci.
the class GitScm method validateAndCreate.
@Override
public HttpResponse validateAndCreate(@JsonBody JSONObject request) {
boolean requirePush = request.has("requirePush");
final String repositoryUrl;
final AbstractGitSCMSource scmSource;
if (request.has("repositoryUrl")) {
scmSource = null;
repositoryUrl = request.getString("repositoryUrl");
} else {
try {
String fullName = request.getJSONObject("pipeline").getString("fullName");
SCMSourceOwner item = Jenkins.getInstance().getItemByFullName(fullName, SCMSourceOwner.class);
if (item != null) {
scmSource = (AbstractGitSCMSource) item.getSCMSources().iterator().next();
repositoryUrl = scmSource.getRemote();
} else {
return HttpResponses.errorJSON("No repository found for: " + fullName);
}
} catch (JSONException e) {
return HttpResponses.errorJSON("No repositoryUrl or pipeline.fullName specified in request.");
} catch (RuntimeException e) {
return HttpResponses.errorWithoutStack(ServiceException.INTERNAL_SERVER_ERROR, e.getMessage());
}
}
try {
String credentialId = request.getString("credentialId");
User user = User.current();
if (user == null) {
throw new ServiceException.UnauthorizedException("Not authenticated");
}
final StandardCredentials creds = CredentialsMatchers.firstOrNull(CredentialsProvider.lookupCredentials(StandardCredentials.class, Jenkins.getInstance(), Jenkins.getAuthentication(), (List<DomainRequirement>) null), CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialId)));
if (creds == null) {
throw new ServiceException.NotFoundException("No credentials found for: " + credentialId);
}
if (requirePush) {
String branch = request.getString("branch");
new GitBareRepoReadSaveRequest(scmSource, branch, null, branch, null, null).invokeOnScm(new GitSCMFileSystem.FSFunction<Void>() {
@Override
public Void invoke(Repository repository) throws IOException, InterruptedException {
GitUtils.validatePushAccess(repository, repositoryUrl, creds);
return null;
}
});
} else {
List<ErrorMessage.Error> errors = GitUtils.validateCredentials(repositoryUrl, creds);
if (!errors.isEmpty()) {
throw new ServiceException.UnauthorizedException(errors.get(0).getMessage());
}
}
} catch (Exception e) {
return HttpResponses.errorWithoutStack(ServiceException.PRECONDITION_REQUIRED, e.getMessage());
}
return HttpResponses.okJSON();
}
use of net.sf.json.JSONException in project jmeter-plugins by undera.
the class JSONFormatter method process.
@Override
public void process() {
JMeterContext context = getThreadContext();
String responseData = context.getPreviousResult().getResponseDataAsString();
try {
String str = this.formatJSON(responseData);
context.getPreviousResult().setResponseData(str.getBytes());
} catch (JSONException e) {
log.warn("Failed to format JSON: " + e.getMessage());
log.debug("Failed to format JSON", e);
}
}
use of net.sf.json.JSONException in project phabricator-jenkins-plugin by uber.
the class DifferentialClient method fetchDiff.
/**
* Fetch a differential from Conduit
* @return the Conduit API response
* @throws IOException if there is a network error talking to Conduit
* @throws ConduitAPIException if any error is experienced talking to Conduit
*/
public JSONObject fetchDiff() throws IOException, ConduitAPIException {
JSONObject params = new JSONObject().element("ids", new String[] { diffID });
JSONObject query = this.callConduit("differential.querydiffs", params);
JSONObject response;
try {
response = query.getJSONObject("result");
} catch (JSONException e) {
throw new ConduitAPIException(String.format("No 'result' object found in conduit call: (%s) %s", e.getMessage(), query.toString(2)));
}
try {
return response.getJSONObject(diffID);
} catch (JSONException e) {
throw new ConduitAPIException(String.format("Unable to find '%s' key in 'result': (%s) %s", diffID, e.getMessage(), response.toString(2)));
}
}
use of net.sf.json.JSONException in project pmph by BCSquad.
the class WechatAccessToken method getAccessToken.
// https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT
/**
* <pre>
* 功能描述:获取access_token
* 使用示范:
*
* @param appid 凭证
* @param appsecret 密钥
* @param type
* @return
* </pre>
*/
public static AccessToken getAccessToken(String appid, String appsecret, int type) {
AccessToken accessToken = null;
String requestUrl = access_token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
if (type == 1) {
requestUrl = company_access_token_url.replace("CORPID", appid).replace("CORPSECRET", appsecret);
// System.err.println(requestUrl);
}
JSONObject jsonObject = HttpRequestUtil.httpRequest(requestUrl, EnumMethod.GET.name(), null);
if (jsonObject == null) {
jsonObject = HttpRequestUtil.httpRequest(requestUrl, EnumMethod.GET.name(), null);
}
// 如果请求成功
if (null != jsonObject) {
try {
accessToken = new AccessToken();
accessToken.setToken(jsonObject.getString("access_token"));
accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
} catch (JSONException e) {
accessToken = null;
// 获取token失败
}
}
return accessToken;
}
Aggregations