I am building a login using an API call to a URL that returns JSON data. The login works and is functional but in terms of "correctness" and "professionalism", I am looking to get input from some of the devs here. Please give me an tips that could possibly aide in making the most functional program here possible. (Please note I am using MATISSE to create the GUI)
This section is the Login GUI. Left out Matisse generate text:
public class LogIn extends javax.swing.JFrame {
ImageIcon imageIcon;
/**
* Creates new form LogIn
*/
public LogIn()
{
imageIcon = new ImageIcon(LogIn.this.getClass().getResource("/Res/loading.gif"));
initComponents();
}
/*
This actually completes the login and displays the LOADING dialog box.
*/
class loginAction extends AbstractAction {
LoginHandle lh = new LoginHandle();
String devid = "&t2mdeveloperid=#####-####-####-####-#######";
String account, username, password;
String[] loginDataArr;
@Override
public void actionPerformed(ActionEvent evt)
{
SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>()
{
@Override
protected Void doInBackground() throws Exception
{
account = "&t2maccount=" + txtAccount.getText();
username = "&t2musername=" + txtUsername.getText();
password = "&t2mpassword=" + txtPassword.getText();
loginDataArr = lh.loginData(account, username, password, devid);
return null;
}
};
Window win = SwingUtilities.getWindowAncestor((AbstractButton)evt.getSource());
mySwingWorker.addPropertyChangeListener(new PropertyChangeListener()
{
@Override
public void propertyChange(PropertyChangeEvent evt)
{
if (evt.getPropertyName().equals("state"))
{
if (evt.getNewValue() == SwingWorker.StateValue.DONE)
{
loadingDialog.dispose();
}
}
}
});
mySwingWorker.execute();
loadingDialog.setLocationRelativeTo(win);
loadingDialog.setVisible(true);
}
}
This class actually does the URL request and parsing of the return data:
public class LoginHandle
{
public String[] loginData(String account, String username, String password, String devId)
{
String[] data = new String[2];
JsonParser parser = new JsonParser();
String receipt = "";
/*
Creating our trust manager. The program isn't accepting the SSL from the server for some reason.
*/
TrustManager[] trustAllCerts = new TrustManager[]
{
new X509TrustManager()
{
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
/*
Setting trust
*/
try
{
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
catch(Exception e)
{
System.err.println("Unable to set secure.");
}
/*
Trying to establish the url connection and getting the data back.
*/
try
{
URL url = new URL("https://m2web.talk2m.com/t2mapi/login?" + devId + account + username + password);
System.out.println(url);
URLConnection yc = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String strTemp = "";
while(null != (strTemp = br.readLine()))
{
receipt = strTemp;
}
}
catch(Exception e)
{
System.err.println("Error getting data.");
}
/*
Parsing the return JSON
*/
try
{
Object obj = parser.parse(receipt);
JsonObject jsonObj = (JsonObject) obj;
JsonElement loginSuccess = jsonObj.get("success");
JsonElement loginSession = jsonObj.get("t2msession");
data[0] = loginSuccess.toString();
data[1] = loginSession.toString();
}
catch(Exception e)
{
System.err.println("Error parsing json");
e.printStackTrace();
}
return data;
}
}