/** * Parses a JSONObject into its appropriate ViewVariable implementation * * @param obj * @return */ private static AbstractViewVariable parseJSONRecursive(JSONObject obj) { if (obj == null || obj.isNullObject()) throw new NullArgumentException("obj"); String type = obj.getString("type"); if (type == null) throw new IllegalArgumentException("Object missing type " + obj); // Parse a SimpleAxis if (type.equals(SimpleAxis.TYPE_STRING)) { SimpleAxis axis = new SimpleAxis( attemptGetString(obj, "name"), attemptGetString(obj, "dataType"), attemptGetString(obj, "units"), null, null); JSONObject dimensionBounds = obj.getJSONObject("dimensionBounds"); JSONObject valueBounds = obj.getJSONObject("valueBounds"); if (dimensionBounds != null && !dimensionBounds.isNullObject()) { axis.setDimensionBounds( new SimpleBounds(dimensionBounds.getDouble("from"), dimensionBounds.getDouble("to"))); } if (valueBounds != null && !valueBounds.isNullObject()) { axis.setValueBounds( new SimpleBounds(valueBounds.getDouble("from"), valueBounds.getDouble("to"))); } return axis; // Parse a SimpleGrid } else if (type.equals(SimpleGrid.TYPE_STRING)) { JSONArray axes = obj.getJSONArray("axes"); SimpleGrid grid = new SimpleGrid( attemptGetString(obj, "name"), attemptGetString(obj, "dataType"), attemptGetString(obj, "units"), null); List<AbstractViewVariable> childAxes = new ArrayList<>(); for (int i = 0; i < axes.size(); i++) { AbstractViewVariable var = parseJSONRecursive(axes.getJSONObject(i)); if (var != null) childAxes.add(var); } if (childAxes.size() > 0) { grid.setAxes(childAxes.toArray(new AbstractViewVariable[childAxes.size()])); return grid; } else { return null; } } else { throw new IllegalArgumentException("Unable to parse type " + type); } }
/** * Construct an object from parameters input by a user. * * <p>Not using {@link DataBoundConstructor}, but using {@link * Descriptor#newInstance(StaplerRequest, JSONObject)}. * * @param req * @param formData * @param fieldName used for exception information. * @return * @throws hudson.model.Descriptor.FormException */ private BuildStep bindJSONWithDescriptor( StaplerRequest req, JSONObject formData, String fieldName) throws hudson.model.Descriptor.FormException { if (formData == null || formData.isNullObject()) { return null; } String clazzName = formData.optString("$class", null); if (clazzName == null) { // Fall back on the legacy stapler-class attribute. clazzName = formData.optString("stapler-class", null); } if (clazzName == null) { throw new FormException("No $class or stapler-class is specified", fieldName); } try { @SuppressWarnings("unchecked") Class<? extends Describable<?>> clazz = (Class<? extends Describable<?>>) Jenkins.getInstance().getPluginManager().uberClassLoader.loadClass(clazzName); Descriptor<?> d = Jenkins.getInstance().getDescriptorOrDie(clazz); return (BuildStep) d.newInstance(req, formData); } catch (ClassNotFoundException e) { throw new FormException( String.format("Failed to instantiate: class not found %s", clazzName), e, fieldName); } }
@Override public AggregatedTestResultPublisher newInstance(StaplerRequest req, JSONObject formData) throws FormException { JSONObject s = formData.getJSONObject("specify"); if (s.isNullObject()) return new AggregatedTestResultPublisher( null, req.getParameter("includeFailedBuilds") != null); else return new AggregatedTestResultPublisher( s.getString("jobs"), req.getParameter("includeFailedBuilds") != null); }
@Override public boolean configure(StaplerRequest req, JSONObject json) throws FormException { JSONObject hookMode = json.getJSONObject("hookMode"); manageHook = "auto".equals(hookMode.getString("value")); JSONObject o = hookMode.getJSONObject("hookUrl"); if (o != null && !o.isNullObject()) { hookUrl = o.getString("url"); } else { hookUrl = null; } credentials = req.bindJSONToList(Credential.class, hookMode.get("credentials")); save(); return true; }
/** Parses the origin JSON object. */ private boolean parseJSON(JSONObject json) { if (json.isNullObject()) return false; setSize(json.getString(TMDbConstants.SIZE)); try { setLink(new URL(json.getString(TMDbConstants.LINK))); } catch (MalformedURLException e) { Log.print(e); } return true; }
public void find(String binFile, List<SourceFileResult> results) { File bin = new File(binFile); String url = null; String fileDownloaded = null; try { if (canceled) return; InputStream is2 = null; String md5; try { md5 = new String(Hex.encodeHex(Files.getDigest(bin, MessageDigest.getInstance("MD5")))); String serviceUrl = SERVICE + "/rest/libraries?md5=" + md5; is2 = new URL(serviceUrl).openStream(); String str = IOUtils.toString(is2); JSONArray json = JSONArray.fromObject(str); for (int i = 0; i < json.size(); i++) { if (canceled) return; JSONObject obj = (JSONObject) json.get(i); JSONObject source = obj.getJSONObject("source"); if (source != null && !source.isNullObject()) { JSONArray ar = source.getJSONArray("urls"); if (ar != null && !ar.isEmpty()) { String url1 = ar.getString(0); String tmpFile = new UrlDownloader().download(url1); if (tmpFile != null && isSourceCodeFor(tmpFile, bin.getAbsolutePath())) { fileDownloaded = tmpFile; url = url1; break; } } } } if (url != null && fileDownloaded != null) { String name = url.substring(url.lastIndexOf('/') + 1); SourceFileResult object = new SourceFileResult(binFile, fileDownloaded, name, 90); Logger.debug(this.toString() + " FOUND: " + object, null); results.add(object); } } finally { IOUtils.closeQuietly(is2); } } catch (Exception e) { e.printStackTrace(); } }
private Page<WeiboUser> getPagedWeiboUser( String url, OAuthTokenPair accessTokenPair, int page, int pageSize) { Map<String, String> additionalParams = new HashMap<String, String>(); additionalParams.put("format", "json"); additionalParams.put("reqnum", pageSize + ""); additionalParams.put("startindex", (page - 1) * pageSize + ""); WeiboResponse response = this.protocal.get(url, additionalParams, accessTokenPair); if (response.isStatusOK()) { try { JSONObject obj = JSONObject.fromObject(response.getHttpResponseText()); JSONObject data = obj.getJSONObject("data"); // 列表为空 if (data != null && data.isNullObject() == false) { JSONArray arr = data.getJSONArray("info"); Page<WeiboUser> pageUser = new Page<WeiboUser>(page, pageSize); List<WeiboUser> users = new ArrayList<WeiboUser>(arr.size()); pageUser.setContent(users); for (int i = 0; i < arr.size(); i++) { JSONObject friend = arr.getJSONObject(i); WeiboUser weiboFriend = new WeiboUser(null); weiboFriend.setNickName(friend.getString("nick")); weiboFriend.setImgUrl(friend.getString("head") + "/180"); // 修正腾讯微博头像url无法访问 weiboFriend.setUid(friend.getString("openid")); String name = friend.getString("name"); weiboFriend.setProfileUrl("http://t.qq.com/" + name + "?preview"); users.add(weiboFriend); } return pageUser; } } catch (Exception e) { response.setLocalError(e); } } if (response.isStatusOK() == false) log.error("error to fetch paged weibo user, resp: " + response); return null; }
/** * 通过json字符串初始化环境变量 格式: {var:value,var:value,.....} * * @param json */ public void initContextVarFromJson(String json) { JSONObject jsonObject = JSONObject.fromObject(json); for (Iterator iterator = jsonObject.keys(); iterator.hasNext(); ) { String key = (String) iterator.next(); Object obj = jsonObject.get(key); if (obj != null && obj instanceof JSONObject) { JSONObject jobj = (JSONObject) obj; if (jobj.isNullObject()) { obj = null; } else { // 日期型数据还原 if (jobj.get("time") != null) { obj = new Date(jobj.getLong("time")); } } } this.context.setVar(key, obj); } }
@Override public HudsonNotificationProperty newInstance(StaplerRequest req, JSONObject formData) throws FormException { List<Endpoint> endpoints = new ArrayList<Endpoint>(); if (formData != null && !formData.isNullObject()) { JSON endpointsData = (JSON) formData.get("endpoints"); if (endpointsData != null && !endpointsData.isEmpty()) { if (endpointsData.isArray()) { JSONArray endpointsArrayData = (JSONArray) endpointsData; endpoints.addAll(req.bindJSONToList(Endpoint.class, endpointsArrayData)); } else { JSONObject endpointsObjectData = (JSONObject) endpointsData; endpoints.add(req.bindJSON(Endpoint.class, endpointsObjectData)); } } } HudsonNotificationProperty notificationProperty = new HudsonNotificationProperty(endpoints); return notificationProperty; }
/** * Extracts field metadata from an editmeta JSON object. * * @param name Field name * @param editmeta Edit metadata JSON object * @return a Meta instance with field metadata * @throws JiraException when the field is missing or metadata is bad */ public static Meta getFieldMetadata(String name, JSONObject editmeta) throws JiraException { if (editmeta.isNullObject() || !editmeta.containsKey(name)) throw new JiraException("Field '" + name + "' does not exist or read-only"); Map f = (Map) editmeta.get(name); Meta m = new Meta(); m.required = Field.getBoolean(f.get("required")); m.name = Field.getString(f.get("name")); if (!f.containsKey("schema")) throw new JiraException("Field '" + name + "' is missing schema metadata"); Map schema = (Map) f.get("schema"); m.type = Field.getString(schema.get("type")); m.items = Field.getString(schema.get("items")); m.system = Field.getString(schema.get("system")); m.custom = Field.getString(schema.get("custom")); m.customId = Field.getInteger(schema.get("customId")); return m; }
@Override public void writeJSON(Patient patient, JSONObject json, Collection<String> selectedFieldNames) { PatientData<String> data = patient.getData(getName()); if (data == null || !data.isNamed()) { return; } Iterator<Entry<String, String>> dataIterator = data.dictionaryIterator(); JSONObject container = json.getJSONObject(getJsonPropertyName()); while (dataIterator.hasNext()) { Entry<String, String> datum = dataIterator.next(); String key = datum.getKey(); if (selectedFieldNames == null || selectedFieldNames.contains(key)) { if (container == null || container.isNullObject()) { // put() is placed here because we want to create the property iff at least one field is // set/enabled json.put(getJsonPropertyName(), new JSONObject()); container = json.getJSONObject(getJsonPropertyName()); } container.put(key, datum.getValue()); } } }
/** * Creates a new instance of a {@link Describable} from the structured form submission data posted * by a radio button group. */ public T newInstanceFromRadioList(JSONObject config) throws FormException { if (config.isNullObject()) return null; // none was selected int idx = config.getInt("value"); return get(idx).newInstance(Stapler.getCurrentRequest(), config); }