List of usage examples for org.json.simple.parser ParseException printStackTrace
public void printStackTrace()
From source file:com.autodesk.client.auth.OAuth2TwoLegged.java
/** * Get the access token in a 2-legged flow * @return/*from www . ja va 2 s . c o m*/ */ public Credentials authenticate() throws Exception { if (flow == OAuthFlow.application) { final String url = this.tokenUrl; Map<String, String> body = new HashMap<>(); body.put("grant_type", "client_credentials"); body.put("client_id", this.clientId); body.put("client_secret", this.clientSecret); String scopeStr = getScopes(); if (!scopeStr.isEmpty()) { body.put("scope", scopeStr); } Credentials response = null; try { String bodyResponse = post(url, body, new HashMap<String, String>()); JSONObject jsonObject = null; // get the access token from json try { jsonObject = (JSONObject) new JSONParser().parse(bodyResponse); String access_token = (String) jsonObject.get("access_token"); //calculate "expires at" long expires_in = (long) jsonObject.get("expires_in"); DateTime later = DateTime.now().plusSeconds((int) expires_in); Long expiresAt = later.toDate().getTime(); this.credentials = new Credentials(access_token, expiresAt); response = new Credentials(access_token, expiresAt); } catch (ParseException e) { throw new RuntimeException("Unable to parse json " + body); } } catch (IOException e) { System.err.println("Exception when trying to get access token"); e.printStackTrace(); } return response; } else { throw new Exception("getAccessToken requires application flow type"); } }
From source file:com.bloomberglp.blpapi.securitysearch.SecuritySearchApiConnection.java
public void run() { System.out.println("New connection started"); try {/*w ww . j a va2s . com*/ while (true) { System.out.println("Waiting"); int c = d_in.read(); System.out.println("Read " + c); char ch = (char) c; if (c == -1) { return; } if (ch != '{') { //if the first char isn't { we ignore it continue; } int brackets = 1; StringBuffer sb = new StringBuffer(); sb.append(ch); while (brackets > 0) { c = d_in.read(); ch = (char) c; if (c == -1) { return; } if (ch == '}') { --brackets; } if (ch == '{') { ++brackets; } sb.append(ch); } //we got a JSON object JSONParser parser = new JSONParser(); JSONObject jobj; try { jobj = (JSONObject) parser.parse(sb.toString()); } catch (ParseException e) { d_in.close(); d_out.close(); d_clientConnection.close(); e.printStackTrace(); return; } System.out.println(jobj.toJSONString()); parseRequest(jobj); processRequest(); returnResponse(); d_in.close(); d_out.close(); d_clientConnection.close(); return; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.autodesk.client.auth.OAuth2ThreeLegged.java
/** * Refresh the access token for a 3-legged flow * @return//ww w.j a va 2s . c om */ public ThreeLeggedCredentials refreshAccessToken(String refreshToken) { Map<String, String> formParams = new HashMap<>(); formParams.put("client_id", this.clientId); formParams.put("client_secret", this.clientSecret); formParams.put("grant_type", "refresh_token"); formParams.put("refresh_token", refreshToken); Map<String, String> headers = new HashMap<>(); headers.put("content-type", "application/x-www-form-urlencoded"); ThreeLeggedCredentials response = null; try { String responseBody = post(this.refreshTokenUrl, formParams, headers); JSONObject jsonObject = null; // get the access token from json try { jsonObject = (JSONObject) new JSONParser().parse(responseBody); String access_token = (String) jsonObject.get("access_token"); String refresh_token = (String) jsonObject.get("refresh_token"); //calculate "expires at" long expires_in = (long) jsonObject.get("expires_in"); DateTime later = DateTime.now().plusSeconds((int) expires_in); Long expiresAt = later.toDate().getTime(); response = new ThreeLeggedCredentials(access_token, expiresAt, refresh_token); } catch (ParseException e) { throw new RuntimeException("Unable to parse json " + responseBody); } } catch (IOException e) { System.err.println("Exception when trying to refresh token"); e.printStackTrace(); } catch (ApiException e) { System.err.println("Exception when trying to refresh token"); e.printStackTrace(); } return response; }
From source file:jp.aegif.nemaki.rest.GroupResource.java
private JSONArray parseJsonArray(String str) { JSONParser parser = new JSONParser(); Object obj;/*from w w w.ja v a2 s . c o m*/ try { obj = parser.parse(str); JSONArray result = (JSONArray) obj; return result; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return new JSONArray(); }
From source file:com.autodesk.client.auth.OAuth2ThreeLegged.java
/** * Get the access token for a 3-legged flow * @return/* w w w . java 2s.c o m*/ */ public ThreeLeggedCredentials getAccessToken(String code) throws Exception { if (flow == OAuthFlow.accessCode) { Map<String, String> formParams = new HashMap<>(); formParams.put("client_id", this.clientId); formParams.put("client_secret", this.clientSecret); formParams.put("code", code); formParams.put("grant_type", "authorization_code"); formParams.put("redirect_uri", this.redirectUri); Map<String, String> headers = new HashMap<>(); headers.put("content-type", "application/x-www-form-urlencoded"); ThreeLeggedCredentials response = null; try { String responseBody = post(this.tokenUrl, formParams, headers); JSONObject jsonObject = null; // get the access token from json try { jsonObject = (JSONObject) new JSONParser().parse(responseBody); String access_token = (String) jsonObject.get("access_token"); String refresh_token = (String) jsonObject.get("refresh_token"); //calculate "expires at" long expires_in = (long) jsonObject.get("expires_in"); DateTime later = DateTime.now().plusSeconds((int) expires_in); Long expiresAt = later.toDate().getTime(); response = new ThreeLeggedCredentials(access_token, expiresAt, refresh_token); } catch (ParseException e) { throw new RuntimeException("Unable to parse json " + responseBody); } } catch (IOException e) { System.err.println("Exception when trying to get access token"); e.printStackTrace(); } return response; } else { throw new Exception("getAuthToken requires accessCode flow type"); } }
From source file:com.turt2live.xmail.entity.folder.Folder.java
@SuppressWarnings("unchecked") @Override/*from w ww. ja v a 2 s. com*/ public void done(Request request, ServerResponse response) { this.mail.clear(); // Clear the folder box first... for (String line : response.getLines()) { JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { @Override public List<Object> creatArrayContainer() { return new LinkedList<Object>(); } @Override public Map<String, Object> createObjectContainer() { return new LinkedHashMap<String, Object>(); } }; Map<String, Object> map = new HashMap<String, Object>(); try { Map<?, ?> json = (Map<?, ?>) parser.parse(line, containerFactory); Iterator<?> iter = json.entrySet().iterator(); // Type check while (iter.hasNext()) { Entry<?, ?> entry = (Entry<?, ?>) iter.next(); if (!(entry.getKey() instanceof String)) { throw new IllegalArgumentException("Not in <String, Object> format"); } } map = (Map<String, Object>) json; } catch (ParseException e) { e.printStackTrace(); return; } catch (IllegalArgumentException e) { e.printStackTrace(); return; } Object mails = map.get("mail"); if (mails != null && mails instanceof List) { List<?> list = (List<?>) mails; List<Map<?, ?>> maps = new ArrayList<Map<?, ?>>(); for (Object o : list) { if (o instanceof Map) { maps.add((Map<?, ?>) o); } } List<Map<String, Object>> revisedMaps = new ArrayList<Map<String, Object>>(); for (Map<?, ?> o : maps) { Map<String, Object> m2 = new HashMap<String, Object>(); for (Object k : o.keySet()) { if (k instanceof String) { Object obj = o.get(k); m2.put((String) k, obj); } } revisedMaps.add(m2); } for (Map<String, Object> map2 : revisedMaps) { Mail m3 = Mail.fromJSON(map2); if (m3 != null) { if (!this.canReadMail) { m3.setViewOnly(true); } this.mail.put(this.mail.size() + 1, m3); } } } } if (lastFirst) { beforeUpdate = getUnread(); } reconstructFolder(); processing = false; }
From source file:de.fraunhofer.iosb.ivct.IVCTcommander.java
/** {@inheritDoc} */ @Override/*www . j a v a 2 s .c o m*/ public void onMessage(final Message message) { if (LOGGER.isTraceEnabled()) { LOGGER.trace("Received Command message"); } if (message instanceof TextMessage) { final TextMessage textMessage = (TextMessage) message; try { final String content = textMessage.getText(); try { JSONObject jsonObject = (JSONObject) jsonParser.parse(content); String commandTypeName = (String) jsonObject.get("commandType"); switch (commandTypeName) { case "announceVerdict": onMessageUiConsumer.run(jsonObject, IVCTcommander.listOfVerdicts); break; case "quit": // Should ignore break; case "setSUT": break; case "startTestCase": break; default: System.out.println("Unknown commandType name is: " + commandTypeName); break; } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (final JMSException ex) { LOGGER.warn("Problems with parsing Message", ex); } } }
From source file:eu.riscoss.rdc.RDCGithub.java
private JSONAware parse(String json) { JSONAware jv = null;//from w w w. j a v a2s . co m if (json.startsWith("WARNING")) { System.err.println(json); //error message - implement different handling if needed } else try { jv = (JSONAware) new JSONParser().parse(json); } catch (ParseException e) { e.printStackTrace();//TODO } return jv; }
From source file:com.larryTheCoder.schematic.IslandBlock.java
/** * Sets this block's sign data//from w w w. j a va 2 s . c om * * @param tileData */ public void setSign(Map<String, Tag> tileData) { signText = new ArrayList<>(); List<String> text = new ArrayList<>(); for (int i = 1; i < 5; i++) { String line = ((StringTag) tileData.get("Text" + String.valueOf(i))).getValue(); // This value can actually be a string that says null sometimes. if (line.equalsIgnoreCase("null")) { line = ""; } //System.out.println("DEBUG: line " + i + " = '"+ line + "' of length " + line.length()); text.add(line); } JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { @Override public List creatArrayContainer() { return new LinkedList<>(); } @Override public Map createObjectContainer() { return new LinkedHashMap<>(); } }; // This just removes all the JSON formatting and provides the raw text for (int line = 0; line < 4; line++) { String lineText = ""; if (!text.get(line).equals("\"\"") && !text.get(line).isEmpty()) { //String lineText = text.get(line).replace("{\"extra\":[\"", "").replace("\"],\"text\":\"\"}", ""); //Bukkit.getLogger().info("DEBUG: sign text = '" + text.get(line) + "'"); if (text.get(line).startsWith("{")) { // JSON string try { Map json = (Map) parser.parse(text.get(line), containerFactory); List list = (List) json.get("extra"); //System.out.println("DEBUG1:" + JSONValue.toJSONString(list)); if (list != null) { Iterator iter = list.iterator(); while (iter.hasNext()) { Object next = iter.next(); String format = JSONValue.toJSONString(next); //System.out.println("DEBUG2:" + format); // This doesn't see right, but appears to be the easiest way to identify this string as JSON... if (format.startsWith("{")) { // JSON string Map jsonFormat = (Map) parser.parse(format, containerFactory); Iterator formatIter = jsonFormat.entrySet().iterator(); while (formatIter.hasNext()) { Map.Entry entry = (Map.Entry) formatIter.next(); //System.out.println("DEBUG3:" + entry.getKey() + "=>" + entry.getValue()); String key = entry.getKey().toString(); String value = entry.getValue().toString(); if (key.equalsIgnoreCase("color")) { try { lineText += TextFormat.valueOf(value.toUpperCase()); } catch (Exception noColor) { Utils.ConsoleMsg("Unknown color " + value + " in sign when pasting schematic, skipping..."); } } else if (key.equalsIgnoreCase("text")) { lineText += value; } else // Formatting - usually the value is always true, but check just in case if (key.equalsIgnoreCase("obfuscated") && value.equalsIgnoreCase("true")) { lineText += TextFormat.OBFUSCATED; } else if (key.equalsIgnoreCase("underlined") && value.equalsIgnoreCase("true")) { lineText += TextFormat.UNDERLINE; } else { // The rest of the formats try { lineText += TextFormat.valueOf(key.toUpperCase()); } catch (Exception noFormat) { // Ignore //System.out.println("DEBUG3:" + key + "=>" + value); Utils.ConsoleMsg("Unknown format " + value + " in sign when pasting schematic, skipping..."); } } } } else// This is unformatted text. It is included in "". A reset is required to clear // any previous formatting { if (format.length() > 1) { lineText += TextFormat.RESET + format.substring(format.indexOf('"') + 1, format.lastIndexOf('"')); } } } } else { // No extra tag json = (Map) parser.parse(text.get(line), containerFactory); String value = (String) json.get("text"); //System.out.println("DEBUG text only?:" + value); lineText += value; } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else // This is unformatted text (not JSON). It is included in "". if (text.get(line).length() > 1) { try { lineText = text.get(line).substring(text.get(line).indexOf('"') + 1, text.get(line).lastIndexOf('"')); } catch (Exception e) { //There may not be those "'s, so just use the raw line lineText = text.get(line); } } else { // just in case it isn't - show the raw line lineText = text.get(line); } //Bukkit.getLogger().info("Line " + line + " is " + lineText); } signText.add(lineText); } }
From source file:QuestionGUI.java
/** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor./*from ww w. jav a 2 s. co m*/ */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { bkgPanel = bkgPanel = new BKGPanel(); jScrollPane1 = new javax.swing.JScrollPane(); questionSelect = new javax.swing.JList(); jScrollPane2 = new javax.swing.JScrollPane(); questionText = new javax.swing.JTextArea(); questionNext = new javax.swing.JButton(); questionPrevious = new javax.swing.JButton(); submit = new javax.swing.JButton(); exit = new javax.swing.JButton(); questionIndexDisplay = new javax.swing.JLabel(); bingHelp = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("TRIVIA!!!!!!!!!!!!!!!!!!!!"); setResizable(false); bkgPanel.setBackground(new java.awt.Color(204, 0, 0)); jScrollPane1.setViewportView(questionSelect); questionText.setEditable(false); questionText.setColumns(20); questionText.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N questionText.setLineWrap(true); questionText.setRows(5); questionText.setWrapStyleWord(true); jScrollPane2.setViewportView(questionText); questionNext.setText("Lock in Answer; Next Question"); questionNext.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { try { questionNextActionPerformed(evt); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); questionPrevious.setText("Previous Question"); questionPrevious.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { questionPreviousActionPerformed(evt); } }); submit.setText("Submit"); submit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { submitActionPerformed(evt); } }); exit.setText("Exit"); exit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitActionPerformed(evt); } }); questionIndexDisplay.setForeground(new java.awt.Color(204, 204, 255)); questionIndexDisplay.setText(" "); bingHelp.setText("Get Help from the G: 3 Left"); bingHelp.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bingHelpActionPerformed(evt); } }); javax.swing.GroupLayout bkgPanelLayout = new javax.swing.GroupLayout(bkgPanel); bkgPanel.setLayout(bkgPanelLayout); bkgPanelLayout.setHorizontalGroup(bkgPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(bkgPanelLayout.createSequentialGroup().addContainerGap().addGroup(bkgPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane2) .addGroup(bkgPanelLayout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 502, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(bkgPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(questionNext, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE) .addComponent(questionPrevious, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(submit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(exit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(bingHelp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(bkgPanelLayout.createSequentialGroup() .addComponent(questionIndexDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); bkgPanelLayout.setVerticalGroup(bkgPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(bkgPanelLayout.createSequentialGroup().addGap(7, 7, 7) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(bkgPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(bkgPanelLayout.createSequentialGroup().addGap(84, 84, 84) .addComponent(questionNext) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(questionPrevious) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(exit) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(submit) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(bingHelp)) .addGroup(bkgPanelLayout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(questionIndexDisplay) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(bkgPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( bkgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); pack(); }