How to manage back end process with Tomcat? - java

I have a web page that the user pushes a button and initiates an action in a proprietery app (which resides in Tomcat).
That action is a long running process and the only way I have to see what's going on is to log onto the server and look at a log file.
I wrote a quick Java function that reads the log file and gives feedback on what's happening. (Essentially it just tails the file and parses out the things I need)
I'd like to be able to add a jsp that I can view the output without logging into the server.
===
From a design standpoint, I understand that the JSP should return quickly with a result and not just keep on processing.
So my idea is to create a simple web page that queries the jsp for an update and writes the latest info to the screen. Wait 30 seconds, poll the server again, and append the latest update.
What I'm struggling to grasp is how to get the JSP to communicate with the back end process, and how that back end process should be spawned / killed.
This is a very occasional thing (once every two weeks, start to completion takes an hour or two), so I don't want a daemon running all the time. I want to be able to turn it on temporarily and turn it off.
If I spawn a process from within a simple servlet, how do I end that process when I'm done?
And how do I communicate with it?

You can create a java.lan.Runnable witch reads the file content an saves it into a buffer. The Runnable reads the file content withing a while loop for witch the break condition can be set from outside, Thread that is executing your Runnable will terminate whe the run method of your Runnable terminates.
In your JSP you can create a java.lang.Thread and pass an instance of your Runnable to it. Save th instace of the runable in the ServletContext so you can access it across the requests. If you want to terminate the polling than just set the break condition of your Runnable from the JSP, the rum method will terminate and thus the thread too.
You can use javascript setInterval() function and XMLHttpRequest refresh the page.
here is a sample basic implemntation (I hope this will meet your requirements):
Polling Runnable
package com.web;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class FilePollingThread implements Runnable {
private String filepath = null;
private boolean polling = false;
private StringBuffer dataWritenAfterLastPoll = null;
private String error = null;
public FilePollingThread(String filepath) {
this.filepath = filepath;
}
#Override
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));
dataWritenAfterLastPoll = new StringBuffer();
polling = true;
String line = null;
while(polling) {
try {
line = br.readLine();
while(line == null) {
try {
Thread.sleep(500L);
} catch (InterruptedException e) {
e.printStackTrace();
error = e.toString();
}
line = br.readLine();
}
dataWritenAfterLastPoll.append(markUp(line));
} catch (IOException e) {
e.printStackTrace();
error = e.toString();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
error = e.toString();
} finally {
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
error = e.toString();
}
}
}
}
private String markUp(String line) {
String markup = "";
if(line != null) {
markup = "<div style=\"height: 6px\"><span style=\"line-height: 1.1;\">" + line + "</span></div>\n";
}
return markup;
}
public synchronized void stopPolling() {
polling = false;
}
public synchronized String poll() {
String tmp = markUp(error == null ? "Not ready" : error);
if(dataWritenAfterLastPoll != null) {
tmp = dataWritenAfterLastPoll.toString();
dataWritenAfterLastPoll = new StringBuffer();
}
return tmp;
}
}
And a JSP witch initiats the polling an keep retrieving data
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="com.web.FilePollingThread" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Poll file</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" href="style/default.css"></link>
<script type="text/javascript">
var c = 1;
var ih;
var polling = false;
var filepath = null;
function startPolling(interval) {
ih = setInterval(function () {
try {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
var w = getElementById('ajax_content');
w.innerHTML = w.innerHTML + xmlHttp.responseText;
getElementById('page_refresh').innerHTML = c++;
polling = true;
window.scrollTo(0, document.body.scrollHeight);
} else {
polling = false;
throw 'HTTP ' + xmlHttp.status;
}
}
};
xmlHttp.open('GET', 'pollfile.jsp?filepath=' + filepath + '&c=' + c, true);
xmlHttp.send();
} catch(e) {
alert('Error at startPolling: ' + e);
clearInterval(ih);
}
}, interval);
}
function startStopPolling() {
var orgPolling = polling;
try {
if(polling) {
polling = false;
clearInterval(ih);
doPolling();
} else {
polling = true;
doPolling();
startPolling(1000);
}
flipStartStopButtonsLabel();
} catch(e) {
polling = orgPolling;
flipStartStopButtonsLabel();
alert('Error at startStopPolling: ' + e);
}
}
function flipStartStopButtonsLabel() {
var label;
if(polling) {
c = 1;
label = 'Stop polling';
getElementById('page_refresh').innerHTML = '0';
} else {
label = 'Sart polling';
getElementById('page_refresh').innerHTML = 'stoped';
}
var buttons = document.getElementsByName('start_stop_polling');
if(buttons) {
for(var i = 0; i < buttons.length; i++) {
buttons[i].value = label;
}
}
}
function doPolling() {
var url = 'pollfile.jsp?polling=';
if(polling) {
filepath = getElementById('filepath');
if(filepath && filepath.value && filepath.value.length > 0) {
url += 'true&filepath=' + encodeURIComponent(filepath.value);
} else {
throw 'No filepath specified.';
}
} else {
url += 'false';
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status != 200) {
throw 'HTTP ' + xmlHttp.status;
}
}
};
xmlHttp.open('POST', url, false);
xmlHttp.send();
}
function clearWindow() {
var w = getElementById('ajax_content');
if(w) {
w.innerHTML = '';
}
}
function getElementById(id) {
try {
if(id) {
elm = document.getElementById(id);
return elm;
}
} catch(e) {
alert('Error at getElementById: ' + e);
}
return null;
}
</script>
</head>
<body>
<%
String polling = request.getParameter("polling");
if("true".equals(polling)) {
String filepath = request.getParameter("filepath");
if(filepath != null && filepath.length() > 0) {
FilePollingThread pollingThread = new FilePollingThread(filepath);
new Thread(pollingThread, "polling thread for file '" + filepath + "'").start();
request.getServletContext().setAttribute("pollingThread", pollingThread);
}
} else if("false".equals(polling)) {
FilePollingThread pollingThread = (FilePollingThread) request.getServletContext().getAttribute("pollingThread");
if(pollingThread != null) {
pollingThread.stopPolling();
}
} else {
FilePollingThread pollingThread = (FilePollingThread) request.getServletContext().getAttribute("pollingThread");
if(pollingThread != null) {
response.getWriter().println(pollingThread.poll());
response.getWriter().close();
return;
}
}
%>
<div class="label">
<span>Page polling:</span>
</div>
<div style="float: left;">
<span id="page_refresh">0</span>
</div>
<div class="clear_both"> </div>
<form id="input_form" action="pollfile.jsp" method="get">
<div>
<div style="float: left;">
<label>Filepath:
<input style="height: 24px;" id="filepath" type="text" size="120" value=""/>
</label>
</div>
<div style="clear: both;"/>
<div style="float: left;">
<input style="height: 24px;" name="start_stop_polling" id="start_stop_polling_button" type="button" onclick="startStopPolling(); return false;" value="Start polling"/>
</div>
<div style="float: left;">
<input style="height: 24px;" name="clear_window" id="clear_window_button" type="button" onclick="clearWindow(); return false;" value="Clear"/>
</div>
<div style="clear: both;"> </div>
</div>
</form>
<div id="ajax_content">
</div>
<div>
<div style="float: left;">
<input style="height: 24px;" name="start_stop_polling" id="start_stop_polling_button" type="button" onclick="startStopPolling(); return false;" value="Start polling"/>
</div>
<div style="float: left;">
<input style="height: 24px;" name="clear_window" id="clear_window_button" type="button" onclick="clearWindow(); return false;" value="Clear"/>
</div>
<div style="clear: both;"> </div>
</div>
</body>
</html>
EDIT
there is a bug in FilePollingThread: if no data is available in the file the thread might get stuck in inner while loop. it should be
while(line == null && polling)
also the JSP will not work on IE (testet on IE9). It seems that the data writen to the response in the line
response.getWriter().println(pollingThread.poll());
contains the HTML of the hole page. If added to the target div IE seems not able to render it.
I made another version using a simple static HTML file an a servlet as it offers more controle on what is writen to response.
If you are interested in the code let me know.

