1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.james.jcr;
18
19 import org.apache.jackrabbit.rmi.client.ClientRepositoryFactory;
20 import org.apache.mailet.Mail;
21 import org.apache.mailet.Mailet;
22 import org.apache.mailet.MailetConfig;
23
24 import javax.jcr.Credentials;
25 import javax.jcr.Item;
26 import javax.jcr.Node;
27 import javax.jcr.Repository;
28 import javax.jcr.RepositoryException;
29 import javax.jcr.Session;
30 import javax.jcr.SimpleCredentials;
31 import javax.mail.MessagingException;
32
33
34
35
36 public class JCRStoreMailet implements Mailet {
37
38
39
40
41 private MailetConfig config;
42
43
44
45
46 private Repository repository;
47
48
49
50
51
52
53 public String getMailetInfo() {
54 return "JCR Store Mailet";
55 }
56
57
58
59
60
61
62 public MailetConfig getMailetConfig() {
63 return config;
64 }
65
66
67
68
69
70
71
72 public void init(MailetConfig config) throws MessagingException {
73 this.config = config;
74
75 String url = config.getInitParameter("url");
76 try {
77 ClientRepositoryFactory factory = new ClientRepositoryFactory();
78 this.repository = factory.getRepository(url);
79 } catch (Exception e) {
80 throw new MessagingException(
81 "Error accessing the content repository: " + url, e);
82 }
83 }
84
85
86
87
88 public void destroy() {
89 this.repository = null;
90 this.config = null;
91 }
92
93
94
95
96
97
98
99 public void service(Mail mail) throws MessagingException {
100 try {
101 String username = config.getInitParameter("username");
102 String password = config.getInitParameter("password");
103 String workspace = config.getInitParameter("workspace");
104 String path = config.getInitParameter("path");
105
106 Credentials credentials = null;
107 if (username != null) {
108 credentials =
109 new SimpleCredentials(username, password.toCharArray());
110 }
111
112 Session session = repository.login(credentials, workspace);
113 try {
114 Item item = session.getItem(path);
115 if (item instanceof Node) {
116 JCRStoreBean bean = new JCRStoreBean();
117 bean.setParentNode((Node) item);
118 bean.storeMessage(mail.getMessage());
119 } else {
120 throw new MessagingException("Invalid path: " + path);
121 }
122 } finally {
123 session.logout();
124 }
125
126
127 } catch (RepositoryException e) {
128 throw new MessagingException("Repository access error", e);
129 }
130 }
131
132 }