snoopdave commented on code in PR #209: URL: https://github.com/apache/roller/pull/209#discussion_r4238859929
########## 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: not fixed as described — the finding was wrong. `moveWeblogCategoryContents()` already makes the target the default (Blogger API) category when the source was the default, so the default is never null after a `moveTo` delete. Added `defaultCategoryRemovedWithMoveToHandsOverTheDefault`, which deletes the default category with `moveTo`, checks the new default, and lists the collection. The NPE can still happen in a weblog that has no default category, so `isWeblogCategoryInUse()` now compares null-safely (`cat.equals(weblog.getBloggerCategory())`). ########## 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: fixed by refusing DELETE with 409 "A weblog must keep at least one category" when the category is the weblog's only one, matching the Categories page. Test: `lastCategoryCannotBeRemoved`. ########## 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: fixed by adding `categoryDocumentIsServedAsAtomcat` to RollerAtomServletTest: GET `/blog/categories.atomcat` is served with `application/atomcat+xml;charset=utf-8`, writes an `app:categories` document with the handler's categories, and never reaches `getCollection`. -- 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]