You should consider using something like JMS to control your background process.
For control, your front-end code can send messages start/stop/inspect the process.
For monitoring, your process can publish to a JMS topic for your front end code to read.

Related

Getting HTML input value and using that value as a Java variable

Sorry for the long post and janky question.
I'm trying to figure out how to use an input textbox inside a form as a way to get the name of a city. With the city name, I'll then use it inside a method (getCitybyId) inside the MainController.
I'll then use it as the city name inside my api URL
Example:"http://api.openweathermap.org/data/2.5/weather?q=citynamevariable&appid=APIKey&units=imperial"
Is there any way I could do this? I'm pretty new to using spring boot and API's, and I have been trying to figure this out for a while.
Here's my code
MainController:
#RestController
public class MainController extends HttpServlet {
DataRepo dataRepo;
#RequestMapping(value = "/get", method = RequestMethod.GET)
public ModelAndView get(#RequestParam("name") String id) {
ModelAndView mv = new ModelAndView ("redirect:/");
String city = getCitybyId(id);
try{
JSONObject json = new JSONObject(city);
mv.addObject("Name", json.getString("name"));
mv.addObject("Temperature", json.getJSONObject("main").get("temp").toString());
mv.addObject("feels_like", json.getJSONObject("main").get("feels_like").toString());
mv.addObject("humidity", json.getJSONObject("main").get("humidity").toString());
mv.addObject("Wind", json.getJSONObject("wind").get("speed").toString());
}
catch (Exception e){
System.out.println(e.toString());
}
return mv;
}
private String getCitybyId(String name) {
try {
String apiKey = "key";
URL urlForGetRequest = new URL("http://api.openweathermap.org/data/2.5/weather?q=PUTCITYNAMEHERE&appid=key&units=imperial");
// These two are test to see if the method is actually getting data.
//String apiKey = "key";
// URL urlForGetRequest = new URL("https://superheroapi.com/api/" + apiKey + "/" + id);
HttpURLConnection connection = (HttpURLConnection) urlForGetRequest.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
int status = connection.getResponseCode();
System.out.println(status);
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
response.append(line);
}
in.close();
return response.toString();
}
else {
return "Unexpected HTTP response";
}
} catch (Exception e) {
return "Exception: " + e.getMessage();
}
}
}
class City
{
private String name;
public String getName()
{
return name;
}
public City()
{
name = "DEFAULT";
}
public City(String n)
{
name = n;
}
}
HTML code
<!DOCTYPE html>
<html>
<head>
<title> Homework 4 application</title>
<style><%#include file ="../css/style.css"%></style>
</head>
<body>
<h1>Please Select A City</h1>
<form method="get" action="/get/">
<input type="text" id = "name" name="name" />
<input type="submit" value="Submit">
</form>
<div>
<h2>City</h2> <h3><%=request.getParameter("Name")%></h3>
<h2>Temperature</h2> <h3><%=request.getParameter("Temperature")%> Fahrenheit</h3>
<h2>Feels Like</h2> <h3><%=request.getParameter("feels_like")%> Fahrenheit</h3>
<h2>Humidity</h2> <h3><%=request.getParameter("humidity")%>%</h3>
<h2>Wind Speed</h2> <h3><%=request.getParameter("Wind")%> Mph</h3>
</div>
<hr>
<div>
<h2>Previously Requested Cities</h2>
<table class = "lemun">
<tr>
<td>City</td>
<td>Temperature</td>
<td>Feels Like</td>
<td>Humidity</td>
<td>Wind Speed</td>
</tr>
<c:forEach var = "Data" items = "${dataList}">
<tr>
<td>${Data.getName}</td>
<td>${Data.getTemperature}</td>
<td>${Data.getFeels_Like}</td>
<td>${Data.getHumidity}</td>
<td>${Data.getWind}</td>
</tr>
</c:forEach>
</table>
</div>
</hr>
</body>
</html>
Basically, I'm trying to use the User input from the textbox as a value for the cityname inside the openweathermap URL. Is this possible? If so may you show a way to do it?

