List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
From source file:com.googlesource.gerrit.plugins.oauth.GoogleOAuthService.java
License:Apache License
/** * @param token//from w w w . jav a 2s . c om * @return OpenID id token, when contained in id_token, null otherwise */ private static String lookupClaimedIdentity(OAuthToken token) { JsonElement idToken = OutputFormat.JSON.newGson().fromJson(token.getRaw(), JsonElement.class); if (idToken.isJsonObject()) { JsonObject idTokenObj = idToken.getAsJsonObject(); JsonElement idTokenElement = idTokenObj.get("id_token"); if (!idTokenElement.isJsonNull()) { String payload = decodePayload(idTokenElement.getAsString()); if (!Strings.isNullOrEmpty(payload)) { JsonElement openidIdToken = OutputFormat.JSON.newGson().fromJson(payload, JsonElement.class); if (openidIdToken.isJsonObject()) { JsonObject openidIdObj = openidIdToken.getAsJsonObject(); JsonElement openidIdElement = openidIdObj.get("openid_id"); if (!openidIdElement.isJsonNull()) { String openIdId = openidIdElement.getAsString(); log.debug("OAuth2: openid_id={}", openIdId); return openIdId; } log.debug("OAuth2: JWT doesn't contain openid_id element"); } } } } return null; }
From source file:com.googlesource.gerrit.plugins.oauth.KeycloakOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { JsonElement tokenJson = JSON.newGson().fromJson(token.getRaw(), JsonElement.class); JsonObject tokenObject = tokenJson.getAsJsonObject(); JsonElement id_token = tokenObject.get("id_token"); JsonElement claimJson = JSON.newGson().fromJson(parseJwt(id_token.getAsString()), JsonElement.class); JsonObject claimObject = claimJson.getAsJsonObject(); if (log.isDebugEnabled()) { log.debug("Claim object: {}", claimObject); }//from w w w . ja v a2s . c o m JsonElement usernameElement = claimObject.get("preferred_username"); JsonElement emailElement = claimObject.get("email"); JsonElement nameElement = claimObject.get("name"); if (usernameElement == null || usernameElement.isJsonNull()) { throw new IOException("Response doesn't contain preferred_username field"); } if (emailElement == null || emailElement.isJsonNull()) { throw new IOException("Response doesn't contain email field"); } if (nameElement == null || nameElement.isJsonNull()) { throw new IOException("Response doesn't contain name field"); } String username = usernameElement.getAsString(); String email = emailElement.getAsString(); String name = nameElement.getAsString(); return new OAuthUserInfo(KEYCLOAK_PROVIDER_PREFIX + username /*externalId*/, username /*username*/, email /*email*/, name /*displayName*/, null /*claimedIdentity*/); }
From source file:com.googlesource.gerrit.plugins.oauth.Office365OAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); request.addHeader("Accept", "*/*"); request.addHeader("Authorization", "Bearer " + token.getToken()); Response response = request.send();//from w ww . j ava2 s . co m if (response.getCode() != HttpServletResponse.SC_OK) { throw new IOException(String.format("Status %s (%s) for request %s", response.getCode(), response.getBody(), request.getUrl())); } JsonElement userJson = JSON.newGson().fromJson(response.getBody(), JsonElement.class); if (log.isDebugEnabled()) { log.debug("User info response: {}", response.getBody()); } if (userJson.isJsonObject()) { JsonObject jsonObject = userJson.getAsJsonObject(); JsonElement id = jsonObject.get("id"); if (id == null || id.isJsonNull()) { throw new IOException("Response doesn't contain id field"); } JsonElement email = jsonObject.get("mail"); JsonElement name = jsonObject.get("displayName"); String login = null; if (useEmailAsUsername && !email.isJsonNull()) { login = email.getAsString().split("@")[0]; } return new OAuthUserInfo(OFFICE365_PROVIDER_PREFIX + id.getAsString() /*externalId*/, login /*username*/, email == null || email.isJsonNull() ? null : email.getAsString() /*email*/, name == null || name.isJsonNull() ? null : name.getAsString() /*displayName*/, null); } throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); }
From source file:com.googlesource.gerrit.plugins.oauth.UoMGitLabOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); service.signRequest(t, request);/*from w ww . j av a 2 s. c o m*/ Response response = request.send(); if (response.getCode() != HttpServletResponse.SC_OK) { throw new IOException(String.format("Status %s (%s) for request %s", response.getCode(), response.getBody(), request.getUrl())); } JsonElement userJson = OutputFormat.JSON.newGson().fromJson(response.getBody(), JsonElement.class); if (log.isDebugEnabled()) { log.debug("User info response: {}", response.getBody()); } if (userJson.isJsonObject()) { JsonObject jsonObject = userJson.getAsJsonObject(); JsonElement id = jsonObject.get("id"); if (id == null || id.isJsonNull()) { throw new IOException(String.format("Response doesn't contain id field")); } JsonElement email = jsonObject.get("email"); JsonElement name = jsonObject.get("name"); JsonElement login = jsonObject.get("username"); return new OAuthUserInfo(id.getAsString(), login == null || login.isJsonNull() ? null : login.getAsString(), email == null || email.isJsonNull() ? null : email.getAsString(), name == null || name.isJsonNull() ? null : name.getAsString(), null); } else { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } }
From source file:com.gst.infrastructure.campaigns.sms.serialization.SmsCampaignValidator.java
License:Apache License
public void validatePreviewMessage(String json) { if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }//from ww w . jav a 2s.co m final Type typeOfMap = new TypeToken<Map<String, Object>>() { }.getType(); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, SmsCampaignValidator.PREVIEW_REQUEST_DATA_PARAMETERS); final JsonElement element = this.fromApiJsonHelper.parse(json); final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) .resource(SmsCampaignValidator.RESOURCE_NAME); final JsonElement paramValueJsonObject = this.fromApiJsonHelper .extractJsonObjectNamed(SmsCampaignValidator.paramValue, element); baseDataValidator.reset().parameter(SmsCampaignValidator.paramValue).value(paramValueJsonObject).notBlank(); if (!paramValueJsonObject.isJsonNull() && paramValueJsonObject.isJsonObject()) { for (Map.Entry<String, JsonElement> entry : paramValueJsonObject.getAsJsonObject().entrySet()) { final JsonElement inner = entry.getValue(); baseDataValidator.reset().parameter(entry.getKey()).value(inner).notBlank(); } } final String message = this.fromApiJsonHelper.extractStringNamed(SmsCampaignValidator.message, element); baseDataValidator.reset().parameter(SmsCampaignValidator.message).value(message).notBlank() .notExceedingLengthOf(480); throwExceptionIfValidationWarningsExist(dataValidationErrors); }
From source file:com.guodong.sun.guodong.entity.picture.PictureContentDeserializer.java
License:Apache License
private JsonObject jsonObjectOrEmpty(JsonElement jsonElement) { return jsonElement == null ? null : jsonElement.isJsonNull() ? null : jsonElement.getAsJsonObject(); }
From source file:com.guodong.sun.guodong.entity.picture.PictureContentDeserializer.java
License:Apache License
private int intOrEmpty(JsonElement jsonElement) { return jsonElement == null ? 0 : jsonElement.isJsonNull() ? 0 : jsonElement.getAsInt(); }
From source file:com.haulmont.cuba.core.app.importexport.EntityImportViewBuilder.java
License:Apache License
protected EntityImportView buildFromJsonObject(JsonObject jsonObject, MetaClass metaClass) { EntityImportView view = new EntityImportView(metaClass.getJavaClass()); for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String propertyName = entry.getKey(); MetaProperty metaProperty = metaClass.getProperty(propertyName); if (metaProperty != null) { Range propertyRange = metaProperty.getRange(); Class<?> propertyType = metaProperty.getJavaType(); if (propertyRange.isDatatype() || propertyRange.isEnum()) { if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) view.addLocalProperty(propertyName); } else if (propertyRange.isClass()) { if (Entity.class.isAssignableFrom(propertyType)) { if (metadataTools.isEmbedded(metaProperty)) { MetaClass propertyMetaClass = metadata.getClass(propertyType); JsonElement propertyJsonObject = entry.getValue(); if (!propertyJsonObject.isJsonObject()) { throw new RuntimeException("JsonObject was expected for property " + propertyName); }/*from w w w . j a va 2s .c om*/ if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) { EntityImportView propertyImportView = buildFromJsonObject( propertyJsonObject.getAsJsonObject(), propertyMetaClass); view.addEmbeddedProperty(propertyName, propertyImportView); } } else { MetaClass propertyMetaClass = metadata.getClass(propertyType); if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) { JsonElement propertyJsonObject = entry.getValue(); if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) { if (propertyJsonObject.isJsonNull()) { //in case of null we must add such import behavior to update the reference with null value later if (metaProperty.getRange() .getCardinality() == Range.Cardinality.MANY_TO_ONE) { view.addManyToOneProperty(propertyName, ReferenceImportBehaviour.IGNORE_MISSING); } else { view.addOneToOneProperty(propertyName, ReferenceImportBehaviour.IGNORE_MISSING); } } else { if (!propertyJsonObject.isJsonObject()) { throw new RuntimeException( "JsonObject was expected for property " + propertyName); } EntityImportView propertyImportView = buildFromJsonObject( propertyJsonObject.getAsJsonObject(), propertyMetaClass); if (metaProperty.getRange() .getCardinality() == Range.Cardinality.MANY_TO_ONE) { view.addManyToOneProperty(propertyName, propertyImportView); } else { view.addOneToOneProperty(propertyName, propertyImportView); } } } } else { if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_ONE) { view.addManyToOneProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING); } else { view.addOneToOneProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING); } } } } else if (Collection.class.isAssignableFrom(propertyType)) { MetaClass propertyMetaClass = metaProperty.getRange().asClass(); switch (metaProperty.getRange().getCardinality()) { case MANY_TO_MANY: if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) view.addManyToManyProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING, CollectionImportPolicy.REMOVE_ABSENT_ITEMS); break; case ONE_TO_MANY: if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) { JsonElement compositionJsonArray = entry.getValue(); if (!compositionJsonArray.isJsonArray()) { throw new RuntimeException( "JsonArray was expected for property " + propertyName); } EntityImportView propertyImportView = buildFromJsonArray( compositionJsonArray.getAsJsonArray(), propertyMetaClass); if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) view.addOneToManyProperty(propertyName, propertyImportView, CollectionImportPolicy.REMOVE_ABSENT_ITEMS); } break; } } } } } return view; }
From source file:com.haulmont.restapi.common.RestParseUtils.java
License:Apache License
public Map<String, String> parseParamsJson(String paramsJson) { Map<String, String> result = new LinkedHashMap<>(); if (Strings.isNullOrEmpty(paramsJson)) return result; JsonParser jsonParser = new JsonParser(); JsonObject jsonObject = jsonParser.parse(paramsJson).getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String paramName = entry.getKey(); JsonElement paramValue = entry.getValue(); if (paramValue.isJsonNull()) { result.put(paramName, null); } else if (paramValue.isJsonPrimitive()) { result.put(paramName, paramValue.getAsString()); } else {// w w w . j av a 2s. c om result.put(paramName, paramValue.toString()); } } return result; }
From source file:com.hbm.devices.jet.JetPeer.java
License:Open Source License
@Override public void call(String path, JsonElement arguments, ResponseCallback responseCallback, int responseTimeoutMs) { if ((path == null) || (path.length() == 0)) { throw new IllegalArgumentException("path"); }/*from www.j a va2s . c o m*/ synchronized (methodCallbacks) { if (methodCallbacks.containsKey(path)) { throw new IllegalArgumentException("Don't call call() on a method you own!"); } } JsonObject parameters = new JsonObject(); parameters.addProperty("path", path); if (!arguments.isJsonNull()) { parameters.add("args", arguments); } parameters.addProperty("timeout", responseTimeoutMs / 1000.0); JetMethod call = new JetMethod(JetMethod.CALL, parameters, responseCallback); this.executeMethod(call, responseTimeoutMs); }