[Java] Set the time from the browser with jsoup

Requirements

Execution environment

Development environment

Source code

Time adjustment start batch

TimeAdjusterMain.bat


@echo off

rem ------------------------------------------------
rem ---Time adjustment batch
rem ------------------------------------------------

rem --Based on the current directory
cd /d %~dp0

echo ****Start time adjustment: %date% %time%

rem --Check arguments(Add xx to prevent it from becoming empty)
if xx%1==xx goto confirmPw
goto setPw



:confirmPw

rem --Enter the administrator password if no arguments are specified
set /P PASSWD=Please enter the administrator password: 

goto exec



:setPw

rem --Set as administrator password if argument is specified
set PASSWD=%1

goto exec



rem --Execution of processing batch
:exec

rem --Start execution of processing batch
call bin\TimeAdjuster.bat %PASSWD%

echo ****Time adjustment end: %date% %time%

rem --Stay without completing
pause

IP address list to be processed tsv

list.tsv


10.20.30.40 abc123 ABC123 A branch B floor AB # 1
10.20.50.60 def456 DEF456 C branch D floor CD # 2

Java source code

Main

TimeAdjusterMain.java


package jp.co.sample.timeadjuster;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jp.co.sample.timeadjuster.bean.SettingBean;
import jp.co.sample.timeadjuster.launcher.TimeAdjusterLauncher;

public class TimeAdjusterMain {

	private static Logger logger = LoggerFactory.getLogger(TimeAdjusterMain.class);

	public static void main(String[] args) {
		logger.info("TimeAdjusterMain started");

		//Setting information
	    SettingBean setting;
	    try {
			// args[0]:Administrator password
			// args[1]: src/dist folder absolute path
			setting = new SettingBean(args[0], args[1], "list.tsv");
		} catch (Exception e) {
			logger.error("Setting information error", e);
			return;
		}

	    //Processing execution
	    TimeAdjusterLauncher launcher = new TimeAdjusterLauncher(setting);
	    try {
	    	launcher.execute();
		} catch (Exception e) {
			logger.error("Time adjustment processing error", e);
			return;
		}
	}
}

Setting information

SettingBean.java


package jp.co.sample.timeadjuster.bean;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SettingBean {

	public static Logger logger = LoggerFactory.getLogger(SettingBean.class);

	/**
	 *Administrator password
	 */
	String passwd;

	/**
	 *Execution path reference directory
	 */
	String basePath;

	/**
	 *tsv filename
	 */
	String tsvName;

	/**
	 *Server information list
	 */
	ArrayList<ServerBean> serverList = new ArrayList<>();


	public SettingBean(String passwd, String basePath, String tsvName) throws IOException {
		this.passwd = passwd;

		this.basePath = basePath;

		logger.debug("this.basePath: "+ this.basePath);

		this.tsvName = tsvName;

		logger.debug("this.tsvName: "+ this.tsvName);

		File targetFile = new File(this.basePath, this.tsvName);
		if (!targetFile.isFile()) {
			logger.error("Please specify an existing file path.: "+ path);
			return;
		}

		//Read the file
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(targetFile),"UTF-8"));) {
			//TAB separated file with header. Exclude whitespace
			Iterable<CSVRecord> records = CSVFormat.TDF.withIgnoreEmptyLines().withIgnoreSurroundingSpaces().parse(reader);
				for (CSVRecord record : records) {

					logger.debug(record.get(0));

					//Check the IP address type for the IP address of the server.
					//When you create a tsv file with Notepad, it starts at the beginning?Because it will enter
					Pattern p = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
					Matcher m = p.matcher(record.get(0));

					if (!m.find()) {
						throw new RuntimeException("The IP address is specified incorrectly. IP address:"+ record.get(0));
					}

					//Get server information and add it to the list
					ServerBean server = new ServerBean(m.group(), record.get(1), record.get(2), record.get(3), record.get(4), record.get(5));
					serverList.add(server);

					logger.debug("server: "+ server.getIpaddress() + " / "+ server.getSerial_no() + " / "+ server.getDevice_name());
				}
		} catch (FileNotFoundException e) {
			logger.error("TSV file read error", e);
			throw e;
		} catch (IOException e) {
			logger.error("TSV file read error", e);
			throw e;
		}

	}


	public String getPasswd() {
		return passwd;
	}


	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}


	public String getBasePath() {
		return basePath;
	}


	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}


	public String getTsvName() {
		return tsvName;
	}


	public void setTsvName(String tsvName) {
		this.tsvName = tsvName;
	}


	public ArrayList<ServerBean> getServerList() {
		return serverList;
	}


	public void setServerList(ArrayList<ServerBean> serverList) {
		this.serverList = serverList;
	}

}