Selenium Java | Trouble finding/clicking button

I'm trying to use Selenium in order to open a HTML page and click a button.
The HTML I get back is:
<html>
<head>
<link rel="shortcut icon" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="http://localhost:5050/style.css">
<title>Test</title>
</head>
<body>
<div class="btn-container">
<button class="btn-orange" id="successButton" name="Success" value="Success"> Success </button>
<button class="btn-orange" id="failButton" name="Fail" value="Fail"></button> Fail
</div>
</div>
<footer class="footer" align="center">
<div class="container-fluid">
<div class="clearfix">
<div class="cards pull-left">
</div>
</div>
</div>
</footer>
</body>
</html>
I'm trying to click the successButton but it does not seem to work, I try to access via its id.
Here is the line I use to click:
driver.findElement(By.id("successButton")).click();
And here's my entire function:
public void openTheHtmlPageAndClickButton(
String pageUrl,
String SiteUrl,
String buttonValue) {
String lastUrl = null;
boolean timeout = true;
for (int tryNumber = 1; tryNumber <= 5 && timeout; tryNumber++) {
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox());
System.out.println("Selenium open: " + pageUrl);
driver.get(pageUrl);
int i = 0;
Alert alert;
while (i++ < 30) {
try {
alert = driver.switchTo().alert();
if (alert != null) {
alert.accept();
}
} catch (NoAlertPresentException e) {
}
String currentUrl = driver.getCurrentUrl();
driver.findElement(By.id("successButton")).click();
if (!currentUrl.equals(lastUrl)) {
System.out.println("currentUrl: " + currentUrl);
lastUrl = currentUrl;
if (currentUrl.startsWith(SiteUrl)) {
timeout = false;
break;
}
} else {
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
Assert.fail();
}
}
}
} catch (Exception e) {
System.out.println("Selenium exception: " + e.toString());
} finally {
if (driver == null) {
Assert.fail("Cannot open web driver, probably Selenium docker is down");
} else {
if (timeout) {
System.out.println("Page got timeout: page source: " + driver.getPageSource());
if (tryNumber == 5) {
Assert.fail("Page got timeout 3 times!!!");
}
}
driver.quit();
}
}
}
}
Please advise on what am I doing wrong.
Apparently the button did not appear when I tried to 'click' it. Changed the timeout slightly and issue was resolved.

