snoopdave commented on code in PR #209: URL: https://github.com/apache/roller/pull/209#discussion_r4238850520
########## app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/CategoryCollection.java: ########## @@ -0,0 +1,221 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. The ASF licenses this file to You + * under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. For additional information regarding + * copyright in this work, please see the NOTICE file in the top level + * directory of this distribution. + */ + +package org.apache.roller.weblogger.webservices.atomprotocol; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.text.StringEscapeUtils; +import org.apache.roller.weblogger.WebloggerException; +import org.apache.roller.weblogger.business.WeblogEntryManager; +import org.apache.roller.weblogger.business.Weblogger; +import org.apache.roller.weblogger.business.WebloggerFactory; +import org.apache.roller.weblogger.pojos.User; +import org.apache.roller.weblogger.pojos.Weblog; +import org.apache.roller.weblogger.pojos.WeblogCategory; +import org.apache.roller.weblogger.pojos.WeblogPermission; +import org.apache.roller.weblogger.util.cache.CacheManager; + +/** + * The categories of a weblog as an AtomPub collection, for users who may + * post to the weblog. Each category is an entry: atom:title is the name, + * atom:summary the description, roller:image the image URL; roller:position + * and roller:inUse are read-only. + * + * <p>A category that entries still use is removed only when the request + * names another category of the same weblog in the {@code moveTo} parameter; + * the entries move there first. Otherwise DELETE answers 409. + */ +public class CategoryCollection { + + private final Weblogger roller; + private final User user; + private final String atomURL; + + public CategoryCollection(User user, String atomURL) { + this.user = user; + this.atomURL = atomURL; + this.roller = WebloggerFactory.getWeblogger(); + } + + public AtomFeed getCollection(AtomRequest areq) throws AtomException { + Weblog weblog = requireWeblog(CollectionSupport.path(areq)); + try { + AtomFeed feed = new AtomFeed(); + String uri = atomURL + "/" + weblog.getHandle() + "/categories"; + feed.setId(uri); + feed.setTitle(weblog.getName() + " categories"); + feed.setUpdated(new Date()); + feed.getLinks().add(CollectionSupport.link("self", uri)); + for (WeblogCategory category : manager().getWeblogCategories(weblog)) { + feed.getEntries().add(createAtomEntry(weblog, category)); + } + return feed; + } catch (WebloggerException e) { + throw new AtomException("Getting category collection", e); + } + } + + /** The weblog's categories as an APP category document (RFC 5023 section 7). */ + public AtomCategories getCategoriesDocument(AtomRequest areq) throws AtomException { + Weblog weblog = requireWeblog(CollectionSupport.path(areq)); + try { + return RollerAtomService.weblogCategories(weblog); + } catch (WebloggerException e) { + throw new AtomException("Getting category document", e); + } + } + + public AtomEntry getEntry(AtomRequest areq) throws AtomException { + String[] pathInfo = CollectionSupport.path(areq); + Weblog weblog = requireWeblog(pathInfo); + try { + return createAtomEntry(weblog, requireCategory(weblog, pathInfo)); + } catch (WebloggerException e) { + throw new AtomException("Getting category", e); + } + } + + public AtomEntry postEntry(AtomRequest areq, AtomEntry entry) throws AtomException { + Weblog weblog = requireWeblog(CollectionSupport.path(areq)); + String name = validName(entry.getTitle()); + if (weblog.hasCategory(name)) { + throw CollectionSupport.conflict("Category name already in use: " + name); + } + try { + WeblogCategory category = new WeblogCategory(); + category.setWeblog(weblog); + category.setName(name); + copyProperties(entry, category); + weblog.addCategory(category); + category.calculatePosition(); + manager().saveWeblogCategory(category); + roller.flush(); + CacheManager.invalidate(weblog); + return createAtomEntry(weblog, category); + } catch (WebloggerException e) { + throw new AtomException("Creating category", e); + } + } + + public void putEntry(AtomRequest areq, AtomEntry entry) throws AtomException { + String[] pathInfo = CollectionSupport.path(areq); + Weblog weblog = requireWeblog(pathInfo); + try { + WeblogCategory category = requireCategory(weblog, pathInfo); + if (entry.getTitle() != null) { + String name = validName(entry.getTitle()); + WeblogCategory existing = weblog.getWeblogCategory(name); + if (existing != null && !existing.getId().equals(category.getId())) { + throw CollectionSupport.conflict("Category name already in use: " + name); + } + category.setName(name); + } + copyProperties(entry, category); + manager().saveWeblogCategory(category); + roller.flush(); + CacheManager.invalidate(category); + } catch (WebloggerException e) { + throw new AtomException("Updating category", e); + } + } + + public void deleteEntry(AtomRequest areq) throws AtomException { + String[] pathInfo = CollectionSupport.path(areq); + Weblog weblog = requireWeblog(pathInfo); + try { + WeblogEntryManager mgr = manager(); + WeblogCategory category = requireCategory(weblog, pathInfo); + String moveTo = areq.getParameter("moveTo"); + if (StringUtils.isNotEmpty(moveTo)) { + WeblogCategory target = mgr.getWeblogCategory(weblog, moveTo); + if (target == null || target.getId().equals(category.getId())) { + throw CollectionSupport.badRequest("moveTo must name another category of this weblog"); + } + mgr.moveWeblogCategoryContents(category, target); Review Comment: 🐞Claude Issue: **Blocking:** Deleting the weblog's default (Blogger API) category with `moveTo` breaks the categories collection. `removeWeblogCategory()` sets `weblog.bloggerCategory` to null, and `JPAWeblogEntryManagerImpl.isWeblogCategoryInUse()` then calls `cat.getWeblog().getBloggerCategory().equals(cat)`, which throws a NullPointerException. Every later GET of `/categories` or `/category/{id}` (`createAtomEntry` calls `isWeblogCategoryInUse`) then returns 500. Fix: when `category` is the weblog's blogger category, set `weblog.setBloggerCategory(target)` and save the weblog before removing it. A test that deletes the default category with `moveTo` and then lists the collection would catch this. ########## app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomServlet.java: ########## @@ -222,6 +222,11 @@ private void doGet(RollerAtomHandler handler, AtomRequest areq, HttpServletRespo response.setContentType(AtomConstants.SERVICE_MEDIA_TYPE); new AtomWriter().writeServiceDoc(response.getOutputStream(), service); + } else if (handler.isCategoriesDocURI(areq)) { Review Comment: 🐞Claude Issue: **Important:** The new category-document branch has no servlet test. `RollerAtomServletTest` does not cover GET of `/{handle}/categories.atomcat`, so nothing checks that it is routed before the collection branch and served as `application/atomcat+xml`. Add a case with the mocked handler returning an `AtomCategories`, and verify the content type and the `app:categories` root element. ########## app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/CategoryCollection.java: ########## @@ -0,0 +1,221 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. The ASF licenses this file to You + * under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. For additional information regarding + * copyright in this work, please see the NOTICE file in the top level + * directory of this distribution. + */ + +package org.apache.roller.weblogger.webservices.atomprotocol; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.text.StringEscapeUtils; +import org.apache.roller.weblogger.WebloggerException; +import org.apache.roller.weblogger.business.WeblogEntryManager; +import org.apache.roller.weblogger.business.Weblogger; +import org.apache.roller.weblogger.business.WebloggerFactory; +import org.apache.roller.weblogger.pojos.User; +import org.apache.roller.weblogger.pojos.Weblog; +import org.apache.roller.weblogger.pojos.WeblogCategory; +import org.apache.roller.weblogger.pojos.WeblogPermission; +import org.apache.roller.weblogger.util.cache.CacheManager; + +/** + * The categories of a weblog as an AtomPub collection, for users who may + * post to the weblog. Each category is an entry: atom:title is the name, + * atom:summary the description, roller:image the image URL; roller:position + * and roller:inUse are read-only. + * + * <p>A category that entries still use is removed only when the request + * names another category of the same weblog in the {@code moveTo} parameter; + * the entries move there first. Otherwise DELETE answers 409. + */ +public class CategoryCollection { + + private final Weblogger roller; + private final User user; + private final String atomURL; + + public CategoryCollection(User user, String atomURL) { + this.user = user; + this.atomURL = atomURL; + this.roller = WebloggerFactory.getWeblogger(); + } + + public AtomFeed getCollection(AtomRequest areq) throws AtomException { + Weblog weblog = requireWeblog(CollectionSupport.path(areq)); + try { + AtomFeed feed = new AtomFeed(); + String uri = atomURL + "/" + weblog.getHandle() + "/categories"; + feed.setId(uri); + feed.setTitle(weblog.getName() + " categories"); + feed.setUpdated(new Date()); + feed.getLinks().add(CollectionSupport.link("self", uri)); + for (WeblogCategory category : manager().getWeblogCategories(weblog)) { + feed.getEntries().add(createAtomEntry(weblog, category)); + } + return feed; + } catch (WebloggerException e) { + throw new AtomException("Getting category collection", e); + } + } + + /** The weblog's categories as an APP category document (RFC 5023 section 7). */ + public AtomCategories getCategoriesDocument(AtomRequest areq) throws AtomException { + Weblog weblog = requireWeblog(CollectionSupport.path(areq)); + try { + return RollerAtomService.weblogCategories(weblog); + } catch (WebloggerException e) { + throw new AtomException("Getting category document", e); + } + } + + public AtomEntry getEntry(AtomRequest areq) throws AtomException { + String[] pathInfo = CollectionSupport.path(areq); + Weblog weblog = requireWeblog(pathInfo); + try { + return createAtomEntry(weblog, requireCategory(weblog, pathInfo)); + } catch (WebloggerException e) { + throw new AtomException("Getting category", e); + } + } + + public AtomEntry postEntry(AtomRequest areq, AtomEntry entry) throws AtomException { + Weblog weblog = requireWeblog(CollectionSupport.path(areq)); + String name = validName(entry.getTitle()); + if (weblog.hasCategory(name)) { + throw CollectionSupport.conflict("Category name already in use: " + name); + } + try { + WeblogCategory category = new WeblogCategory(); + category.setWeblog(weblog); + category.setName(name); + copyProperties(entry, category); + weblog.addCategory(category); + category.calculatePosition(); + manager().saveWeblogCategory(category); + roller.flush(); + CacheManager.invalidate(weblog); + return createAtomEntry(weblog, category); + } catch (WebloggerException e) { + throw new AtomException("Creating category", e); + } + } + + public void putEntry(AtomRequest areq, AtomEntry entry) throws AtomException { + String[] pathInfo = CollectionSupport.path(areq); + Weblog weblog = requireWeblog(pathInfo); + try { + WeblogCategory category = requireCategory(weblog, pathInfo); + if (entry.getTitle() != null) { + String name = validName(entry.getTitle()); + WeblogCategory existing = weblog.getWeblogCategory(name); + if (existing != null && !existing.getId().equals(category.getId())) { + throw CollectionSupport.conflict("Category name already in use: " + name); + } + category.setName(name); + } + copyProperties(entry, category); + manager().saveWeblogCategory(category); + roller.flush(); + CacheManager.invalidate(category); + } catch (WebloggerException e) { + throw new AtomException("Updating category", e); + } + } + + public void deleteEntry(AtomRequest areq) throws AtomException { + String[] pathInfo = CollectionSupport.path(areq); + Weblog weblog = requireWeblog(pathInfo); + try { + WeblogEntryManager mgr = manager(); + WeblogCategory category = requireCategory(weblog, pathInfo); + String moveTo = areq.getParameter("moveTo"); + if (StringUtils.isNotEmpty(moveTo)) { + WeblogCategory target = mgr.getWeblogCategory(weblog, moveTo); + if (target == null || target.getId().equals(category.getId())) { + throw CollectionSupport.badRequest("moveTo must name another category of this weblog"); + } + mgr.moveWeblogCategoryContents(category, target); + roller.flush(); + } else if (mgr.isWeblogCategoryInUse(category)) { + throw CollectionSupport.conflict( + "Category is in use; name a category to move its entries to with moveTo"); + } + CacheManager.invalidate(category); + mgr.removeWeblogCategory(category); Review Comment: 🐞Claude Issue: **Important:** Nothing stops a client from deleting the weblog's last category. The Categories page hides the delete link when only one category is left (`AllCategories.size() > 1` in `Categories.jsp`), but AtomPub does not. With no categories, `saveWeblogEntry()` falls back to `getWeblogCategories().iterator().next()`, which throws `NoSuchElementException`, so new entries fail from both the UI and AtomPub. Refuse the delete with 409 when it is the only category. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
