diff options
Diffstat (limited to 'dozentenmodul/src/main/java/util')
| -rw-r--r-- | dozentenmodul/src/main/java/util/News.java | 51 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/util/ShibbolethECP.java | 108 |
2 files changed, 125 insertions, 34 deletions
diff --git a/dozentenmodul/src/main/java/util/News.java b/dozentenmodul/src/main/java/util/News.java index 22f78c92..aaee7f2a 100644 --- a/dozentenmodul/src/main/java/util/News.java +++ b/dozentenmodul/src/main/java/util/News.java @@ -14,6 +14,7 @@ import javax.xml.parsers.ParserConfigurationException; import models.SessionData; +import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -33,6 +34,9 @@ import org.xml.sax.SAXException; public class News { + // Logger instance for this class + private final static Logger LOGGER = Logger.getLogger(News.class); + // Private members representing the news private static String headline = null; private static String content = null; @@ -114,20 +118,14 @@ public class News { URL url = null; try { url = new URL("http://" + SessionData.session.getServerAdress() + "/slx-admin/api.php?do=news"); - } catch (MalformedURLException e2) { - e2.printStackTrace(); - // This shouldn't happen, but in case it does, alert user. - JOptionPane.showMessageDialog(null, - "News-URL falsch formatiert!", - "Fehler", JOptionPane.ERROR_MESSAGE); + } catch (MalformedURLException e) { + LOGGER.error("Malformated URL to News-API, see trace: ", e); } - // check for argument + // check is the url got created. if (url == null) { - // no URL, alert user. - JOptionPane.showMessageDialog(null, - "Keine News-URL angegben!", - "Fehler", JOptionPane.ERROR_MESSAGE); + // no URL, log it and return + LOGGER.error("URL to the News-API not defined!"); return; } @@ -139,11 +137,8 @@ public class News { urlCon.setConnectTimeout(5000); // 10 seconds timeout for reading the stream urlCon.setReadTimeout(10000); - } catch (IOException e1) { - e1.printStackTrace(); - JOptionPane.showMessageDialog(null, - "Satellite antwortete auf die Anfrage der News-XML nicht!", - "Fehler", JOptionPane.ERROR_MESSAGE); + } catch (IOException e) { + LOGGER.error("IO error while requesting News URL, see trace: ", e); return; } @@ -151,10 +146,7 @@ public class News { try { is = urlCon.getInputStream(); } catch (IOException e1) { - e1.printStackTrace(); - JOptionPane.showMessageDialog(null, - "Konnte den News-URL-Stream nicht öffnen!", - "Fehler", JOptionPane.ERROR_MESSAGE); + LOGGER.error("Could not open stream to URL '" + url.toString() + "', see trace: ", e1); return; } @@ -164,11 +156,8 @@ public class News { try { builder = factory.newDocumentBuilder(); - } catch (ParserConfigurationException e1) { - e1.printStackTrace(); - JOptionPane.showMessageDialog(null, - "XML-Parser falsch konfiguriert!", - "Fehler", JOptionPane.ERROR_MESSAGE); + } catch (ParserConfigurationException e) { + LOGGER.error("XML parsing configuration error, see trace: ", e); return; } @@ -176,16 +165,10 @@ public class News { try { doc = builder.parse(is); } catch (SAXException e) { - e.printStackTrace(); - JOptionPane.showMessageDialog(null, - "Konnte die News-XML nicht parsen!", - "Fehler", JOptionPane.ERROR_MESSAGE); + LOGGER.error("XML parsing error, see trace: ", e); return; } catch (IOException e) { - JOptionPane.showMessageDialog(null, - "IO-Fehler!", - "Fehler", JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); + LOGGER.error("IO-Error, see trace: ", e); return; } @@ -228,7 +211,7 @@ public class News { try { tmpDate = new Date(Long.parseLong(dateNode.getNodeValue().trim()) * 1000); } catch (NumberFormatException nfe) { - nfe.printStackTrace(); + LOGGER.error("Date format is invalid, see trace: ", nfe); JOptionPane.showMessageDialog(null, "Zeitstempel aus der XML is invalid!", "Fehler", JOptionPane.ERROR_MESSAGE); diff --git a/dozentenmodul/src/main/java/util/ShibbolethECP.java b/dozentenmodul/src/main/java/util/ShibbolethECP.java new file mode 100644 index 00000000..374e3a6e --- /dev/null +++ b/dozentenmodul/src/main/java/util/ShibbolethECP.java @@ -0,0 +1,108 @@ +package util; + +import java.io.IOException; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.util.EntityUtils; +import org.apache.log4j.Logger; +import org.opensaml.DefaultBootstrap; +import org.opensaml.xml.ConfigurationException; + +import de.tudarmstadt.ukp.shibhttpclient.ShibHttpClient; + + +public class ShibbolethECP { + + // Logger + private final static Logger LOGGER = Logger.getLogger(ShibbolethECP.class); + + // IdP URL + private static String identityProviderUrl; + + public static void setIdentityProviderUrl(String identityProviderUrl) { + ShibbolethECP.identityProviderUrl = identityProviderUrl; + } + + public static boolean init(String idpUrl) { + try { + DefaultBootstrap.bootstrap(); + } catch (ConfigurationException ce) { + ce.printStackTrace(); + LOGGER.error("OpenSAML wrongly configured."); + return false; + } + + if (idpUrl != null) { + // TODO sanity check on the URL? + setIdentityProviderUrl(idpUrl); + } else { + // no IdP given + return false; + } + + // everything fine, return true + return true; + } + + public static Boolean doLogin(final String user, final String pass) { + + // first lets do some sanity checks + if (user == null) { + LOGGER.error("No username given, aborting..."); + return false; + } + + if (pass == null) { + LOGGER.error("No password given, aborting..."); + return false; + } + + if (identityProviderUrl == null) { + LOGGER.error("Identity provider is not set, did you initialize this class correctly?"); + return false; + } + + // The last argument indicates to accept any certificate + HttpClient client = new ShibHttpClient(identityProviderUrl, user, pass, true); + HttpGet req = new HttpGet("https://bwlp-masterserver.ruf.uni-freiburg.de/secure-all/test.php"); + String res = null; + ResponseHandler<String> respHandler = new ResponseHandler<String>() { + public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { + int status = response.getStatusLine().getStatusCode(); + if (status == 200 || status == 302) { + HttpEntity entity = response.getEntity(); + return entity != null ? EntityUtils.toString(entity) : null; + } else { + throw new ClientProtocolException("Unexpected response status: " + status); + } + } + }; + try { + res = client.execute(req, respHandler); + } catch (ClientProtocolException e) { + // this is thrown on http return code not 200 or 302, indicates wrong login + // TODO handle this with possible error causes: creds wrong, etc... + LOGGER.error("Fatal error requesting '" + req.getURI() + "':", e); + return false; + } catch (IOException e) { + LOGGER.error("Fatal protocol error requesting '" + req.getURI() + "':", e); + return false; + } + + // did we get a response? + if (res != null) { + LOGGER.info(res); + // return true, to signal a successful login + return true; + } else { + // we shouldn't actually reach this code... + LOGGER.error("Seems like the request worked, but the response is empty. Something is very wrong..."); + return false; + } + } +} |