Server information (information read from tsv)

ServerBean.java


package jp.co.sample.timeadjuster.bean;

public class ServerBean {
	/**
	 *IP address
	 */
	String ipaddress;

	/**
	 *hostname
	 */
	String host_name;

	/**
	 *Serial number
	 */
	String serial_no;

	/**
	 *Base name
	 */
	String base_name;


	/**
	 *Installation location
	 */
	String installation_location;

	/**
	 *Equipment name
	 */
	String device_name;

	public ServerBean(String ipaddress, String host_name, String serial_no, String base_name,
			String installation_location, String device_name) {
		super();
		this.ipaddress = ipaddress;
		this.host_name = host_name;
		this.serial_no = serial_no;
		this.base_name = base_name;
		this.installation_location = installation_location;
		this.device_name = device_name;
	}

	public String getIpaddress() {
		return ipaddress;
	}

	public void setIpaddress(String ipaddress) {
		this.ipaddress = ipaddress;
	}

	public String getHost_name() {
		return host_name;
	}

	public void setHost_name(String host_name) {
		this.host_name = host_name;
	}

	public String getSerial_no() {
		return serial_no;
	}

	public void setSerial_no(String serial_no) {
		this.serial_no = serial_no;
	}

	public String getBase_name() {
		return base_name;
	}

	public void setBase_name(String base_name) {
		this.base_name = base_name;
	}

	public String getInstallation_location() {
		return installation_location;
	}

	public void setInstallation_location(String installation_location) {
		this.installation_location = installation_location;
	}

	public String getDevice_name() {
		return device_name;
	}

	public void setDevice_name(String device_name) {
		this.device_name = device_name;
	}

}

Time adjustment process

TimeAdjusterLauncher.java


package jp.co.sample.timeadjuster.launcher;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jp.co.sample.timeadjuster.bean.ServerBean;
import jp.co.sample.timeadjuster.bean.SettingBean;

public class TimeAdjusterLauncher {

	public static Logger logger = LoggerFactory.getLogger(TimeAdjusterLauncher.class);

	/**
	 *Setting information
	 */
	SettingBean setting;

	/**
	 *Login screen display result
	 *Hold cookies etc.
	 */
	Response loginRes;

	/**
	 *constructor
	 *
	 * @param setting setting information
	 */
	public TimeAdjusterLauncher(SettingBean setting) {
		super();

		//Hold settings
		this.setting = setting;
	}

	/**
	 *Executes the time adjustment process
	 * @throws RuntimeException
	 * @throws IOException
	 */
	public void execute() throws RuntimeException, IOException {

		//Loop processing for each server
		for (ServerBean server : setting.getServerList()) {
			logger.info("Start time adjustment****************** ");
			logger.info("Target server: "+ server.getIpaddress() + " / "+ server.getDevice_name());

			//Login
			login(server);

			//Check the currently set time
			String zone = getTimer(server);

			//New time setting
			adjustTimer(server, zone);
		}

	}

