Install4J: give feedback to user after successful JDBC connection check - java

I'm trying to build the following form in Install4J 6.1.6:
After entering the host and a port number the user can check if these MySQL settings are correct by pressing the Test connection button.
The Host's variable is called mysqlHost
and the Port's variable is mysqlPort
I initially had a problem that the user entered input only gets saved to the variables when the Next > button is pressed, but I could solve this by adding
formEnvironment.saveFormComponents() to the Key listener script for both input fields.
I set up the following "Check JDBC connection" action for the Test Connection button:
And it works fine, when the host and port are set incorrectly the user will see a pop up with the error message.
However I'm not able to display a Success message or label when the JDBC connection works.
I tried adding a green Success label (as seen on the first picture) with the following visibility script:
String errorMessage = (String)context.getVariable("mysqlTestError");
return errorMessage.length() == 0;
So I'm trying to check if the mysqlTestError variable is empty or not.
But it's not working, the Success label is NEVER displayed.
How can it be only displayed when the connection check was successful?

The visibility script of the label form component is only evaluated when the screen is shown, so it will not update the visibility after the JDBC connection check.
You can update the label programmatically by adding a "Run script" action to the "Action list" of the button form component:
String errorMessage = (String)context.getVariable("mysqlTestError");
FormComponent formComponent = formEnvironment.getFormComponentById("ID of label");
formComponent.setVisible(!errorMessage.isEmpty());
((JLabel)formComponent.getConfigurationObject()).setText(errorMessage);
return true;
In install4j 6, the "formEnvironment" parameter is not available in action lists and you would have to get it via
FormEnvironment formEnvironment = ((FormPanelContainer)context.getScreenById("ID of screen")).getFormEnvironment();

Related

Using htmlunit acquire the content of a webpage after a successful login

I am trying to do the following thing using htmlunit:
Get on a page, and login.
AFTER a successful login, acquire a particular page so that I can work with its content.
Suppose that the server is located at mysite.mydomain.com.
First let me describe what happens using an actual browser, e.g. Chrome.
I type mysite.mydomain.com on the address bar.
I am arriving at
mysite.mydomain.com/blahb/blahb.exe/index?SOMETHING=0&SOMETHINGELSE=1
There I can fill in my info for logging in, i.e. I can enter my username and password and press the submit button.
Upon successful login, I end up looking at the following page:
mysite.mydomain.com/blahb/blahinfo.exe/index (blahb.exe became blahinfo.exe)
There I have options. Say, a menu. Clicking on the particular option I am interested in, will bring up a button in the main window frame. Let's call this button "SHOW".
Clicking SHOW, the URL remains unchanged but the screen is split in two frames. TOP and BOTTOM. The TOP frame is the one that contains the button SHOW that we just pressed as well as two new buttons that just appeared: one to PRINT something and one to save that something as a PDF.
At the BOTTOM frame is the content that one could print or save, and which I am interested in.
A simple inspection of the page's code shows that the content I am interested in and which is depicted at the bottom frame was taken from:
mysite.mydomain.com/blahb/blahinfo.exe/somethingBody
Indeed, If I simply login to the site, and instead of pressing any menu buttons after that, I simply visit:
mysite.mydomain.com/blahb/blahinfo.exe/somethingBody
Will result in me getting the content I need on my screen, in a single frame.
If I am logged in, I can open another tab, paste
mysite.mydomain.com/blahb/blahinfo.exe/somethingBody
And I will get the content.
So far pretty simple.
Now let's see what happens when I use htmlunit.
I can do the following:
private static HtmlPage loginAndGetPageOfInterest(WebClient webClient) throws Exception
{
//enable whatever I want to enable for the webClient
webClient.getOptions().setJavaScriptEnabled(true);
//deal with cookies
CookieManager cookieManager = new CookieManager(); //seems we need cookies
cookieManager = webClient.getCookieManager();
cookieManager.setCookiesEnabled(true);
String mainURL = "mysite.mydomain.com/blahb/blahb.exe/index?SOMETHING=0&SOMETHINGELSE=1";//want to go there
HtmlPage currentPage = webClient.getPage(mainURL) ;
//Fill the login info and press the button
HtmlTextInput nameInput = currentPage.getHtmlElementById("Id");//find the username field
nameInput.setValueAttribute("Iamtheuser"); //fill it in
HtmlInput passInput = currentPage.getHtmlElementById("Pass"); //likewise for password
passInput.setValueAttribute("andthisismypass");
HtmlButton submit = (HtmlButton) currentPage.getElementsById("login_button").get(0);//find the submit button
currentPage = submit.click();
Thread.sleep(2000); //wait some time to ensure that we have indeed logged in.
//if I were to print currentPage in a file NOW... ALL is well. I get exactly what I get in the browser. If I were to return the currentPage here, everything is fine.
String urlIActuallyWant = "mysite.mydomain.com/blahb/blahinfo.exe/somethingBody";
currentPage = webClient.getPage(urlIActuallyWant);//try to get what I actually want...
return currentPage;
}
Now I can do:
public static void main(String[] args) throws Exception
{
WebClient webClient = new WebClient(BrowserVersion.CHROME); //Could do without saying anything about client version
HtmlPage currentPage = loginAndGetPageOfInterest(webClient);
//save what we have so that we can look at it.
String pageSource = currentPage.asXml();
File file = new File("howInteresting.html");
try
{
Files.write(file.toPath(), pageSource.getBytes());
Desktop.getDesktop().browse(file.toURI());
}
catch (IOException e)
{ //end of the world }
}
However... when I attempt this, I am getting:
Server Error
500 - Internal server
error. There is a problem with the resource you are looking
for, and it cannot be displayed.
Exception in thread "main"
com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 500
Internal Server Error for
https://mysite.mydomain.com/blahb/blahinfo.exe/somethingBody at
com.gargoylesoftware.htmlunit.WebClient.throwFailingHttpStatusCodeExceptionIfNecessary(WebClient.java:595)
at
com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:410)
at
com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:317)
at
com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:469)
at
com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:450)
at TestWTH.loginAndGetPageOfInterest(TestWTH.java:46) at
TestWTH.main(TestWTH.java:54)
Now I understand that the server, is sending back a 500 Internal Server Error and I suspect that this is because it thinks that we are NOT logged in.
But why would that be?
The fact that if I log in and then go directly to the page of interest works fine on the browser but not when using htmlunit makes me suspect that it is an issue with cookie handling. But why would there be an issue with the cookies? Any ideas?

Hibernate - Tomcat - SQL Server - Connection closed issue

I've a webapp running on TOMCAT 7.0.5 - JDK 1.7 - and connecting to SQL Server 2012.
The hibernate version is 4.3.5. Browser used is IE 11.0.
The issue Is I have a webpage: A normal FORM submit (submit.jsp). It shows a readonly page (submitConfirm.jsp) and has two buttons.
Submit - Saves the application and redirects the page to the home page to start subscription again
Print - opens a new tab via javascript to print a PDF form that likely takes 2 to 5 seconds to render in a "new" tab.
The submit button is disabled by defualt and enabled only when user clicks on the PRINT Button. While the item is still rendering If someone clicks on Continue button - all actions perform well and at the I get both the print and subscriptions saved.
The next subscriptions entered would throw a http 500 error indicating the "Connection is already closed".
I use the Standard HibernateUtil paradigm to begin/end/commit/rollback transactions.
The models are all XML based (not entity decorator based).
For normal workflows where if the user waits till the PDF is rendered all is fine.
But If the user is pressing the button of continue - Seems there is an issue.
Attached is the screen-grab of the error is someone finds it useful provide some hints.
Based on the user feedback (the concerned method):
public String reportSessionId(String userId) throws InfrastructureException {
String sid = null;
try {
sid = (String) HibernateUtil.getSession()
.getNamedQuery("com.ttsme.ims.model.IMSUser.reportSessionId")
.setString("userId", userId).uniqueResult();
}
catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
catch (Exception exp)
{
exp.printStackTrace();
}
return sid;
}

How do I implement popup dialog for login in Android that never pops up until logout?

I am trying to do the code in android for first time showing the dialog popup for login screen and after login the popup will be removed and after that it will not open until you do not logout from the screen.
You can store a boolean variable i.e. True or False value in SQLite database or Shared Preference... Check its value whenever someone is opening your app If true than dont show Login screen otherwise Show login Screen :)
You can check boolean variable when your app is showing Splash Screen ...
For this, first set value in SharedPreference as false. Every time when you open the app, check value from SharedPreference. If it is false, then show the login popup.
Once login is successfully performed, set value as true in shared preference and again set it to false on logout.
use SharedPreferences
SharedPreferences preferences =PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
//login success
preferences.edit().putBoolean("login",true).commit();
//logout success
preferences.edit().putBoolean("login",false).commit();
//Whether the display dialog
boolean isLogin = preferences.getBoolean("login",false);
if(!isLogin){
showLoginDialog();
}else{
enterHome();
}
Storing shared preferences is probably not the best idea since after login you probably have some type of access token or something to use for request authorisation. By saving a boolean, you will have two variables stored that can be used to deduce the same thing. So you can just write a function that checks whether you have a valid token stored or what every it is you use to authorise requests after login. If you are using the accountmanager or something then you should use that to verify the user is still logged in.

How to show error message when internet/server/vpn is not available

I have a code something goes like this;
<a4j:commandLink
actionListener="#{controller.validate}"
onclick="showFadeLoadingImg();"
oncomplete="confirmOperation();}"
value="#{actionItem.actionDisplayName}"
reRender="text1,text2">
</a4j:commandLink>
Here, when my VPN is down, my oncomplete event has jscript function confirmOperation(), which gets executed and undesired output from that function is being displayed. But, what I ultimately want is, when I click the button, I want to check whether I can able to connect to the server(VPN/internet enabled or not), and if not I have to show some error message like 'Unable to reach server' or else, can I redirect the page to login page?
You can connect to the server on validate() method,If you can't connect to the server, you can record the information,and then you can user you jscript function confirmOperation() to display this infomation.

appropriate way to validate the existence of URI (URL/URN)?

here is the deal, I have a JTextField with a KeyListener so any time I press (release/type) a key, it calls a method (validateButton()), which check if the text on the JTextFile is a valid URL (in the title says it could be whether a URL or a URN, but, by now, I'm more interested on recognize URL's, but I would appreciate if you consider both of them in your answer) the thing is that every time a press a key, my code tries to establish a connection to the server, so my GUI stops working until the connection is over, so I thought, I'm going to use a thread to the connection, but I would like to "kill/terminate" the current thread (the one that is checking a URL(if any)) any time a key is press (release/type), to save bandwidth, I know I could do it by pressing a button, (wich will save even more bandwidth) at the end of the edit and if it is valid proceed and if it is not then do something else, but that is not what I'm trying to do; the problem is that is not recommended to kill a thread, but while the thread is establishing the connection I won't be able to call a method or change a property variable to kill him, any idea to do this (using or not the approximation described here).
Thanks in advance
P.D: I'm using the next code to check the existance of the url:
URL u = new URL (tfURI.getText());
HttpURLConnection huc = ( HttpURLConnection ) u.openConnection ();
huc.setRequestMethod ("HEAD");
huc.connect () ;
code = huc.getResponseCode() ;
if the code is different from 404 or any other error code I'm enabling my button otherwise I'm disabling it.

Categories

Resources