List of usage examples for com.google.gson.internal LinkedTreeMap containsKey
@Override public boolean containsKey(Object key)
From source file:com.gemini.httpclienttest.HttpClientTestMain.java
public static void main(String[] args) { //authenticate with the server URL url;//from w w w .j av a 2 s.co m try { url = new URL("http://198.11.209.34:5000/v2.0/tokens"); } catch (MalformedURLException ex) { System.out.printf("Invalid Endpoint - not a valid URL {}", "http://198.11.209.34:5000/v2.0"); return; } CloseableHttpClient httpclient = HttpClientBuilder.create().build(); try { HttpPost httpPost = new HttpPost(url.toString()); httpPost.setHeader("Content-Type", "application/json"); StringEntity strEntity = new StringEntity( "{\"auth\":{\"tenantName\":\"Gemini-network-prj\",\"passwordCredentials\":{\"username\":\"sri\",\"password\":\"srikumar12\"}}}"); httpPost.setEntity(strEntity); //System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpPost); try { //get the response status code String respStatus = response.getStatusLine().getReasonPhrase(); //get the response body int bytes = response.getEntity().getContent().available(); InputStream body = response.getEntity().getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(body)); String line; StringBuffer sbJSON = new StringBuffer(); while ((line = in.readLine()) != null) { sbJSON.append(line); } String json = sbJSON.toString(); EntityUtils.consume(response.getEntity()); GsonBuilder gsonBuilder = new GsonBuilder(); Object parsedJson = gsonBuilder.create().fromJson(json, Object.class); System.out.printf("Parsed json is of type %s\n\n", parsedJson.getClass().toString()); if (parsedJson instanceof com.google.gson.internal.LinkedTreeMap) { com.google.gson.internal.LinkedTreeMap treeJson = (com.google.gson.internal.LinkedTreeMap) parsedJson; if (treeJson.containsKey("id")) { String s = (String) treeJson.getOrDefault("id", "no key provided"); System.out.printf("\n\ntree contained id %s\n", s); } else { System.out.printf("\n\ntree does not contain key id\n"); } } } catch (IOException ex) { System.out.print(ex); } finally { response.close(); } } catch (IOException ex) { System.out.print(ex); } finally { //lots of exceptions, just the connection and exit try { httpclient.close(); } catch (IOException | NoSuchMethodError ex) { System.out.print(ex); return; } } }
From source file:com.inverseinnovations.VBulletinAPI.VBulletinAPI.java
License:Apache License
/**Parses response, designed specifically for gathering the list of all messages. Messages only have the header at this point, the actual message is not included * @param response from callMethod//from w w w . j ava 2s. co m * @return ArrayList<Message> */ @SuppressWarnings({ "rawtypes", "unchecked" }) private static ArrayList<Message> parseMessages(LinkedTreeMap<String, Object> response) throws NoPermissionLoggedout, NoPermissionLoggedin, VBulletinAPIException { ArrayList<Message> messages = new ArrayList<Message>(); if (response != null) { if (response.containsKey("response")) { //Need more object Data Type checks LinkedTreeMap<String, Object> response2 = (LinkedTreeMap<String, Object>) response.get("response"); if (response2.containsKey("HTML")) { LinkedTreeMap<String, Object> HTML = (LinkedTreeMap<String, Object>) response2.get("HTML"); if (HTML.containsKey("messagelist_periodgroups")) { if (HTML.get("messagelist_periodgroups") instanceof LinkedTreeMap) { LinkedTreeMap<String, Object> messageGroup = (LinkedTreeMap<String, Object>) HTML .get("messagelist_periodgroups"); if (messageGroup.containsKey("messagesingroup")) { if ((double) (messageGroup.get("messagesingroup")) > 0) {//if there are messages if (messageGroup.containsKey("messagelistbits")) { if (messageGroup.get("messagelistbits") instanceof LinkedTreeMap) {//single message Message parsedMessage = new Message(); LinkedTreeMap<String, Object> message = (LinkedTreeMap<String, Object>) messageGroup .get("messagelistbits"); LinkedTreeMap<String, Object> pm = (LinkedTreeMap<String, Object>) message .get("messagelistbits"); parsedMessage.pmid = Functions.convertToInt(pm.get("pmid")); parsedMessage.sendtime = Functions.convertToInt(pm.get("sendtime")); parsedMessage.statusicon = Functions .convertToString(pm.get("statusicon")); parsedMessage.title = Functions.convertToString(pm.get("title")); parsedMessage.userid = Functions.convertToInt( ((LinkedTreeMap) ((LinkedTreeMap) message.get("userbit")) .get("userinfo")).get("userid")); parsedMessage.username = Functions.convertToString( ((LinkedTreeMap) ((LinkedTreeMap) message.get("userbit")) .get("userinfo")).get("username")); messages.add(parsedMessage); } else if (messageGroup.get("messagelistbits") instanceof ArrayList) {//multiple messages for (LinkedTreeMap<String, Object> message : (ArrayList<LinkedTreeMap<String, Object>>) messageGroup .get("messagelistbits")) { Message parsedMessage = new Message(); LinkedTreeMap<String, Object> pm = (LinkedTreeMap<String, Object>) message .get("messagelistbits"); parsedMessage.pmid = Functions.convertToInt(pm.get("pmid")); parsedMessage.sendtime = Functions.convertToInt(pm.get("sendtime")); parsedMessage.statusicon = Functions .convertToString(pm.get("statusicon")); parsedMessage.title = Functions.convertToString(pm.get("title")); parsedMessage.userid = Functions.convertToInt( ((LinkedTreeMap) ((LinkedTreeMap) message.get("userbit")) .get("userinfo")).get("userid")); parsedMessage.username = Functions.convertToString( ((LinkedTreeMap) ((LinkedTreeMap) message.get("userbit")) .get("userinfo")).get("username")); messages.add(parsedMessage); } } } } } } else if (HTML.get("messagelist_periodgroups") instanceof ArrayList) { ArrayList messageGroups = (ArrayList) HTML.get("messagelist_periodgroups"); for (Object obj : messageGroups) { LinkedTreeMap messageGroup = (LinkedTreeMap) obj; if (messageGroup.containsKey("messagesingroup")) { if ((double) (messageGroup.get("messagesingroup")) > 0) {//if there are messages if (messageGroup.containsKey("messagelistbits")) { if (messageGroup.get("messagelistbits") instanceof LinkedTreeMap) {//single message Message parsedMessage = new Message(); LinkedTreeMap<String, Object> message = (LinkedTreeMap<String, Object>) messageGroup .get("messagelistbits"); LinkedTreeMap<String, Object> pm = (LinkedTreeMap<String, Object>) message .get("messagelistbits"); parsedMessage.pmid = Functions.convertToInt(pm.get("pmid")); parsedMessage.sendtime = Functions.convertToInt(pm.get("sendtime")); parsedMessage.statusicon = Functions .convertToString(pm.get("statusicon")); parsedMessage.title = Functions.convertToString(pm.get("title")); parsedMessage.userid = Functions.convertToInt( ((LinkedTreeMap) ((LinkedTreeMap) message.get("userbit")) .get("userinfo")).get("userid")); parsedMessage.username = Functions.convertToString( ((LinkedTreeMap) ((LinkedTreeMap) message.get("userbit")) .get("userinfo")).get("username")); messages.add(parsedMessage); } else if (messageGroup.get("messagelistbits") instanceof ArrayList) {//multiple messages for (LinkedTreeMap<String, Object> message : (ArrayList<LinkedTreeMap<String, Object>>) messageGroup .get("messagelistbits")) { Message parsedMessage = new Message(); LinkedTreeMap<String, Object> pm = (LinkedTreeMap<String, Object>) message .get("messagelistbits"); parsedMessage.pmid = Functions.convertToInt(pm.get("pmid")); parsedMessage.sendtime = Functions .convertToInt(pm.get("sendtime")); parsedMessage.statusicon = Functions .convertToString(pm.get("statusicon")); parsedMessage.title = Functions .convertToString(pm.get("title")); parsedMessage.userid = Functions .convertToInt(((LinkedTreeMap) ((LinkedTreeMap) message .get("userbit")).get("userinfo")) .get("userid")); parsedMessage.username = Functions.convertToString( ((LinkedTreeMap) ((LinkedTreeMap) message .get("userbit")).get("userinfo")) .get("username")); messages.add(parsedMessage); } } } } } } } } } Functions.responseErrorCheck(response); } } System.out.println("message all ->"); System.out.println(response.toString()); return messages; }
From source file:com.inverseinnovations.VBulletinAPI.VBulletinAPI.java
License:Apache License
/**Grabs the 'errormessage' from within the json pulled form callMethod() * Known errors:// w w w .j a v a 2 s.c o m * pm_messagesent = message successfully sent * pmrecipientsnotfound = Forum user doesn't exist * invalid_accesstoken * @param response data from callMethod() * @return the 'errormessage' inside, if none: null */ @SuppressWarnings("rawtypes") private static String parseResponse(LinkedTreeMap<String, Object> response) { //LinkedTreeMap response = (LinkedTreeMap) response2; String theReturn = null; String className = null; if (response != null) { if (DEBUG) { System.out.println("all ->"); System.out.println(response.toString()); } if (response.containsKey("response")) { //errormessage if (((LinkedTreeMap) response.get("response")).containsKey("errormessage")) { className = ((LinkedTreeMap) response.get("response")).get("errormessage").getClass().getName(); if (className.equals("java.lang.String")) { theReturn = ((String) ((LinkedTreeMap) response.get("response")).get("errormessage")); if (theReturn.equals("redirect_postthanks")) {//this is for newthread and newpost if (response.containsKey("show")) { if (((LinkedTreeMap) response.get("show")).containsKey("threadid")) { theReturn = (String) ((LinkedTreeMap) response.get("show")).get("threadid"); theReturn += " " + (double) ((LinkedTreeMap) response.get("show")).get("postid"); } } } } else if (className.equals("java.util.ArrayList")) { Object[] errors = ((ArrayList) ((LinkedTreeMap) response.get("response")) .get("errormessage")).toArray(); if (errors.length > 0) { theReturn = errors[0].toString(); } } else { System.out.println("responseError response -> errormessage type unknown: " + className); } } //HTML else if (((LinkedTreeMap) response.get("response")).containsKey("HTML")) { LinkedTreeMap HTML = (LinkedTreeMap) ((LinkedTreeMap) response.get("response")).get("HTML"); if (HTML.containsKey("totalmessages")) { theReturn = "totalmessages"; } else if (HTML.containsKey("postpreview")) { if (HTML.get("postpreview") instanceof LinkedTreeMap) { LinkedTreeMap postpreview = (LinkedTreeMap) HTML.get("postpreview"); if (postpreview.containsKey("errorlist")) { if (postpreview.get("errorlist") instanceof LinkedTreeMap) { LinkedTreeMap errorlist = (LinkedTreeMap) postpreview.get("errorlist"); if (errorlist.containsKey("errors")) { if (errorlist.get("errors") instanceof ArrayList) { ArrayList errors = (ArrayList) errorlist.get("errors"); if (errors.get(0) instanceof ArrayList) { //response -> postpreview -> errorlist -> errors[0] ArrayList errorSub = (ArrayList) errors.get(0); theReturn = errorSub.get(0).toString(); } } } } } } } } //errorlist else if (((LinkedTreeMap) response.get("response")).containsKey("errorlist")) { ArrayList errorlist = (ArrayList) ((LinkedTreeMap) response.get("response")).get("errorlist"); System.out.println("Unknown Responses(errorlsit ->): " + errorlist.toString()); } else {//has response..but not common System.out.println( "Unknown Responses: " + ((LinkedTreeMap) response.get("response")).keySet().toString()); } } else if (response.containsKey("custom")) { theReturn = (String) response.get("custom"); } //testing this: } //Base.Console.debug("SC2Mafia API return error: "+theReturn); return theReturn; }
From source file:edu.vt.vbi.patric.portlets.Downloads.java
License:Apache License
public static boolean isLoggedIn(PortletRequest request) { String sessionId = request.getPortletSession(true).getId(); Gson gson = new Gson(); LinkedTreeMap sessionMap = gson.fromJson(SessionHandler.getInstance().get(sessionId), LinkedTreeMap.class); return sessionMap.containsKey("authorizationToken"); }
From source file:edu.vt.vbi.patric.portlets.FIGfam.java
License:Apache License
public boolean isLoggedIn(PortletRequest request) { String sessionId = request.getPortletSession(true).getId(); Gson gson = new Gson(); LinkedTreeMap sessionMap = gson.fromJson(SessionHandler.getInstance().get(sessionId), LinkedTreeMap.class); return sessionMap.containsKey("authorizationToken"); }
From source file:edu.vt.vbi.patric.portlets.TranscriptomicsGene.java
License:Apache License
public String getAuthorizationToken(PortletRequest request) { String token = null;/*from ww w . ja v a 2s.c om*/ String sessionId = request.getPortletSession(true).getId(); Gson gson = new Gson(); LinkedTreeMap sessionMap = gson.fromJson(SessionHandler.getInstance().get(sessionId), LinkedTreeMap.class); if (sessionMap.containsKey("authorizationToken")) { token = (String) sessionMap.get("authorizationToken"); } return token; }
From source file:org.cgiar.ccafs.marlo.action.center.json.autosave.AutoSaveWriterAction.java
License:Open Source License
@Override public String execute() throws Exception { String fileId = ""; String fileClass = ""; String nameClass = ""; String fileAction = ""; status = new HashMap<String, Object>(); if (autoSave.length > 0) { Gson gson = new Gson(); byte ptext[] = autoSave[0].getBytes(ISO_8859_1); String value = new String(ptext, UTF_8); @SuppressWarnings("unchecked") LinkedTreeMap<String, Object> result = gson.fromJson(value, LinkedTreeMap.class); String userModifiedBy = fileId = (String) result.get("modifiedBy.id"); if (result.containsKey("id")) { fileId = (String) result.get("id"); } else {//from www. j a v a2 s.c o m fileId = "XX"; } if (result.containsKey("className")) { String ClassName = (String) result.get("className"); String[] composedClassName = ClassName.split("\\."); nameClass = ClassName; fileClass = composedClassName[composedClassName.length - 1]; } if (result.containsKey("actionName")) { fileAction = (String) result.get("actionName"); fileAction = fileAction.replace("/", "_"); } ArrayList<String> keysRemove = new ArrayList<>(); for (Map.Entry<String, Object> entry : result.entrySet()) { if (entry.getKey().contains("__")) { keysRemove.add(entry.getKey()); } } for (String string : keysRemove) { result.remove(string); } Date generatedDate = new Date(); result.put("activeSince", generatedDate); String jSon = gson.toJson(result); if (nameClass.equals(CenterOutcome.class.getName())) { jSon = jSon.replaceAll("outcome\\.", ""); } if (nameClass.equals(CenterOutput.class.getName())) { jSon = jSon.replaceAll("output\\.", ""); } if (nameClass.equals(CenterProject.class.getName())) { jSon = jSon.replaceAll("project\\.", ""); } if (nameClass.equals(CenterDeliverable.class.getName())) { jSon = jSon.replaceAll("deliverable\\.", ""); } if (nameClass.equals(CapacityDevelopment.class.getName())) { jSon = jSon.replaceAll("capdev\\.", ""); } try { String fileName = fileId + "_" + fileClass + "_" + fileAction + ".json"; String pathFile = config.getAutoSaveFolder(); Path path = Paths.get(pathFile); if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) { File file = new File(config.getAutoSaveFolder() + fileName); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8")); out.append(jSon).append("\r\n"); ; out.flush(); out.close(); } else { Files.createDirectories(path); File file = new File(config.getAutoSaveFolder() + fileName); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8")); out.append(jSon).append("\r\n"); ; out.flush(); out.close(); } status.put("status", true); SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss"); status.put("modifiedBy", userService.getUser(Long.parseLong(userModifiedBy)).getComposedCompleteName()); status.put("activeSince", dt.format(generatedDate)); } catch (IOException e) { status.put("status", false); e.printStackTrace(); } catch (Exception e) { status.put("status", false); e.printStackTrace(); } } return Action.SUCCESS; }
From source file:org.cgiar.ccafs.marlo.action.json.autosave.AutoSaveWriterAction.java
License:Open Source License
@Override public String execute() throws Exception { String fileId = ""; String fileClass = ""; String nameClass = ""; String fileAction = ""; status = new HashMap<String, Object>(); if (autoSave.length > 0) { Gson gson = new Gson(); byte ptext[] = autoSave[0].getBytes(ISO_8859_1); String value = new String(ptext, UTF_8); @SuppressWarnings("unchecked") LinkedTreeMap<String, Object> result = gson.fromJson(value, LinkedTreeMap.class); String userModifiedBy = fileId = (String) result.get("modifiedBy.id"); if (result.containsKey("id")) { fileId = (String) result.get("id"); } else {//ww w . j av a2 s. c o m fileId = "XX"; } if (result.containsKey("className")) { String ClassName = (String) result.get("className"); String[] composedClassName = ClassName.split("\\."); nameClass = ClassName; fileClass = composedClassName[composedClassName.length - 1]; } if (result.containsKey("actionName")) { fileAction = (String) result.get("actionName"); fileAction = fileAction.replace("/", "_"); } ArrayList<String> keysRemove = new ArrayList<>(); for (Map.Entry<String, Object> entry : result.entrySet()) { if (entry.getKey().contains("__")) { keysRemove.add(entry.getKey()); } } for (String string : keysRemove) { result.remove(string); } Date generatedDate = new Date(); result.put("activeSince", generatedDate); String jSon = gson.toJson(result); if (nameClass.equals(ResearchOutcome.class.getName())) { jSon = jSon.replaceAll("outcome\\.", ""); } if (nameClass.equals(ResearchOutput.class.getName())) { jSon = jSon.replaceAll("output\\.", ""); } try { String fileName = fileId + "_" + fileClass + "_" + fileAction + ".json"; String pathFile = config.getAutoSaveFolder(); System.out.println(pathFile); Path path = Paths.get(pathFile); if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) { File file = new File(config.getAutoSaveFolder() + fileName); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8")); out.append(jSon).append("\r\n"); ; out.flush(); out.close(); } else { Files.createDirectories(path); File file = new File(config.getAutoSaveFolder() + fileName); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8")); out.append(jSon).append("\r\n"); ; out.flush(); out.close(); } status.put("status", true); SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss"); status.put("modifiedBy", userService.getUser(Long.parseLong(userModifiedBy)).getComposedCompleteName()); status.put("activeSince", dt.format(generatedDate)); } catch (IOException e) { status.put("status", false); e.printStackTrace(); } catch (Exception e) { status.put("status", false); e.printStackTrace(); } } return Action.SUCCESS; }