KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > adminapi > RollerWeblogHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18 package org.apache.roller.webservices.adminapi;
19
20 import java.io.Reader JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Date JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.jdom.Document;
32 import org.apache.roller.RollerException;
33 import org.apache.roller.config.RollerRuntimeConfig;
34 import org.apache.roller.model.UserManager;
35 import org.apache.roller.pojos.PermissionsData;
36 import org.apache.roller.pojos.UserData;
37 import org.apache.roller.pojos.WebsiteData;
38 import org.apache.roller.util.cache.CacheManager;
39 import org.apache.roller.util.Utilities;
40 import org.apache.roller.webservices.adminapi.sdk.Entry;
41 import org.apache.roller.webservices.adminapi.sdk.EntrySet;
42 import org.apache.roller.webservices.adminapi.sdk.MissingElementException;
43 import org.apache.roller.webservices.adminapi.sdk.UnexpectedRootElementException;
44 import org.apache.roller.webservices.adminapi.sdk.WeblogEntry;
45 import org.apache.roller.webservices.adminapi.sdk.WeblogEntrySet;
46
47 /**
48  * This class handles requests concerning Roller weblog resources.
49  */

50 class RollerWeblogHandler extends Handler {
51     private static Log log =
52             LogFactory.getFactory().getInstance(RollerWeblogHandler.class);
53     
54     /** Theme name used when creating weblogs */
55     private static final String JavaDoc DEFAULT_THEME = "basic";
56     
57     public RollerWeblogHandler(HttpServletRequest JavaDoc request) throws HandlerException {
58         super(request);
59     }
60     
61     protected EntrySet getEntrySet(Document d) throws MissingElementException, UnexpectedRootElementException {
62         return new WeblogEntrySet(d, getUrlPrefix());
63     }
64     
65     public EntrySet processGet() throws HandlerException {
66         if (getUri().isCollection()) {
67             return getCollection();
68         } else if (getUri().isEntry()) {
69             return getEntry();
70         } else {
71             throw new BadRequestException("ERROR: Unknown GET URI type");
72         }
73     }
74     
75     public EntrySet processPost(Reader JavaDoc r) throws HandlerException {
76         if (getUri().isCollection()) {
77             return postCollection(r);
78         } else {
79             throw new BadRequestException("ERROR: Unknown POST URI type");
80         }
81     }
82     
83     public EntrySet processPut(Reader JavaDoc r) throws HandlerException {
84         if (getUri().isCollection()) {
85             return putCollection(r);
86         } else if (getUri().isEntry()) {
87             return putEntry(r);
88         } else {
89             throw new BadRequestException("ERROR: Unknown PUT URI type");
90         }
91     }
92     
93     public EntrySet processDelete() throws HandlerException {
94         if (getUri().isEntry()) {
95             return deleteEntry();
96         } else {
97             throw new BadRequestException("ERROR: Unknown DELETE URI type");
98         }
99     }
100     
101     private EntrySet getCollection() throws HandlerException {
102         try {
103             List JavaDoc users = getRoller().getUserManager().getUsers(0, -1);
104             if (users == null) {
105                 users = Collections.EMPTY_LIST;
106             }
107             EntrySet c = toWeblogEntrySet((UserData[])users.toArray(new UserData[0]));
108             
109             return c;
110         } catch (RollerException re) {
111             throw new InternalException("ERROR: Could not get weblog collection", re);
112         }
113     }
114     
115     private EntrySet getEntry() throws HandlerException {
116         String JavaDoc handle = getUri().getEntryId();
117         try {
118             WebsiteData wd = getRoller().getUserManager().getWebsiteByHandle(handle);
119             if (wd == null) {
120                 throw new NotFoundException("ERROR: Unknown weblog handle: " + handle);
121             }
122             WebsiteData[] wds = new WebsiteData[] { wd };
123             EntrySet c = toWeblogEntrySet(wds);
124             
125             return c;
126         } catch (RollerException re) {
127             throw new InternalException("ERROR: Could not get weblog collection", re);
128         }
129     }
130     
131     private EntrySet postCollection(Reader JavaDoc r) throws HandlerException {
132         EntrySet c = getEntrySet(r);
133         if (c.isEmpty()) {
134             throw new BadRequestException("ERROR: No entries");
135         }
136         c = createWeblogs((WeblogEntrySet)c);
137         
138         return c;
139     }
140     
141     private EntrySet putCollection(Reader JavaDoc r) throws HandlerException {
142         EntrySet c = getEntrySet(r);
143         if (c.isEmpty()) {
144             throw new BadRequestException("ERROR: No entries");
145         }
146         c = updateWeblogs((WeblogEntrySet)c);
147         
148         return c;
149     }
150     
151     private EntrySet putEntry(Reader JavaDoc r) throws HandlerException {
152         EntrySet c = getEntrySet(r);
153         if (c.isEmpty()) {
154             throw new BadRequestException("ERROR: No entries");
155         }
156         if (c.getEntries().length > 1) {
157             throw new BadRequestException("ERROR: Cannot put >1 entries per request");
158         }
159         
160         WeblogEntry entry = (WeblogEntry)c.getEntries()[0];
161         if (entry.getHandle() != null && !entry.getHandle().equals(getUri().getEntryId())) {
162             throw new BadRequestException("ERROR: Content handle does not match URI handle");
163         }
164         entry.setHandle(getUri().getEntryId());
165         c = updateWeblogs((WeblogEntrySet)c);
166         
167         return c;
168     }
169     
170     private WeblogEntrySet createWeblogs(WeblogEntrySet c) throws HandlerException {
171         try {
172             UserManager mgr = getRoller().getUserManager();
173             HashMap JavaDoc pages = null; //getRollerContext().readThemeMacros(form.getTheme());
174

175             List JavaDoc websiteDatas = new ArrayList JavaDoc();
176             for (int i = 0; i < c.getEntries().length; i++) {
177                 WeblogEntry entry = (WeblogEntry)c.getEntries()[i];
178                 UserData user = mgr.getUserByUserName(entry.getCreatingUser());
179                 WebsiteData wd = new WebsiteData(
180                         entry.getHandle(),
181                         user,
182                         entry.getName(),
183                         entry.getDescription(),
184                         entry.getEmailAddress(),
185                         entry.getEmailAddress(),
186                         DEFAULT_THEME,
187                         entry.getLocale().toString(),
188                         entry.getTimezone().getID());
189                 
190                 Date JavaDoc dateCreated = entry.getDateCreated();
191                 if (dateCreated == null) {
192                     dateCreated = new Date JavaDoc();
193                 }
194                 wd.setDateCreated(dateCreated);
195                 
196                 try {
197                     String JavaDoc def = RollerRuntimeConfig.getProperty("users.editor.pages");
198                     String JavaDoc[] defs = Utilities.stringToStringArray(def,",");
199                     wd.setEditorPage(defs[0]);
200                 } catch (Exception JavaDoc ex) {
201                     log.error("ERROR setting default editor page for weblog", ex);
202                 }
203                 
204                 mgr.addWebsite(wd);
205                 getRoller().flush();
206                 CacheManager.invalidate(wd);
207                 websiteDatas.add(wd);
208             }
209             
210             return toWeblogEntrySet((WebsiteData[])websiteDatas.toArray(new WebsiteData[0]));
211         } catch (RollerException re) {
212             throw new InternalException("ERROR: Could not create weblogs: " + c, re);
213         }
214     }
215     
216     private WeblogEntrySet updateWeblogs(WeblogEntrySet c) throws HandlerException {
217         try {
218             UserManager mgr = getRoller().getUserManager();
219             
220             //TODO: group blogging check?
221

222             HashMap JavaDoc pages = null;
223             
224             List JavaDoc websiteDatas = new ArrayList JavaDoc();
225             for (int i = 0; i < c.getEntries().length; i++) {
226                 WeblogEntry entry = (WeblogEntry)c.getEntries()[i];
227                 WebsiteData wd = mgr.getWebsiteByHandle(entry.getHandle());
228                 if (wd == null) {
229                     throw new NotFoundException("ERROR: Unknown weblog: " + entry.getHandle());
230                 }
231                 updateWebsiteData(wd, entry);
232                 websiteDatas.add(wd);
233             }
234             return toWeblogEntrySet((WebsiteData[])websiteDatas.toArray(new WebsiteData[0]));
235         } catch (RollerException re) {
236             throw new InternalException("ERROR: Could not update weblogs: " + c, re);
237         }
238     }
239     
240     private void updateWebsiteData(WebsiteData wd, WeblogEntry entry) throws HandlerException {
241         if (entry.getName() != null) {
242             wd.setName(entry.getName());
243         }
244         if (entry.getDescription() != null) {
245             wd.setDescription(entry.getDescription());
246         }
247         if (entry.getLocale() != null) {
248             wd.setLocale(entry.getLocale().toString());
249         }
250         if (entry.getTimezone() != null) {
251             wd.setTimeZone(entry.getTimezone().getID());
252         }
253         if (entry.getEmailAddress() != null) {
254             wd.setEmailAddress(entry.getEmailAddress());
255         }
256         
257         try {
258             UserManager mgr = getRoller().getUserManager();
259             mgr.saveWebsite(wd);
260             getRoller().flush();
261             CacheManager.invalidate(wd);
262         } catch (RollerException re) {
263             throw new InternalException("ERROR: Could not update website data", re);
264         }
265     }
266     
267     private EntrySet deleteEntry() throws HandlerException {
268         String JavaDoc handle = getUri().getEntryId();
269         
270         try {
271             UserManager mgr = getRoller().getUserManager();
272             
273             WebsiteData wd = mgr.getWebsiteByHandle(handle);
274             if (wd == null) {
275                 throw new NotFoundException("ERROR: Unknown weblog handle: " + handle);
276             }
277             
278             WebsiteData[] wds = new WebsiteData[] { wd };
279             EntrySet es = toWeblogEntrySet(wds);
280             
281             mgr.removeWebsite(wd);
282             getRoller().flush();
283             CacheManager.invalidate(wd);
284             
285             return es;
286         } catch (RollerException re) {
287             throw new InternalException("ERROR: Could not delete entry: " + handle, re);
288         }
289     }
290     
291     private WeblogEntry toWeblogEntry(WebsiteData wd) throws HandlerException {
292         if (wd == null) {
293             throw new NullPointerException JavaDoc("ERROR: Null website data not allowed");
294         }
295         WeblogEntry we = new WeblogEntry(wd.getHandle(), getUrlPrefix());
296         we.setName(wd.getName());
297         we.setDescription(wd.getDescription());
298         we.setLocale(wd.getLocale());
299         we.setTimezone(wd.getTimeZone());
300         we.setCreatingUser(wd.getCreator().getUserName());
301         we.setEmailAddress(wd.getEmailAddress());
302         we.setDateCreated(wd.getDateCreated());
303         try {
304             AppUrl appUrl = new AppUrl(RollerRuntimeConfig.getAbsoluteContextURL(), wd.getHandle());
305             we.setAppEntriesUrl(appUrl.getEntryUrl().toString());
306             we.setAppResourcesUrl(appUrl.getResourceUrl().toString());
307         } catch (MalformedURLException JavaDoc mfue) {
308             throw new InternalException("ERROR: Could not get APP URLs", mfue);
309         }
310         
311         return we;
312     }
313     
314     private WeblogEntrySet toWeblogEntrySet(UserData[] uds) throws HandlerException {
315         if (uds == null) {
316             throw new NullPointerException JavaDoc("ERROR: Null user data not allowed");
317         }
318         
319         WeblogEntrySet wes = new WeblogEntrySet(getUrlPrefix());
320         List JavaDoc entries = new ArrayList JavaDoc();
321         for (int i = 0; i < uds.length; i++) {
322             UserData ud = uds[i];
323             List JavaDoc permissions = ud.getPermissions();
324             for (Iterator JavaDoc j = permissions.iterator(); j.hasNext(); ) {
325                 PermissionsData pd = (PermissionsData)j.next();
326                 WebsiteData wd = pd.getWebsite();
327                 WeblogEntry we = toWeblogEntry(wd);
328                 entries.add(we);
329             }
330         }
331         wes.setEntries((Entry[])entries.toArray(new Entry[0]));
332         
333         return wes;
334     }
335     
336     private WeblogEntrySet toWeblogEntrySet(WebsiteData[] wds) throws HandlerException {
337         if (wds == null) {
338             throw new NullPointerException JavaDoc("ERROR: Null website datas not allowed");
339         }
340         
341         WeblogEntrySet wes = new WeblogEntrySet(getUrlPrefix());
342         List JavaDoc entries = new ArrayList JavaDoc();
343         for (int i = 0; i < wds.length; i++) {
344             WeblogEntry we = toWeblogEntry(wds[i]);
345             entries.add(we);
346         }
347         wes.setEntries((Entry[])entries.toArray(new Entry[0]));
348         
349         return wes;
350     }
351 }
352
353
Popular Tags