1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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 * Mailet that stores messages to a JCR content repository.
35 */
36 public class JCRStoreMailet implements Mailet {
37
38 /**
39 * Mailet configuration.
40 */
41 private MailetConfig config;
42
43 /**
44 * JCR content repository.
45 */
46 private Repository repository;
47
48 /**
49 * Returns information about this mailet.
50 *
51 * @return mailet information
52 */
53 public String getMailetInfo() {
54 return "JCR Store Mailet";
55 }
56
57 /**
58 * Returns the mailet configuration.
59 *
60 * @return mailet configuration
61 */
62 public MailetConfig getMailetConfig() {
63 return config;
64 }
65
66 /**
67 * Initializes this mailet by connecting to the configured JCR repository.
68 *
69 * @param config mailet configuration
70 * @throws MessagingException if the JCR repository can not be accessed
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 * Closes this mailet by releasing the JCR connection.
87 */
88 public void destroy() {
89 this.repository = null;
90 this.config = null;
91 }
92
93 /**
94 * Stores the given mail message to the content repository.
95 *
96 * @param mail mail message
97 * @throws MessagingException if the message could not be saved
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 // } catch (IOException e) {
126 // throw new MessagingException("IO error", e);
127 } catch (RepositoryException e) {
128 throw new MessagingException("Repository access error", e);
129 }
130 }
131
132 }