use of net.sf.json.JSONException in project zaproxy by zaproxy.
the class ForcedUserAPI method handleApiAction.
@Override
public ApiResponse handleApiAction(String name, JSONObject params) throws ApiException {
log.debug("handleApiAction " + name + " " + params.toString());
Context context;
switch(name) {
case ACTION_SET_FORCED_USER:
context = ApiUtils.getContextByParamId(params, PARAM_CONTEXT_ID);
int userId = ApiUtils.getIntParam(params, PARAM_USER_ID);
try {
extension.setForcedUser(context.getIndex(), userId);
} catch (IllegalStateException ex) {
throw new ApiException(Type.USER_NOT_FOUND);
}
context.save();
return ApiResponseElement.OK;
case ACTION_SET_FORCED_USER_MODE_ENABLED:
if (!params.containsKey(PARAM_MODE_ENABLED))
throw new ApiException(Type.MISSING_PARAMETER, PARAM_MODE_ENABLED);
boolean newModeStatus;
try {
newModeStatus = params.getBoolean(PARAM_MODE_ENABLED);
} catch (JSONException ex) {
throw new ApiException(Type.ILLEGAL_PARAMETER, PARAM_MODE_ENABLED);
}
extension.setForcedUserModeEnabled(newModeStatus);
return ApiResponseElement.OK;
default:
throw new ApiException(Type.BAD_ACTION);
}
}
use of net.sf.json.JSONException in project pmph by BCSquad.
the class WechatAccessToken method getWXjsTicket.
/**
* <pre>
* 功能描述:获取wx_js_ticket
* 使用示范:
*
* @param accessToken access_token
* @return
* </pre>
*/
public static WXjsTicket getWXjsTicket(String accessToken) {
WXjsTicket wXjsTicket = null;
String requestUrl = WXURLUtil.JSAPIURL.replace("ACCESS_TOKEN", accessToken);
// 发起GET请求获取凭证
JSONObject jsonObject = HttpRequestUtil.httpRequest(requestUrl, "GET", null);
System.out.println("CommonUtil.java 调用了一次getWXjsTicket接口");
if (null != jsonObject) {
try {
wXjsTicket = new WXjsTicket();
wXjsTicket.setJsTicket(jsonObject.getString("ticket"));
wXjsTicket.setJsTicketExpiresIn(jsonObject.getInt("expires_in"));
} catch (JSONException e) {
wXjsTicket = null;
// 获取wXjsTicket失败
log.error("获取wXjsTicket失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
}
}
return wXjsTicket;
}
use of net.sf.json.JSONException in project telegram-notifications-plugin by jenkinsci.
the class UserApprover method approve.
public ApprovalType approve(JSONObject formData) {
ApprovalType approvalType;
JSONObject approval = (JSONObject) formData.get("approval");
if (approval == null) {
approvalType = ApprovalType.ALL;
} else {
String value = approval.getString("value");
approvalType = ApprovalType.valueOf(value);
}
JSONArray loaded;
if (approval == null) {
loaded = null;
} else {
try {
// Only one user is subscriber
JSONObject users = approval.getJSONObject("users");
loaded = new JSONArray();
loaded.add(users);
} catch (JSONException e) {
// More than one subscribers
loaded = approval.getJSONArray("users");
}
}
Map<User, Boolean> userBooleanMap = new HashMap<>();
switch(approvalType) {
case MANUAL:
userBooleanMap = collectUsersToApprove(loaded);
break;
case ALL:
userBooleanMap = users.stream().collect(Collectors.toMap(Function.identity(), e -> true));
break;
}
userBooleanMap.forEach(this::updateUserApproval);
return approvalType;
}
use of net.sf.json.JSONException in project compiler by boalang.
the class GetGitHubRepoNames method main.
public static void main(String[] args) {
File inDir = new File(Config.githubRepoListDir);
StringBuilder sb = new StringBuilder();
for (File file : inDir.listFiles()) {
if (file.getName().endsWith(".json")) {
String jsonTxt = "";
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[(int) file.length()];
in.read(bytes);
in.close();
jsonTxt = new String(bytes);
} catch (Exception e) {
System.err.println("Error reading file " + file.getAbsolutePath());
continue;
}
if (jsonTxt.isEmpty()) {
System.err.println("File is empty " + file.getAbsolutePath());
continue;
}
//System.out.println(jsonTxt);
JSONArray repos = null;
try {
repos = (JSONArray) JSONSerializer.toJSON(jsonTxt);
} catch (JSONException e) {
}
if (repos == null) {
System.err.println("Error parsing file " + file.getAbsolutePath());
continue;
}
for (int i = 0; i < repos.size(); i++) {
//System.out.println(repos.getJSONObject(i));
JSONObject repo = repos.getJSONObject(i);
String id = repo.getString("id");
String name = repo.getString("full_name");
System.out.println(id + ": " + name);
sb.append(id + "," + name + "\n");
//FileIO.writeFileContents(new File(Config.githubRepoMetadataDir + "/" + id + ".json"), repo.toString());
}
}
}
FileIO.writeFileContents(new File(inDir.getParentFile().getAbsolutePath() + "/list.csv"), sb.toString());
}
use of net.sf.json.JSONException in project compiler by boalang.
the class RepoMetadata method build.
public boolean build() {
String jsonTxt = "";
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(metadataFile));
byte[] bytes = new byte[(int) metadataFile.length()];
in.read(bytes);
in.close();
jsonTxt = new String(bytes);
} catch (Exception e) {
System.err.println("Error reading file " + metadataFile.getAbsolutePath());
return false;
}
if (jsonTxt.isEmpty()) {
System.err.println("File is empty " + metadataFile.getAbsolutePath());
return false;
}
// System.out.println(jsonTxt);
JSONObject json = null;
try {
json = (JSONObject) JSONSerializer.toJSON(jsonTxt);
} catch (JSONException e) {
}
if (json == null) {
System.err.println("Error parsing file " + metadataFile.getAbsolutePath());
return false;
}
JSONObject jsonProject = json;
if (jsonProject.has(GIT_ID))
this.id = jsonProject.getString(GIT_ID);
if (jsonProject.has(GIT_NAME))
this.name = jsonProject.getString(GIT_NAME);
if (jsonProject.has(GIT_SHORT_DESCRIPTION))
this.shortDescription = jsonProject.getString(GIT_SHORT_DESCRIPTION);
if (jsonProject.has(GIT_HOME_PAGE)) {
this.homepage = jsonProject.getString(GIT_HOME_PAGE);
}
if (jsonProject.has(GIT_SUMMARY_PAGE)) {
this.summaryPage = jsonProject.getString(GIT_SUMMARY_PAGE);
}
if (jsonProject.has(GIT_CREATE)) {
String time = jsonProject.getString(GIT_CREATE);
// project.setCreatedDate(timestamp
this.created_timestamp = getTimeStamp(time);
// * 1000000);
}
if (jsonProject.has(GIT_DESCRIPTION))
this.description = jsonProject.getString(GIT_DESCRIPTION);
/*
* if (jsonProject.has("os")) { JSONArray jsonOSes =
* jsonProject.getJSONArray("os"); if (jsonOSes != null &&
* jsonOSes.isArray()) { for (int i = 0; i < jsonOSes.size(); i++)
* project.addOperatingSystems(jsonOSes.getString(i)); } }
*/
if (jsonProject.has(GIT_PROGRAMMING_LANGUAGES)) {
buildProgrammingLanguages(metadataFile, id);
if (this.programmingLanguages == null || this.programmingLanguages.length == 0)
this.programmingLanguages = new String[] { jsonProject.getString(GIT_PROGRAMMING_LANGUAGES) };
}
/*
* if (jsonProject.has("trackers")) { ArrayList<BugRepository> bugs =
* new ArrayList<BugRepository>(); JSONArray trackers =
* jsonProject.getJSONArray("trackers"); if (trackers.isArray()) { for
* (int i = 0; i < trackers.size(); i++) { JSONObject tracker =
* trackers.getJSONObject(i); if (tracker.has("name") &&
* tracker.getString("name").equals("Bugs")) { if
* (tracker.has("location")) { BugRepository.Builder bug =
* BugRepository.newBuilder();
* bug.setUrl(tracker.getString("location")); bugs.add(bug.build()); }
* break; } } } if (!bugs.isEmpty())
* project.addAllBugRepositories(bugs); }
*/
if (jsonProject.has(GIT_GIT_REPO)) {
this.gitRepository = jsonProject.getString(GIT_GIT_REPO);
}
return true;
}
Aggregations