List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
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 ww.j av a2 s . co 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 w w. j a va 2s. c o 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 ww w .j a va2 s . co 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.graphaware.module.es.Neo4jElasticVerifier.java
License:Open Source License
private void checkStringArray(JsonObject source, String key, Map<String, Object> properties) { assertTrue(source.get(key) instanceof JsonArray); JsonArray jsonArray = source.get(key).getAsJsonArray(); TreeSet<String> esSet = new TreeSet(); for (JsonElement element : jsonArray) { esSet.add(element.getAsString()); }/* www. java 2 s .com*/ String[] propertyArray = (String[]) properties.get(key); TreeSet<String> nodeSet = new TreeSet<>(Arrays.asList(propertyArray)); assertEquals(esSet, nodeSet); }
From source file:com.graphaware.module.es.Neo4jElasticVerifier.java
License:Open Source License
private void checkCharArray(JsonObject source, String key, Map<String, Object> properties) { assertTrue(source.get(key) instanceof JsonArray); JsonArray jsonArray = source.get(key).getAsJsonArray(); TreeSet<String> esSet = new TreeSet(); for (JsonElement element : jsonArray) { esSet.add(element.getAsString()); }/*from w w w. j a v a 2 s .com*/ TreeSet<String> nodeSet = new TreeSet(); char[] propertyArray = (char[]) properties.get(key); for (char element : propertyArray) { nodeSet.add(String.valueOf(element)); } assertEquals(esSet, nodeSet); }
From source file:com.graphaware.module.es.Neo4jElasticVerifier.java
License:Open Source License
private void checkLabels(JsonObject source, Set labels) { JsonArray jsonArray = source.get(AdvancedMapping.LABELS_FIELD).getAsJsonArray(); TreeSet<String> esSet = new TreeSet(); for (JsonElement element : jsonArray) { esSet.add(element.getAsString()); }//from w w w .ja v a 2 s. co m assertEquals(esSet, labels); }
From source file:com.gsaul.AethonSimulator.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//from ww w . j av a 2 s .c o m final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName); //used to be .remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:com.gst.batch.service.ResolutionHelper.java
License:Apache License
private JsonElement resolveDependentVariable(final JsonElement element, final JsonModel responseJsonModel) { JsonElement value = element;/*from w w w . j av a 2 s. c om*/ String paramVal = element.getAsString(); if (paramVal.contains("$.")) { // Get the value of the parameter from parent response final String resParamValue = responseJsonModel.get(paramVal).toString(); value = this.fromJsonHelper.parse(resParamValue); } return value; }
From source file:com.hagreve.android.lib.HaGreveApi.java
License:Apache License
/** * Obtains the list of current strikes.//from www . ja va 2 s .c o m * @return Array of Strike objects */ public static Strike[] getStrikes() { Strike[] items = new Strike[0]; String result = doGetRequest(BASE_URL + "/strikes"); if (result != null) { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return formatter.parse(json.getAsString()); } catch (ParseException e) { throw new JsonParseException(e.getMessage()); } } }); Gson gson = builder.create(); items = gson.fromJson(result, Strike[].class); } return items; }
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 {/*from w w w .j av a 2 s. c om*/ result.put(paramName, paramValue.toString()); } } return result; }