	/**
	 *Login process
	 * @throws RuntimeException
	 * @throws IOException
	 */
	private void login(ServerBean server) throws RuntimeException, IOException {
		//Login process execution
		String loginUrl = "http://"+ server.getIpaddress() + "/logon.cgi";
		try {
			loginRes = Jsoup.connect(loginUrl)
				    .data("Password", setting.getPasswd())
				    .data("Mode", "Admin")
				    .data("Lang", "Japanese")
				    .method(Method.POST)
				    .execute();
		} catch (IOException e) {
			logger.error("Initial screen login error", e);
			throw e;
		}

//		logger.debug(loginRes.parse().outerHtml());

		//HTTP status matching
		checkStatusCode(loginRes, loginUrl);


		if (StringUtils.contains(loginRes.parse().outerHtml(), "/pages/_top2.htm") ) {
			//If the login error screen is redirected
			//It means that the administrator login failed, so the error ended.
			throw new RuntimeException("Administrator login failed. It suspends processing.");
		}


		//Redirect after successful login
		//Processing that is not really necessary. I just went for confirmation.
		String redirectUrl = "http://"+ server.getIpaddress() + "/pages/_devadm.htm";
		Response redirectRes;
		try {
			redirectRes = Jsoup.connect(redirectUrl)
				//Set the cookie obtained at login
			    .cookies(loginRes.cookies())
			    .method(Method.GET)
			    .execute();
		} catch (IOException e) {
			logger.error("Initial screen login redirect error", e);
			throw e;
		}

//		logger.debug(redirectRes.parse().outerHtml());

		//HTTP status matching
		checkStatusCode(redirectRes, redirectUrl);

	}

	/**
	 *Gets the time currently set on the server
	 *
	 * @param server
	 * @throws RuntimeException
	 * @return zone
	 * @throws IOException
	 */
	private String getTimer(ServerBean server) throws RuntimeException, IOException {

		//Get the server time setting screen
		String timerUrl = "http://"+ server.getIpaddress() + "/pages/ed_time.htm";
		Response timerRes;
		try {
			timerRes = Jsoup.connect(timerUrl)
					//Set the cookie obtained at login
				    .cookies(loginRes.cookies())
				    .method(Method.GET)
				    .execute();
		} catch (IOException e) {
			logger.error("Time setting screen acquisition error", e);
			throw e;
		}

		//HTTP status matching
		checkStatusCode(timerRes, timerUrl);

//		logger.debug(timerRes.parse().outerHtml());

		Document doc = timerRes.parse();

		//Gets the time currently set on the server.
		String year = doc.getElementsByAttributeValue("name", "DateYYYY").get(0).val();
		String month = doc.getElementsByAttributeValue("name", "DateMM").get(0).val();
		String day = doc.getElementsByAttributeValue("name", "DateDD").get(0).val();
		String hour = doc.getElementsByAttributeValue("name", "TimeHH").get(0).val();
		String minutes = doc.getElementsByAttributeValue("name", "TimeMM").get(0).val();
		String zone = doc.select("select option[selected]").get(0).val();

		logger.info("Current server time: "+ year + "/"+ month + "/"+ day + " "+ hour +":"+ minutes + " ("+ zone + ")");

		//Only zone uses the existing value, so it is returned.
		return zone;
	}