How can I display an already saved image in mongodb using angularjs?

I already know how to save images in mongodb using angularjs and java to save it in my mongodb, it is working fine.
Now, I need to get the saved image from mongodb and display it in an html page using AngularJS.
ps: I am aware of ngSrc.
My html:
<form method="post" enctype="multipart/form-data" name="formCadastroFotos" id="formCadastroFotos" role="form" novalidate>
<div class="form-group">
<label for="foto">Upload da Foto</label>
<input type="file" name="foto" id="foto" onchange="angular.element(this).scope().uploadFile(this.files)">
</div>
<br>
<div class="alert alert-success" ng-show="show" role="alert">
<center>
<strong>OK!</strong> Foto cadastrada com sucesso.
</center>
</div>
<div class="alert alert-danger" ng-show=showErro role="alert">
<center>
<strong>Erro!</strong> Foto não foi cadastrada com sucesso.
</center>
</div>
</form>
My js:
var myApp = angular.module('AntInformaticaApp', ['ngMessages']);
myApp.controller('CadastroFotosController', ['$scope', '$http', ' $window', function ($scope, $http, $window) {
console.log("Entrou no javascript para envio da foto.");
$scope.uploadFile = function(files) {
var fd = new FormData();
//Take the first selected file
fd.append("file", files[0]);
$http.post('cadastroFotos', fd, {
withCredentials: true,
headers: {'Content-Type': undefined }}).then(function(response) {
$scope.showOk();
}, function(response) {
console.log("erro");
$scope.showNOk();
});
};
$scope.logout = function(){
window.location = '/logout';
}
$scope.showOk = function(){
$scope.show=true;
}
$scope.showNOk = function(){
$scope.showErro=true;
}
}]);
It is going correctly to my java controller:
#Controller
public class CadastroFotosController {
#RequestMapping(value="/cadastroFotos", method=RequestMethod.POST)
public #ResponseBody MsgRetornoDTO cadastroPecas(#RequestParam("file") MultipartFile file) throws IOException {
MsgRetornoDTO retorno = new MsgRetornoDTO();
retorno = UtilGeral.salvaFotoMongo(file);
return retorno;
}
}
public static MsgRetornoDTO salvaFotoMongo(MultipartFile file){
MsgRetornoDTO retorno = new MsgRetornoDTO();
if (!file.isEmpty()) {
try {
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("antinformatica");
DBCollection collection = db.getCollection("dummyCol");
String newFileName = file.getOriginalFilename();
File imageFile = new File(file.getOriginalFilename());
imageFile.createNewFile();
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(file.getBytes());
fos.close();
// create a "photo" namespace
GridFS gfsPhoto = new GridFS(db, "photo");
// get image file from local drive
GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);
// set a new filename for identify purpose
gfsFile.setFilename(newFileName);
// save the image file into mongoDB
gfsFile.save();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("empty file.");
}
retorno.setCodReturn(0);
retorno.setDescReturn("OK");
return retorno;
}
I appreciate your time helping, thanks.
have you got the answer if you get it please reply me or post it i am in same stuck. I also save image in mongodb via Angular.And I know how to fetch that image by download and dont know how to display in html page. If you get it please email id- symonkt#gmail.com

