/** * @see org.alfresco.repo.jscript.app.PropertyDecorator#decorate(org.alfresco.service.namespace.QName, org.alfresco.service.cmr.repository.NodeRef, java.io.Serializable) */ @SuppressWarnings("unchecked") public JSONAware decorate(QName propertyName, NodeRef nodeRef, Serializable value) { Collection<NodeRef> collection = (Collection<NodeRef>)value; JSONArray array = new JSONArray(); for (NodeRef obj : collection) { try { JSONObject jsonObj = new JSONObject(); jsonObj.put("name", this.nodeService.getProperty(obj, ContentModel.PROP_NAME)); jsonObj.put("nodeRef", obj.toString()); array.add(jsonObj); } catch (InvalidNodeRefException e) { logger.warn("Tag with nodeRef " + obj.toString() + " does not exist."); } } return array; }
/** * @see org.alfresco.repo.jscript.app.PropertyDecorator#decorate(org.alfresco.service.namespace.QName, org.alfresco.service.cmr.repository.NodeRef, java.io.Serializable) */ @SuppressWarnings("unchecked") public JSONAware decorate(QName propertyName, NodeRef nodeRef, Serializable value) { Collection<NodeRef> collection = (Collection<NodeRef>)value; JSONArray array = new JSONArray(); for (NodeRef obj : collection) { try { JSONObject jsonObj = new JSONObject(); jsonObj.put("name", this.nodeService.getProperty(obj, ContentModel.PROP_NAME)); jsonObj.put("path", this.getPath(obj)); jsonObj.put("nodeRef", obj.toString()); array.add(jsonObj); } catch (InvalidNodeRefException e) { logger.warn("Category with nodeRef " + obj.toString() + " does not exist."); } } return array; }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public String toString(Object value, Object meta, boolean pretty, boolean insertMeta) { return toString(value, meta, insertMeta, (input) -> { String json; if (input instanceof Map) { json = JSONObject.toJSONString((Map) input); } else if (input instanceof List) { json = JSONArray.toJSONString((List) input); } else if (input instanceof JSONAware) { json = ((JSONAware) input).toJSONString(); } else if (input instanceof Set) { json = JSONArray.toJSONString(new ArrayList((Set) input)); } else if (input.getClass().isArray()) { json = JSONArray.toJSONString(Arrays.asList(input)); } else { throw new IllegalArgumentException("Unsupported data type (" + input + ")!"); } if (pretty) { return JsonBuiltin.format(json); } return json; }); }
@SuppressWarnings("unchecked") public static JSONAware encodeO(FolderInventoryItem input) { JSONObject out = new JSONObject(); out.put("name", input.getName()); out.put("uuid", input.getUuid().toString()); JSONArray files = input.getFiles().stream() .map(FileInventoryItemJSON::encodeO) .collect(Collectors.toCollection(JSONArray::new)); out.put("files", files); JSONArray folders = input.getFolders().stream() .map(FolderInventoryItemJSON::encodeO) .collect(Collectors.toCollection(JSONArray::new)); out.put("folders", folders); return out; }
@SuppressWarnings("unchecked") public static JSONAware encodeO(Inventory input) { JSONObject out = new JSONObject(); JSONArray files = input.getFiles().stream() .map(FileInventoryItemJSON::encodeO) .collect(Collectors.toCollection(JSONArray::new)); out.put("files", files); JSONArray folders = input.getFolders().stream() .map(FolderInventoryItemJSON::encodeO) .collect(Collectors.toCollection(JSONArray::new)); out.put("folders", folders); out.put("defaultEncryptionAlgorithm", input.getDefaultEncryption().toString()); return out; }
private void parseJsonContent( JSONAware jv, String entity, Map<String, RiskData> values ) { int hasTravis = 0; if( jv instanceof JSONArray ) { JSONArray ja = (JSONArray)jv; for (Object o : ja){ JSONObject jo = (JSONObject)o; //System.out.println(jo.get("contributions")); String filename = jo.get("name").toString(); if (filename.equals(".travis.yml")) { hasTravis = 1; break; } } } RiskData rd = new RiskData( GITHUB_PREFIX + "ci_link", entity, new Date(), RiskDataType.NUMBER, hasTravis ); values.put( rd.getId(), rd ); }
private void parseJsonParticipation(JSONAware jv, String entity, Map<String, RiskData> values) { if (jv instanceof JSONObject) { JSONObject jo = (JSONObject) jv; if (jo.containsKey("all")) { // JSONArray ja = (JSONArray)jo.get("all")); ArrayList<Long> ll = (ArrayList<Long>) jo.get("all"); ArrayList<Double> doublelist = new ArrayList<Double>(); Long sum = 0L; for (Long l : ll) { doublelist.add(l.doubleValue()); sum += l; } Distribution d = new Distribution(); d.setValues(doublelist); //weekly commit count for the repository owner and everyone else, 52 weeks RiskData rd = new RiskData(GITHUB_PREFIX + "participation", entity, new Date(), RiskDataType.DISTRIBUTION, d); values.put(rd.getId(), rd); rd = new RiskData(GITHUB_PREFIX + "participation_sum", entity, new Date(), RiskDataType.NUMBER, sum); values.put(rd.getId(), rd); } } }
@SuppressWarnings("unchecked") public <T extends JSONAware> T request(String uri) throws IOException { Request.Response response = new Request(Cli.api + "/api" + uri).send(); if (response.code() != 200) throw new IOException("Error " + response.code() + ": " + response.message()); String json = response.text(); if (json == null) return null; try { return (T) new JSONParser().parse(json); } catch (ParseException e) { throw new IOException(e); } }
@SuppressWarnings("unchecked") public static JSONAware encodeO(FragmentedRange fr) { JSONArray ja = new JSONArray(); for (int i : fr.toRepr()) { ja.add(i); } return ja; }
@SuppressWarnings("unchecked") public static JSONAware encodeO(FileInventoryItem input) { JSONObject out = new JSONObject(); out.put(KEY_NAME, input.getName()); out.put(KEY_UUID, input.getUuid().toString()); out.put(KEY_BLOCKS, FragmentedRangeJSON.encodeO(input.getBlocks())); out.put(KEY_SIZE_ON_DISK, input.getSizeOnDisk()); out.put(KEY_ACTUAL_SIZE, input.getActualSize()); out.put(KEY_MODIFIED_AT, input.getModifiedAt()); out.put(KEY_ENCRYPTION_ALGORITHM, input.getEncryptionAlgorithm().toString()); if (input.getEncryptionData() != null) out.put(KEY_ENCRYPTION_DATA, DatatypeConverter.printBase64Binary(input.getEncryptionData())); else out.put(KEY_ENCRYPTION_DATA, null); if (input.getIntegrityHash() != null) out.put(KEY_INTEGRITY_HASH, DatatypeConverter.printBase64Binary(input.getIntegrityHash())); else out.put(KEY_INTEGRITY_HASH, null); out.put(KEY_MEDIA_TYPE, input.getMediaType()); return out; }
private void parseJsonContributors( JSONAware jv, String entity, Map<String, RiskData> values ) { int contributions = 0; int contributors = 0; if( jv instanceof JSONArray ) { JSONArray ja = (JSONArray)jv; contributors = ja.size(); //System.out.println("contributors: "+contributors); for (Object o : ja){ JSONObject jo = (JSONObject)o; //System.out.println(jo.get("contributions")); contributions += Integer.parseInt(jo.get("contributions").toString()); } getContribDistrib(ja, contributions, 99, entity, values); getContribDistrib(ja, contributions, 95, entity, values); getContribDistrib(ja, contributions, 90, entity, values); getContribDistrib(ja, contributions, 80, entity, values); getContribDistrib(ja, contributions, 50, entity, values); } //number of contributors (i.e. persons that did a commit) RiskData rd = new RiskData( GITHUB_PREFIX + "contributors", entity, new Date(), RiskDataType.NUMBER, contributors ); values.put( rd.getId(), rd ); //sum of all the commits done rd = new RiskData( GITHUB_PREFIX + "contributions_sum", entity, new Date(), RiskDataType.NUMBER, contributions ); values.put( rd.getId(), rd ); //commits per contributor if( contributors > 0 ) { rd = new RiskData( GITHUB_PREFIX + "commits_per_contributor", entity, new Date(), RiskDataType.NUMBER, contributions/contributors ); values.put( rd.getId(), rd ); } }
static public String ToJSONString(Object object) { if(object instanceof JSONAware) { return ((JSONAware)object).toJSONString(); } else { JSONArray array = new JSONArray(); array.add(object); return array.toJSONString(); } }
private static <T> InternalTranslater<T> makeSerializer(TypeAndAnnos info, SourJson sour) { Class<T> fromClass = (Class<T>) GenericTypeReflector.erase(info.type); SJTranslater<T> translater = sour.getTranslater(fromClass); if (translater != null) return new SJTranslaterTranslater<>(translater, info); if ( JSONObject.class.isAssignableFrom(fromClass) || JSONArray.class.isAssignableFrom(fromClass) || Number.class.isAssignableFrom(fromClass) || Boolean.class.isAssignableFrom(fromClass) || JSONAware.class.isAssignableFrom(fromClass) || JSONStreamAware.class.isAssignableFrom(fromClass) || String.class.isAssignableFrom(fromClass) || fromClass.isPrimitive() ) { return new SelfTranslater<>(fromClass); } if (Collection.class.isAssignableFrom(fromClass)) return new CollectionTranslater<>(info); if (fromClass.isArray()) return new ArrayTranslater<>(info); if (Map.class.isAssignableFrom(fromClass)) return new MapTranslater<>(info); if (fromClass.isEnum()) return new EnumTranslater<>(info.type); if (SJUtils.isSystem(fromClass)) return new StringTranslater<>(fromClass); if (!sour.isClassKnown(fromClass)) throw new UnknownClassException(fromClass); return new ObjectTranslater<>(info.type); }
/** * Handles the work of converting values to JSON. * * @param nodeRef NodeRef * @param propertyName QName * @param key String * @param value Serializable * @return the JSON value */ @SuppressWarnings({ "unchecked", "rawtypes" }) protected Object propertyToJSON(final NodeRef nodeRef, final QName propertyName, final String key, final Serializable value) { if (value != null) { // Has a decorator has been registered for this property? if (propertyDecorators.containsKey(propertyName)) { JSONAware jsonAware = propertyDecorators.get(propertyName).decorate(propertyName, nodeRef, value); if (jsonAware != null) { return jsonAware; } } else { // Built-in data type processing if (value instanceof Date) { JSONObject dateObj = new JSONObject(); dateObj.put("value", JSONObject.escape(value.toString())); dateObj.put("iso8601", JSONObject.escape(ISO8601DateFormat.format((Date)value))); return dateObj; } else if (value instanceof List) { // Convert the List to a JSON list by recursively calling propertyToJSON List<Object> jsonList = new ArrayList<Object>(((List<Serializable>) value).size()); for (Serializable listItem : (List<Serializable>) value) { jsonList.add(propertyToJSON(nodeRef, propertyName, key, listItem)); } return jsonList; } else if (value instanceof Double) { return (Double.isInfinite((Double)value) || Double.isNaN((Double)value) ? null : value.toString()); } else if (value instanceof Float) { return (Float.isInfinite((Float)value) || Float.isNaN((Float)value) ? null : value.toString()); } else { return value.toString(); } } } return null; }
/** * @see org.alfresco.repo.jscript.app.PropertyDecorator#decorate(QName, org.alfresco.service.cmr.repository.NodeRef, java.io.Serializable) */ public JSONAware decorate(QName propertyName, NodeRef nodeRef, Serializable value) { return null; }
public JSONWebServiceResponse(JSONAware pJSONObject) { mJSONObject = pJSONObject; mStatusCode = HttpServletResponse.SC_OK; }
public JSONWebServiceResponse(JSONAware pJSONObject, int pStatusCode) { mJSONObject = pJSONObject; mStatusCode = pStatusCode; }
@Override public Map<String, RiskData> getIndicators( String entity ) { Map<String,RiskData> retValues = new HashMap<>(); repository = values.get( "repository" ); if( repository.startsWith( "https://github.com/") ) { repository = repository.substring( "https://github.com/".length() ); } repository = "https://api.github.com/repos/" + repository; // if (!repository.startsWith("https://") ) //&& !(repository.startsWith("http://"))) //to make possible that also entering the whole https address is allowed // repository="https://api.github.com/repos/"+repository; // try { // new URL(repository); // } catch (MalformedURLException e1) { // e1.printStackTrace(); // } try { String data = getDataWithLicense(); if (data != null && data.indexOf("WARNING ") == 0) { return retValues; } JSONAware json = parse(data); if (json!=null) parseJsonRepo( json, entity , retValues); //json.substring(json.indexOf( "{" ) ), entity , values); json = parsePaged("/contributors", 20, 0); //30 per page here if (json!=null) parseJsonContributors(json, entity , retValues); json = parse(getRepoData("/contents")); if (json!=null) parseJsonContent(json, entity , retValues); json = parse(getRepoData("/stats/participation")); if (json!=null) parseJsonParticipation(json, entity , retValues); json = parsePaged("/issues?state=all", 10, 1); //32 per page here, 1 = max 1 year old (creation) if (json!=null) parseJsonIssues(json, entity , retValues, 1); return retValues; } catch( Exception e ) { e.printStackTrace(); throw new RuntimeException( e ); //TODO: how are exceptions handled server-side?? } }
JSONAware decorate(QName propertyName, NodeRef nodeRef, Serializable value);