KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > actions > area > EmailPostMessage


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.forum.actions.area;
27
28 import net.killingar.Utils;
29 import net.killingar.forum.internal.Message;
30 import net.killingar.forum.internal.managers.AreaManager;
31 import net.killingar.forum.internal.managers.ForumManager;
32 import webwork.action.ServletActionContext;
33
34 import javax.imageio.ImageIO JavaDoc;
35 import java.awt.geom.AffineTransform JavaDoc;
36 import java.awt.image.AffineTransformOp JavaDoc;
37 import java.awt.image.BufferedImage JavaDoc;
38 import java.io.File JavaDoc;
39 import java.util.Properties JavaDoc;
40
41 public class EmailPostMessage extends net.killingar.actions.email.List
42 {
43     // Types ---------------------------------------------------------
44

45     // Attributes ----------------------------------------------------
46
String JavaDoc storePath;
47     long areaID = -1;
48     ForumManager manager;
49     java.text.SimpleDateFormat JavaDoc dateFormat = new java.text.SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
50
51     // getters
52

53     // setters
54
public void setStorePath(String JavaDoc in) { storePath = in; }
55     public void setManager(ForumManager in) { manager = in; }
56     public void setAreaID(long in) { areaID = in; }
57
58     // Implementation
59
public String JavaDoc doExecute()
60     {
61         try
62         {
63             System.err.println("executing EmailPostMessage");
64
65             if (ServletActionContext.getServletContext() == null)
66             {
67                 Properties JavaDoc prop = Utils.getProperties();
68
69                 manager = new ForumManager();
70                 manager.login((String JavaDoc)prop.get("net.killingar.forum.username"), (String JavaDoc)prop.get("net.killingar.forum.password"));
71             }
72
73             if (super.doExecute() == ERROR)
74                 return ERROR;
75
76             AreaManager areamgr = (AreaManager)manager.getManager(AreaManager.class.getName());
77
78             File JavaDoc dir = new File JavaDoc(storePath);
79
80             for (int j = 0; j != getMessages().size(); j++)
81             {
82                 Message message = new Message();
83                 message.body = "";
84
85                 javax.mail.Message JavaDoc m = ((net.killingar.actions.email.Mail)getMessages().get(j)).getMessage();
86
87                 if (m.getContent() instanceof String JavaDoc && m.getContent() != null)
88                     message.body += "body:\n"+m.getContent()+"\n";
89                 if (m.getSubject() != null)
90                     message.body += "subject:\n"+m.getSubject()+"\n";
91
92                 if (m.getContent() instanceof javax.mail.internet.MimeMultipart JavaDoc)
93                 {
94                     javax.mail.internet.MimeMultipart JavaDoc foo = (javax.mail.internet.MimeMultipart JavaDoc)m.getContent();
95                     for (int i = 0; i != foo.getCount(); i++)
96                     {
97                         javax.mail.Part JavaDoc part = foo.getBodyPart(i);
98
99                         if (part.isMimeType("image/jpeg") || part.isMimeType("image/gif") || part.isMimeType("image/png"))// || part.isMimeType("application/smil"))
100
{
101                             String JavaDoc name = part.getFileName();
102                             String JavaDoc extension = "";
103                             try
104                             {
105                                 extension = name.substring(name.lastIndexOf('.'));
106                                 name = name.substring(0, name.lastIndexOf('.'));
107                             }
108                             catch (IndexOutOfBoundsException JavaDoc e){}
109
110                             // create file
111
java.io.File JavaDoc file = new java.io.File JavaDoc(dir, part.getFileName());
112                             if (file.exists())
113                             {
114                                 file = java.io.File.createTempFile(name+"-", extension, dir);
115                             }
116                             else
117                                 file.createNewFile();
118
119                             // rotate if needed
120
if (name.charAt(name.length()-1) == 'l')
121                             {
122                                 BufferedImage JavaDoc bimage = ImageIO.read(part.getInputStream());
123
124                                 BufferedImage JavaDoc rotatedImage = new BufferedImage JavaDoc(bimage.getHeight(), bimage.getWidth(), bimage.getType());
125
126                                 AffineTransform JavaDoc transform = AffineTransform.getRotateInstance(-Math.PI/2);
127                                 transform.translate(-bimage.getWidth(), 0);
128
129                                 AffineTransformOp JavaDoc op =
130                                     new AffineTransformOp JavaDoc(
131                                         transform,
132                                         AffineTransformOp.TYPE_BILINEAR);
133
134                                 op.filter(bimage, rotatedImage);
135                                 bimage.flush();
136
137                                 ImageIO.write(rotatedImage, "jpg", file);
138                             }
139                             else
140                             {
141                                 // write file normally
142
java.io.OutputStream JavaDoc out = new java.io.FileOutputStream JavaDoc(file);
143                                 Utils.writeStreamToStream(part.getInputStream(), out);
144                                 out.close();
145                             }
146
147                             message.body += "image:\n"+file.getName()+"\n";
148                         }
149                         else if (part.isMimeType("text/plain") && part.getFileName() != null)// && "mms.txt".equals(part.getFileName()))
150
{
151                             message.body += part.getFileName()+":\n"+SignGuestbook.escape(part.getContent().toString())+"\n";
152                         }
153                         else if (part.isMimeType("text/plain") && part.getDescription() != null)// && "mms.txt".equals(part.getFileName()))
154
{
155                             message.body += part.getDescription()+":\n"+SignGuestbook.escape(part.getContent().toString())+"\n";
156                         }
157                     }
158                 }
159                 else
160                 {
161                     message.body = m.getContent().toString();
162                 }
163
164                 message.subject = dateFormat.format(m.getSentDate());
165                 message.areaID = areaID;
166
167                 // post message to area
168
areamgr.addMessage(message);
169
170                 // delete message j when done with it
171
m.setFlag(javax.mail.Flags.Flag.DELETED, true);
172             }
173
174             try
175             {
176                 System.err.println("net.killingar.forum.actions.area.EmailPostMessage: deleted "+getFolder().expunge().length+" messages");
177             }
178             catch (javax.mail.MethodNotSupportedException JavaDoc e){} // for pop3 this will happen, we should ignore this
179
}
180         catch (Exception JavaDoc e)
181         {
182             e.printStackTrace();
183             return ERROR;
184         }
185
186         return SUCCESS;
187     }
188 }
189
Popular Tags