public static void stringToFile(String filepath, String s) { /* creating empty file */ File file = null; try { /* create new file */ file = new File(filepath); file.createNewFile(); boolean goodFile = file.canWrite(); if (!goodFile) { System.out.println("can't make new file..."); return; } } catch (Exception e) { e.printStackTrace(); return; } /* prepare for writing to the file */ PrintWriter writer; try { writer = new PrintWriter(filepath, "UTF-8"); } catch (Exception e) { e.printStackTrace(); return; } writer.write(s); // String string = JSON.stringify(mixnet); /* javascript :( */ writer.close(); /* in JAVA we can't close file. or open it. */ }
public StockQuote getStockQuote(String ticker) { StockQuote quote = new StockQuote(); if (!ticker.isEmpty()) { System.out.println("Getting data for: " + ticker); try { String queryUrl = BASE_URL + YQL + "'" + ticker + "'" + FORMAT; URL url = new URL(queryUrl); System.out.println(queryUrl); URLConnection yc = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); String inputLine = ""; String buffer = ""; while ((inputLine = in.readLine()) != null) { buffer += inputLine; } try { System.out.println("Called at initialization: " + inputLine); JSONObject result = new JSONObject(buffer); System.out.println("Received Data: " + result); } catch (Exception e) { e.printStackTrace(); } in.close(); } catch (Exception e) { e.printStackTrace(); } } return quote; }
public void applySettingStatusReport(JSONObject js) { /** * This breaks the mold a bit.. but its more efficient. This gets called off the top of * ParseJson if it has an "SR" in it. Sr's are called so often that instead of walking the * normal parsing tree.. this skips to the end */ try { Iterator ii = js.keySet().iterator(); // This is a special multi single value response object while (ii.hasNext()) { String key = ii.next().toString(); responseCommand rc = new responseCommand(MNEMONIC_GROUP_SYSTEM, key.toString(), js.get(key).toString()); TinygDriver.getInstance().machine.applyJsonStatusReport(rc); // _applySettings(rc.buildJsonObject(), rc.getSettingParent()); //we will // supply the parent object name for each key pair } setChanged(); message[0] = "STATUS_REPORT"; message[1] = null; notifyObservers(message); } catch (Exception ex) { logger.error("[!] Error in applySettingStatusReport(JsonOBject js) : " + ex.getMessage()); logger.error("[!]js.tostring " + js.toString()); setChanged(); message[0] = "STATUS_REPORT"; message[1] = null; notifyObservers(message); } }
// Lee la configuracion que se encuentra en conf/RegistryConf private void readConfXml() { try { File inputFile = new File("src/java/Conf/RegistryConf.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("RegistryConf"); for (int i = 0; i < nList.getLength(); i++) { Node nNode = nList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; RegistryConf reg = new RegistryConf(); String aux; int idSensor; int value; aux = eElement.getElementsByTagName("idSensor").item(0).getTextContent(); idSensor = Integer.parseInt(aux); reg.setIdSensor(idSensor); aux = eElement.getElementsByTagName("saveType").item(0).getTextContent(); reg.setSaveTypeString(aux); aux = eElement.getElementsByTagName("value").item(0).getTextContent(); value = Integer.parseInt(aux); reg.setValue(value); registryConf.add(reg); lastRead.put(idSensor, 0); } } } catch (Exception e) { e.printStackTrace(); } }
public void markLastRead() { try { if (mLastReadUrl != null) { NetworkUtils.get(Constants.BASE_URL + mLastReadUrl); } } catch (Exception e) { e.printStackTrace(); } }
/** * Returns the configuration object found at the requested path. * * @param path The search path * @return The config value * @throws ConfigException thrown if nothing is found at the path */ public Object get(String path) throws ConfigException { try { Object obj = Utils.findPath(configObject, context + path); if (obj == null) throw new ConfigException(""); return obj; } catch (Exception e) { e.printStackTrace(); throw new ConfigException("No config object found at " + path); } }
public void onReceive(ByteBuffer b) { if (b.remaining() == 4) { Common.logError("invalid packet:"); byte[] data = new byte[b.remaining()]; b.get(data); Common.print_dump(data); // retry( TLMessage.keyAt( TLMessage.size() - 1) ); return; } try { if (b.getLong() == 0) { // auth_key_id b.getLong(); // message_id b.getInt(); // message length process(TL.deserialize(b)); } else { byte[] msg_key = new byte[16]; b.get(msg_key); byte[] data = new byte[b.remaining()]; b.get(data); synchronized (aes) { aes.prepare(false, msg_key, auth_key); data = aes.IGE(data, false); } ByteBuffer btmp = ByteBuffer.wrap(data); btmp.order(ByteOrder.LITTLE_ENDIAN); server_salt = btmp.getLong(); btmp.getLong(); // session_id cur_message_id = btmp.getLong(); // message_id cur_msg_seq = btmp.getInt(); // seq_no // if (cur_msg_seq > seqno) // seqno = cur_msg_seq + cur_msg_seq % 2; int bsize = btmp.getInt(); if (bsize < 0 || bsize > 1024 * 1024) { Common.print_dump(btmp.array()); Common.logError(new String(btmp.array())); Common.logError("FFFUUUUUUUUUUU!!!"); } b = BufferAlloc(bsize); btmp.get(b.array()); b.getInt(); ack_message(cur_msg_seq, cur_message_id); b.position(0); process(TL.deserialize(b)); send_accept(); } } catch (Exception e) { e.printStackTrace(); } }
/** Helper method for main to load tests into a thread group and start each test case. */ private static void loadThread(String name) { try { ThreadGroup myThreadGroup = new ThreadGroup(name); String urlSpec = "http://54.201.122.231:8181/v4/gumball"; // String urlSpec = "http://localhost:8080/v4/gumball" ; RunLoadTest test = new RunLoadTest(myThreadGroup, urlSpec); test.runTest(); // Run The Test in its own thread myThreadGroup.list(); } catch (Exception e) { System.out.println("ERROR: " + e.getMessage()); } }
@RequestMapping( value = "/home", method = {RequestMethod.GET, RequestMethod.POST}) public String getCity(HttpServletRequest request, ModelMap model) { try { List<City> cityList = cityService.getCity(); utilities.setSuccessResponse(response, cityList); } catch (Exception ex) { logger.error("getCity :" + ex.getMessage()); } model.addAttribute("model", response); return "home"; }
/** * Performs a test by sending HTTP Gets to the URL, and records the number of bytes returned. Each * test results are documented and displayed in standard out with the following information: * * <p>URL - The source URL of the test REQ CNT - How many times this test has run START - The * start time of the last test run STOP - The stop time of the last test run THREAD - The thread * id handling this test case RESULT - The result of the test. Either the number of bytes returned * or an error message. */ private void doTest() { String result = ""; this.reqCount++; // Connect and run test steps Date startTime = new Date(); try { ClientResource client = new ClientResource(this.myURL); // place order JSONObject json = new JSONObject(); json.put("payment", "quarter"); json.put("action", "place-order"); client.post(new JsonRepresentation(json), MediaType.APPLICATION_JSON); // Get Gumball Count Representation result_string = client.get(); JSONObject json_count = new JSONObject(result_string.getText()); result = Integer.toString((int) json_count.get("count")); } catch (Exception e) { result = e.toString(); } finally { Date stopTime = new Date(); // Print Report of Result: System.out.println( "======================================\n" + "URL => " + this.myURL + "\n" + "REQ CNT => " + this.reqCount + "\n" + "START => " + startTime + "\n" + "STOP => " + stopTime + "\n" + "THREAD => " + Thread.currentThread().getName() + "\n" + "RESULT => " + result + "\n"); } }
// Guarda registro en la BD public boolean saveRegistry(int idSensor, int value) { try { Registry r = new Registry(); r.setIdsensor(idSensor); r.setValue(value); r.setDate(new Date()); Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); session.save(r); tx.commit(); session.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public Resolution getUniqueValues() throws JSONException { JSONObject json = new JSONObject(); json.put("success", Boolean.FALSE); try { Layer layer = applicationLayer.getService().getSingleLayer(applicationLayer.getLayerName()); if (layer != null && layer.getFeatureType() != null) { SimpleFeatureType sft = layer.getFeatureType(); List<String> beh = sft.calculateUniqueValues(attribute); json.put("uniqueValues", new JSONArray(beh)); json.put("success", Boolean.TRUE); } } catch (Exception e) { json.put("msg", e.toString()); } return new StreamingResolution("application/json", new StringReader(json.toString())); }
// El string debe estar em formato json public boolean updateRegistry(String dataJson) { try { // Crea objeto json con string por parametro JSONObject json = new JSONObject(dataJson); // Obtiene el array de Registos JSONArray arr = json.getJSONArray("Registry"); // Recorre el array for (int i = 0; i < arr.length(); i++) { // Obtiene los datos idSensor y value int idSensor = arr.getJSONObject(i).getInt("idSensor"); int value = arr.getJSONObject(i).getInt("value"); // Recorre la configuracion de registro for (RegistryConf reg : registryConf) { // Se fija si el registro corresponde a esta configuracion if (reg.getIdSensor() == idSensor) { // Checkea el criterio para guardar, o no en la BD // Checkea tambien si el valor es igual al anterior if (reg.getSaveTypeString() == "ONCHANGE" && lastRead.get(idSensor) != value) { // Actualizo la ultima lectura y guardo en la BD lastRead.put(idSensor, value); saveRegistry(idSensor, value); } else if (reg.getSaveTypeString() == "ONTIME") { // Variables auxiliares, para checkear tiempo Long auxLong = System.currentTimeMillis() / 1000; int now = auxLong.intValue(); int timeToSave = lastRead.get(idSensor) + reg.getValue(); // Checkea si ya es tiempo para guerdar un nuevo registro if (now >= timeToSave) { // Actualizo el ultimo guardado lastRead.put(idSensor, now); saveRegistry(idSensor, value); } } } } } } catch (Exception e) { e.printStackTrace(); } return false; }
private boolean is200(String url) { if (DEBUG) log("is200() :: url=%s", url); HttpURLConnection conn = null; try { conn = (HttpURLConnection) new URL(url).openConnection(); return conn.getResponseCode() == 200; } catch (Exception ex) { if (DEBUG) ex.printStackTrace(); return false; } finally { if (conn != null) try { conn.disconnect(); } catch (Exception ex) { if (DEBUG) ex.printStackTrace(); } } }
public void invoke(String name, TL.Object obj) { Method m; try { m = MTProto.class.getDeclaredMethod("TL_" + name.replace(".", "_"), TL.Object.class); m.setAccessible(true); } catch (Exception e) { Common.logWarning("not handled: Main::" + name); return; } try { Common.logInfo("invoke: Main::" + name); m.invoke(this, obj); } catch (Exception e) { Common.logError("invoke error in Main"); e.printStackTrace(); } }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.receiver); final Button cancelbtn = (Button) findViewById(R.id.ButtonCancel); // cancelbtn.setEnabled(false); cancelbtn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // streamtask.cancel(true); finish(); } }); try { Intent intent = getIntent(); String action = intent.getAction(); String type = intent.getType(); if ((!(Intent.ACTION_SEND.equals(action))) || (type == null)) { throw new RuntimeException("Unknown intent action or type"); } if (!("text/plain".equals(type))) { throw new RuntimeException("Type is not text/plain"); } String extra = intent.getStringExtra(Intent.EXTRA_TEXT); if (extra == null) { throw new RuntimeException("Cannot get shared text"); } final DownloadStreamTask streamtask = new DownloadStreamTask(this); // Once created, a task is executed very simply: streamtask.execute(extra); } catch (Exception e) { Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show(); } }
@DELETE @Path("/{id}") @Consumes(MediaType.APPLICATION_JSON) @Produces("text/plain") public String deleteNote(String inputData, @PathParam("id") int id) { JSONObject inputJSON = new JSONObject(inputData); if ((inputJSON.has("sessionID")) && UsersController.checkLogin(inputJSON.getString("sessionID"))) { try { hibernate.controllers.NotesController notesController = new hibernate.controllers.NotesController(); notesController.deleteNote(id); return "OK"; } catch (Exception ex) { return ex.getMessage(); } } else return "Invalid session!"; }
private void process(TL.Object obj) { // if (obj instanceof TL.Vector) // return; Method m; try { m = MTProto.class.getDeclaredMethod("TL_" + obj.type.replace(".", "_"), TL.Object.class); m.setAccessible(true); } catch (Exception e) { Common.logWarning("not handled: " + obj.type + " (" + obj.name + ")"); return; } try { Common.logInfo("invoke: " + obj.type + " (" + obj.name + ")"); m.invoke(this, obj); } catch (Exception e) { Common.logError("process error"); e.printStackTrace(); } }
public static void main(String args[]) { if (args.length < 1) { System.err.println( "Usage: java org.globusonline.transfer.ErrorTest " + "username [cafile certfile keyfile [baseurl]]]"); System.exit(1); } String username = args[0]; String cafile = null; if (args.length > 1 && args[1].length() > 0) cafile = args[1]; String certfile = null; if (args.length > 2 && args[2].length() > 0) certfile = args[2]; String keyfile = null; if (args.length > 3 && args[3].length() > 0) keyfile = args[3]; String baseUrl = null; if (args.length > 4 && args[4].length() > 0) baseUrl = args[4]; try { JSONTransferAPIClient c = new JSONTransferAPIClient(username, cafile, certfile, keyfile, baseUrl); System.out.println("base url: " + c.getBaseUrl()); ErrorTest et = new ErrorTest(c); et.run(); // test auth error System.out.println("=== 400 Auth Failed ==="); c = new JSONTransferAPIClient(username, cafile, null, null, baseUrl); try { c.getResult("/tasksummary"); } catch (APIError e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("HI From data supply manager"); String app = (String) request.getParameter("app"); String sdate = (String) request.getParameter("sdate"); String edate = (String) request.getParameter("edate"); String typeOfChart = (String) request.getParameter("typeOfChart"); String typeofReport = (String) request.getParameter("typeOfReport"); System.out.println( "In data supply : App name : " + app + " sdate : " + sdate + " edate : " + edate + " type of chart : " + typeOfChart + " type of report :" + typeofReport); Main mainthread = new Main(); JSONArray htags = new JSONArray(); try { htags.put(mainthread.getTickets(typeOfChart, app, typeofReport, sdate, edate)); } catch (Exception e) { e.printStackTrace(); } response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); out.print(htags); }
private void notifyUser(Object message) { try { if (message instanceof JSONObject) { final JSONObject obj = (JSONObject) message; this.runOnUiThread( new Runnable() { public void run() { Toast.makeText(getApplicationContext(), obj.toString(), Toast.LENGTH_LONG).show(); Log.i("Received msg : ", String.valueOf(obj)); } }); } else if (message instanceof String) { final String obj = (String) message; this.runOnUiThread( new Runnable() { public void run() { Toast.makeText(getApplicationContext(), obj, Toast.LENGTH_LONG).show(); Log.i("Received msg : ", obj.toString()); } }); } else if (message instanceof JSONArray) { final JSONArray obj = (JSONArray) message; this.runOnUiThread( new Runnable() { public void run() { Toast.makeText(getApplicationContext(), obj.toString(), Toast.LENGTH_LONG).show(); Log.i("Received msg : ", obj.toString()); } }); } } catch (Exception e) { e.printStackTrace(); } }
private boolean saveAndVerify(ZipInputStream data) throws IOException { try { ZipEntry ze; while ((ze = data.getNextEntry()) != null) { // Filename + reference to file. String filename = ze.getName(); File output = new File(local_path + filename); if (filename.endsWith("/")) { output.mkdirs(); } else { if (output.exists()) { // Delete the file if it already exists. if (!output.delete()) { return false; } } if (output.createNewFile()) { FileOutputStream out = new FileOutputStream(output); byte[] buffer = new byte[1024]; int count; while ((count = data.read(buffer)) != -1) { out.write(buffer, 0, count); } } else { return false; } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { data.close(); } return true; }
@RequestMapping( value = "/getCityApi", method = {RequestMethod.GET, RequestMethod.POST}) public String getCityApi( HttpServletRequest request, @RequestParam(value = "locationname") String locationname, ModelMap model) { Map<Object, Object> map = new HashMap<Object, Object>(); JSONArray jSONArray = new JSONArray(); try { String status = "active"; List<City> cityList = cityService.getCityApi(locationname, status); if (cityList != null && cityList.size() > 0) { for (int i = 0; i < cityList.size(); i++) { City city = (City) cityList.get(i); String cityName = (String) city.getCity(); String stateName = (String) city.getState(); int ID = (Integer) (city.getId()); JSONObject jSONObject = new JSONObject(); jSONObject.put("id", ID); jSONObject.put("text", cityName); jSONArray.put(jSONObject); } utilities.setSuccessResponse(response, jSONArray.toString()); } else { throw new ConstException(ConstException.ERR_CODE_NO_DATA, ConstException.ERR_MSG_NO_DATA); } } catch (Exception ex) { logger.error("getCity :" + ex.getMessage()); utilities.setErrResponse(ex, response); } model.addAttribute("model", jSONArray.toString()); return "home"; }
public static ArrayList<ContentValues> parsePosts( TagNode aThread, int aThreadId, int unreadIndex, int opId, AwfulPreferences prefs, int startIndex) { ArrayList<ContentValues> result = new ArrayList<ContentValues>(); boolean lastReadFound = false; int index = startIndex; String update_time = new Timestamp(System.currentTimeMillis()).toString(); Log.v(TAG, "Update time: " + update_time); try { if (!Constants.isICS() || !prefs.inlineYoutube) { // skipping youtube support for now, it kinda sucks. aThread = convertVideos(aThread); } TagNode[] postNodes = aThread.getElementsByAttValue("class", "post", true, true); for (TagNode node : postNodes) { // fyad status, to prevent processing postbody twice if we are in fyad ContentValues post = new ContentValues(); post.put(THREAD_ID, aThreadId); // We'll just reuse the array of objects rather than create // a ton of them int id = Integer.parseInt(node.getAttributeByName("id").replaceAll("post", "")); post.put(ID, id); post.put(AwfulProvider.UPDATED_TIMESTAMP, update_time); post.put(POST_INDEX, index); if (index > unreadIndex) { post.put(PREVIOUSLY_READ, 0); lastReadFound = true; } else { post.put(PREVIOUSLY_READ, 1); } index++; post.put(IS_MOD, 0); post.put(IS_ADMIN, 0); TagNode[] postContent = node.getElementsHavingAttribute("class", true); for (TagNode pc : postContent) { if (pc.getAttributeByName("class").contains("author")) { post.put(USERNAME, pc.getText().toString().trim()); } if (pc.getAttributeByName("class").contains("role-mod")) { post.put(IS_MOD, 1); } if (pc.getAttributeByName("class").contains("role-admin")) { post.put(IS_ADMIN, 1); } if (pc.getAttributeByName("class").equalsIgnoreCase("title") && pc.getChildTags().length > 0) { TagNode[] avatar = pc.getElementsByName("img", true); if (avatar.length > 0) { post.put(AVATAR, avatar[0].getAttributeByName("src")); } post.put(AVATAR_TEXT, pc.getText().toString().trim()); } if (pc.getAttributeByName("class").equalsIgnoreCase("postbody") || pc.getAttributeByName("class").contains("complete_shit")) { TagNode[] images = pc.getElementsByName("img", true); for (TagNode img : images) { // don't alter video mock buttons if ((img.hasAttribute("class") && img.getAttributeByName("class").contains("videoPlayButton"))) { continue; } boolean dontLink = false; TagNode parent = img.getParent(); String src = img.getAttributeByName("src"); if ((parent != null && parent.getName().equals("a")) || (img.hasAttribute("class") && img.getAttributeByName("class") .contains("nolink"))) { // image is linked, don't override dontLink = true; } if (src.contains(".gif")) { img.setAttribute( "class", (img.hasAttribute("class") ? img.getAttributeByName("class") + " " : "") + "gif"); } if (img.hasAttribute("title")) { if (!prefs.showSmilies) { // kill all emotes String name = img.getAttributeByName("title"); img.setName("p"); img.addChild(new ContentNode(name)); } } else { if (!lastReadFound && prefs.hideOldImages || !prefs.imagesEnabled) { if (!dontLink) { img.setName("a"); img.setAttribute("href", src); img.addChild(new ContentNode(src)); } else { img.setName("p"); img.addChild(new ContentNode(src)); } } else { if (!dontLink) { img.setName("a"); img.setAttribute("href", src); TagNode newimg = new TagNode("img"); if (!prefs.imgurThumbnails.equals("d") && src.contains("i.imgur.com")) { int lastSlash = src.lastIndexOf('/'); if (src.length() - lastSlash <= 9) { int pos = src.length() - 4; src = src.substring(0, pos) + prefs.imgurThumbnails + src.substring(pos); } } newimg.setAttribute("src", src); img.addChild(newimg); } } } } StringBuffer fixedContent = new StringBuffer(); Matcher fixCharMatch = fixCharacters_regex.matcher(NetworkUtils.getAsString(pc)); while (fixCharMatch.find()) { fixCharMatch.appendReplacement(fixedContent, ""); } fixCharMatch.appendTail(fixedContent); post.put(CONTENT, fixedContent.toString()); } if (pc.getAttributeByName("class").equalsIgnoreCase("postdate")) { post.put( DATE, NetworkUtils.unencodeHtml(pc.getText().toString()) .replaceAll("[^\\w\\s:,]", "") .trim()); } if (pc.getAttributeByName("class").equalsIgnoreCase("profilelinks")) { TagNode[] links = pc.getElementsHavingAttribute("href", true); if (links.length > 0) { String href = links[0].getAttributeByName("href").trim(); String userId = href.substring(href.lastIndexOf("rid=") + 4); post.put(USER_ID, userId); if (Integer.toString(opId).equals(userId)) { // ugh post.put(IS_OP, 1); } else { post.put(IS_OP, 0); } } } if (pc.getAttributeByName("class").equalsIgnoreCase("editedby") && pc.getChildTags().length > 0) { post.put(EDITED, "<i>" + pc.getChildTags()[0].getText().toString() + "</i>"); } } TagNode[] editImgs = node.getElementsByAttValue("alt", "Edit", true, true); if (editImgs.length > 0) { post.put(EDITABLE, 1); } else { post.put(EDITABLE, 0); } result.add(post); } Log.i( TAG, Integer.toString(postNodes.length) + " posts found, " + result.size() + " posts parsed."); } catch (Exception e) { e.printStackTrace(); } return result; }
private static TagNode convertVideos(TagNode contentNode) { TagNode[] videoNodes = contentNode.getElementsByAttValue("class", "bbcode_video", true, true); TagNode[] youtubeNodes = contentNode.getElementsByAttValue("class", "youtube-player", true, true); for (TagNode youTube : youtubeNodes) { String src = youTube.getAttributeByName("src"); int height = Integer.parseInt(youTube.getAttributeByName("height")); int width = Integer.parseInt(youTube.getAttributeByName("width")); Matcher youtube = youtubeHDId_regex.matcher(src); if (youtube.find()) { String videoId = youtube.group(1); String link = "http://www.youtube.com/watch?v=" + videoId; String image = "http://img.youtube.com/vi/" + videoId + "/0.jpg"; youTube.setName("a"); youTube.setAttribute("href", link); youTube.removeAttribute("type"); youTube.removeAttribute("frameborder"); youTube.removeAttribute("src"); youTube.removeAttribute("height"); youTube.removeAttribute("width"); youTube.setAttribute( "style", "background-image:url(" + image + ");background-size:cover;background-repeat:no-repeat;background-position:center; position:relative;display:block;text-align:center; width:" + width + "; height:" + height); TagNode img = new TagNode("img"); img.setAttribute("class", "nolink videoPlayButton"); img.setAttribute("src", "file:///android_res/drawable/ic_menu_video.png"); img.setAttribute( "style", "position:absolute;top:50%;left:50%;margin-top:-16px;margin-left:-16px;"); youTube.addChild(img); } } for (TagNode node : videoNodes) { try { String src = null; int height = 0; int width = 0; TagNode[] object = node.getElementsByName("object", false); if (object.length > 0) { height = Integer.parseInt(object[0].getAttributeByName("height")); width = Integer.parseInt(object[0].getAttributeByName("width")); TagNode[] emb = object[0].getElementsByName("embed", true); if (emb.length > 0) { src = emb[0].getAttributeByName("src"); } } if (src != null && height != 0 && width != 0) { String link = null, image = null; Matcher youtube = youtubeId_regex.matcher(src); Matcher vimeo = vimeoId_regex.matcher(src); if (youtube .find()) { // we'll leave in the old youtube code in case something gets reverted String videoId = youtube.group(1); link = "http://www.youtube.com/watch?v=" + videoId; image = "http://img.youtube.com/vi/" + videoId + "/0.jpg"; } else if (vimeo.find()) { String videoId = vimeo.group(1); TagNode vimeoXML; try { vimeoXML = NetworkUtils.get("http://vimeo.com/api/v2/video/" + videoId + ".xml"); } catch (Exception e) { e.printStackTrace(); continue; } if (vimeoXML.findElementByName("mobile_url", true) != null) { link = vimeoXML.findElementByName("mobile_url", true).getText().toString(); } else { link = vimeoXML.findElementByName("url", true).getText().toString(); } image = vimeoXML.findElementByName("thumbnail_large", true).getText().toString(); } else { node.removeAllChildren(); TagNode ln = new TagNode("a"); ln.setAttribute("href", src); ln.addChild(new ContentNode(src)); node.addChild(ln); continue; } node.removeAllChildren(); node.setAttribute( "style", "background-image:url(" + image + ");background-size:cover;background-repeat:no-repeat;background-position:center; position:relative;text-align:center; width:" + width + "; height:" + height); node.setAttribute("onclick", "location.href=\"" + link + "\""); TagNode img = new TagNode("img"); img.setAttribute("class", "nolink videoPlayButton"); img.setAttribute("src", "file:///android_res/drawable/ic_menu_video.png"); img.setAttribute( "style", "position:absolute;top:50%;left:50%;margin-top:-23px;margin-left:-32px;"); node.addChild(img); } } catch (Exception e) { continue; // if we fail to convert the video tag, we can still display the rest. } } return contentNode; }
public void doProcess(HttpServletRequest req, HttpServletResponse res, boolean isPost) { StringBuffer bodyContent = null; OutputStream out = null; PrintWriter writer = null; String serviceKey = null; try { BufferedReader in = req.getReader(); String line = null; while ((line = in.readLine()) != null) { if (bodyContent == null) bodyContent = new StringBuffer(); bodyContent.append(line); } } catch (Exception e) { } try { if (requireSession) { // check to see if there was a session created for this request // if not assume it was from another domain and blow up // Wrap this to prevent Portlet exeptions HttpSession session = req.getSession(false); if (session == null) { res.setStatus(HttpServletResponse.SC_FORBIDDEN); return; } } serviceKey = req.getParameter("id"); // only to preven regressions - Remove before 1.0 if (serviceKey == null) serviceKey = req.getParameter("key"); // check if the services have been loaded or if they need to be reloaded if (services == null || configUpdated()) { getServices(res); } String urlString = null; String xslURLString = null; String userName = null; String password = null; String format = "json"; String callback = req.getParameter("callback"); String urlParams = req.getParameter("urlparams"); String countString = req.getParameter("count"); // encode the url to prevent spaces from being passed along if (urlParams != null) { urlParams = urlParams.replace(' ', '+'); } try { if (services.has(serviceKey)) { JSONObject service = services.getJSONObject(serviceKey); // default to the service default if no url parameters are specified if (urlParams == null && service.has("defaultURLParams")) { urlParams = service.getString("defaultURLParams"); } String serviceURL = service.getString("url"); // build the URL if (urlParams != null && serviceURL.indexOf("?") == -1) { serviceURL += "?"; } else if (urlParams != null) { serviceURL += "&"; } String apikey = ""; if (service.has("username")) userName = service.getString("username"); if (service.has("password")) password = service.getString("password"); if (service.has("apikey")) apikey = service.getString("apikey"); urlString = serviceURL + apikey; if (urlParams != null) urlString += "&" + urlParams; if (service.has("xslStyleSheet")) { xslURLString = service.getString("xslStyleSheet"); } } // code for passing the url directly through instead of using configuration file else if (req.getParameter("url") != null) { String serviceURL = req.getParameter("url"); // build the URL if (urlParams != null && serviceURL.indexOf("?") == -1) { serviceURL += "?"; } else if (urlParams != null) { serviceURL += "&"; } urlString = serviceURL; if (urlParams != null) urlString += urlParams; } else { writer = res.getWriter(); if (serviceKey == null) writer.write("XmlHttpProxyServlet Error: id parameter specifying serivce required."); else writer.write( "XmlHttpProxyServlet Error : service for id '" + serviceKey + "' not found."); writer.flush(); return; } } catch (Exception ex) { getLogger().severe("XmlHttpProxyServlet Error loading service: " + ex); } Map paramsMap = new HashMap(); paramsMap.put("format", format); // do not allow for xdomain unless the context level setting is enabled. if (callback != null && allowXDomain) { paramsMap.put("callback", callback); } if (countString != null) { paramsMap.put("count", countString); } InputStream xslInputStream = null; if (urlString == null) { writer = res.getWriter(); writer.write( "XmlHttpProxyServlet parameters: id[Required] urlparams[Optional] format[Optional] callback[Optional]"); writer.flush(); return; } // default to JSON res.setContentType(responseContentType); out = res.getOutputStream(); // get the stream for the xsl stylesheet if (xslURLString != null) { // check the web root for the resource URL xslURL = null; xslURL = ctx.getResource(resourcesDir + "xsl/" + xslURLString); // if not in the web root check the classpath if (xslURL == null) { xslURL = XmlHttpProxyServlet.class.getResource(classpathResourcesDir + "xsl/" + xslURLString); } if (xslURL != null) { xslInputStream = xslURL.openStream(); } else { String message = "Could not locate the XSL stylesheet provided for service id " + serviceKey + ". Please check the XMLHttpProxy configuration."; getLogger().severe(message); try { out.write(message.getBytes()); out.flush(); return; } catch (java.io.IOException iox) { } } } if (!isPost) { xhp.doGet(urlString, out, xslInputStream, paramsMap, userName, password); } else { if (bodyContent == null) getLogger() .info( "XmlHttpProxyServlet attempting to post to url " + urlString + " with no body content"); xhp.doPost( urlString, out, xslInputStream, paramsMap, bodyContent.toString(), req.getContentType(), userName, password); } } catch (Exception iox) { iox.printStackTrace(); getLogger().severe("XmlHttpProxyServlet: caught " + iox); try { writer = res.getWriter(); writer.write(iox.toString()); writer.flush(); } catch (java.io.IOException ix) { ix.printStackTrace(); } return; } finally { try { if (out != null) out.close(); if (writer != null) writer.close(); } catch (java.io.IOException iox) { } } }
/** * Sends an HTTP GET request to a url * * @param url the url * @return - HTTP response code */ public int sendGetRequest(String url) throws IOException, OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException { System.out.println("url in sendrequest= " + url); int responseCode = 500; try { HttpURLConnection uc = getConnection(url); responseCode = uc.getResponseCode(); if (200 == responseCode || 401 == responseCode || 404 == responseCode) { BufferedReader rd = new BufferedReader( new InputStreamReader( responseCode == 200 ? uc.getInputStream() : uc.getErrorStream())); StringBuffer sb = new StringBuffer(); String line; while ((line = rd.readLine()) != null) { System.out.println("line = " + line); sb.append(line); System.out.println("sb.append = " + sb); } String response = sb.toString(); try { JSONObject json = new JSONObject(response); System.out.println("\nResults:"); System.out.println( "Total results = " + json.getJSONObject("bossresponse") .getJSONObject("web") .getString("totalresults")); System.out.println(); JSONArray ja = json.getJSONObject("bossresponse").getJSONObject("web").getJSONArray("results"); BufferedWriter out = new BufferedWriter(new FileWriter("outfile.csv", true)); String str = ""; System.out.println("\nResults:"); for (int i = 0; i < ja.length(); i++) { // System.out.print((i+1) + ". "); JSONObject j = ja.getJSONObject(i); str += j.getString("url") + "," + j.getString("abstract") + "\n"; out.write(str); } } catch (Exception e) { System.err.println("Something went wrong..."); e.printStackTrace(); } rd.close(); setResponseBody(sb.toString()); } } catch (MalformedURLException ex) { throw new IOException(url + " is not valid"); } catch (IOException ie) { throw new IOException("IO Exception " + ie.getMessage()); } return responseCode; }
/** * Parses a text file of JSONs into an array of tweets * * @param textFile The textfile containing the JSONs */ public parseJSON() { // get folder paths String currentDir = System.getProperty("user.dir"); String root = currentDir; String textFile = root + "/tweet_input/tweets.txt"; String outputFolderF1 = root + "/tweet_output/f1.txt"; String outputFolderF2 = root + "/tweet_output/f2.txt"; int numTweets = 0; int numGoodTweets = 0; hashtagEdges hashEdges = new hashtagEdges(); // try objects try { // create a reader object for tweet's textfile and read-in first line BufferedReader reader = new BufferedReader(new FileReader(textFile)); String currentJSON = reader.readLine(); // System.out.println(textFile); // initate a writer object with the outputFolder name PrintWriter writerF1 = new PrintWriter(outputFolderF1, "UTF-8"); PrintWriter writerF2 = new PrintWriter(outputFolderF2, "UTF-8"); // create an array list to save parsedTweets and declare problem variables List<parsedTweet> tweetList = new ArrayList<parsedTweet>(); String tweetText, tweetTime; JSONObject objJSON; String[] hashArray; parsedTweet tweetObj; float currentAverage; while (currentJSON != null) { try { objJSON = new JSONObject(currentJSON); // if the JSON has a text and time stamp, process it numTweets = numTweets + 1; if (objJSON.has("created_at") && objJSON.has("text")) { // get timestamp and text from JSON object tweetTime = objJSON.getString("created_at"); tweetText = objJSON.getString("text"); // create a tweet object and add it to folder tweetObj = new parsedTweet(tweetTime, tweetText, currentJSON); tweetList.add(tweetObj); // update hashObjet hashArray = tweetObj.hashtags.toArray(new String[tweetObj.hashtags.size()]); currentAverage = hashEdges.updateEdges(hashArray, tweetTime); // write correctly-formated cleen-tweet to output folder writerF1.println(tweetObj.formatTweet()); numGoodTweets = numGoodTweets + 1; writerF2.format("%.2f%n", currentAverage); } } catch (Exception e) { System.out.println("Error in parseJSON - 1"); e.printStackTrace(); } // read next line (which has the next JSON object) currentJSON = reader.readLine(); } writerF1.close(); writerF2.close(); } catch (Exception e) { System.out.println("Error in parseJSON - 2"); e.printStackTrace(); } }
public void applySetting(JSONObject js) { try { if (js.length() == 0) { // This is a blank object we just return and move on } else if (js.keySet().size() > 1) { // If there are more than one object in the json response Iterator ii = js.keySet().iterator(); // This is a special multi single value response object while (ii.hasNext()) { String key = ii.next().toString(); switch (key) { case "f": parseFooter(js.getJSONArray("f")); // This is very important. // We break out our response footer.. error codes.. bytes availble in hardware buffer // etc. break; case "msg": message[0] = "TINYG_USER_MESSAGE"; message[1] = (String) js.get(key) + "\n"; logger.info("[+]TinyG Message Sent: " + js.get(key) + "\n"); setChanged(); notifyObservers(message); break; case "rx": TinygDriver.getInstance().serialWriter.setBuffer(js.getInt(key)); break; default: if (TinygDriver.getInstance().mneManager.isMasterGroupObject(key)) { // logger.info("Group Status Report Detected: " + key); applySettingMasterGroup(js.getJSONObject(key), key); continue; } responseCommand rc = TinygDriver.getInstance().mneManager.lookupSingleGroup(key); rc.setSettingValue(js.get(key).toString()); _applySettings( rc.buildJsonObject(), rc.getSettingParent()); // we will supply the parent object name for each key pair break; } } } else { /* This else follows: * Contains a single object in the JSON response */ if (js.keySet().contains("f")) { /** This is a single response footer object: Like So: {"f":[1,0,5,3330]} */ parseFooter(js.getJSONArray("f")); } else { /** * Contains a single object in the json response I am not sure this else is needed any * longer. */ _applySettings(js, js.keys().next().toString()); } } } catch (JSONException ex) { logger.error("[!] Error in applySetting(JsonOBject js) : " + ex.getMessage()); logger.error("[!]JSON String Was: " + js.toString()); logger.error("Error in Line: " + js); } catch (Exception ex) { logger.error("[!] Error in applySetting(JsonOBject js) : " + ex.getMessage()); logger.error(ex.getClass().toString()); } }
/** * Request URL * * @param List<String> request of url directories. * @return JSONArray from JSON response. */ public JSONArray _request(List<String> url_components) { String json = ""; StringBuilder url = new StringBuilder(); Iterator url_iterator = url_components.iterator(); url.append(this.ORIGIN); // Generate URL with UTF-8 Encoding while (url_iterator.hasNext()) { try { String url_bit = (String) url_iterator.next(); url.append("/").append(_encodeURIcomponent(url_bit)); } catch (Exception e) { e.printStackTrace(); JSONArray jsono = new JSONArray(); try { jsono.put("Failed UTF-8 Encoding URL."); } catch (Exception jsone) { } return jsono; } } // Fail if string too long if (url.length() > this.LIMIT) { JSONArray jsono = new JSONArray(); try { jsono.put(0); jsono.put("Message Too Long."); } catch (Exception jsone) { } return jsono; } try { URL request = new URL(url.toString()); URLConnection conn = request.openConnection(); String line = ""; conn.setConnectTimeout(200000); conn.setReadTimeout(200000); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); try { // Read JSON Message while ((line = reader.readLine()) != null) { json += line; } } catch (Exception ex) { } finally { reader.close(); } } catch (Exception e) { JSONArray jsono = new JSONArray(); try { jsono.put("Failed JSONP HTTP Request."); } catch (Exception jsone) { } e.printStackTrace(); System.out.println(e); return jsono; } // Parse JSON String try { return new JSONArray(json); } catch (Exception e) { JSONArray jsono = new JSONArray(); try { jsono.put("Failed JSON Parsing."); } catch (Exception jsone) { } e.printStackTrace(); System.out.println(e); // Return Failure to Parse return jsono; } }