From 01014c5b00fec87101d971acdf88f523a9b4254a Mon Sep 17 00:00:00 2001 From: yvesf Date: Thu, 24 Mar 2011 22:09:06 +0100 Subject: JavaDoc Source Documentation, replace sun.misc Base64 with commons --- pom.xml | 7 +- .../java/de/dhbw/horb/calendar/ICSServlet.java | 39 ------ .../horb/calendar/dualis/DualisConnection.java | 10 +- .../java/de/dhbw/horb/calendar/dualis/package.html | 5 + .../de/dhbw/horb/calendar/ics/AbstractICSType.java | 9 +- .../de/dhbw/horb/calendar/ics/ICalendarObject.java | 58 ++++++++ .../java/de/dhbw/horb/calendar/ics/VCalendar.java | 51 ------- .../java/de/dhbw/horb/calendar/ics/VEvent.java | 85 ------------ .../de/dhbw/horb/calendar/ics/VEventComponent.java | 85 ++++++++++++ .../java/de/dhbw/horb/calendar/ics/package.html | 5 + .../calendar/servlet/AuthenticatedServlet.java | 79 +++++++++++ .../de/dhbw/horb/calendar/servlet/ICSServlet.java | 42 ++++++ .../de/dhbw/horb/calendar/servlet/package.html | 5 + .../horb/calendar/ui/InteractiveLoginWorker.java | 149 +++++++++++++++++++++ .../java/de/dhbw/horb/calendar/ui/LoginWorker.java | 149 --------------------- .../de/dhbw/horb/calendar/ui/MyApplication.java | 4 +- .../horb/calendar/ui/StundenplanComponent.java | 4 +- .../java/de/dhbw/horb/calendar/ui/package.html | 5 + .../horb/calendar/util/AuthenticatedServlet.java | 84 ------------ src/main/webapp/WEB-INF/web.xml | 2 +- 20 files changed, 457 insertions(+), 420 deletions(-) delete mode 100644 src/main/java/de/dhbw/horb/calendar/ICSServlet.java create mode 100644 src/main/java/de/dhbw/horb/calendar/dualis/package.html create mode 100644 src/main/java/de/dhbw/horb/calendar/ics/ICalendarObject.java delete mode 100644 src/main/java/de/dhbw/horb/calendar/ics/VCalendar.java delete mode 100644 src/main/java/de/dhbw/horb/calendar/ics/VEvent.java create mode 100644 src/main/java/de/dhbw/horb/calendar/ics/VEventComponent.java create mode 100644 src/main/java/de/dhbw/horb/calendar/ics/package.html create mode 100644 src/main/java/de/dhbw/horb/calendar/servlet/AuthenticatedServlet.java create mode 100644 src/main/java/de/dhbw/horb/calendar/servlet/ICSServlet.java create mode 100644 src/main/java/de/dhbw/horb/calendar/servlet/package.html create mode 100644 src/main/java/de/dhbw/horb/calendar/ui/InteractiveLoginWorker.java delete mode 100644 src/main/java/de/dhbw/horb/calendar/ui/LoginWorker.java create mode 100644 src/main/java/de/dhbw/horb/calendar/ui/package.html delete mode 100644 src/main/java/de/dhbw/horb/calendar/util/AuthenticatedServlet.java diff --git a/pom.xml b/pom.xml index 9027821..d3eb37b 100644 --- a/pom.xml +++ b/pom.xml @@ -58,7 +58,7 @@ dependencies - index + index license @@ -92,6 +92,11 @@ 3.8.1 test + + ws-commons-util + ws-commons-util + 1.0.1 + diff --git a/src/main/java/de/dhbw/horb/calendar/ICSServlet.java b/src/main/java/de/dhbw/horb/calendar/ICSServlet.java deleted file mode 100644 index 8a1be3e..0000000 --- a/src/main/java/de/dhbw/horb/calendar/ICSServlet.java +++ /dev/null @@ -1,39 +0,0 @@ -package de.dhbw.horb.calendar; - -import java.io.IOException; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import de.dhbw.horb.calendar.dualis.DualisConnection; -import de.dhbw.horb.calendar.dualis.DualisException; -import de.dhbw.horb.calendar.ics.VCalendar; -import de.dhbw.horb.calendar.ics.VEvent; -import de.dhbw.horb.calendar.util.AuthenticatedServlet; - -public class ICSServlet extends AuthenticatedServlet { - private static final long serialVersionUID = 1L; - - public ICSServlet() { - setLoginRealm("Dualis Calendar Connector"); - } - - @Override - protected void doGet(HttpServletRequest request, - HttpServletResponse response, String username, String password) - throws IOException { - response.setHeader("Content-Type", "text/calendar; charset=utf-8"); - - VCalendar calendar = new VCalendar("DHBW Calendar"); - try { - DualisConnection connection = new DualisConnection(username, - password); - for (VEvent event : connection.getEvents()) { - calendar.add(event); - } - } catch (DualisException e) { - e.printStackTrace(); - } - response.getOutputStream().write(calendar.serialize().getBytes()); - } -} \ No newline at end of file diff --git a/src/main/java/de/dhbw/horb/calendar/dualis/DualisConnection.java b/src/main/java/de/dhbw/horb/calendar/dualis/DualisConnection.java index c60349f..89e566c 100644 --- a/src/main/java/de/dhbw/horb/calendar/dualis/DualisConnection.java +++ b/src/main/java/de/dhbw/horb/calendar/dualis/DualisConnection.java @@ -31,7 +31,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage; import de.dhbw.horb.calendar.dualis.DualisException.DualisAuthenticationException; import de.dhbw.horb.calendar.dualis.DualisException.DualisScrapingException; -import de.dhbw.horb.calendar.ics.VEvent; +import de.dhbw.horb.calendar.ics.VEventComponent; public class DualisConnection { public static final String DUALIS_URL = "https://dualis.dhbw.de/scripts/mgrqcgi?APPNAME=CampusNet&PRGNAME=EXTERNALPAGES&ARGUMENTS=-N000000000000001,-N,-Awelcome"; @@ -56,13 +56,13 @@ public class DualisConnection { this.password = password; } - public List getEvents() throws FailingHttpStatusCodeException, + public List getEvents() throws FailingHttpStatusCodeException, IOException, DualisScrapingException, DualisAuthenticationException { HtmlPage page, loginPage; final HtmlForm loginForm; final HtmlInput usernameField, passwordField, submitButton; final WebClient webClient = new WebClient(); - final List events = new ArrayList(); + final List events = new ArrayList(); webClient.setIncorrectnessListener(new IncorrectnessListener() { @Override @@ -179,7 +179,7 @@ public class DualisConnection { return events; } - private void getEventsFromPage(HtmlPage page, final List events) { + private void getEventsFromPage(HtmlPage page, final List events) { for (HtmlElement elem : page.getElementsByTagName("div")) { if (!elem.getAttribute("class").equals("tbMonthDay")) continue; @@ -223,7 +223,7 @@ public class DualisConnection { cstart.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); cend.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); - VEvent event = new VEvent.Builder().dtstart(cstart) + VEventComponent event = new VEventComponent.Builder().dtstart(cstart) .dtstamp(cstart).dtend(cend).summary(title) .description(title).location(room) .status("CONFIRMED").build(); diff --git a/src/main/java/de/dhbw/horb/calendar/dualis/package.html b/src/main/java/de/dhbw/horb/calendar/dualis/package.html new file mode 100644 index 0000000..7a37853 --- /dev/null +++ b/src/main/java/de/dhbw/horb/calendar/dualis/package.html @@ -0,0 +1,5 @@ + + +This package contains the Dualis Webscraper Classes based on htmlunit. + + \ No newline at end of file diff --git a/src/main/java/de/dhbw/horb/calendar/ics/AbstractICSType.java b/src/main/java/de/dhbw/horb/calendar/ics/AbstractICSType.java index a03de64..272148f 100644 --- a/src/main/java/de/dhbw/horb/calendar/ics/AbstractICSType.java +++ b/src/main/java/de/dhbw/horb/calendar/ics/AbstractICSType.java @@ -4,6 +4,12 @@ import java.text.SimpleDateFormat; import org.apache.commons.lang.StringEscapeUtils; +/** + * icalendar specifies Events, Todo, Journal, Free/Buse, TimeZone, Alarm + * Components. + * + * {@link AbstractICSType} is a superclass for those. + */ public abstract class AbstractICSType { /** * Serialisierert dieses Calenderteil. Dem Ergebnis Fehlt noch der vCalendar @@ -14,6 +20,7 @@ public abstract class AbstractICSType { /** * RFC says: + * *
 	 * FORM #2: DATE WITH UTC TIME
 	 *
@@ -38,6 +45,6 @@ public abstract class AbstractICSType {
 	 * @return
 	 */
 	public static String escapeText(String text) {
-		return StringEscapeUtils.escapeJava(text); //XXX TODO
+		return StringEscapeUtils.escapeJava(text); // XXX TODO
 	}
 }
diff --git a/src/main/java/de/dhbw/horb/calendar/ics/ICalendarObject.java b/src/main/java/de/dhbw/horb/calendar/ics/ICalendarObject.java
new file mode 100644
index 0000000..e687d04
--- /dev/null
+++ b/src/main/java/de/dhbw/horb/calendar/ics/ICalendarObject.java
@@ -0,0 +1,58 @@
+package de.dhbw.horb.calendar.ics;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Represent an iCalender Object.
+ *
+ * A iCalender object can consist of many components of different type. They are
+ * implemented as subclasses of {@link AbstractICSType} and can be added to an
+ * instance of {@link ICalendarObject}.
+ */
+public class ICalendarObject {
+	List icsObjects = new ArrayList();
+	private final String title;
+
+	public ICalendarObject(final String title) {
+		this.title = title;
+	}
+
+	public void add(AbstractICSType ics) {
+		icsObjects.add(ics);
+	}
+
+	/**
+	 *
+	 * @return serialized iCalendar Object
+	 */
+	public String serialize() {
+		final StringBuilder sb = new StringBuilder();
+		// Einleiten des Kalenders
+		sb.append("BEGIN:VCALENDAR\n");
+		// This property specifies the identifier for the product that created
+		// the iCalendar object.
+		sb.append("PRODID:-//DHBW Horb i08005//DHBW Calender 0.1//EN\n");
+		// Datenformat version
+		sb.append("VERSION:2.0\n");
+		// This property defines the calendar scale used for the
+		// calendar information specified in the iCalendar object.
+		sb.append("CALSCALE:GREGORIAN\n");
+		// This property defines the iCalendar object method
+		// associated with the calendar object.
+		sb.append("METHOD:PUBLISH\n");
+		/* Extensions */
+		//
+		sb.append("X-WR-TIMEZONE:UTC\n");
+		// Name des Kalenders
+		sb.append("X-WR-CALDESC:" + AbstractICSType.escapeText(title) + "\n");
+		sb.append("X-WR-CALNAME:" + AbstractICSType.escapeText(title) + "\n");
+
+		// Elemente
+		for (AbstractICSType ics : icsObjects) {
+			sb.append(ics.serialize());
+		}
+		sb.append("END:VCALENDAR\n");
+		return sb.toString();
+	}
+}
diff --git a/src/main/java/de/dhbw/horb/calendar/ics/VCalendar.java b/src/main/java/de/dhbw/horb/calendar/ics/VCalendar.java
deleted file mode 100644
index fcf083a..0000000
--- a/src/main/java/de/dhbw/horb/calendar/ics/VCalendar.java
+++ /dev/null
@@ -1,51 +0,0 @@
-package de.dhbw.horb.calendar.ics;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class VCalendar {
-	List icsObjects = new ArrayList();
-	private final String title;
-
-	public VCalendar(final String title) {
-		this.title = title;
-	}
-
-	public void add(AbstractICSType ics) {
-		icsObjects.add(ics);
-	}
-
-	/**
-	 * 
-	 * @return
-	 */
-	public String serialize() {
-		final StringBuilder sb = new StringBuilder();
-		// Einleiten des Kalenders
-		sb.append("BEGIN:VCALENDAR\n");
-		// This property specifies the identifier for the product that created
-		// the iCalendar object.
-		sb.append("PRODID:-//DHBW Horb i08005//DHBW Calender 0.1//EN\n");
-		// Datenformat version
-		sb.append("VERSION:2.0\n");
-		// This property defines the calendar scale used for the
-		// calendar information specified in the iCalendar object.
-		sb.append("CALSCALE:GREGORIAN\n");
-		// This property defines the iCalendar object method
-		// associated with the calendar object.
-		sb.append("METHOD:PUBLISH\n");
-		/* Extensions */
-		//
-		sb.append("X-WR-TIMEZONE:UTC\n");
-		// Name des Kalenders
-		sb.append("X-WR-CALDESC:" + AbstractICSType.escapeText(title)+"\n");
-		sb.append("X-WR-CALNAME:" + AbstractICSType.escapeText(title)+"\n");
-		
-		// Elemente
-		for (AbstractICSType ics : icsObjects) {
-			sb.append(ics.serialize());
-		}
-		sb.append("END:VCALENDAR\n");
-		return sb.toString();
-	}
-}
diff --git a/src/main/java/de/dhbw/horb/calendar/ics/VEvent.java b/src/main/java/de/dhbw/horb/calendar/ics/VEvent.java
deleted file mode 100644
index dea4a17..0000000
--- a/src/main/java/de/dhbw/horb/calendar/ics/VEvent.java
+++ /dev/null
@@ -1,85 +0,0 @@
-package de.dhbw.horb.calendar.ics;
-
-import java.util.Calendar;
-import java.util.TimeZone;
-
-public class VEvent extends AbstractICSType {
-	public Calendar dtstamp = Calendar.getInstance();
-	public Calendar dtstart = Calendar.getInstance();
-	public Calendar dtend = Calendar.getInstance();
-	public String status = "";
-	public String summary = "";
-	public String description = "";
-	public String location = "";
-
-	/**
-	 * {@inheritDoc} 
- *
- * See RFC section 3.6.1 for VEVENT Specification - */ - @Override - public String serialize() { - final StringBuffer sb = new StringBuffer(); - dateTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - - sb.append("BEGIN:VEVENT\n"); - sb.append("DTSTART:" + dateTimeFormat.format(dtstart.getTime()) + "\n"); - sb.append("DTEND:" + dateTimeFormat.format(dtend.getTime()) + "\n"); - sb.append("DTSTAMP:" + dateTimeFormat.format(dtstamp.getTime()) + "\n"); - sb.append("UID:vevent_" + dateTimeFormat.format(dtstamp.getTime()) - + "@dhbw.de\n"); - sb.append("CREATED:" - + dateTimeFormat.format(Calendar.getInstance(TimeZone - .getTimeZone("UTC")).getTime()) + "\n"); - sb.append("STATUS:" + escapeText(status) + "\n"); - sb.append("SUMMARY:" + escapeText(summary) + "\n"); - sb.append("DESCRIPTION:" + escapeText(description) + "\n"); - sb.append("LOCATION:" + escapeText(location) + "\n"); - sb.append("TRANSP:OPAQUE\n"); - sb.append("END:VEVENT\n"); - return sb.toString(); - } - - public static class Builder { - VEvent event = new VEvent(); - - public Builder dtstamp(Calendar cstart) { - event.dtstamp = cstart; - return this; - } - - public Builder dtstart(Calendar cstart) { - event.dtstart = cstart; - return this; - } - - public Builder dtend(Calendar cend) { - event.dtend = cend; - return this; - } - - public Builder summary(String summary) { - event.summary = summary; - return this; - } - - public Builder description(String description) { - event.description = description; - return this; - } - - public Builder location(String location) { - event.location = location; - return this; - } - - public VEvent build() { - return event; - } - - public Builder status(String status) { - event.status = status; - return this; - } - } -} diff --git a/src/main/java/de/dhbw/horb/calendar/ics/VEventComponent.java b/src/main/java/de/dhbw/horb/calendar/ics/VEventComponent.java new file mode 100644 index 0000000..bfc2162 --- /dev/null +++ b/src/main/java/de/dhbw/horb/calendar/ics/VEventComponent.java @@ -0,0 +1,85 @@ +package de.dhbw.horb.calendar.ics; + +import java.util.Calendar; +import java.util.TimeZone; + +public class VEventComponent extends AbstractICSType { + public Calendar dtstamp = Calendar.getInstance(); + public Calendar dtstart = Calendar.getInstance(); + public Calendar dtend = Calendar.getInstance(); + public String status = ""; + public String summary = ""; + public String description = ""; + public String location = ""; + + /** + * {@inheritDoc}
+ *
+ * See RFC section 3.6.1 for VEVENT Specification + */ + @Override + public String serialize() { + final StringBuffer sb = new StringBuffer(); + dateTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + + sb.append("BEGIN:VEVENT\n"); + sb.append("DTSTART:" + dateTimeFormat.format(dtstart.getTime()) + "\n"); + sb.append("DTEND:" + dateTimeFormat.format(dtend.getTime()) + "\n"); + sb.append("DTSTAMP:" + dateTimeFormat.format(dtstamp.getTime()) + "\n"); + sb.append("UID:vevent_" + dateTimeFormat.format(dtstamp.getTime()) + + "@dhbw.de\n"); + sb.append("CREATED:" + + dateTimeFormat.format(Calendar.getInstance(TimeZone + .getTimeZone("UTC")).getTime()) + "\n"); + sb.append("STATUS:" + escapeText(status) + "\n"); + sb.append("SUMMARY:" + escapeText(summary) + "\n"); + sb.append("DESCRIPTION:" + escapeText(description) + "\n"); + sb.append("LOCATION:" + escapeText(location) + "\n"); + sb.append("TRANSP:OPAQUE\n"); + sb.append("END:VEVENT\n"); + return sb.toString(); + } + + public static class Builder { + VEventComponent event = new VEventComponent(); + + public Builder dtstamp(Calendar cstart) { + event.dtstamp = cstart; + return this; + } + + public Builder dtstart(Calendar cstart) { + event.dtstart = cstart; + return this; + } + + public Builder dtend(Calendar cend) { + event.dtend = cend; + return this; + } + + public Builder summary(String summary) { + event.summary = summary; + return this; + } + + public Builder description(String description) { + event.description = description; + return this; + } + + public Builder location(String location) { + event.location = location; + return this; + } + + public VEventComponent build() { + return event; + } + + public Builder status(String status) { + event.status = status; + return this; + } + } +} diff --git a/src/main/java/de/dhbw/horb/calendar/ics/package.html b/src/main/java/de/dhbw/horb/calendar/ics/package.html new file mode 100644 index 0000000..d35c7ea --- /dev/null +++ b/src/main/java/de/dhbw/horb/calendar/ics/package.html @@ -0,0 +1,5 @@ + + +Classes to produce iCalendar formatted data + + \ No newline at end of file diff --git a/src/main/java/de/dhbw/horb/calendar/servlet/AuthenticatedServlet.java b/src/main/java/de/dhbw/horb/calendar/servlet/AuthenticatedServlet.java new file mode 100644 index 0000000..df6d499 --- /dev/null +++ b/src/main/java/de/dhbw/horb/calendar/servlet/AuthenticatedServlet.java @@ -0,0 +1,79 @@ +package de.dhbw.horb.calendar.servlet; + +import java.io.IOException; +import java.util.StringTokenizer; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.ws.commons.util.Base64; + +public abstract class AuthenticatedServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + private String loginRealm = "Servlet Login"; + + public void setLoginRealm(String loginRealm) { + this.loginRealm = loginRealm; + } + + public String getLoginRealm() { + return loginRealm; + } + + String[] getUsernamePassword(HttpServletRequest request) throws IOException { + String authHeader = request.getHeader("Authorization"); + if (authHeader != null) { + StringTokenizer st = new StringTokenizer(authHeader); + if (st.hasMoreTokens()) { + String basic = st.nextToken(); + if (basic.equalsIgnoreCase("Basic")) { + String credentials = st.nextToken(); + + String userPass = new String(Base64.decode(credentials)); + + // The decoded string is in the form + // "userID:password". + int p = userPass.indexOf(":"); + if (p != -1) { + String username = userPass.substring(0, p).trim(); + String password = userPass.substring(p + 1).trim(); + String[] userPassword = new String[] { username, + password }; + if ("".equals(userPassword) || "".equals(password)) { + return null; + } else { + return new String[] { username, password }; + } + } + } + } + } + return null; + } + + void writeAuthenticationRequest(HttpServletResponse response) { + String realm = StringEscapeUtils.escapeJava(getLoginRealm()); + String s = "Basic realm=\"" + realm + "\""; + response.setHeader("WWW-Authenticate", s); + response.setStatus(401); + } + + @Override + protected void doGet(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + + String[] usernamePassword = getUsernamePassword(request); + if (usernamePassword == null) { + writeAuthenticationRequest(response); + return; + } + doGet(request, response, usernamePassword[0], usernamePassword[1]); + } + + protected abstract void doGet(HttpServletRequest request, + HttpServletResponse response, String username, String password) throws ServletException, IOException; +} diff --git a/src/main/java/de/dhbw/horb/calendar/servlet/ICSServlet.java b/src/main/java/de/dhbw/horb/calendar/servlet/ICSServlet.java new file mode 100644 index 0000000..a76f781 --- /dev/null +++ b/src/main/java/de/dhbw/horb/calendar/servlet/ICSServlet.java @@ -0,0 +1,42 @@ +package de.dhbw.horb.calendar.servlet; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import de.dhbw.horb.calendar.dualis.DualisConnection; +import de.dhbw.horb.calendar.dualis.DualisException; +import de.dhbw.horb.calendar.ics.ICalendarObject; +import de.dhbw.horb.calendar.ics.VEventComponent; + +/** + * Das ICS-Servlet ist ein HTTP-Servlet das von der Kalenderapplikation + * aufgerufen wird um die iCalendar Daten zu bekommen. + */ +public class ICSServlet extends AuthenticatedServlet { + private static final long serialVersionUID = 1L; + + public ICSServlet() { + setLoginRealm("Dualis Calendar Connector"); + } + + @Override + protected void doGet(HttpServletRequest request, + HttpServletResponse response, String username, String password) + throws IOException { + response.setHeader("Content-Type", "text/calendar; charset=utf-8"); + + ICalendarObject calendar = new ICalendarObject("DHBW Calendar"); + try { + DualisConnection connection = new DualisConnection(username, + password); + for (VEventComponent event : connection.getEvents()) { + calendar.add(event); + } + } catch (DualisException e) { + e.printStackTrace(); + } + response.getOutputStream().write(calendar.serialize().getBytes()); + } +} \ No newline at end of file diff --git a/src/main/java/de/dhbw/horb/calendar/servlet/package.html b/src/main/java/de/dhbw/horb/calendar/servlet/package.html new file mode 100644 index 0000000..a8d65b8 --- /dev/null +++ b/src/main/java/de/dhbw/horb/calendar/servlet/package.html @@ -0,0 +1,5 @@ + + +HTTP-Servlet to serve /ics + + \ No newline at end of file diff --git a/src/main/java/de/dhbw/horb/calendar/ui/InteractiveLoginWorker.java b/src/main/java/de/dhbw/horb/calendar/ui/InteractiveLoginWorker.java new file mode 100644 index 0000000..01258e9 --- /dev/null +++ b/src/main/java/de/dhbw/horb/calendar/ui/InteractiveLoginWorker.java @@ -0,0 +1,149 @@ +package de.dhbw.horb.calendar.ui; + +import java.io.IOException; +import java.net.URL; + +import org.w3c.css.sac.CSSException; +import org.w3c.css.sac.CSSParseException; +import org.w3c.css.sac.ErrorHandler; + +import com.gargoylesoftware.htmlunit.ElementNotFoundException; +import com.gargoylesoftware.htmlunit.IncorrectnessListener; +import com.gargoylesoftware.htmlunit.Page; +import com.gargoylesoftware.htmlunit.StatusHandler; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.HTMLParserListener; +import com.gargoylesoftware.htmlunit.html.HtmlForm; +import com.gargoylesoftware.htmlunit.html.HtmlInput; +import com.gargoylesoftware.htmlunit.html.HtmlPage; + +import de.dhbw.horb.calendar.dualis.DualisConnection; +import de.dhbw.horb.calendar.dualis.DualisException; +import de.dhbw.horb.calendar.dualis.DualisException.DualisAuthenticationException; + +class InteractiveLoginWorker implements Runnable { + private final WebClient client; + private HtmlPage page = null; + private HtmlForm loginForm = null; + private HtmlInput usernameField; + private HtmlInput passwordField; + private HtmlInput submitButton; + private final String username; + private final String password; + private final LoginWorkerListener listener; + + public InteractiveLoginWorker(final LoginWorkerListener listener, final String username, final String password) { + this.listener = listener; + this.username = username; + this.password = password; + + client = new WebClient(); + client.setIncorrectnessListener(new IncorrectnessListener() { + @Override + public void notify(String message, Object origin) { + System.out.println("======="); + System.out.println(origin.getClass()); + } + }); + // not used while setCssEnabled(false) + client.setCssErrorHandler(new ErrorHandler() { + @Override + public void warning(CSSParseException exception) + throws CSSException { + System.out.println("CSS Exception: " + exception.getMessage()); + } + + @Override + public void fatalError(CSSParseException exception) + throws CSSException { + System.out.println("CSS Exception: " + exception.getMessage()); + } + + @Override + public void error(CSSParseException exception) throws CSSException { + System.out.println("CSS Exception: " + exception.getMessage()); + } + }); + client.setStatusHandler(new StatusHandler() { + @Override + public void statusMessageChanged(Page page, String message) { + System.out + .println("DualisConnection.getEvents().new StatusHandler() {...}.statusMessageChanged()"); + } + }); + client.setHTMLParserListener(new HTMLParserListener() { + @Override + public void warning(String message, URL url, int line, int column, + String key) { + // ignore silently + } + + @Override + public void error(String message, URL url, int line, int column, + String key) { + // ignore silently + } + }); + client.setPrintContentOnFailingStatusCode(false); + client.setJavaScriptEnabled(false); + client.setCssEnabled(false); + + } + + private void step1() throws Exception { + page = client.getPage(DualisConnection.DUALIS_URL); + } + + private void step2() { + loginForm = page.getFormByName("cn_loginForm"); + usernameField = loginForm.getInputByName("usrname"); + passwordField = loginForm.getInputByName("pass"); + submitButton = loginForm.getInputByValue("Anmelden"); + } + + private void step3() throws IOException { + usernameField.type(username); + passwordField.type(password); + page = submitButton.click(); + } + + private void step4() throws DualisAuthenticationException { + try { + page.getAnchorByText("Stundenplan"); + } catch (ElementNotFoundException e) { + throw new DualisException.DualisAuthenticationException( + "Authentifizierung fehltgeschlagen", e); + } + } + + @Override + public void run() { + try { + listener.setProgress(0.2, "Lade Seite"); + step1(); + + listener.setProgress(0.4, "Analysiere Seite"); + step2(); + + listener.setProgress(0.6, "Sende Login"); + step3(); + + listener.setProgress(0.8, "Prüfe Login"); + step4(); + listener.setProgress(1.0, "Erfolgreich"); + + listener.success(username,password); + } catch (Exception e) { + e.printStackTrace(); + listener.error(e); + } + } + + public interface LoginWorkerListener { + public void setProgress(double d, String description); + + public void success(String username, String password); + + public void error(Exception e); + } +} \ No newline at end of file diff --git a/src/main/java/de/dhbw/horb/calendar/ui/LoginWorker.java b/src/main/java/de/dhbw/horb/calendar/ui/LoginWorker.java deleted file mode 100644 index 34941c0..0000000 --- a/src/main/java/de/dhbw/horb/calendar/ui/LoginWorker.java +++ /dev/null @@ -1,149 +0,0 @@ -package de.dhbw.horb.calendar.ui; - -import java.io.IOException; -import java.net.URL; - -import org.w3c.css.sac.CSSException; -import org.w3c.css.sac.CSSParseException; -import org.w3c.css.sac.ErrorHandler; - -import com.gargoylesoftware.htmlunit.ElementNotFoundException; -import com.gargoylesoftware.htmlunit.IncorrectnessListener; -import com.gargoylesoftware.htmlunit.Page; -import com.gargoylesoftware.htmlunit.StatusHandler; -import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.html.HTMLParserListener; -import com.gargoylesoftware.htmlunit.html.HtmlForm; -import com.gargoylesoftware.htmlunit.html.HtmlInput; -import com.gargoylesoftware.htmlunit.html.HtmlPage; - -import de.dhbw.horb.calendar.dualis.DualisConnection; -import de.dhbw.horb.calendar.dualis.DualisException; -import de.dhbw.horb.calendar.dualis.DualisException.DualisAuthenticationException; - -class LoginWorker implements Runnable { - private final WebClient client; - private HtmlPage page = null; - private HtmlForm loginForm = null; - private HtmlInput usernameField; - private HtmlInput passwordField; - private HtmlInput submitButton; - private final String username; - private final String password; - private final LoginWorkerListener listener; - - public LoginWorker(final LoginWorkerListener listener, final String username, final String password) { - this.listener = listener; - this.username = username; - this.password = password; - - client = new WebClient(); - client.setIncorrectnessListener(new IncorrectnessListener() { - @Override - public void notify(String message, Object origin) { - System.out.println("======="); - System.out.println(origin.getClass()); - } - }); - // not used while setCssEnabled(false) - client.setCssErrorHandler(new ErrorHandler() { - @Override - public void warning(CSSParseException exception) - throws CSSException { - System.out.println("CSS Exception: " + exception.getMessage()); - } - - @Override - public void fatalError(CSSParseException exception) - throws CSSException { - System.out.println("CSS Exception: " + exception.getMessage()); - } - - @Override - public void error(CSSParseException exception) throws CSSException { - System.out.println("CSS Exception: " + exception.getMessage()); - } - }); - client.setStatusHandler(new StatusHandler() { - @Override - public void statusMessageChanged(Page page, String message) { - System.out - .println("DualisConnection.getEvents().new StatusHandler() {...}.statusMessageChanged()"); - } - }); - client.setHTMLParserListener(new HTMLParserListener() { - @Override - public void warning(String message, URL url, int line, int column, - String key) { - // ignore silently - } - - @Override - public void error(String message, URL url, int line, int column, - String key) { - // ignore silently - } - }); - client.setPrintContentOnFailingStatusCode(false); - client.setJavaScriptEnabled(false); - client.setCssEnabled(false); - - } - - private void step1() throws Exception { - page = client.getPage(DualisConnection.DUALIS_URL); - } - - private void step2() { - loginForm = page.getFormByName("cn_loginForm"); - usernameField = loginForm.getInputByName("usrname"); - passwordField = loginForm.getInputByName("pass"); - submitButton = loginForm.getInputByValue("Anmelden"); - } - - private void step3() throws IOException { - usernameField.type(username); - passwordField.type(password); - page = submitButton.click(); - } - - private void step4() throws DualisAuthenticationException { - try { - page.getAnchorByText("Stundenplan"); - } catch (ElementNotFoundException e) { - throw new DualisException.DualisAuthenticationException( - "Authentifizierung fehltgeschlagen", e); - } - } - - @Override - public void run() { - try { - listener.setProgress(0.2, "Lade Seite"); - step1(); - - listener.setProgress(0.4, "Analysiere Seite"); - step2(); - - listener.setProgress(0.6, "Sende Login"); - step3(); - - listener.setProgress(0.8, "Prüfe Login"); - step4(); - listener.setProgress(1.0, "Erfolgreich"); - - listener.success(username,password); - } catch (Exception e) { - e.printStackTrace(); - listener.error(e); - } - } - - public interface LoginWorkerListener { - public void setProgress(double d, String description); - - public void success(String username, String password); - - public void error(Exception e); - } -} \ No newline at end of file diff --git a/src/main/java/de/dhbw/horb/calendar/ui/MyApplication.java b/src/main/java/de/dhbw/horb/calendar/ui/MyApplication.java index 9a0f222..4fc5749 100644 --- a/src/main/java/de/dhbw/horb/calendar/ui/MyApplication.java +++ b/src/main/java/de/dhbw/horb/calendar/ui/MyApplication.java @@ -13,7 +13,7 @@ import com.vaadin.ui.ProgressIndicator; import com.vaadin.ui.TabSheet; import com.vaadin.ui.Window; -import de.dhbw.horb.calendar.ui.LoginWorker.LoginWorkerListener; +import de.dhbw.horb.calendar.ui.InteractiveLoginWorker.LoginWorkerListener; public class MyApplication extends Application { private static final long serialVersionUID = 1L; @@ -142,7 +142,7 @@ public class MyApplication extends Application { @Override public void onLogin(LoginEvent event) { - new Thread(new LoginWorker(loginWorkerListener, event + new Thread(new InteractiveLoginWorker(loginWorkerListener, event .getLoginParameter("username"), event .getLoginParameter("password"))).start(); loginForm.setVisible(false); diff --git a/src/main/java/de/dhbw/horb/calendar/ui/StundenplanComponent.java b/src/main/java/de/dhbw/horb/calendar/ui/StundenplanComponent.java index 0357610..29ddecc 100644 --- a/src/main/java/de/dhbw/horb/calendar/ui/StundenplanComponent.java +++ b/src/main/java/de/dhbw/horb/calendar/ui/StundenplanComponent.java @@ -12,7 +12,7 @@ import com.vaadin.ui.Table; import com.vaadin.ui.VerticalLayout; import de.dhbw.horb.calendar.dualis.DualisConnection; -import de.dhbw.horb.calendar.ics.VEvent; +import de.dhbw.horb.calendar.ics.VEventComponent; import de.dhbw.horb.calendar.ui.MyApplication.MyMainWindow; public class StundenplanComponent extends FormLayout { @@ -34,7 +34,7 @@ public class StundenplanComponent extends FormLayout { myMainWindow.getUsername(), myMainWindow.getPassword()); try { - for (VEvent vevent : dualisConnection.getEvents()) { + for (VEventComponent vevent : dualisConnection.getEvents()) { synchronized (getApplication()) { table.addItem(new Object[] { vevent.dtstart.getTime(), vevent.dtend.getTime(), vevent.summary, diff --git a/src/main/java/de/dhbw/horb/calendar/ui/package.html b/src/main/java/de/dhbw/horb/calendar/ui/package.html new file mode 100644 index 0000000..3591a6b --- /dev/null +++ b/src/main/java/de/dhbw/horb/calendar/ui/package.html @@ -0,0 +1,5 @@ + + +vaadin based Webinterface. + + \ No newline at end of file diff --git a/src/main/java/de/dhbw/horb/calendar/util/AuthenticatedServlet.java b/src/main/java/de/dhbw/horb/calendar/util/AuthenticatedServlet.java deleted file mode 100644 index 47ca537..0000000 --- a/src/main/java/de/dhbw/horb/calendar/util/AuthenticatedServlet.java +++ /dev/null @@ -1,84 +0,0 @@ -package de.dhbw.horb.calendar.util; - -import java.io.IOException; -import java.util.StringTokenizer; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.lang.StringEscapeUtils; - -import sun.misc.BASE64Decoder; - -public abstract class AuthenticatedServlet extends HttpServlet { - private static final long serialVersionUID = 1L; - - - private String loginRealm = "Servlet Login"; - - public void setLoginRealm(String loginRealm) { - this.loginRealm = loginRealm; - } - - public String getLoginRealm() { - return loginRealm; - } - - String[] getUsernamePassword(HttpServletRequest request) throws IOException { - String authHeader = request.getHeader("Authorization"); - if (authHeader != null) { - StringTokenizer st = new StringTokenizer(authHeader); - if (st.hasMoreTokens()) { - String basic = st.nextToken(); - if (basic.equalsIgnoreCase("Basic")) { - String credentials = st.nextToken(); - - // XXX fixme, dont use sun.misc - BASE64Decoder decoder = new BASE64Decoder(); - String userPass = new String( - decoder.decodeBuffer(credentials)); - - // The decoded string is in the form - // "userID:password". - int p = userPass.indexOf(":"); - if (p != -1) { - String username = userPass.substring(0, p).trim(); - String password = userPass.substring(p + 1).trim(); - String[] userPassword = new String[] { username, - password }; - if ("".equals(userPassword) || "".equals(password)) { - return null; - } else { - return new String[] { username, password }; - } - } - } - } - } - return null; - } - - void writeAuthenticationRequest(HttpServletResponse response) { - String realm = StringEscapeUtils.escapeJava(getLoginRealm()); - String s = "Basic realm=\"" + realm + "\""; - response.setHeader("WWW-Authenticate", s); - response.setStatus(401); - } - - @Override - protected void doGet(HttpServletRequest request, - HttpServletResponse response) throws ServletException, IOException { - - String[] usernamePassword = getUsernamePassword(request); - if (usernamePassword == null) { - writeAuthenticationRequest(response); - return; - } - doGet(request, response, usernamePassword[0], usernamePassword[1]); - } - - protected abstract void doGet(HttpServletRequest request, - HttpServletResponse response, String username, String password) throws ServletException, IOException; -} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 1bed2f5..119e38f 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -6,7 +6,7 @@ ics-connector - de.dhbw.horb.calendar.ICSServlet + de.dhbw.horb.calendar.servlet.ICSServlet ics-connector -- cgit v1.2.1