List of usage examples for com.google.gson JsonElement isJsonObject
public boolean isJsonObject()
From source file:com.googleapis.ajax.services.impl.TransliterateLanguageQueryImpl.java
License:Apache License
@Override public TransliterateLanguageResult singleResult() { InputStream jsonContent = null; try {/*from ww w .j a v a 2 s . c o m*/ jsonContent = callApiGet(apiUrlBuilder.buildUrl()); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { PagedList<TransliterateLanguageResult> responseList = unmarshallList(response.getAsJsonObject()); notifyObservers(responseList); return responseList.isEmpty() ? null : responseList.get(0); } throw new GoogleSearchException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new GoogleSearchException(e); } finally { closeStream(jsonContent); } }
From source file:com.googleapis.ajax.services.impl.TransliterateLanguageQueryImpl.java
License:Apache License
/** * Unmarshall list./*from www.j av a2 s.co m*/ * * @param response the response * * @return the paged list< t> */ protected PagedList<TransliterateLanguageResult> unmarshallList(JsonObject response) { int status = response.get("responseStatus").getAsInt(); if (status != 200) { throw new GoogleSearchException(String.valueOf(response.get("responseDetails").getAsString())); } JsonObject data = response.get("responseData").getAsJsonObject(); PagedArrayList<TransliterateLanguageResult> list = new PagedArrayList<TransliterateLanguageResult>(); if (data != null) { JsonArray dataArray = data.get("transliterations").getAsJsonArray(); for (JsonElement element : dataArray) { if (element.isJsonObject()) { JsonObject json = element.getAsJsonObject(); list.add(unmarshall(json)); } } } return list; }
From source file:com.googleapis.maps.services.impl.BaseGoogleMapsApiQuery.java
License:Apache License
@Override public List<T> list() { InputStream jsonContent = null; try {//from www. j a v a2 s.c o m jsonContent = callApiGet(apiUrlBuilder.buildUrl()); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { List<T> responseList = unmarshallList(response.getAsJsonObject()); notifyObservers(responseList); return responseList; } throw new GoogleMapsException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new GoogleMapsException(e); } finally { closeStream(jsonContent); } }
From source file:com.googlecode.goclipse.tooling.oracle.GuruPackageDescribeParser.java
License:Open Source License
protected ArrayList2<StructureElement> parseElements(JsonArray members, boolean parsingMethods) throws CommonException { ArrayList2<StructureElement> elements = new ArrayList2<>(); if (members != null) { for (int i = 0; i < members.size(); i++) { JsonElement arrayElement = members.get(i); if (arrayElement.isJsonObject()) { JsonObject jsonObject = arrayElement.getAsJsonObject(); StructureElement structureElement = parseStructureElement(jsonObject, parsingMethods); if (structureElement == null) { continue; // Can happen for external elements }/*w w w .ja va 2 s .co m*/ elements.add(structureElement); } else { throw new CommonException("'members' array element is not a JSONObject: " + arrayElement); } } } Collections.sort(elements, new Comparator<StructureElement>() { @Override public int compare(StructureElement o1, StructureElement o2) { SourceRange sr1 = o1.getSourceRange(); SourceRange sr2 = o2.getSourceRange(); int cmp = sr1.getOffset() - sr2.getOffset(); if (cmp == 0) { int offset1 = o1.getNameSourceRange2() == null ? 0 : o1.getNameSourceRange2().getOffset(); int offset2 = o2.getNameSourceRange2() == null ? 0 : o2.getNameSourceRange2().getOffset(); return offset1 - offset2; } return cmp; } }); return elements; }
From source file:com.googlesource.gerrit.plugins.oauth.AirVantageOAuthService.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 a 2 s . c om*/ Response response = request.send(); if (response.getCode() != 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("uid"); if (id == null || id.isJsonNull()) { throw new IOException("Response doesn't contain uid field"); } JsonElement email = jsonObject.get("email"); JsonElement name = jsonObject.get("name"); return new OAuthUserInfo(AV_PROVIDER_PREFIX + id.getAsString(), null, email.getAsString(), name.getAsString(), id.getAsString()); } throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); }
From source file:com.googlesource.gerrit.plugins.oauth.BitbucketOAuthService.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.jav a 2 s. c o m*/ Response response = request.send(); if (response.getCode() != 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(); JsonObject userObject = jsonObject.getAsJsonObject("user"); if (userObject == null || userObject.isJsonNull()) { throw new IOException("Response doesn't contain 'user' field"); } JsonElement usernameElement = userObject.get("username"); String username = usernameElement.getAsString(); JsonElement displayName = jsonObject.get("display_name"); return new OAuthUserInfo(username, username, null, displayName == null || displayName.isJsonNull() ? null : displayName.getAsString(), null); } else { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } }
From source file:com.googlesource.gerrit.plugins.oauth.CasOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { final String protectedResourceUrl = String.format(PROTECTED_RESOURCE_URL, rootUrl); OAuthRequest request = new OAuthRequest(Verb.GET, protectedResourceUrl); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); service.signRequest(t, request);/*from w ww. ja v a 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())); } if (log.isDebugEnabled()) { log.debug("User info response: {}", response.getBody()); } JsonElement userJson = JSON.newGson().fromJson(response.getBody(), JsonElement.class); if (!userJson.isJsonObject()) { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } JsonObject jsonObject = userJson.getAsJsonObject(); JsonElement id = jsonObject.get("id"); if (id == null || id.isJsonNull()) { throw new IOException(String.format("CAS response missing id: %s", response.getBody())); } JsonElement attrListJson = jsonObject.get("attributes"); if (attrListJson == null) { throw new IOException(String.format("CAS response missing attributes: %s", response.getBody())); } String email = null, name = null, login = null; if (attrListJson.isJsonArray()) { // It is possible for CAS to be configured to not return any attributes (email, name, login), // in which case, // CAS returns an empty JSON object "attributes":{}, rather than "null" or an empty JSON array // "attributes": [] JsonArray attrJson = attrListJson.getAsJsonArray(); for (JsonElement elem : attrJson) { if (elem == null || !elem.isJsonObject()) { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", elem)); } JsonObject obj = elem.getAsJsonObject(); String property = getStringElement(obj, "email"); if (property != null) email = property; property = getStringElement(obj, "name"); if (property != null) name = property; property = getStringElement(obj, "login"); if (property != null) login = property; } } return new OAuthUserInfo(CAS_PROVIDER_PREFIX + id.getAsString(), login, email, name, fixLegacyUserId ? id.getAsString() : null); }
From source file:com.googlesource.gerrit.plugins.oauth.FacebookOAuthService.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()); request.addQuerystringParameter(FIELDS_QUERY, FIELDS); service.signRequest(t, request);//from ww w. j av a2 s . c om 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 = 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("email"); JsonElement name = jsonObject.get("name"); // Heads up! // Lets keep `login` equal to `email`, since `username` field is // deprecated for Facebook API versions v2.0 and higher JsonElement login = jsonObject.get("email"); return new OAuthUserInfo(FACEBOOK_PROVIDER_PREFIX + id.getAsString(), login == null || login.isJsonNull() ? null : login.getAsString(), email == null || email.isJsonNull() ? null : email.getAsString(), name == null || name.isJsonNull() ? null : name.getAsString(), null); } throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); }
From source file:com.googlesource.gerrit.plugins.oauth.GitCafeOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); request.addHeader("Authorization", "Bearer " + token.getToken()); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); service.signRequest(t, request);/*from w w w . j a v a 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 fullname = jsonObject.get("fullname"); JsonElement username = jsonObject.get("username"); return new OAuthUserInfo(id.getAsString(), username == null || username.isJsonNull() ? null : username.getAsString(), email == null || email.isJsonNull() ? null : email.getAsString(), fullname == null || fullname.isJsonNull() ? null : fullname.getAsString(), null); } else { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } }
From source file:com.googlesource.gerrit.plugins.oauth.GitHubOAuthService.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 .ja v a2s.c om 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 email = jsonObject.get("email"); JsonElement name = jsonObject.get("name"); JsonElement id = jsonObject.get("id"); JsonElement login = jsonObject.get("login"); return new OAuthUserInfo(id.getAsString(), login.isJsonNull() ? null : login.getAsString(), email.isJsonNull() ? null : email.getAsString(), name.isJsonNull() ? null : name.getAsString(), null); } else { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } }