Not getting sessin value in vaadin framework - java

Not getting sessin value in vaadin framework
Used below :
private void setCurrentUsername(String username){
VaadinService.getCurrentRequest().getWrappedSession().setAttribute("LOGGED_IN_AS_USER",username);
userSubMenu.setText(username);
}
public static String getCurrentUsername() {
//log.info("User:::::::::::::::::" + (String) VaadinService.getCurrentRequest().getWrappedSession().getAttribute("LOGGED_IN_AS_USER"));
return (String) VaadinService.getCurrentRequest().getWrappedSession().getAttribute("LOGGED_IN_AS_USER");
}
getting value as null when flow going to other class

You could try using VaadinSession.getCurrent().getSession() instead?

iIf you are using this for a user interface i.e a user logging in. Don't forget that when your app first builds/ when the user first enters it they will of course have a null value(including any sub pages).
Try adding a ViewChangeListener to the page that the user now enters (apologies for any errors in coding i'm not currently able to gain access to my machine) into:
#Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
this.username = VaadinSession.getCurrent().getAttribute("LOGGED_IN_AS_USER");
}

Related

How to disable jTextfield again when the other jTextfield is empty in java

Heya guys! Nolankr here.
I've got this code that enables my password jField when I place something inside the username jField but when I delete my inputs from the username jField my password jField stays enabled. I wanted it to go back to being disabled though. I'm still a starter so I'm so sorry.
private void usernameKeyTyped(java.awt.event.KeyEvent evt) {
String usern = username.getText();
if(usern != null){
password.setEnabled(true);
}else{
password.setEnabled(false);
}
}
I tried coding an infinite loop to it but it just made my .jar file to stop responding / won't close, so I had to close netbeans itself and restart it . xD
username and password are both jTextfields by the way and password is disabled by default
basically,
if username != null then enable password but if username = null again then disable password again
What you probably want is a document listener that will allow you to detect when the username field is changed and take appropriate action.
I'm writing this answer with the mobile app so it's hard to provide a code sample right now.
Basically you would set up the listener on username to check if username is null or empty and enable/disable the password field based on the result of that check.
EDIT:
I'm back at my computer now, and am able to provide a code sample. See below:
userNameTextBox.getDocument().addDocumentListener(new DocumentListener(){
#Override
public void insertUpdate(DocumentEvent e) {
handleTextChange();
}
#Override
public void removeUpdate(DocumentEvent e) {
handleTextChange();
}
#Override
public void changedUpdate(DocumentEvent e) {
//Do nothing here
}
private void handleTextChange(){
if(userNameTextBox.getText() == null ||
userNameTextBox.getText().trim().length() == 0){
passwordBox.setEnabled(false);
}else{
passwordBox.setEnabled(true);
}
}
});
Note that the changedUpdate method does nothing because it is not fired when the document text changes, it is fired when attributes change. See the javadoc for complete details.

Parse (Java for Android) login returns Object Not Found

I'm trying to login using Parse for android.
If I enter the correct username and password, I log in successfully.
But when I use a wrong password or username, I always get error 101: object not found.
Here's the code (Notice "username" and "password" are EditText):
private void doLogin() {
if (!validate()) { // IF VALIDATION FAILS, DO NOTHING
return;
} // ELSE...
String name = email.getText().toString();
String pass = password.getText().toString();
ParseUser.logInInBackground(name, pass, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
goToMainActivity(user.getUsername());
} else {
handleParseError(e);
}
}
});
}
Thanks for your help.
Update: Parse does not have means to check if there was an incorrect login field. Hence they use the general 101: Object Not Found error to catch it. Reference: https://parse.com/docs/android/api/com/parse/ParseException.html
Previous stackoverflow link: Parse : invalid username, password
If you want your app to respond to an incorrect login, just replace the line handleParseError(e); with code to handle it.
For example, if you want a message box to show up, place that code there. If you do not want to do anything, comment out that line. Not sure what else you are looking for...
I would suggest replacing it with a Toast message

Wicket 6 !continueToOriginalDestination: operator ! is undefined

Situation
I'm migrating a project from Wicket 1.5.7 to Wicket 6.12, one of the errors I get is explained below.
Code
#Override
protected void onSubmit() {
final String usernameValue = mail.getModelObject();
//Password is left empty in this particular case
AuthenticatedWebSession.get().signIn(usernameValue,"");
if (!continueToOriginalDestination())
{
setResponsePage(getApplication().getHomePage());
}
}
Error
This is the error I got when changing wicket versions: The operator !
is undefined for the argument type(s) void
Note: I see this error when hovering over !continueToOriginalDestination
What did I try
In my search on stackoverflow I came accross this question:
continueToOriginalDestination does not bring me back to originating page
Also checked this topic on apache wicket:
http://apache-wicket.1842946.n4.nabble.com/Handling-ReplaceHandlerException-on-continueToOriginalDestination-in-wicket-1-5-td4101981.html#a4115437
So I changed my code to this:
#Override
public void onSubmit() {
final String usernameValue = mail.getModelObject();
AuthenticatedWebSession.get().signIn(usernameValue,"");
setResponsePage(getApplication().getHomePage());
throw new RestartResponseAtInterceptPageException(SignInPage.class);
}
Question
The old situation nor the code change seem to work in my particular case.
Maybe it's a small change, is my new code wrong, how should this work?
Has Wicket changed that much, so that the old code is not supported anymore, or can !continueToOriginalDestination be used as well?
This helps
http://www.skybert.net/java/wicket/changes-in-wicket-after-1.5/
In 1.5, you could do the following to break out of the rendering of one page, go to another page (like login page) and then send the user back to where he/she was:
public class BuyProductPage extends WebPage {
public BuyProductPage() {
User user = session.getLoggedInUser();
if (user null) {
throw new RestartResponseAtInterceptPageException(LoginPage.class);
}
}
}
and then in LoginPage.java have this to redirect the user back to BuyProductPage after he/she's logged in:
public class LoginPage extends WebPage {
public LoginPage() {
// first, login the user, then check were to send him/her:
if (!continueToOriginalDestination()) {
// redirect the user to the default page.
setResponsePage(HomePage.class);
}
}
}
The method continueToOriginalDestination has changed in Wicket 6, it's now void which makes your code look more magic and less than logic IMO:
public class LoginPage extends WebPage {
public LoginPage() {
// first, login the user, then check were to send him/her:
continueToOriginalDestination();
// Magic! If we get this far, it means that we should redirect the
// to the default page.
setResponsePage(HomePage.class);
}
}

JoptionPane Validation

I have a Swing GUI where I am restricting the user registration so that the username and the password cannot be the same. I am using JoptionPane for the task with the following code:
public void actionPerformed(ActionEvent e) {
String username = tuser.getText();
String password1 = pass1.getText();
String password2 = pass2.getText();
String workclass = wclass.getText();
Connection conn = null;
try {
if(username.equalsIgnoreCase(password1)) {
JOptionPane.showMessageDialog(null,
"Username and Password Cannot be the same. Click OK to Continue",
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
...
The problem is that I had to use System.exit(0); without it, the next code was getting executed. Even after the JOptionPane poped up, the registration was succeeding. I do not need the system to exit, but I need the user to be kept on the registration page after the validation. What is the best way to do this? Is there other convenient ways of doing this rather than using the JOptionPane?
Replace
System.exit(0);
with
return;
if you do not want the rest of the method to be performed
You need to place your code within endless loop, and break it upon successful result. Something like:
while(true)
{
// get input from user
if(vlaidInput) break;
}
place that next code into else part may be it works
if(username.equalsIgnoreCase(password1))
{
JOptionPane.showMessageDialog(null, "Username and Password Cannot be the same. Click OK to Continue","Error",JOptionPane.ERROR_MESSAGE);
}
else
{
//place that next code
// username and password not equals, then it will execute the code
}
First of all, it is best if the UI and business logic (in this case, validation) are separated. Have them separate sort of suggest a better way of handling interaction on its own. Thus, it makes sense to create a separate class UserValidation with method boolean isValid(). Something like this:
public class UserValidation {
private final String name;
private final String passwd;
private final String passwdRpt;
public UserValidation(final String name, final String passwd, final String passwdRpt) {
this.name = name;
this.passwd = passwd;
this.passwdRpt = passwdRpt;
}
public boolean isValid() {
// do your validation logic and return true if successful, or false otherwise
}
}
Then the action code would look like this:
public void actionPerformed(ActionEvent e) {
if (new UserValidation(tuser.getText(), pass1.getText(), pass2.getText()).isValid()) {
// do everything needed is validation passes, which should include closing of the frame of dialog used for entering credentials.
}
// else update the UI with appropriate error message -- the dialog would not close by itself and would keep prompting user for a valid entry
}
The suggested approach gives you a way to easily unit test the validation logic and use it in different situations. Please also note that if the logic in method isValid() is heavy than it should be executed by a SwingWorker. The invocation of SwingWorker is the responsibility of the action (i.e. UI) logic.

Java ssh client

I'm trying to create a web app which will check several service status, server stats etc. I found this http://www.jcraft.com/jsch/ seems to be pretty nice ssh java implementation. But every time I log in in to server I'm prompted to confirm RSA key fingerprint like this :
How can I override this, to always confirm yes without any prompts? I want to remove the whole swing part, I want to make this without any interaction, like this example code I took from the examples available on jscraft.com :
http://www.jcraft.com/jsch/examples/Exec.java
I'm not so familiar with swing and with java in general.
public class ManoUserInfo implements UserInfo {
String passwd;
public void setPassword(String pass) {
passwd = pass;
}
#Override
public String getPassphrase() {
return null;
}
public String getPassword() {
return passwd;
}
public boolean promptPassword(String arg0) {
return true;
}
public boolean promptPassphrase(String arg0) {
return true;
}
//this method responsible for that message, so just make it return true
public boolean promptYesNo(String arg0) {
// Object[] options = {"yes", "no"};
/* int foo = JOptionPane.showOptionDialog(null,
arg0,
"Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null, options, options[0]);*/
return true;
}
public void showMessage(String message) {
JOptionPane.showMessageDialog(null, message);
}
You have to understand why this message pops up.
SSH is a secure service, meaning that the identity of the client and the identity of the server are guaranteed. To guarantee that you actually reached the server you want to reach (for how it is possible that you connect to the wrong server without your knowledge, google "DNS cache poisoning"), the client displays the recieved server name and fingerprint. These values identify the server. You are supposed to look if this is in fact the fingerprint you generated on the server by comparing the fingerprint through a secure channel (via telephone with the server admin, for instance).
Having said that, usual SSH clients save your decision to accept the fingerprint/server name and do not ask again. It seems that your client does not. So you either have the option to change the source code (if it's licensed under an open license) or find a way to automatically press "Yes" whenever this question pops up (this can be achieved with toolkits like Autoit 3 with a very short script).
check out http://sourceforge.net/projects/sshtools/

Categories

Resources