001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.webservices.adminapi;
019:
020: import java.io.Reader;
021: import java.net.MalformedURLException;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Collections;
027: import java.util.Date;
028: import javax.servlet.http.HttpServletRequest;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.jdom.Document;
032: import org.apache.roller.RollerException;
033: import org.apache.roller.config.RollerRuntimeConfig;
034: import org.apache.roller.business.UserManager;
035: import org.apache.roller.pojos.PermissionsData;
036: import org.apache.roller.pojos.UserData;
037: import org.apache.roller.pojos.WebsiteData;
038: import org.apache.roller.util.cache.CacheManager;
039: import org.apache.roller.util.Utilities;
040: import org.apache.roller.webservices.adminapi.sdk.Entry;
041: import org.apache.roller.webservices.adminapi.sdk.EntrySet;
042: import org.apache.roller.webservices.adminapi.sdk.UnexpectedRootElementException;
043: import org.apache.roller.webservices.adminapi.sdk.WeblogEntry;
044: import org.apache.roller.webservices.adminapi.sdk.WeblogEntrySet;
045:
046: /**
047: * This class handles requests concerning Roller weblog resources.
048: */
049: class RollerWeblogHandler extends Handler {
050: private static Log log = LogFactory.getFactory().getInstance(
051: RollerWeblogHandler.class);
052:
053: /** Theme name used when creating weblogs */
054: private static final String DEFAULT_THEME = "basic";
055:
056: public RollerWeblogHandler(HttpServletRequest request)
057: throws HandlerException {
058: super (request);
059: }
060:
061: protected EntrySet getEntrySet(Document d)
062: throws UnexpectedRootElementException {
063: return new WeblogEntrySet(d, getUrlPrefix());
064: }
065:
066: public EntrySet processGet() throws HandlerException {
067: if (getUri().isCollection()) {
068: return getCollection();
069: } else if (getUri().isEntry()) {
070: return getEntry();
071: } else {
072: throw new BadRequestException("ERROR: Unknown GET URI type");
073: }
074: }
075:
076: public EntrySet processPost(Reader r) throws HandlerException {
077: if (getUri().isCollection()) {
078: return postCollection(r);
079: } else {
080: throw new BadRequestException(
081: "ERROR: Unknown POST URI type");
082: }
083: }
084:
085: public EntrySet processPut(Reader r) throws HandlerException {
086: if (getUri().isCollection()) {
087: return putCollection(r);
088: } else if (getUri().isEntry()) {
089: return putEntry(r);
090: } else {
091: throw new BadRequestException("ERROR: Unknown PUT URI type");
092: }
093: }
094:
095: public EntrySet processDelete() throws HandlerException {
096: if (getUri().isEntry()) {
097: return deleteEntry();
098: } else {
099: throw new BadRequestException(
100: "ERROR: Unknown DELETE URI type");
101: }
102: }
103:
104: private EntrySet getCollection() throws HandlerException {
105: try {
106: List users = getRoller().getUserManager().getUsers(null,
107: null, null, 0, -1);
108: if (users == null) {
109: users = Collections.EMPTY_LIST;
110: }
111: EntrySet c = toWeblogEntrySet((UserData[]) users
112: .toArray(new UserData[0]));
113:
114: return c;
115: } catch (RollerException re) {
116: throw new InternalException(
117: "ERROR: Could not get weblog collection", re);
118: }
119: }
120:
121: private EntrySet getEntry() throws HandlerException {
122: String handle = getUri().getEntryId();
123: WebsiteData wd = getWebsiteData(handle);
124: WebsiteData[] wds = new WebsiteData[] { wd };
125: EntrySet c = toWeblogEntrySet(wds);
126:
127: return c;
128: }
129:
130: private EntrySet postCollection(Reader r) throws HandlerException {
131: EntrySet c = getEntrySet(r);
132: if (c.isEmpty()) {
133: throw new BadRequestException("ERROR: No entries");
134: }
135: c = createWeblogs((WeblogEntrySet) c);
136:
137: return c;
138: }
139:
140: private EntrySet putCollection(Reader r) throws HandlerException {
141: EntrySet c = getEntrySet(r);
142: if (c.isEmpty()) {
143: throw new BadRequestException("ERROR: No entries");
144: }
145: c = updateWeblogs((WeblogEntrySet) c);
146:
147: return c;
148: }
149:
150: private EntrySet putEntry(Reader r) throws HandlerException {
151: EntrySet c = getEntrySet(r);
152: if (c.isEmpty()) {
153: throw new BadRequestException("ERROR: No entries");
154: }
155: if (c.getEntries().length > 1) {
156: throw new BadRequestException(
157: "ERROR: Cannot put >1 entries per request");
158: }
159:
160: WeblogEntry entry = (WeblogEntry) c.getEntries()[0];
161: if (entry.getHandle() != null
162: && !entry.getHandle().equals(getUri().getEntryId())) {
163: throw new BadRequestException(
164: "ERROR: Content handle does not match URI handle");
165: }
166: entry.setHandle(getUri().getEntryId());
167: c = updateWeblogs((WeblogEntrySet) c);
168:
169: return c;
170: }
171:
172: private WeblogEntrySet createWeblogs(WeblogEntrySet c)
173: throws HandlerException {
174: try {
175: UserManager mgr = getRoller().getUserManager();
176: HashMap pages = null; //getRollerContext().readThemeMacros(form.getTheme());
177:
178: List websiteDatas = new ArrayList();
179: for (int i = 0; i < c.getEntries().length; i++) {
180: WeblogEntry entry = (WeblogEntry) c.getEntries()[i];
181: UserData user = mgr.getUserByUserName(entry
182: .getCreatingUser());
183: WebsiteData wd = new WebsiteData(entry.getHandle(),
184: user, entry.getName(), entry.getDescription(),
185: entry.getEmailAddress(), entry
186: .getEmailAddress(), DEFAULT_THEME,
187: entry.getLocale().toString(), entry
188: .getTimezone().getID());
189:
190: Date dateCreated = entry.getDateCreated();
191: if (dateCreated == null) {
192: dateCreated = new Date();
193: }
194: wd.setDateCreated(dateCreated);
195:
196: Boolean enabled = entry.getEnabled();
197: if (enabled != null) {
198: wd.setEnabled(enabled);
199: }
200:
201: try {
202: String def = RollerRuntimeConfig
203: .getProperty("users.editor.pages");
204: String[] defs = Utilities.stringToStringArray(def,
205: ",");
206: wd.setEditorPage(defs[0]);
207: } catch (Exception ex) {
208: log
209: .error(
210: "ERROR setting default editor page for weblog",
211: ex);
212: }
213:
214: mgr.addWebsite(wd);
215: getRoller().flush();
216: CacheManager.invalidate(wd);
217: websiteDatas.add(wd);
218: }
219:
220: return toWeblogEntrySet((WebsiteData[]) websiteDatas
221: .toArray(new WebsiteData[0]));
222: } catch (RollerException re) {
223: throw new InternalException(
224: "ERROR: Could not create weblogs: " + c, re);
225: }
226: }
227:
228: private WeblogEntrySet updateWeblogs(WeblogEntrySet c)
229: throws HandlerException {
230: try {
231: UserManager mgr = getRoller().getUserManager();
232:
233: //TODO: group blogging check?
234:
235: HashMap pages = null;
236:
237: List websiteDatas = new ArrayList();
238: for (int i = 0; i < c.getEntries().length; i++) {
239: WeblogEntry entry = (WeblogEntry) c.getEntries()[i];
240: WebsiteData wd = getWebsiteData(entry.getHandle());
241: updateWebsiteData(wd, entry);
242: websiteDatas.add(wd);
243: }
244: return toWeblogEntrySet((WebsiteData[]) websiteDatas
245: .toArray(new WebsiteData[0]));
246: } catch (RollerException re) {
247: throw new InternalException(
248: "ERROR: Could not update weblogs: " + c, re);
249: }
250: }
251:
252: private void updateWebsiteData(WebsiteData wd, WeblogEntry entry)
253: throws HandlerException {
254: if (entry.getName() != null) {
255: wd.setName(entry.getName());
256: }
257: if (entry.getDescription() != null) {
258: wd.setDescription(entry.getDescription());
259: }
260: if (entry.getLocale() != null) {
261: wd.setLocale(entry.getLocale().toString());
262: }
263: if (entry.getTimezone() != null) {
264: wd.setTimeZone(entry.getTimezone().getID());
265: }
266: if (entry.getEmailAddress() != null) {
267: wd.setEmailAddress(entry.getEmailAddress());
268: }
269: if (entry.getEnabled() != null) {
270: wd.setEnabled(entry.getEnabled());
271: }
272:
273: try {
274: UserManager mgr = getRoller().getUserManager();
275: mgr.saveWebsite(wd);
276: getRoller().flush();
277: CacheManager.invalidate(wd);
278: } catch (RollerException re) {
279: throw new InternalException(
280: "ERROR: Could not update website data", re);
281: }
282: }
283:
284: private EntrySet deleteEntry() throws HandlerException {
285: String handle = getUri().getEntryId();
286:
287: try {
288: UserManager mgr = getRoller().getUserManager();
289:
290: WebsiteData wd = getWebsiteData(handle);
291: WebsiteData[] wds = new WebsiteData[] { wd };
292: EntrySet es = toWeblogEntrySet(wds);
293:
294: mgr.removeWebsite(wd);
295: getRoller().flush();
296: CacheManager.invalidate(wd);
297:
298: return es;
299: } catch (RollerException re) {
300: throw new InternalException(
301: "ERROR: Could not delete entry: " + handle, re);
302: }
303: }
304:
305: private WeblogEntry toWeblogEntry(WebsiteData wd)
306: throws HandlerException {
307: if (wd == null) {
308: throw new NullPointerException(
309: "ERROR: Null website data not allowed");
310: }
311: WeblogEntry we = new WeblogEntry(wd.getHandle(), getUrlPrefix());
312: we.setName(wd.getName());
313: we.setDescription(wd.getDescription());
314: we.setLocale(wd.getLocale());
315: we.setTimezone(wd.getTimeZone());
316: we.setCreatingUser(wd.getCreator().getUserName());
317: we.setEmailAddress(wd.getEmailAddress());
318: we.setDateCreated(wd.getDateCreated());
319: we.setEnabled(wd.getEnabled());
320:
321: try {
322: AppUrl appUrl = new AppUrl(RollerRuntimeConfig
323: .getAbsoluteContextURL(), wd.getHandle());
324: we.setAppEntriesUrl(appUrl.getEntryUrl().toString());
325: we.setAppResourcesUrl(appUrl.getResourceUrl().toString());
326: } catch (MalformedURLException mfue) {
327: throw new InternalException(
328: "ERROR: Could not get APP URLs", mfue);
329: }
330:
331: return we;
332: }
333:
334: private WeblogEntrySet toWeblogEntrySet(UserData[] uds)
335: throws HandlerException {
336: if (uds == null) {
337: throw new NullPointerException(
338: "ERROR: Null user data not allowed");
339: }
340:
341: WeblogEntrySet wes = new WeblogEntrySet(getUrlPrefix());
342: List entries = new ArrayList();
343: for (int i = 0; i < uds.length; i++) {
344: UserData ud = uds[i];
345: List permissions = ud.getPermissions();
346: for (Iterator j = permissions.iterator(); j.hasNext();) {
347: PermissionsData pd = (PermissionsData) j.next();
348: WebsiteData wd = pd.getWebsite();
349: WeblogEntry we = toWeblogEntry(wd);
350: entries.add(we);
351: }
352: }
353: wes.setEntries((Entry[]) entries.toArray(new Entry[0]));
354:
355: return wes;
356: }
357:
358: private WeblogEntrySet toWeblogEntrySet(WebsiteData[] wds)
359: throws HandlerException {
360: if (wds == null) {
361: throw new NullPointerException(
362: "ERROR: Null website datas not allowed");
363: }
364:
365: WeblogEntrySet wes = new WeblogEntrySet(getUrlPrefix());
366: List entries = new ArrayList();
367: for (int i = 0; i < wds.length; i++) {
368: WeblogEntry we = toWeblogEntry(wds[i]);
369: entries.add(we);
370: }
371: wes.setEntries((Entry[]) entries.toArray(new Entry[0]));
372:
373: return wes;
374: }
375: }
|