private static JSONValue toJSON(SerValue serValue) { if (serValue.isString()) { return new JSONString(serValue.asString()); } else if (serValue.isReal()) { return new JSONNumber(serValue.asReal()); } else if (serValue.isArray()) { SerArray serArray = serValue.asArray(); JSONArray jsonArray = new JSONArray(); for (int i = 0; i != serArray.size(); ++i) { jsonArray.set(i, toJSON(serArray.get(i))); } return jsonArray; } else if (serValue.isObject()) { SerObject serObject = serValue.asObject(); JSONObject jsonObject = new JSONObject(); for (String key : serObject.keySet()) { jsonObject.put(key, toJSON(serObject.get(key))); } return jsonObject; } else { throw new IllegalArgumentException(); } }
protected JSONObject writeStringRepeated( JSONObject jsonObject, String fieldLabel, Collection<String> fieldStringRepeated) { if (jsonObject != null && fieldLabel != null && fieldStringRepeated != null && !fieldStringRepeated.isEmpty()) { JSONArray fieldJSONArray = new JSONArray(); int i = 0; for (String fieldString : fieldStringRepeated) { fieldJSONArray.set(i++, new JSONString(fieldString)); } jsonObject.put(fieldLabel, fieldJSONArray); } return jsonObject; }
protected JSONObject writeBooleanRepeated( JSONObject jsonObject, String fieldLabel, Collection<Boolean> fieldBooleanRepeated) { if (jsonObject != null && fieldLabel != null && fieldBooleanRepeated != null && !fieldBooleanRepeated.isEmpty()) { JSONArray fieldJSONArray = new JSONArray(); int i = 0; for (Boolean fieldBoolean : fieldBooleanRepeated) { fieldJSONArray.set(i++, JSONBoolean.getInstance(fieldBoolean.booleanValue())); } jsonObject.put(fieldLabel, fieldJSONArray); } return jsonObject; }
protected JSONObject writeDoubleRepeated( JSONObject jsonObject, String fieldLabel, Collection<Double> fieldDoubleRepeated) { if (jsonObject != null && fieldLabel != null && fieldDoubleRepeated != null && !fieldDoubleRepeated.isEmpty()) { JSONArray fieldJSONArray = new JSONArray(); int i = 0; for (Double fieldDouble : fieldDoubleRepeated) { fieldJSONArray.set(i++, new JSONNumber(fieldDouble.doubleValue())); } jsonObject.put(fieldLabel, fieldJSONArray); } return jsonObject; }
protected JSONObject writeFloatRepeated( JSONObject jsonObject, String fieldLabel, Collection<Float> fieldFloatRepeated) { if (jsonObject != null && fieldLabel != null && fieldFloatRepeated != null && !fieldFloatRepeated.isEmpty()) { JSONArray fieldJSONArray = new JSONArray(); int i = 0; for (Float fieldFloat : fieldFloatRepeated) { fieldJSONArray.set(i++, new JSONNumber(fieldFloat.floatValue())); } jsonObject.put(fieldLabel, fieldJSONArray); } return jsonObject; }
protected JSONObject writeIntegerRepeated( JSONObject jsonObject, String fieldLabel, Collection<Integer> fieldIntegerRepeated) { if (jsonObject != null && fieldLabel != null && fieldIntegerRepeated != null && !fieldIntegerRepeated.isEmpty()) { JSONArray fieldJSONArray = new JSONArray(); int i = 0; for (Integer fieldInteger : fieldIntegerRepeated) { fieldJSONArray.set(i++, new JSONNumber(fieldInteger.intValue())); } jsonObject.put(fieldLabel, fieldJSONArray); } return jsonObject; }
protected JSONObject writeStreamRepeated( JSONObject jsonObject, String fieldLabel, Collection<JsonStream> fieldStreamRepeated) throws IOException { if (jsonObject != null && fieldLabel != null && fieldStreamRepeated != null && !fieldStreamRepeated.isEmpty()) { JSONArray fieldJSONArray = new JSONArray(); int i = 0; for (JsonStream fieldStream : fieldStreamRepeated) { if (fieldStream instanceof GWTJsonStream) { GWTJsonStream fieldGWTJsonStream = (GWTJsonStream) fieldStream; fieldJSONArray.set(i++, fieldGWTJsonStream.getJSONObject()); } else { throw new IOException("Failed to write repeated stream field " + fieldLabel); } } jsonObject.put(fieldLabel, fieldJSONArray); } return jsonObject; }
/** * Prepares a JSON object * * @return JSONObject the whole query */ private JSONObject prepareJSONObject() { JSONNumber selectedVoId = new JSONNumber(voId); // attributes object !! login !! JSONObject attrs = new JSONObject(); if (!namespace.isEmpty() && !namespace.equals("mu")) { attrs.put( "urn:perun:user:attribute-def:def:login-namespace:" + namespace, new JSONString(login)); } attrs.put("urn:perun:member:attribute-def:def:mail", new JSONString(email)); // create new form of candidate JSONObject newCandidate = new JSONObject(); newCandidate.put("attributes", attrs); newCandidate.put("additionalUserExtSources", null); newCandidate.put("id", null); newCandidate.put("firstName", new JSONString("")); newCandidate.put("lastName", new JSONString(name)); newCandidate.put("middleName", null); newCandidate.put("titleAfter", null); newCandidate.put("titleBefore", null); if (!certDN.isEmpty() && !caCertDN.isEmpty()) { JSONObject userExtSource = new JSONObject(); userExtSource.put("id", null); userExtSource.put("login", new JSONString(certDN)); // we do not trust manually added certs userExtSource.put("loa", new JSONNumber(0)); // create ext source JSONObject extSource = new JSONObject(); extSource.put("id", null); extSource.put("name", new JSONString(caCertDN)); extSource.put("type", new JSONString("cz.metacentrum.perun.core.impl.ExtSourceX509")); userExtSource.put("extSource", extSource); newCandidate.put("userExtSource", userExtSource); } else { newCandidate.put("userExtSource", null); } JSONArray array = new JSONArray(); for (int i = 0; i < users.size(); i++) { JSONObject user = new JSONObject(users.get(i)); JSONValue id = user.get("id"); JSONValue firstName = user.get("firstName"); JSONValue lastName = user.get("lastName"); JSONValue middleName = user.get("middleName"); JSONValue titleAfter = user.get("titleAfter"); JSONValue titleBefore = user.get("titleBefore"); JSONValue service = user.get("serviceUser"); JSONValue sponsored = user.get("sponsoredUser"); JSONObject newUser = new JSONObject(); newUser.put("id", id); newUser.put("firstName", firstName); newUser.put("lastName", lastName); newUser.put("middleName", middleName); newUser.put("titleAfter", titleAfter); newUser.put("titleBefore", titleBefore); newUser.put("serviceUser", service); newUser.put("sponsoredUser", sponsored); array.set(i, newUser); } // create whole JSON query JSONObject jsonQuery = new JSONObject(); jsonQuery.put("vo", selectedVoId); jsonQuery.put("candidate", newCandidate); jsonQuery.put("specificUserOwners", array); jsonQuery.put("specificUserType", new JSONString(specificUserType)); return jsonQuery; }