List of usage examples for org.json JSONObject get
public Object get(String key) throws JSONException
From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.JSONTypesExtractorImpl.java
/** * Given either a {@link JSONArray} or a {@link JSONObject}, a key ending and a prefix to add, * it looks for keys at any object inside the provided JSON ending with the key ending and adds * the given prefix to that ending.//from ww w. j av a 2 s . co m * This method calls itself recursively to traverse inner parts of {@link JSONObject}s. * @param jsonRoot The root {@link JSONObject} or {@link JSONArray}. If an object of any other type is provided, * the method just does nothing. * @param oldKeyEnding the ending to look for. * @param prefixToAdd the prefix to add to that ending. */ private void addPrefixToJSONKeysEndingsRecursive(Object jsonRoot, String oldKeyEnding, String prefixToAdd) { if (jsonRoot instanceof JSONObject) { JSONObject jsonObject = (JSONObject) jsonRoot; SortedSet<String> keys = ImmutableSortedSet.<String>naturalOrder().addAll(jsonObject.keySet()).build(); for (String key : keys) { Object value = jsonObject.get(key); addPrefixToJSONKeysEndingsRecursive(value, oldKeyEnding, prefixToAdd); if (key.endsWith(oldKeyEnding)) { String newKey = key.replaceAll(Pattern.quote(oldKeyEnding) + "$", prefixToAdd + oldKeyEnding); jsonObject.remove(key); jsonObject.put(newKey, value); } } } else if (jsonRoot instanceof JSONArray) { JSONArray jsonArray = (JSONArray) jsonRoot; for (int i = 0; i < jsonArray.length(); i++) { Object value = jsonArray.get(i); addPrefixToJSONKeysEndingsRecursive(value, oldKeyEnding, prefixToAdd); } } else { return; } }
From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.JSONTypesExtractorImpl.java
/** * This method adds additional quotes to any String value inside a JSON. * This helps to distinguish whether values come from strings or other * primitive types when the JSON is converted to XML. * @param jsonRoot//from ww w .j a v a2 s. co m */ private void quoteStringsAtJSON(Object jsonRoot) { if (jsonRoot instanceof JSONObject) { JSONObject jsonObject = (JSONObject) jsonRoot; ImmutableSet<String> jsonObjectKeys = ImmutableSet.copyOf(jsonObject.keys()); for (String key : jsonObjectKeys) { Object value = jsonObject.get(key); if (value instanceof String) { String valueStr = (String) value; jsonObject.put(key, "\"" + valueStr + "\""); } else if (value instanceof JSONObject || value instanceof JSONArray) { quoteStringsAtJSON(value); } } } else if (jsonRoot instanceof JSONArray) { JSONArray jsonArray = (JSONArray) jsonRoot; for (int i = 0; i < jsonArray.length(); i++) { Object value = jsonArray.get(i); if (value instanceof String) { String valueStr = (String) value; jsonArray.put(i, "\"" + valueStr + "\""); } else if (value instanceof JSONObject || value instanceof JSONArray) { quoteStringsAtJSON(value); } } } else { return; } }
From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.JSONTypesExtractorImpl.java
/** * This method looks recursively for arrays and encapsulates each of them into another array. * So, anything like this:<br/>// w ww. j a v a2 s . c om * <code> * {"myKey":["stuff1","stuff2"]} * </code><br/> * would become this:<br/> * <code> * {"myKey":[["stuff1","stuff2"]]} * </code><br/> * We do this strange preprocessing because structures like the first example are * converted to XML producing this result:<br/> * <code> * <myKey>stuff1</myKey><myKey>stuff2</myKey> * </code><br/> * Which makes impossible to distinguish single-element arrays from an element * outside any array, because, both this: * <code> * {"singleElement":["stuff"]} * </code><br/> * and this:<br/> * <code> * {"singleElement":"stuff"} * </code><br/> * Would be converted to:<br/> * <code> * <singleElement>stuff</singleElement> * </code><br/> * By doing this, we allow distingushing a single-element array from an non-array element, because * the first one would be converted to:<br/> * <code> * <singleElement><array>stuff</array></singleElement> * * @param jsonRoot The {@link JSONObject} or {@link JSONArray} which is the root of our JSON document. */ private void encapsulateArraysAtJSONRecursive(Object jsonRoot) { if (jsonRoot instanceof JSONObject) { JSONObject jsonObject = (JSONObject) jsonRoot; Set<String> keys = ImmutableSet.copyOf(jsonObject.keySet()); for (String key : keys) { Object value = jsonObject.get(key); encapsulateArraysAtJSONRecursive(value); if (value instanceof JSONArray) { JSONArray encapsulatingJsonArray = new JSONArray(); encapsulatingJsonArray.put(0, value); jsonObject.put(key, encapsulatingJsonArray); } } } else if (jsonRoot instanceof JSONArray) { JSONArray jsonArray = (JSONArray) jsonRoot; for (int i = 0; i < jsonArray.length(); i++) { Object value = jsonArray.get(i); encapsulateArraysAtJSONRecursive(value); if (value instanceof JSONArray) { JSONObject encapsulatingJsonObject = new JSONObject(); encapsulatingJsonObject.put(XSDInferenceConfiguration.ARRAY_ELEMENT_NAME, value); jsonArray.put(i, encapsulatingJsonObject); } } } else { return; } }
From source file:org.wso2.carbon.identity.agent.userstore.WebSocketClientHandler.java
/** * Process authentication request// w w w. ja v a 2s . c o m * @param channel netty channel * @param requestObj json request data object * @throws UserStoreException */ private void processAuthenticationRequest(Channel channel, JSONObject requestObj) throws UserStoreException { JSONObject requestData = requestObj.getJSONObject(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA); UserStoreManager userStoreManager = UserStoreManagerBuilder.getUserStoreManager(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Starting to authenticate user " + requestData.get(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_USER_NAME)); } boolean isAuthenticated = userStoreManager.doAuthenticate( requestData.getString(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_USER_NAME), requestData.getString(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_USER_PASSWORD)); String authenticationResult = UserAgentConstants.UM_OPERATION_AUTHENTICATE_RESULT_FAIL; if (LOGGER.isDebugEnabled()) { LOGGER.debug("Authentication completed. User: " + requestData.get(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_USER_NAME) + " result: " + isAuthenticated); } if (isAuthenticated) { authenticationResult = UserAgentConstants.UM_OPERATION_AUTHENTICATE_RESULT_SUCCESS; } writeResponse(channel, (String) requestObj.get(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA_CORRELATION_ID), authenticationResult); }
From source file:org.wso2.carbon.identity.agent.userstore.WebSocketClientHandler.java
/** * Process Get claims request//from w ww . j av a2s. c om * @param channel netty channel * @param requestObj json request data object * @throws UserStoreException */ private void processGetClaimsRequest(Channel channel, JSONObject requestObj) throws UserStoreException { JSONObject requestData = requestObj.getJSONObject(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Starting to get claims for user: " + requestData.get(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_USER_NAME)); } String username = (String) requestData.get(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_USER_NAME); String claims = (String) requestData.get(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_CLAIMS); String[] claimArray = claims.split(CommonConstants.ATTRIBUTE_LIST_SEPERATOR); UserStoreManager userStoreManager = UserStoreManagerBuilder.getUserStoreManager(); Map<String, String> propertyMap = userStoreManager.getUserClaimValues(username, claimArray); JSONObject returnObject = new JSONObject(propertyMap); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Claims retrieval completed. User: " + requestData.get(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_USER_NAME) + " claims: " + propertyMap.toString()); } writeResponse(channel, (String) requestObj.get(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA_CORRELATION_ID), returnObject.toString()); }
From source file:org.wso2.carbon.identity.agent.userstore.WebSocketClientHandler.java
/** * Process get user roles request/*from w ww. j a va 2 s.c o m*/ * @param channel netty channel * @param requestObj json request data object * @throws UserStoreException */ private void processGetUserRolesRequest(Channel channel, JSONObject requestObj) throws UserStoreException { JSONObject requestData = requestObj.getJSONObject(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Starting to get user roles for user: " + requestData.get(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_USER_NAME)); } String username = (String) requestData.get(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_USER_NAME); UserStoreManager userStoreManager = UserStoreManagerBuilder.getUserStoreManager(); String[] roles = userStoreManager.doGetExternalRoleListOfUser(username); JSONObject jsonObject = new JSONObject(); JSONArray usernameArray = new JSONArray(roles); jsonObject.put("groups", usernameArray); if (LOGGER.isDebugEnabled()) { LOGGER.debug("User roles retrieval completed. User: " + requestData.get(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_USER_NAME) + " roles: " + Arrays.toString(roles)); } writeResponse(channel, (String) requestObj.get(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA_CORRELATION_ID), jsonObject.toString()); }
From source file:org.wso2.carbon.identity.agent.userstore.WebSocketClientHandler.java
/** * Process get roles request//from w ww . ja va2s .c om * @param channel netty channel * @param requestObj json request data object * @throws UserStoreException */ private void processGetRolesRequest(Channel channel, JSONObject requestObj) throws UserStoreException { JSONObject requestData = requestObj.getJSONObject(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Starting to get roles."); } int limit = requestData.getInt(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_GET_ROLE_LIMIT); if (limit == 0) { limit = CommonConstants.MAX_USER_LIST; } UserStoreManager userStoreManager = UserStoreManagerBuilder.getUserStoreManager(); String[] roleNames = userStoreManager.doGetRoleNames("*", limit); JSONObject returnObject = new JSONObject(); JSONArray usernameArray = new JSONArray(roleNames); returnObject.put("groups", usernameArray); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Roles retrieval completed."); } writeResponse(channel, (String) requestObj.get(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA_CORRELATION_ID), returnObject.toString()); }
From source file:org.wso2.carbon.identity.agent.userstore.WebSocketClientHandler.java
/** * Process get roles request//from ww w.ja v a 2s. c om * @param channel netty channel * @param requestObj json request data object * @throws UserStoreException */ private void processGetUsersListRequest(Channel channel, JSONObject requestObj) throws UserStoreException { JSONObject requestData = requestObj.getJSONObject(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Starting to get users"); } int limit = requestData.getInt(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_GET_USER_LIMIT); String filter = (String) requestData.get(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_GET_USER_FILTER); if (limit == 0) { limit = CommonConstants.MAX_USER_LIST; } UserStoreManager userStoreManager = UserStoreManagerBuilder.getUserStoreManager(); String[] roleNames = userStoreManager.doListUsers(filter, limit); JSONObject returnObject = new JSONObject(); JSONArray usernameArray = new JSONArray(roleNames); returnObject.put("usernames", usernameArray); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Users list retrieval completed."); } writeResponse(channel, (String) requestObj.get(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA_CORRELATION_ID), returnObject.toString()); }
From source file:org.wso2.carbon.identity.agent.userstore.WebSocketClientHandler.java
/** * Process user operation request/* w ww. ja v a2 s . c o m*/ * @param channel netty channel * @param requestObj json request data object * @throws UserStoreException */ private void processUserOperationRequest(Channel channel, JSONObject requestObj) throws UserStoreException { String type = (String) requestObj.get(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA_TYPE); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Message receive for operation " + type); } switch (type) { case UserStoreConstants.UM_OPERATION_TYPE_AUTHENTICATE: processAuthenticationRequest(channel, requestObj); break; case UserStoreConstants.UM_OPERATION_TYPE_GET_CLAIMS: processGetClaimsRequest(channel, requestObj); break; case UserStoreConstants.UM_OPERATION_TYPE_GET_USER_ROLES: processGetUserRolesRequest(channel, requestObj); break; case UserStoreConstants.UM_OPERATION_TYPE_GET_ROLES: processGetRolesRequest(channel, requestObj); break; case UserStoreConstants.UM_OPERATION_TYPE_GET_USER_LIST: processGetUsersListRequest(channel, requestObj); break; case UserStoreConstants.UM_OPERATION_TYPE_ERROR: logError(requestObj); if (!isDisconnected) { client.setShutdownFlag(true); System.exit(0); } break; default: LOGGER.error("Invalid user operation request type : " + type + " received."); break; } }
From source file:org.wso2.carbon.identity.agent.userstore.WebSocketClientHandler.java
private void logError(JSONObject requestObj) { JSONObject requestData = (JSONObject) requestObj.get(UserStoreConstants.UM_JSON_ELEMENT_REQUEST_DATA); String message = (String) requestData.get(UserAgentConstants.UM_JSON_ELEMENT_REQUEST_DATA_MESSAGE); LOGGER.error(message);/*from w ww .ja v a 2 s .com*/ }