/*****************************************************************************
/* Dieses Servlet liest die Maildaten aus der Datenbank und wandelt sie in ein
/* XML-Dokument um. Dann wird mit Hilfe einer XSLT Datei die Mail als HTML-Seite
/* dargestellt.
/*
******************************************************************************/
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.*;
import javax.xml.parsers.*;
import org.apache.xalan.xsltc.trax.*;
import java.sql.*;
import java.net.*;
import java.text.*;
import javax.mail.internet.*;
import java.util.regex.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import net.dehaen.beans.*;
public class ReadMailServlet extends HttpServlet {
private ServletConfig config;
ServletContext context;
HttpSession session;
UtilBean uBean;
SimpleDateFormat myformatter = new SimpleDateFormat("dd.MM.yyyy 'um' HH:mm");
String from_name="";
String from_address="";
String attach_dir = "";
String inhalt = "";
String subject = "";
String an, bcc,cc,ordner,erstellt, erhalten,isHtml,msg_id;
Hashtable fields;
boolean isFullView;
boolean showHtml, hasHtml;
Document document = null;
PrintWriter out;
HttpServletRequest request;
long start;
public void init (ServletConfig config)throws ServletException {
this.config = config;
}
public void destroy() {} // do nothing
public ServletConfig getServletConfig() {
return config;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=ISO-8859-1");
start = System.currentTimeMillis();
out = response.getWriter();
this.request = request;
this.session = request.getSession(true);
this.context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/reload.htm");
if(session.getAttribute("uBean")==null || session.getAttribute("status")==null){
dispatcher.forward(request, response);
}
this.uBean = (UtilBean)session.getAttribute("uBean");
if(uBean==null || uBean.getUserId()==null){
dispatcher.forward(request, response);
}
readMail();
}
private void readMail(){
fields = new Hashtable();
boolean isHtmlView;
if(request.getParameter("view")!=null && request.getParameter("view").equals("full")){
isFullView = true;
fields.put("view","full");
}
else {
isFullView = false;
}
if(request.getParameter("showHtml")!=null && request.getParameter("showHtml").equals("true")){
showHtml = true;
fields.put("showHtml","true");
}
else showHtml = false;
String userId = uBean.getUserId();
ResultSet rs;
Connection conn = null;
URLDecoder decoder = new URLDecoder();
InternetAddress from;
if(request.getParameter("msg_id")!=null){
try{
conn = uBean.getConnection();
Statement stmnt = conn.createStatement();
msg_id = request.getParameter("msg_id");
fields.put("msgId",msg_id);
String sql="SELECT * from messages WHERE id = '" + msg_id + "' AND user_id = '" + userId + "'";
rs = stmnt.executeQuery(sql);
if(rs.next()){
String s = "UPDATE messages SET gelesen = 1 WHERE id = '" + msg_id + "' AND user_id = '" + userId + "'";
stmnt.executeUpdate(s);
if(rs.getString("an")!=null){
fields.put("an",rs.getString("an"));
}
if(rs.getString("subject")!=null){
fields.put("subject",rs.getString("subject"));
}
try{
InternetAddress ad = new InternetAddress(rs.getString("von"));
if(ad.getPersonal()!=null){
fields.put("from_name",ad.getPersonal());
}
if(ad.getAddress()!=null){
fields.put("from_address", ad.getAddress());
}
}
catch (AddressException e){
fields.put("from_address","unbekannt");
}
fields.put("ordner",rs.getString("ordner"));
fields.put("attachments",rs.getString("attachments"));
fields.put("attachment_sizes",rs.getString("attachment_size"));
fields.put("attach_dir",rs.getString("attach_dir"));
if(!rs.getString("erstellt").equals("unbekannt")){
erstellt = myformatter.format(rs.getTimestamp("erstellt"));
}
else erstellt = "unbekannt";
fields.put("erstellt",erstellt);
fields.put("erhalten",myformatter.format(rs.getTimestamp("erhalten")));
if(rs.getString("BCC")!=null && !rs.getString("BCC").equals("")){
fields.put("bcc",rs.getString("BCC"));
}
if(rs.getString("CC")!=null && !rs.getString("CC").equals("")){
fields.put("cc",rs.getString("CC"));
}
if(rs.getString("html")==null || rs.getString("html").equals("")){
hasHtml=false;
}
else {
hasHtml=true;
fields.put("hasHtml","yes");
}
String view;
if(isFullView)view="?view=full";
else view="";
if(!showHtml){
if(hasHtml && (rs.getString("text")==null || rs.getString("text").equals(""))){
inhalt = "Dies Mail ist nur im Html-Format lesbar. Klicken Sie hier um sie anzuzeigen.";
}
else {
String text = rs.getString("text");
inhalt = text;
inhalt = cleanText(text);
}//Ende if text==""
}//Ende if isHtmlView
else {
inhalt = rs.getString("html").replaceAll("(?si)","");
if(inhalt.indexOf("-1){
inhalt = inhalt.substring(inhalt.indexOf("-1){
inhalt = inhalt.substring(0,inhalt.indexOf("-1){
inhalt = inhalt.substring(inhalt.indexOf("-1){
inhalt = inhalt.substring(0,inhalt.indexOf("")+1);
inhalt = inhalt.substring(0,inhalt.lastIndexOf(">"));
}
fields.put("inhalt",inhalt);
}
else {
fields.put("error","Keine Mail mit dieser ID in der Datenbank!");
}
if(conn!=null && !conn.isClosed())conn.close();
}
catch (SQLException e){
fields.put("error",e.getMessage());
}
catch (NullPointerException e){
fields.put("error",e.getMessage());
}
finally {
try{
if(conn!=null && !conn.isClosed())conn.close();
}
catch (SQLException e){
fields.put("error",e.getMessage());
}
}
}
long temp = System.currentTimeMillis() - start;
session.setAttribute("ReadMailServlet-before createDoc"," " + temp);
temp = System.currentTimeMillis();
createDocument();
temp = System.currentTimeMillis() - temp;
session.setAttribute("ReadMailServlet-createDoc-Dauer", " " + temp);
temp = System.currentTimeMillis();
transformDocument("readMail.xsl");
temp = System.currentTimeMillis() - temp;
session.setAttribute("ReadMailServlet-transform-Dauer", " " + temp);
temp = System.currentTimeMillis() - start;
session.setAttribute("ReadMailServlet-Dauer", " " + temp);
//transformDocument(null);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doGet(request, response);
}
private String cleanText (String text){
String inhalt;
if(text!=null){
try{
inhalt = text.replaceAll("<","<");
inhalt = inhalt.replaceAll(">",">");
////RegExp-Sonderzeichen ersetzen//////////////////////
inhalt = inhalt.replaceAll("\\]","]");
inhalt = inhalt.replaceAll("\\[","[");
inhalt = inhalt.replaceAll("\\(","(");
inhalt = inhalt.replaceAll("\\)"," )");
inhalt = inhalt.replaceAll("\\*","*");
inhalt = inhalt.replaceAll("\\?","?");
inhalt = inhalt.replaceAll("\\\\","\");
inhalt = inhalt.replaceAll("\\{","{");
inhalt = inhalt.replaceAll("\\}","}");
inhalt = inhalt.replaceAll("\\|","|");
inhalt = inhalt.replaceAll("\\$","$");
inhalt = inhalt.replaceAll("\\^","^");
///////////////////////Zeilenumbrüche einfügen////////////////////////////
Matcher m;
Pattern p;
String temp,r1,r2;
StringBuffer sb = new StringBuffer();
try{
p = Pattern.compile(".{80}\\S*"); //wenn eine Zeile länger als 80 Zeichen ist,
m = p.matcher(inhalt);
r1 = "";
int ind = 0;
int c = 0; //pro Ersetzung muss der Index um eins versetzt werden
while(m.find()){
r1 = m.group();
ind = m.end()+c;
if(r1.length()>110 && r1.lastIndexOf(" ")>0){
int ws = r1.length() - r1.lastIndexOf(" "); //wenn das letzte Wort zu lang ist,
if(ws > 0)ind-= ws; //wird vorher ein Leeraum gesucht
}
String temp1 = inhalt.substring(0,ind); //der String bis zum Leerraum
String temp2 = inhalt.substring(ind); //der String ab dem Leerraum
while(temp2.charAt(0)==' '){
temp2=temp2.substring(1);
c--;
}
inhalt = temp1 + "\n" + temp2;
c++;
}
}
catch(PatternSyntaxException e){inhalt = text;}
catch(IndexOutOfBoundsException e){inhalt = text;}
///////////////////////Links als Link schreiben///////////////////////
p = Pattern.compile("(http||https||ftp)://\\S*");
m = p.matcher(inhalt);
while(m.find()){
r1 = m.group();
//r2 = r1.replaceAll("/","/");
temp = "" + r1 + "";
m.appendReplacement(sb, temp);
}
m.appendTail(sb);
if(sb.length()>0)inhalt = sb.toString();
sb.delete(0,sb.length());
///////////e-mail-Adressen/////////////////////////////////////
p = Pattern.compile("[0-9a-zA-Z\\.\\-_]*@[0-9A-Za-z\\.\\-_]*");
m = p.matcher(inhalt);
temp="";
r1 = "";
r2 = "";
while(m.find()){
r1 = m.group();
temp = "" + r1 + "";
m.appendReplacement(sb, temp);
}
m.appendTail(sb);
if(sb.length()>0)inhalt = sb.toString();
}
catch(PatternSyntaxException e){
inhalt = text;
}
catch(IndexOutOfBoundsException e){
inhalt = text;
}
return inhalt;
} //Ende if text!=null
return "";
}
private void createDocument(){
try{
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = builder.newDocument();
Element root = (Element) document.createElement("mail");
document.appendChild(root);
String[] mailAtt ={"msgId","hasHtml","view","showHtml"};
//if(fields.contains(mailAtt[0]))
String tempStr = mailAtt[0];
//if(fields.contains(tempStr))root.setAttribute(tempStr,(String)fields.get(tempStr));
for(int i=0;i