Websocket disconnects Openshift

Compiled files of chat websocket example that comes with Tomcat 7 works fine on localhost and Openshift.
However, when I compile those files myself it doesn't work on Openshift, although on localhost still works great (tomcat 7).
I tried to compile through netbeans, eclipse, and terminal (OSX). Tried all versions of dependencies, tried building as java webapp, maven webapp, tried to upload as war file but no success.
The module I'm trying to test is the chat websocket. It is composed by two classes, one xhtml file and two dependencies.
working: http://chatoriginal-helioha.rhcloud.com:8000/examples/websocket/chat.xhtml
not working: http://chat-helioha.rhcloud.com:8000/websocket/chat.xhtml
I'm not changing a single line of code, so the files I compile myself should be the same as the ones that comes already compiled right?
I also tried to decompile files using www.javadecompilers.com to see if there was anything extra there but I couldn't find any difference.
There are only three files on this project and they are shown bellow.
ChatAnnotation.java
package websockets.chat;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import util.HTMLFilter;
#ServerEndpoint(value = "/websocket/chat")
public class ChatAnnotation {
private static final Log log = LogFactory.getLog(ChatAnnotation.class);
private static final String GUEST_PREFIX = "Guest";
private static final AtomicInteger connectionIds = new AtomicInteger(0);
private static final Set<ChatAnnotation> connections =
new CopyOnWriteArraySet<>();
private final String nickname;
private Session session;
public ChatAnnotation() {
nickname = GUEST_PREFIX + connectionIds.getAndIncrement();
}
#OnOpen
public void start(Session session) {
this.session = session;
connections.add(this);
String message = String.format("* %s %s", nickname, "has joined.");
broadcast(message);
}
#OnClose
public void end() {
connections.remove(this);
String message = String.format("* %s %s",
nickname, "has disconnected.");
broadcast(message);
}
#OnMessage
public void incoming(String message) {
// Never trust the client
String filteredMessage = String.format("%s: %s",
nickname, HTMLFilter.filter(message.toString()));
broadcast(filteredMessage);
}
#OnError
public void onError(Throwable t) throws Throwable {
log.error("Chat Error: " + t.toString(), t);
}
private static void broadcast(String msg) {
for (ChatAnnotation client : connections) {
try {
synchronized (client) {
client.session.getBasicRemote().sendText(msg);
}
} catch (IOException e) {
log.debug("Chat Error: Failed to send message to client", e);
connections.remove(client);
try {
client.session.close();
} catch (IOException e1) {
// Ignore
}
String message = String.format("* %s %s",
client.nickname, "has been disconnected.");
broadcast(message);
}
}
}
}
HTMLFilter.java
package util;
/**
* HTML filter utility.
*
* #author Craig R. McClanahan
* #author Tim Tye
*/
public final class HTMLFilter {
/**
* Filter the specified message string for characters that are sensitive
* in HTML. This avoids potential attacks caused by including JavaScript
* codes in the request URL that is often reported in error messages.
*
* #param message The message string to be filtered
*/
public static String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuilder result = new StringBuilder(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
chat.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Apache Tomcat WebSocket Examples: Chat</title>
<style type="text/css"><![CDATA[
input#chat {
width: 410px
}
#console-container {
width: 400px;
}
#console {
border: 1px solid #CCCCCC;
border-right-color: #999999;
border-bottom-color: #999999;
height: 170px;
overflow-y: scroll;
padding: 5px;
width: 100%;
}
#console p {
padding: 0;
margin: 0;
}
]]></style>
<script type="application/javascript"><![CDATA[
"use strict";
var Chat = {};
Chat.socket = null;
Chat.connect = (function(host) {
if ('WebSocket' in window) {
Chat.socket = new WebSocket(host);
} else if ('MozWebSocket' in window) {
Chat.socket = new MozWebSocket(host);
} else {
Console.log('Error: WebSocket is not supported by this browser.');
return;
}
Chat.socket.onopen = function () {
Console.log('Info: WebSocket connection opened.');
document.getElementById('chat').onkeydown = function(event) {
if (event.keyCode == 13) {
Chat.sendMessage();
}
};
};
Chat.socket.onclose = function () {
document.getElementById('chat').onkeydown = null;
Console.log('Info: WebSocket closed.');
};
Chat.socket.onmessage = function (message) {
Console.log(message.data);
};
});
Chat.initialize = function() {
if (window.location.protocol == 'http:') {
Chat.connect('ws://' + window.location.host + '/examples/websocket/chat');
} else {
Chat.connect('wss://' + window.location.host + '/examples/websocket/chat');
}
};
Chat.sendMessage = (function() {
var message = document.getElementById('chat').value;
if (message != '') {
Chat.socket.send(message);
document.getElementById('chat').value = '';
}
});
var Console = {};
Console.log = (function(message) {
var console = document.getElementById('console');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.innerHTML = message;
console.appendChild(p);
while (console.childNodes.length > 25) {
console.removeChild(console.firstChild);
}
console.scrollTop = console.scrollHeight;
});
Chat.initialize();
document.addEventListener("DOMContentLoaded", function() {
// Remove elements with "noscript" class - <noscript> is not allowed in XHTML
var noscripts = document.getElementsByClassName("noscript");
for (var i = 0; i < noscripts.length; i++) {
noscripts[i].parentNode.removeChild(noscripts[i]);
}
}, false);
]]></script>
</head>
<body>
<div class="noscript"><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable
Javascript and reload this page!</h2></div>
<div>
<p>
<input type="text" placeholder="type and press enter to chat" id="chat" />
</p>
<div id="console-container">
<div id="console"/>
</div>
</div>
</body>
</html>

