List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
From source file:com.google.identitytoolkit.GitkitUser.java
License:Open Source License
public GitkitUser setProviders(JsonArray providers) { List<ProviderInfo> providerInfo = new ArrayList<ProviderInfo>(); if (providers != null) { for (int i = 0; i < providers.size(); i++) { JsonObject provider = providers.get(i).getAsJsonObject(); JsonElement displayNameElement = provider.get("displayName"); JsonElement photoUrlElement = provider.get("photoUrl"); providerInfo.add(new ProviderInfo(provider.get("providerId").getAsString(), provider.get("federatedId").getAsString(), (displayNameElement == null) ? "" : displayNameElement.getAsString(), (photoUrlElement == null) ? "" : photoUrlElement.getAsString())); }//from w w w. j a v a 2 s . c o m } this.providers = providerInfo; return this; }
From source file:com.google.identitytoolkit.RpcHelper.java
License:Open Source License
@VisibleForTesting JsonObject checkGitkitException(String response) throws GitkitClientException, GitkitServerException { JsonElement resultElement = new JsonParser().parse(response); if (!resultElement.isJsonObject()) { throw new GitkitServerException("null error code from Gitkit server"); }//from w w w. j a v a2 s . co m JsonObject result = resultElement.getAsJsonObject(); if (!result.has("error")) { return result; } // Error handling JsonObject error = result.getAsJsonObject("error"); JsonElement codeElement = error.get("code"); if (codeElement != null) { JsonElement messageElement = error.get("message"); String message = (messageElement == null) ? "" : messageElement.getAsString(); if (codeElement.getAsString().startsWith("4")) { // 4xx means client input error throw new GitkitClientException(message); } else { throw new GitkitServerException(message); } } throw new GitkitServerException("null error code from Gitkit server"); }
From source file:com.google.iosched.model.DataCheck.java
License:Open Source License
/** * @param sources/*ww w .j ava 2s . c o m*/ */ public CheckResult check(JsonDataSources sources, JsonObject newSessionData, ManifestData manifest) throws IOException { newSessionData = clone(newSessionData); JsonObject newData = new JsonObject(); merge(newSessionData, newData); JsonObject oldData = new JsonObject(); for (JsonElement dataFile : manifest.dataFiles) { String filename = dataFile.getAsString(); // except for session data, merge all other files: Matcher matcher = Config.SESSIONS_PATTERN.matcher(filename); if (!matcher.matches()) { JsonObject data = fileManager.readFileAsJsonObject(filename); merge(data, oldData); merge(data, newData); } } CheckResult result = new CheckResult(); // check if array of entities is more than 80% the size of the old data: checkUsingPredicator(result, oldData, newData, new ArraySizeValidator()); // Check that no existing tag was removed or had its name changed in a significant way checkUsingPredicator(result, oldData, newData, OutputJsonKeys.MainTypes.tags, OutputJsonKeys.Tags.tag, new EntityValidator() { @Override public void evaluate(CheckResult result, String entity, JsonObject oldData, JsonObject newData) { if (newData == null) { String tagName = get(oldData, OutputJsonKeys.Tags.tag).getAsString(); String originalId = get(oldData, OutputJsonKeys.Tags.original_id).getAsString(); result.failures.add(new CheckFailure(entity, tagName, "Tag could not be found or changed name. Original category ID = " + originalId)); } } }); // Check that no room was removed checkUsingPredicator(result, oldData, newData, OutputJsonKeys.MainTypes.rooms, OutputJsonKeys.Rooms.id, new EntityValidator() { @Override public void evaluate(CheckResult result, String entity, JsonObject oldData, JsonObject newData) { if (newData == null) { String id = get(oldData, OutputJsonKeys.Rooms.id).getAsString(); result.failures.add(new CheckFailure(entity, id, "Room could not be found. Original room: " + oldData)); } } }); // Check if blocks start and end timestamps are valid JsonArray newBlocks = getAsArray(newData, OutputJsonKeys.MainTypes.blocks); if (newBlocks == null) { StringBuilder sb = new StringBuilder(); for (Map.Entry<String, JsonElement> entry : newData.entrySet()) { sb.append(entry.getKey()).append(", "); } throw new IllegalArgumentException( "Could not find the blocks entities. Entities in newData are: " + sb); } for (JsonElement el : newBlocks) { JsonObject block = el.getAsJsonObject(); try { Date start = blockDateFormat.parse(get(block, OutputJsonKeys.Blocks.start).getAsString()); Date end = blockDateFormat.parse(get(block, OutputJsonKeys.Blocks.end).getAsString()); if (start.getTime() >= end.getTime() || // check for invalid start/end combinations start.getTime() < Config.CONFERENCE_DAYS[0][0] || // check for block starting before the conference end.getTime() > Config.CONFERENCE_DAYS[1][1]) { // check for block ending after the conference result.failures.add(new CheckFailure(OutputJsonKeys.MainTypes.blocks.name(), null, "Invalid block start or end date. Block=" + block)); } } catch (ParseException ex) { result.failures.add(new CheckFailure(OutputJsonKeys.MainTypes.blocks.name(), null, "Could not parse block start or end date. Exception=" + ex.getMessage() + ". Block=" + block)); } } // Check if sessions start and end timestamps are valid JsonArray newSessions = getAsArray(newData, OutputJsonKeys.MainTypes.sessions); for (JsonElement el : newSessions) { JsonObject session = el.getAsJsonObject(); try { Date start = sessionDateFormat .parse(get(session, OutputJsonKeys.Sessions.startTimestamp).getAsString()); Date end = sessionDateFormat .parse(get(session, OutputJsonKeys.Sessions.endTimestamp).getAsString()); if (start.getTime() >= end.getTime()) { // check for invalid start/end combinations result.failures.add(new CheckFailure(OutputJsonKeys.MainTypes.sessions.name(), get(session, OutputJsonKeys.Sessions.id).getAsString(), "Session ends before or at the same time as it starts. Session=" + session)); } else if (end.getTime() - start.getTime() > 6 * 60 * 60 * 1000L) { // check for session longer than 6 hours result.failures.add(new CheckFailure(OutputJsonKeys.MainTypes.sessions.name(), get(session, OutputJsonKeys.Sessions.id).getAsString(), "Session is longer than 6 hours. Session=" + session)); } else if (start.getTime() < Config.CONFERENCE_DAYS[0][0] || // check for session starting before the conference end.getTime() > Config.CONFERENCE_DAYS[1][1]) { // check for session ending after the conference result.failures.add(new CheckFailure(OutputJsonKeys.MainTypes.sessions.name(), get(session, OutputJsonKeys.Sessions.id).getAsString(), "Session starts before or ends after the days of the conference. Session=" + session)); } else { // Check if all sessions are covered by at least one free block (except the keynote): boolean valid = false; if (!get(session, OutputJsonKeys.Sessions.id).getAsString().equals("__keynote__")) { for (JsonElement bl : newBlocks) { JsonObject block = bl.getAsJsonObject(); Date blockStart = blockDateFormat .parse(get(block, OutputJsonKeys.Blocks.start).getAsString()); Date blockEnd = blockDateFormat .parse(get(block, OutputJsonKeys.Blocks.end).getAsString()); String blockType = get(block, OutputJsonKeys.Blocks.type).getAsString(); if ("free".equals(blockType) && start.compareTo(blockStart) >= 0 && start.compareTo(blockEnd) < 0) { valid = true; break; } } if (!valid) { result.failures.add(new CheckFailure(OutputJsonKeys.MainTypes.sessions.name(), get(session, OutputJsonKeys.Sessions.id).getAsString(), "There is no FREE block where this session start date lies on. Session=" + session)); } } } } catch (ParseException ex) { result.failures.add(new CheckFailure(OutputJsonKeys.MainTypes.sessions.name(), get(session, OutputJsonKeys.Sessions.id).getAsString(), "Could not parse session start or end date. Exception=" + ex.getMessage() + ". Session=" + session)); } } // Check if video sessions (video library) have valid video URLs JsonArray newVideoLibrary = getAsArray(newData, OutputJsonKeys.MainTypes.video_library); for (JsonElement el : newVideoLibrary) { JsonObject session = el.getAsJsonObject(); JsonPrimitive videoUrl = (JsonPrimitive) get(session, OutputJsonKeys.VideoLibrary.vid); if (videoUrl == null || !videoUrl.isString() || videoUrl.getAsString() == null || videoUrl.getAsString().isEmpty()) { result.failures.add(new CheckFailure(InputJsonKeys.VendorAPISource.MainTypes.topics.name(), "" + get(session, OutputJsonKeys.VideoLibrary.id), "Video Session has empty vid info. Session: " + session)); } } return result; }
From source file:com.google.iosched.model.DataExtractor.java
License:Open Source License
public JsonArray extractRooms(JsonDataSources sources) { HashSet<String> ids = new HashSet<String>(); JsonArray result = new JsonArray(); JsonDataSource source = sources.getSource(VendorAPISource.MainTypes.rooms.name()); if (source != null) { for (JsonObject origin : source) { JsonObject dest = new JsonObject(); JsonElement originalId = get(origin, VendorAPISource.Rooms.id); String id = Config.ROOM_MAPPING.getRoomId(originalId.getAsString()); if (!ids.contains(id)) { String title = Config.ROOM_MAPPING.getTitle(id, get(origin, VendorAPISource.Rooms.name).getAsString()); set(new JsonPrimitive(id), dest, OutputJsonKeys.Rooms.id); set(originalId, dest, OutputJsonKeys.Rooms.original_id); set(new JsonPrimitive(title), dest, OutputJsonKeys.Rooms.name); result.add(dest);/*from ww w .j a v a 2 s. c o m*/ ids.add(id); } } } return result; }
From source file:com.google.iosched.model.DataExtractor.java
License:Open Source License
public JsonArray extractTags(JsonDataSources sources) { JsonArray result = new JsonArray(); JsonDataSource source = sources.getSource(VendorAPISource.MainTypes.categories.name()); JsonDataSource tagCategoryMappingSource = sources .getSource(ExtraSource.MainTypes.tag_category_mapping.name()); JsonDataSource tagsConfSource = sources.getSource(ExtraSource.MainTypes.tag_conf.name()); categoryToTagMap = new HashMap<String, JsonObject>(); // Only for checking duplicates. HashSet<String> originalTagNames = new HashSet<String>(); if (source != null) { for (JsonObject origin : source) { JsonObject dest = new JsonObject(); // set tag category, looking for parentid in the tag_category_mapping data source JsonElement parentId = get(origin, VendorAPISource.Categories.parentid); // Ignore categories with null parents, because they are roots (tag categories). if (parentId != null && !parentId.getAsString().equals("")) { JsonElement category = null; if (tagCategoryMappingSource != null) { JsonObject categoryMapping = tagCategoryMappingSource .getElementById(parentId.getAsString()); if (categoryMapping != null) { category = get(categoryMapping, ExtraSource.CategoryTagMapping.tag_name); JsonPrimitive isDefault = (JsonPrimitive) get(categoryMapping, ExtraSource.CategoryTagMapping.is_default); if (isDefault != null && isDefault.getAsBoolean()) { mainCategory = category; }/* w ww . j a va2 s . com*/ } set(category, dest, OutputJsonKeys.Tags.category); } // Ignore categories unrecognized parents (no category) if (category == null) { continue; } // Tag name is by convention: "TAGCATEGORY_TAGNAME" JsonElement name = get(origin, VendorAPISource.Categories.name); JsonElement tagName = new JsonPrimitive( category.getAsString() + "_" + Converters.TAG_NAME.convert(name).getAsString()); JsonElement originalTagName = tagName; set(tagName, dest, OutputJsonKeys.Tags.tag); set(name, dest, OutputJsonKeys.Tags.name); set(origin, VendorAPISource.Categories.id, dest, OutputJsonKeys.Tags.original_id); set(origin, VendorAPISource.Categories.description, dest, OutputJsonKeys.Tags._abstract, null); if (tagsConfSource != null) { JsonObject tagConf = tagsConfSource.getElementById(originalTagName.getAsString()); if (tagConf != null) { set(tagConf, ExtraSource.TagConf.order_in_category, dest, OutputJsonKeys.Tags.order_in_category); set(tagConf, ExtraSource.TagConf.color, dest, OutputJsonKeys.Tags.color); set(tagConf, ExtraSource.TagConf.hashtag, dest, OutputJsonKeys.Tags.hashtag); } } categoryToTagMap.put(get(origin, VendorAPISource.Categories.id).getAsString(), dest); if (originalTagNames.add(originalTagName.getAsString())) { result.add(dest); } } } } return result; }
From source file:com.google.iosched.model.DataExtractor.java
License:Open Source License
public JsonArray extractSpeakers(JsonDataSources sources) { speakersById = new HashMap<String, JsonObject>(); JsonArray result = new JsonArray(); JsonDataSource source = sources.getSource(VendorAPISource.MainTypes.speakers.name()); if (source != null) { for (JsonObject origin : source) { JsonObject dest = new JsonObject(); JsonElement id = get(origin, VendorAPISource.Speakers.id); set(id, dest, OutputJsonKeys.Speakers.id); set(origin, VendorAPISource.Speakers.name, dest, OutputJsonKeys.Speakers.name, null); set(origin, VendorAPISource.Speakers.bio, dest, OutputJsonKeys.Speakers.bio, null); set(origin, VendorAPISource.Speakers.companyname, dest, OutputJsonKeys.Speakers.company, null); JsonElement originalPhoto = get(origin, VendorAPISource.Speakers.photo); if (originalPhoto != null && !"".equals(originalPhoto.getAsString())) { // Note that the input for SPEAKER_PHOTO_ID converter is the entity ID. We simply ignore the original // photo URL, because that will be processed by an offline cron script, resizing the // photos and saving them to a known location with the entity ID as its base name. set(origin, VendorAPISource.Speakers.id, dest, OutputJsonKeys.Speakers.thumbnailUrl, Converters.SPEAKER_PHOTO_URL); }// w ww . j a va2 s . c o m JsonElement info = origin.get(VendorAPISource.Speakers.info.name()); JsonPrimitive plusUrl = getMapValue(info, InputJsonKeys.VendorAPISource.Speakers.INFO_PUBLIC_PLUS_ID, Converters.GPLUS_URL, null); if (plusUrl != null) { set(plusUrl, dest, OutputJsonKeys.Speakers.plusoneUrl); } result.add(dest); speakersById.put(id.getAsString(), dest); } } return result; }
From source file:com.google.iosched.model.DataExtractor.java
License:Open Source License
public JsonArray extractSessions(JsonDataSources sources) { if (videoSessionsById == null) { throw new IllegalStateException( "You need to extract video sessions before attempting to extract sessions"); }/*from w w w.j av a 2 s . c om*/ if (categoryToTagMap == null) { throw new IllegalStateException("You need to extract tags before attempting to extract sessions"); } JsonArray result = new JsonArray(); JsonDataSource source = sources.getSource(VendorAPISource.MainTypes.topics.name()); if (source != null) { for (JsonObject origin : source) { if (isVideoSession(origin)) { // Sessions with the Video tag are processed as video library content continue; } if (isHiddenSession(origin)) { // Sessions with a "Hidden from schedule" flag should be ignored continue; } JsonElement title = get(origin, VendorAPISource.Topics.title); // Since the CMS returns an empty keynote as a session, we need to ignore it if (title != null && title.isJsonPrimitive() && "keynote".equalsIgnoreCase(title.getAsString())) { continue; } JsonObject dest = new JsonObject(); set(origin, VendorAPISource.Topics.id, dest, OutputJsonKeys.Sessions.id); set(origin, VendorAPISource.Topics.id, dest, OutputJsonKeys.Sessions.url, Converters.SESSION_URL); set(origin, VendorAPISource.Topics.title, dest, OutputJsonKeys.Sessions.title, null); set(origin, VendorAPISource.Topics.description, dest, OutputJsonKeys.Sessions.description, null); set(origin, VendorAPISource.Topics.start, dest, OutputJsonKeys.Sessions.startTimestamp, Converters.DATETIME); set(origin, VendorAPISource.Topics.finish, dest, OutputJsonKeys.Sessions.endTimestamp, Converters.DATETIME); JsonElement documents = get(origin, VendorAPISource.Topics.documents); if (documents != null && documents.isJsonArray() && documents.getAsJsonArray().size() > 0) { // Note that the input for SessionPhotoURL is the entity ID. We simply ignore the original // photo URL, because that will be processed by an offline cron script, resizing the // photos and saving them to a known location with the entity ID as its base name. set(origin, VendorAPISource.Topics.id, dest, OutputJsonKeys.Sessions.photoUrl, Converters.SESSION_PHOTO_URL); } setVideoPropertiesInSession(origin, dest); setRelatedVideos(origin, dest); JsonElement mainTag = null; JsonElement hashtag = null; JsonElement mainTagColor = null; JsonArray categories = origin.getAsJsonArray(VendorAPISource.Topics.categoryids.name()); JsonArray tags = new JsonArray(); for (JsonElement category : categories) { JsonObject tag = categoryToTagMap.get(category.getAsString()); if (tag != null) { JsonElement tagName = get(tag, OutputJsonKeys.Tags.tag); tags.add(tagName); usedTags.add(tagName.getAsString()); if (mainTag == null) { // check if the tag is from a "default" category. For example, if THEME is the default // category, all sessions will have a "mainTag" property set to the first tag of type THEME JsonElement tagCategory = get(tag, OutputJsonKeys.Tags.category); // THEME, TYPE or TOPIC if (tagCategory.equals(mainCategory)) { mainTag = tagName; mainTagColor = get(tag, OutputJsonKeys.Tags.color); } if (hashtag == null && isHashtag(tag)) { hashtag = get(tag, OutputJsonKeys.Tags.hashtag); if (hashtag == null || hashtag.getAsString() == null || hashtag.getAsString().isEmpty()) { // If no hashtag set in the tagsconf file, we will convert the tagname to find one: hashtag = new JsonPrimitive( get(tag, OutputJsonKeys.Tags.name, Converters.TAG_NAME).getAsString() .toLowerCase()); } } } } } set(tags, dest, OutputJsonKeys.Sessions.tags); if (mainTag != null) { set(mainTag, dest, OutputJsonKeys.Sessions.mainTag); } if (mainTagColor != null) { set(mainTagColor, dest, OutputJsonKeys.Sessions.color); } if (hashtag != null) { set(hashtag, dest, OutputJsonKeys.Sessions.hashtag); } JsonArray speakers = getAsArray(origin, VendorAPISource.Topics.speakerids); if (speakers != null) for (JsonElement speaker : speakers) { String speakerId = speaker.getAsString(); usedSpeakers.add(speakerId); } set(speakers, dest, OutputJsonKeys.Sessions.speakers); JsonArray sessions = origin.getAsJsonArray(VendorAPISource.Topics.sessions.name()); if (sessions != null && sessions.size() > 0) { String roomId = get(sessions.get(0).getAsJsonObject(), VendorAPISource.Sessions.roomid) .getAsString(); roomId = Config.ROOM_MAPPING.getRoomId(roomId); set(new JsonPrimitive(roomId), dest, OutputJsonKeys.Sessions.room); // captions URL is set based on the session room, so keep it here. String captionsURL = Config.ROOM_MAPPING.getCaptions(roomId); if (captionsURL != null) { set(new JsonPrimitive(captionsURL), dest, OutputJsonKeys.Sessions.captionsUrl); } } result.add(dest); } } return result; }
From source file:com.google.iosched.model.DataExtractor.java
License:Open Source License
public JsonArray extractVideoSessions(JsonDataSources sources) { videoSessionsById = new HashMap<String, JsonObject>(); if (categoryToTagMap == null) { throw new IllegalStateException("You need to extract tags before attempting to extract video sessions"); }//from w w w .j ava2 s . c o m if (speakersById == null) { throw new IllegalStateException( "You need to extract speakers before attempting to extract video sessions"); } JsonArray result = new JsonArray(); JsonDataSource source = sources.getSource(VendorAPISource.MainTypes.topics.name()); if (source != null) { for (JsonObject origin : source) { if (!isVideoSession(origin)) { continue; } if (isHiddenSession(origin)) { // Sessions with a "Hidden from schedule" flag should be ignored continue; } JsonObject dest = new JsonObject(); JsonPrimitive vid = setVideoForVideoSession(origin, dest); JsonElement id = get(origin, VendorAPISource.Topics.id); // video library id must be the Youtube video id set(vid, dest, OutputJsonKeys.VideoLibrary.id); set(origin, VendorAPISource.Topics.title, dest, OutputJsonKeys.VideoLibrary.title, null); set(origin, VendorAPISource.Topics.description, dest, OutputJsonKeys.VideoLibrary.desc, null); set(new JsonPrimitive(Config.CONFERENCE_YEAR), dest, OutputJsonKeys.VideoLibrary.year); JsonElement videoTopic = null; JsonArray categories = origin.getAsJsonArray(VendorAPISource.Topics.categoryids.name()); for (JsonElement category : categories) { JsonObject tag = categoryToTagMap.get(category.getAsString()); if (tag != null) { if (isHashtag(tag)) { videoTopic = get(tag, OutputJsonKeys.Tags.name); // by definition, the first tag that can be a hashtag (usually a TOPIC) is considered the video tag break; } } } if (videoTopic != null) { set(videoTopic, dest, OutputJsonKeys.VideoLibrary.topic); } // Concatenate speakers: JsonArray speakers = getAsArray(origin, VendorAPISource.Topics.speakerids); StringBuilder sb = new StringBuilder(); if (speakers != null) for (int i = 0; i < speakers.size(); i++) { String speakerId = speakers.get(i).getAsString(); usedSpeakers.add(speakerId); JsonObject speaker = speakersById.get(speakerId); if (speaker != null) { sb.append(get(speaker, OutputJsonKeys.Speakers.name).getAsString()); if (i < speakers.size() - 1) sb.append(", "); } } set(new JsonPrimitive(sb.toString()), dest, OutputJsonKeys.VideoLibrary.speakers); videoSessionsById.put(id.getAsString(), dest); result.add(dest); } } return result; }
From source file:com.google.iosched.model.DataExtractor.java
License:Open Source License
private boolean isVideoSession(JsonObject sessionObj) { JsonArray tags = sessionObj.getAsJsonArray(VendorAPISource.Topics.categoryids.name()); for (JsonElement category : tags) { if (Config.VIDEO_CATEGORY.equals(category.getAsString())) { return true; }//from ww w . j av a 2 s . c o m } return false; }
From source file:com.google.iosched.model.DataExtractor.java
License:Open Source License
private void setRelatedVideos(JsonObject origin, JsonObject dest) { JsonArray related = getAsArray(origin, VendorAPISource.Topics.related); if (related == null) { return;/* w w w .j a v a 2 s . c om*/ } for (JsonElement el : related) { if (!el.isJsonObject()) { continue; } JsonObject obj = el.getAsJsonObject(); if (!obj.has("name") || !obj.has("values")) { continue; } if (InputJsonKeys.VendorAPISource.Topics.RELATED_NAME_VIDEO .equals(obj.getAsJsonPrimitive("name").getAsString())) { JsonElement values = obj.get("values"); if (!values.isJsonArray()) { continue; } // As per the data specification, related content is formatted as // "video1 title1\nvideo2 title2\n..." StringBuilder relatedContentStr = new StringBuilder(); for (JsonElement value : values.getAsJsonArray()) { String relatedSessionId = value.getAsString(); JsonObject relatedVideo = videoSessionsById.get(relatedSessionId); if (relatedVideo != null) { JsonElement vid = get(relatedVideo, OutputJsonKeys.VideoLibrary.vid); JsonElement title = get(relatedVideo, OutputJsonKeys.VideoLibrary.title); if (vid != null && title != null) { relatedContentStr.append(vid.getAsString()).append(" ").append(title.getAsString()) .append("\n"); } } } set(new JsonPrimitive(relatedContentStr.toString()), dest, OutputJsonKeys.Sessions.relatedContent); } } }