1 18 package org.apache.roller.webservices.adminapi; 19 20 import java.io.Reader ; 21 import java.net.MalformedURLException ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Collections ; 27 import java.util.Date ; 28 import javax.servlet.http.HttpServletRequest ; 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 50 class RollerWeblogHandler extends Handler { 51 private static Log log = 52 LogFactory.getFactory().getInstance(RollerWeblogHandler.class); 53 54 55 private static final String DEFAULT_THEME = "basic"; 56 57 public RollerWeblogHandler(HttpServletRequest 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 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 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 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 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 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 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 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 pages = null; 175 List websiteDatas = new ArrayList (); 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 dateCreated = entry.getDateCreated(); 191 if (dateCreated == null) { 192 dateCreated = new Date (); 193 } 194 wd.setDateCreated(dateCreated); 195 196 try { 197 String def = RollerRuntimeConfig.getProperty("users.editor.pages"); 198 String [] defs = Utilities.stringToStringArray(def,","); 199 wd.setEditorPage(defs[0]); 200 } catch (Exception 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 222 HashMap pages = null; 223 224 List websiteDatas = new ArrayList (); 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 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 ("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 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 ("ERROR: Null user data not allowed"); 317 } 318 319 WeblogEntrySet wes = new WeblogEntrySet(getUrlPrefix()); 320 List entries = new ArrayList (); 321 for (int i = 0; i < uds.length; i++) { 322 UserData ud = uds[i]; 323 List permissions = ud.getPermissions(); 324 for (Iterator 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 ("ERROR: Null website datas not allowed"); 339 } 340 341 WeblogEntrySet wes = new WeblogEntrySet(getUrlPrefix()); 342 List entries = new ArrayList (); 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 |