	/**
	 *Set the time.
	 *
	 * @param server
	 * @param zone
	 * @throws RuntimeException
	 * @throws IOException
	 */
	private void adjustTimer(ServerBean server, String zone) throws RuntimeException, IOException {
		//Gets the current time of the execution environment.
		LocalDateTime now = LocalDateTime.now();

		logger.info("Current time: "+ now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

		//Acquires the timing of the next time adjustment in order to adjust to 0 seconds after 1 minute.
		LocalDateTime nextLdt =
		        LocalDateTime.now()
		        .plusMinutes(1)
		        .truncatedTo(ChronoUnit.MINUTES);

		logger.debug("Time to match: "+ nextLdt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

		//Wait milliseconds
		long localDiffMsec = ChronoUnit.MILLIS.between(now, nextLdt);

		logger.debug("Wait for milliseconds until the time to match: "+ localDiffMsec);

		try {
			//Wait milliseconds, wait
			Thread.sleep(localDiffMsec);
		} catch (InterruptedException e) {
			logger.error("Server time adjustment millisecond wait failure");
		}

		logger.info("Time adjustment execution");

		//Server time adjustment processing execution(POST)
		//For some reason it worked without cookies, so I just throw POST
		String timerUrl = "http://"+ server.getIpaddress() + "/settime.cgi";
		Response timerRes;
		try {
			timerRes = Jsoup.connect(timerUrl)
				    .data("DateYYYY", ""+ nextLdt.getYear())
				    .data("DateMM", ""+ nextLdt.getMonthValue())
				    .data("DateDD", ""+ nextLdt.getDayOfMonth())
				    .data("TimeHH", ""+ nextLdt.getHour())
				    .data("TimeMM", ""+ nextLdt.getMinute())
				    .data("TimeZone", ""+ zone)
				    .method(Method.POST)
				    .execute();
		} catch (IOException e) {
			logger.error("Server time adjustment posting error", e);
			throw e;
		}

//		logger.debug(timerRes.parse().outerHtml());

		//HTTP status matching
		checkStatusCode(timerRes, timerUrl);

		logger.info("Time adjustment completed: "+ server.getIpaddress() + " / "+ server.getDevice_name() + " / " + nextLdt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

	}

	/**
	 *HTTP status check
	 *
	 * @param res
	 * @param url
	 * @throws RuntimeException
	 */
	private void checkStatusCode(Response res, String url) throws RuntimeException {

		if (res.statusCode() != 200) {
			//HTTP status is 200(normal)If not, an error
			throw new RuntimeException("URL access failure: url="+ url + " status="+ res.statusCode());
		}
	}

}

Impressions

I would like to do my best this year as well. Thank you very much.

Recommended Posts

[Java] Set the time from the browser with jsoup
Set the date and time from the character string with POI
[Java] How to set the Date time to 00:00:00
Find the address class and address type from the IP address with Java
Code Java from Emacs with Eclim
Follow the link with Selenium (Java)
Work with Google Sheets from Java
The road from JavaScript to Java
[Gradle] Build a Java project with a configuration different from the convention
Find the address class and address type from the IP address with Java [No. 2 decoction]
SetCookie from the client side with OkHttp3
For the time being, run the war file delivered in Java with Docker
Introduction to java for the first time # 2
Call Java library from C with JNI
API integration from Java with Jersey Client
Java (set)
Kick ShellScript on the server from Java
[First Java] Make something that works with Intellij for the time being
Convert from java UTC time to JST time
Hit the Salesforce REST API from Java
Getting Started with Java Starting from 0 Part 1
[Java] Write the programming quiz SET INTERSECTION in one line with StreamAPI
How to compare only the time with Rails (from what time to what time, something like)
Learning for the first time java [Introduction]
java Calendar class (time set, comparison, format)
I translated the grammar of R and Java [Updated from time to time]
Execute Java code from cpp with cocos2dx
Try using the Wii remote with Java
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
[Java] Get the date with the LocalDateTime class
Easily monitor the indoor environment-⑪ Obtain the illuminance with Java from BH1750FVI (substitute)-(I2C / Pi4J)-
[Java] Get and display the date 10 days later using the Time API added from Java 8.
Initialize Ruby array with 0 like Java, that is, set the default value to 0
Increment with the third argument of iterate method of Stream class added from Java9
[Java] Rewrite the functions created by myself in the past from java.io.File with NIO.2.
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
[Java] Understand the difference between List and Set
Check the options set for the running Java process
Run Rust from Java with JNA (Java Native Access)
[LeJOS] Let's control the EV3 motor with Java
Understanding the MVC framework with server-side Java 1/4 View
Try calling the CORBA service from Spring (Java)
Set the time of LocalDateTime to a specific time
Understanding the MVC framework with server-side Java 3/4 Controller
Feel the passage of time even in Java
Text extraction in Java from PDF with pdfbox-2.0.8
Calculate the similarity score of strings with JAVA
Use Matplotlib from Java or Scala with Matplotlib4j
Try accessing the dataset from Java using JZOS
[Java] (for MacOS) How to set the classpath
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Use Java external library for the time being
The date time of java8 has been updated
Java starting with JShell-A peek into the Java world
Understanding the MVC framework with server-side Java 2/4 Model
Run Dataflow, Java, streaming for the time being
[Android development] Get an image from the server in Java and set it in ImageView! !!
Easily monitor the indoor environment-⑤ Obtain PM2.5 concentration from PPD42NS with Java (GPIO / Pi4J)-
Set a signed cookie (for CloudFront) with a custom policy using the AWS SDK for Java
Form and process file and String data at the same time with Spring Boot + Java