Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2017 Newhouse (nhitbh at gmail dot com) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.fooock.shodan.model.exploit; import com.google.gson.*; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; /** * Exploit json deserializer */ public class ExploitDeserializer implements JsonDeserializer<List<Exploit>> { @Override public List<Exploit> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { List<Exploit> exploits = new ArrayList<>(); try { JsonObject jsonObject = json.getAsJsonObject(); JsonElement matches = jsonObject.get("matches"); if (matches == null || matches.isJsonNull()) { return exploits; } JsonArray array = matches.getAsJsonArray(); if (array == null || array.isJsonNull()) { return exploits; } for (JsonElement element : array) { Exploit exploit = parseJsonExploit(element); exploits.add(exploit); } } catch (IllegalStateException e) { // parsing the array of exploits directly, used when search with facets JsonArray elements = json.getAsJsonArray(); if (elements == null || elements.isJsonNull()) { return exploits; } for (JsonElement element : elements) { Exploit exploit = parseJsonExploit(element); exploits.add(exploit); } } return exploits; } private Exploit parseJsonExploit(JsonElement json) { Exploit exploit = new Exploit(); JsonObject jsonObject = json.getAsJsonObject(); String id = jsonObject.get("_id").getAsString(); String desc = jsonObject.get("description").getAsString(); String source = jsonObject.get("source").getAsString(); JsonElement jsonAuthor = jsonObject.get("author"); if (jsonAuthor != null && !jsonAuthor.isJsonNull()) { if (jsonAuthor.isJsonPrimitive()) { String author = jsonAuthor.getAsString(); exploit.setAuthor(author); } else { JsonArray array = jsonAuthor.getAsJsonArray(); if (array != null) { String resAuthors = ""; for (JsonElement element : array) { resAuthors += ", " + element.getAsString(); } exploit.setAuthor(resAuthors); } } } JsonElement jsonCode = jsonObject.get("code"); if (jsonCode != null && !jsonCode.isJsonNull()) { String code = jsonCode.getAsString(); exploit.setCode(code); } JsonElement jsonType = jsonObject.get("type"); if (jsonType != null && !jsonType.isJsonNull()) { String type = jsonType.getAsString(); exploit.setType(type); } JsonElement jsonVersion = jsonObject.get("version"); if (jsonVersion != null && !jsonVersion.isJsonNull()) { String version = jsonVersion.getAsString(); exploit.setVersion(version); } JsonElement jsonPrivileged = jsonObject.get("privileged"); if (jsonPrivileged != null && !jsonPrivileged.isJsonNull()) { boolean privileged = jsonPrivileged.getAsBoolean(); exploit.setPrivileged(privileged); } JsonElement jsonPort = jsonObject.get("port"); if (jsonPort != null && !jsonPort.isJsonNull()) { int port = jsonPort.getAsInt(); exploit.setPort(port); } JsonArray jsonBid = jsonObject.getAsJsonArray("bid"); if (jsonBid != null) { String[] bid = new String[jsonBid.size()]; for (int i = 0; i < jsonBid.size(); i++) { bid[i] = jsonBid.get(i).getAsString(); } exploit.setBid(bid); } JsonArray jsonCve = jsonObject.getAsJsonArray("cve"); if (jsonCve != null) { String[] cve = new String[jsonCve.size()]; for (int i = 0; i < jsonCve.size(); i++) { cve[i] = jsonCve.get(i).getAsString(); } exploit.setCve(cve); } JsonArray jsonMsb = jsonObject.getAsJsonArray("msb"); if (jsonMsb != null) { String[] msb = new String[jsonMsb.size()]; for (int i = 0; i < jsonMsb.size(); i++) { msb[i] = jsonMsb.get(i).getAsString(); } exploit.setMsb(msb); } JsonArray jsonOsvdb = jsonObject.getAsJsonArray("osvdb"); if (jsonOsvdb != null) { String[] osvdb = new String[jsonOsvdb.size()]; for (int i = 0; i < jsonOsvdb.size(); i++) { osvdb[i] = jsonOsvdb.get(i).getAsString(); } exploit.setOsvdb(osvdb); } try { JsonArray jsonPlatform = jsonObject.getAsJsonArray("platform"); if (jsonPlatform != null && jsonPlatform.isJsonArray()) { String[] platform = new String[jsonPlatform.size()]; for (int i = 0; i < jsonPlatform.size(); i++) { platform[i] = jsonPlatform.get(i).getAsString(); } exploit.setPlatform(platform); } } catch (ClassCastException err) { JsonElement platPrimitive = jsonObject.get("platform"); if (platPrimitive != null && !platPrimitive.isJsonNull()) { exploit.setPlatform(new String[] { platPrimitive.getAsString() }); } } exploit.setId(id); exploit.setDescription(desc); exploit.setSource(source); return exploit; } }