Java - JSOUP: selecting a specific part of a website

I try to readout the Office 365 Website to compare it to a proxy configuration. But i cant get the select right so that it just gets me a specific section of those urls and ip addresses.
public class Office365WebsiteParser {
Document doc = null;
String WebseitenInhalt;
public void Parser() {
System.setProperty("http.proxyHost", "xxx");
System.setProperty("http.proxyPort", "8081");
System.setProperty("https.proxyHost", "xxx");
System.setProperty("https.proxyPort", "8081");
for (int i = 1; i <= 5; i++) {
try {
doc = Jsoup.connect("https://technet.microsoft.com/de-de/library/hh373144.aspx").userAgent("Mozilla").get();
break; // Break immediately if successful
} catch (IOException e) {
// Swallow exception and try again
System.out.println("jsoup Timeout occurred " + i + " time(s)");
}
}
if (doc == null) {
System.out.println("Connection timeout after 5 tries");
} else { // Wenn alles funktioniert hat Webseite auswerten
Elements urls_Office365_URLs = doc.select("div.codeSnippetContainerCode");
// HTML auswahl der Webseite nach div class und div id
// urls_Office365_URLs_global = urls_Office365_URLs;
WebseitenInhalt=urls_Office365_URLs.text();
}
}
public void Print() {
System.out.println(WebseitenInhalt);
}
public String get() {
return WebseitenInhalt;
}
}
I just want to select the containers like this:
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="In Zwischenablage kopieren" href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_0f6f9acf-6aa4-471f-8600-f8d059f95493');">Kopieren</a>
</div>
</div>
<div id="CodeSnippetContainerCode_0f6f9acf-6aa4-471f-8600-f8d059f95493" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>
*.live.com
*.officeapps.live.com
*.microsoft.com
*.glbdns.microsoft.com
*.microsoftonline.com
*.office365.com
*.office.com
Portal.Office.com
*.onmicrosoft.com
*.microsoftonline-p.com^
*.microsoftonline-p.net^
*.microsoftonlineimages.com^
*.microsoftonlinesupport.net^
*.msecnd.net^
*.msocdn.com^
*.msn.com^
*.msn.co.jp^
*.msn.co.uk^
*.office.net^
*.aadrm.com^^
*.cloudapp.net^^
*.activedirectory.windowsazure.com^^^
*.phonefactor.net^^^
</pre></div>
</div>
</div>
</div>
Try this CSS selector:
table:has(th:matches(.+-URLs?)) td:first-of-type pre
DEMO

Categories

Resources