List of usage examples for com.google.gson JsonElement isJsonObject
public boolean isJsonObject()
From source file:com.googlesource.gerrit.plugins.oauth.GoogleOAuthService.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 w w . j a v a2 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 (userJson.isJsonObject()) { JsonObject jsonObject = userJson.getAsJsonObject(); JsonElement id = jsonObject.get("id"); if (id.isJsonNull()) { throw new IOException(String.format("Response doesn't contain id field")); } JsonElement email = jsonObject.get("email"); JsonElement name = jsonObject.get("name"); String claimedIdentifier = null; if (linkToExistingOpenIDAccounts) { claimedIdentifier = lookupClaimedIdentity(token); } return new OAuthUserInfo(id.getAsString() /*externalId*/, null /*username*/, email.isJsonNull() ? null : email.getAsString() /*email*/, name.isJsonNull() ? null : name.getAsString() /*displayName*/, claimedIdentifier /*claimedIdentity*/); } else { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } }
From source file:com.googlesource.gerrit.plugins.oauth.GoogleOAuthService.java
License:Apache License
/** * @param token/*from w w w .j a v a 2 s. c o m*/ * @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.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 www. j av a2 s . c om 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);// w w w.java 2s. com 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.accounting.journalentry.serialization.JournalEntryCommandFromApiJsonDeserializer.java
License:Apache License
@Override public JournalEntryCommand commandFromApiJson(final String json) { if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }/* w w w . j a va2s . c o m*/ final Type typeOfMap = new TypeToken<Map<String, Object>>() { }.getType(); final Set<String> supportedParameters = JournalEntryJsonInputParams.getAllValues(); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); final JsonElement element = this.fromApiJsonHelper.parse(json); final Long officeId = this.fromApiJsonHelper .extractLongNamed(JournalEntryJsonInputParams.OFFICE_ID.getValue(), element); final String currencyCode = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.CURRENCY_CODE.getValue(), element); final String comments = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.COMMENTS.getValue(), element); final LocalDate transactionDate = this.fromApiJsonHelper .extractLocalDateNamed(JournalEntryJsonInputParams.TRANSACTION_DATE.getValue(), element); final String referenceNumber = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.REFERENCE_NUMBER.getValue(), element); final Long accountingRuleId = this.fromApiJsonHelper .extractLongNamed(JournalEntryJsonInputParams.ACCOUNTING_RULE.getValue(), element); final JsonObject topLevelJsonElement = element.getAsJsonObject(); final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement); final BigDecimal amount = this.fromApiJsonHelper .extractBigDecimalNamed(JournalEntryJsonInputParams.AMOUNT.getValue(), element, locale); final Long paymentTypeId = this.fromApiJsonHelper .extractLongNamed(JournalEntryJsonInputParams.PAYMENT_TYPE_ID.getValue(), element); final String accountNumber = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.ACCOUNT_NUMBER.getValue(), element); final String checkNumber = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.CHECK_NUMBER.getValue(), element); final String receiptNumber = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.RECEIPT_NUMBER.getValue(), element); final String bankNumber = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.BANK_NUMBER.getValue(), element); final String routingCode = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.ROUTING_CODE.getValue(), element); SingleDebitOrCreditEntryCommand[] credits = null; SingleDebitOrCreditEntryCommand[] debits = null; if (element.isJsonObject()) { if (topLevelJsonElement.has(JournalEntryJsonInputParams.CREDITS.getValue()) && topLevelJsonElement.get(JournalEntryJsonInputParams.CREDITS.getValue()).isJsonArray()) { credits = populateCreditsOrDebitsArray(topLevelJsonElement, locale, credits, JournalEntryJsonInputParams.CREDITS.getValue()); } if (topLevelJsonElement.has(JournalEntryJsonInputParams.DEBITS.getValue()) && topLevelJsonElement.get(JournalEntryJsonInputParams.DEBITS.getValue()).isJsonArray()) { debits = populateCreditsOrDebitsArray(topLevelJsonElement, locale, debits, JournalEntryJsonInputParams.DEBITS.getValue()); } } return new JournalEntryCommand(officeId, currencyCode, transactionDate, comments, credits, debits, referenceNumber, accountingRuleId, amount, paymentTypeId, accountNumber, checkNumber, receiptNumber, bankNumber, routingCode); }
From source file:com.gst.batch.service.ResolutionHelper.java
License:Apache License
private JsonElement resolveDependentVariables(final Entry<String, JsonElement> entryElement, final JsonModel responseJsonModel) { JsonElement value = null;/*from www . ja va 2 s.c om*/ final JsonElement element = entryElement.getValue(); if (element.isJsonObject()) { final JsonObject jsObject = element.getAsJsonObject(); value = processJsonObject(jsObject, responseJsonModel); } else if (element.isJsonArray()) { final JsonArray jsElementArray = element.getAsJsonArray(); value = processJsonArray(jsElementArray, responseJsonModel); } else { value = resolveDependentVariable(element, responseJsonModel); } return value; }
From source file:com.gst.batch.service.ResolutionHelper.java
License:Apache License
private JsonArray processJsonArray(final JsonArray elementArray, final JsonModel responseJsonModel) { JsonArray valueArr = new JsonArray(); for (JsonElement element : elementArray) { if (element.isJsonObject()) { final JsonObject jsObject = element.getAsJsonObject(); valueArr.add(processJsonObject(jsObject, responseJsonModel)); }//from w w w.j a v a2s. com } return valueArr; }
From source file:com.gst.infrastructure.campaigns.sms.serialization.SmsCampaignValidator.java
License:Apache License
public void validateCreate(String json) { if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }//from w ww. ja v a 2 s .co m final Type typeOfMap = new TypeToken<Map<String, Object>>() { }.getType(); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, SmsCampaignValidator.supportedParams); final JsonElement element = this.fromApiJsonHelper.parse(json); final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) .resource(SmsCampaignValidator.RESOURCE_NAME); final String campaignName = this.fromApiJsonHelper.extractStringNamed(SmsCampaignValidator.campaignName, element); baseDataValidator.reset().parameter(SmsCampaignValidator.campaignName).value(campaignName).notBlank() .notExceedingLengthOf(100); final Long campaignType = this.fromApiJsonHelper.extractLongNamed(SmsCampaignValidator.campaignType, element); baseDataValidator.reset().parameter(SmsCampaignValidator.campaignType).value(campaignType).notNull() .integerGreaterThanZero(); final Long triggerType = this.fromApiJsonHelper.extractLongNamed(SmsCampaignValidator.triggerType, element); baseDataValidator.reset().parameter(SmsCampaignValidator.triggerType).value(triggerType).notNull() .integerGreaterThanZero(); if (triggerType.intValue() == SmsCampaignTriggerType.SCHEDULE.getValue()) { final Integer frequencyParam = this.fromApiJsonHelper .extractIntegerWithLocaleNamed(SmsCampaignValidator.frequencyParamName, element); baseDataValidator.reset().parameter(SmsCampaignValidator.frequencyParamName).value(frequencyParam) .notNull().integerGreaterThanZero(); final String intervalParam = this.fromApiJsonHelper .extractStringNamed(SmsCampaignValidator.intervalParamName, element); baseDataValidator.reset().parameter(SmsCampaignValidator.intervalParamName).value(intervalParam) .notBlank(); if (frequencyParam != null && frequencyParam.equals(CalendarFrequencyType.WEEKLY.getValue())) { final String repeatsOnDayParam = this.fromApiJsonHelper .extractStringNamed(SmsCampaignValidator.repeatsOnDayParamName, element); baseDataValidator.reset().parameter(SmsCampaignValidator.repeatsOnDayParamName) .value(repeatsOnDayParam).notBlank(); } final String recurrenceStartDate = this.fromApiJsonHelper .extractStringNamed(SmsCampaignValidator.recurrenceStartDate, element); baseDataValidator.reset().parameter(SmsCampaignValidator.recurrenceStartDate).value(recurrenceStartDate) .notBlank(); } final Long runReportId = this.fromApiJsonHelper.extractLongNamed(SmsCampaignValidator.runReportId, element); baseDataValidator.reset().parameter(SmsCampaignValidator.runReportId).value(runReportId).notNull() .integerGreaterThanZero(); final String message = this.fromApiJsonHelper.extractStringNamed(SmsCampaignValidator.message, element); baseDataValidator.reset().parameter(SmsCampaignValidator.message).value(message).notBlank() .notExceedingLengthOf(480); final JsonElement paramValueJsonObject = this.fromApiJsonHelper .extractJsonObjectNamed(SmsCampaignValidator.paramValue, element); if (triggerType.intValue() != SmsCampaignTriggerType.TRIGGERED.getValue()) { baseDataValidator.reset().parameter(SmsCampaignValidator.paramValue).value(paramValueJsonObject) .notBlank(); if (paramValueJsonObject != null && paramValueJsonObject.isJsonObject()) { for (Map.Entry<String, JsonElement> entry : paramValueJsonObject.getAsJsonObject().entrySet()) { final JsonElement inner = entry.getValue(); baseDataValidator.reset().parameter(entry.getKey()).value(inner).notBlank(); } } } if (this.fromApiJsonHelper.parameterExists(SmsCampaignValidator.submittedOnDateParamName, element)) { final LocalDate submittedOnDate = this.fromApiJsonHelper .extractLocalDateNamed(SmsCampaignValidator.submittedOnDateParamName, element); baseDataValidator.reset().parameter(SmsCampaignValidator.submittedOnDateParamName) .value(submittedOnDate).notNull(); } throwExceptionIfValidationWarningsExist(dataValidationErrors); }
From source file:com.gst.infrastructure.campaigns.sms.serialization.SmsCampaignValidator.java
License:Apache License
public void validateForUpdate(String json) { if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }/* w w w . j av a 2 s .co m*/ final Type typeOfMap = new TypeToken<Map<String, Object>>() { }.getType(); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, SmsCampaignValidator.supportedParamsForUpdate); final JsonElement element = this.fromApiJsonHelper.parse(json); final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) .resource(SmsCampaignValidator.RESOURCE_NAME); final String campaignName = this.fromApiJsonHelper.extractStringNamed(SmsCampaignValidator.campaignName, element); baseDataValidator.reset().parameter(SmsCampaignValidator.campaignName).value(campaignName).notBlank() .notExceedingLengthOf(100); final Long campaignType = this.fromApiJsonHelper.extractLongNamed(SmsCampaignValidator.campaignType, element); baseDataValidator.reset().parameter(SmsCampaignValidator.campaignType).value(campaignType).notNull() .integerGreaterThanZero(); final Long triggerType = this.fromApiJsonHelper.extractLongNamed(SmsCampaignValidator.triggerType, element); baseDataValidator.reset().parameter(SmsCampaignValidator.triggerType).value(triggerType).notNull() .integerGreaterThanZero(); if (triggerType.intValue() == SmsCampaignTriggerType.SCHEDULE.getValue()) { if (this.fromApiJsonHelper.parameterExists(SmsCampaignValidator.recurrenceParamName, element)) { final String recurrenceParamName = this.fromApiJsonHelper .extractStringNamed(SmsCampaignValidator.recurrenceParamName, element); baseDataValidator.reset().parameter(SmsCampaignValidator.recurrenceParamName) .value(recurrenceParamName).notBlank(); } if (this.fromApiJsonHelper.parameterExists(SmsCampaignValidator.recurrenceStartDate, element)) { final String recurrenceStartDate = this.fromApiJsonHelper .extractStringNamed(SmsCampaignValidator.recurrenceStartDate, element); baseDataValidator.reset().parameter(SmsCampaignValidator.recurrenceStartDate) .value(recurrenceStartDate).notBlank(); } } if (this.fromApiJsonHelper.parameterExists(SmsCampaignValidator.runReportId, element)) { final Long runReportId = this.fromApiJsonHelper.extractLongNamed(SmsCampaignValidator.runReportId, element); baseDataValidator.reset().parameter(SmsCampaignValidator.runReportId).value(runReportId).notNull() .integerGreaterThanZero(); } final String message = this.fromApiJsonHelper.extractStringNamed(SmsCampaignValidator.message, element); baseDataValidator.reset().parameter(SmsCampaignValidator.message).value(message).notBlank() .notExceedingLengthOf(480); final JsonElement paramValueJsonObject = this.fromApiJsonHelper .extractJsonObjectNamed(SmsCampaignValidator.paramValue, element); if (triggerType.intValue() != SmsCampaignTriggerType.TRIGGERED.getValue()) { baseDataValidator.reset().parameter(SmsCampaignValidator.paramValue).value(paramValueJsonObject) .notBlank(); if (paramValueJsonObject != null && paramValueJsonObject.isJsonObject()) { for (Map.Entry<String, JsonElement> entry : paramValueJsonObject.getAsJsonObject().entrySet()) { final JsonElement inner = entry.getValue(); baseDataValidator.reset().parameter(entry.getKey()).value(inner).notBlank(); } } } throwExceptionIfValidationWarningsExist(dataValidationErrors); }
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(); }//w ww .j a v a 2 s.c om 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); }