📄 rolleratomhandler.java
字号:
/* * Copyright 2005 David M Johnson (For RSS and Atom In Action) * * Licensed 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. */package org.roller.presentation.atomapi;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.List;import java.util.StringTokenizer;import javax.servlet.http.HttpServletRequest;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts.util.RequestUtils;import org.roller.model.FileManager;import org.roller.model.Roller;import org.roller.model.WeblogManager;import org.roller.pojos.UserData;import org.roller.pojos.WeblogCategoryData;import org.roller.pojos.WeblogEntryData;import org.roller.pojos.WebsiteData;import org.roller.presentation.LoginServlet;import org.roller.presentation.RollerContext;import org.roller.util.RollerMessages;import org.roller.util.Utilities;import com.sun.syndication.feed.atom.Content;import com.sun.syndication.feed.atom.Entry;import com.sun.syndication.feed.atom.Link;import com.sun.syndication.io.impl.Base64;/** * Roller's Atom Protocol implementation. * <pre> * Here are the URIs suppored: * * URI type URI form Handled by * -------- -------- ---------- * Introspection URI /blog-name getIntrosection() * Collection URI /blog-name/<collection-name> getCollection() * Collection-next URI /blog-name/<collection-name>/id getCollection() * Member URI /blog-name/<object-name> post<object-name>() * Member URI /blog-name/<object-name>/id get<object-name>() * Member URI /blog-name/<object-name>/id put<object-name>() * Member URI /blog-name/<object-name>/id delete<object-name>() * * Until group blogging is supported username == blogname. * * Collection-names Object-names * ---------------- ------------ * entries entry * resources resource * categories categories * soon: * users user * templates template * </pre> * * @author David M Johnson */public class RollerAtomHandler implements AtomHandler{ private HttpServletRequest mRequest; private Roller mRoller; private RollerContext mRollerContext; private String mUsername; private int mMaxEntries = 20; //private MessageDigest md5Helper = null; //private MD5Encoder md5Encoder = new MD5Encoder(); private static Log mLogger = LogFactory.getFactory().getInstance(RollerAtomHandler.class); private SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmss" ); //---------------------------------------------------------------- construction /** * Create Atom handler for a request and attempt to authenticate user. * If user is authenticated, then getAuthenticatedUsername() will return * then user's name, otherwise it will return null. */ public RollerAtomHandler(HttpServletRequest request) { mRequest = request; mRoller = RollerContext.getRoller(request); mRollerContext = RollerContext.getRollerContext(request); // TODO: decide what to do about authentication, is WSSE going to fly? mUsername = authenticateWSSE(request); //mUsername = authenticateBASIC(request); if (mUsername != null) { try { UserData user = mRoller.getUserManager().getUser(mUsername); mRoller.setUser(user); } catch (Exception e) { mLogger.error("ERROR: setting user", e); } } // try // { // md5Helper = MessageDigest.getInstance("MD5"); // } // catch (NoSuchAlgorithmException e) // { // mLogger.debug("ERROR creating MD5 helper", e); // } } /** * Return username of authenticated user or null if there is none. */ public String getAuthenticatedUsername() { return mUsername; } //---------------------------------------------------------------- introspection /** * Return Atom service document for site, getting blog-name from pathInfo. * Since a user can (currently) have only one blog, one workspace is returned. * The workspace will contain collections for entries, categories and resources. */ public AtomService getIntrospection(String[] pathInfo) throws Exception { if (pathInfo.length == 1) { String username = pathInfo[0]; String absUrl = mRollerContext.getAbsoluteContextUrl(mRequest); AtomService service = new AtomService(); AtomService.Workspace workspace = new AtomService.Workspace(); workspace.setTitle("Workspace: Collections for " + username); service.addWorkspace(workspace); AtomService.Collection entryCol = new AtomService.Collection(); entryCol.setTitle("Collection: Weblog Entries for " + username); entryCol.setContents("entries"); entryCol.setHref(absUrl + "/atom/"+mUsername+"/entries"); workspace.addCollection(entryCol); AtomService.Collection catCol = new AtomService.Collection(); catCol.setTitle("Collection: Categories for " + username); catCol.setContents("categories"); catCol.setHref(absUrl + "/atom/"+mUsername+"/categories"); workspace.addCollection(catCol); AtomService.Collection uploadCol = new AtomService.Collection(); uploadCol.setTitle("Collection: File uploads for " + username); uploadCol.setContents("generic"); uploadCol.setHref(absUrl + "/atom/"+mUsername+"/resources"); workspace.addCollection(uploadCol); return service; } throw new Exception("ERROR: bad URL in getIntrospection()"); } //----------------------------------------------------------------- collections /** * Returns collection specified by pathInfo with no date range specified. * Just calls the other getCollection(), but with offset = -1. */ public AtomCollection getCollection(String[] pathInfo) throws Exception { return getCollection(pathInfo, null, new Date(), -1); } /** * Returns collection specified by pathInfo, constrained by a date range and * starting at an offset within the collection.Returns 20 items at a time. * <pre> * Supports these three collection URI forms: * /<blog-name>/entries * /<blog-name>/resources * /<blog-name>/categories * </pre> * @param pathInfo Path info from URI * @param start Don't include members updated before this date (null allowed) * @param end Don't include members updated after this date (null allowed) * @param offset Offset within collection (for paging) */ public AtomCollection getCollection( String[] pathInfo, Date start, Date end, int offset) throws Exception { if (pathInfo.length > 0 && pathInfo[1].equals("entries")) { return getCollectionOfEntries(pathInfo, start, end, offset); } else if (pathInfo.length > 0 && pathInfo[1].equals("resources")) { return getCollectionOfResources(pathInfo, start, end, offset); } else if (pathInfo.length > 0 && pathInfo[1].equals("categories")) { return getCollectionOfCategories(pathInfo, start, end, offset); } throw new Exception("ERROR: bad URL in getCollection()"); } /** * Helper method that returns collection of entries, called by getCollection(). */ public AtomCollection getCollectionOfEntries( String[] pathInfo, Date start, Date end, int offset) throws Exception { String username = pathInfo[0]; String absUrl = mRollerContext.getAbsoluteContextUrl(mRequest); WebsiteData website = mRoller.getUserManager().getWebsite(username); List entries = null; if (canView(website)) { if (pathInfo.length == 2) // handle /blogname/entries { // return most recent blog entries if (offset == -1) { entries = mRoller.getWeblogManager().getWeblogEntries( website, // website start, // startDate end, // endDate null, // catName WeblogManager.ALL, // status new Integer(mMaxEntries + 1)); // maxEntries } else { entries = mRoller.getWeblogManager().getWeblogEntries( website, // website start, // startDate end, // endDate null, // catName WeblogManager.ALL, // status offset, // offset (for range paging) mMaxEntries + 1); // maxEntries } } else if (pathInfo.length == 3) // handle /blogname/entries/entryid { // return entries previous to entry specified by pathInfo String entryid = pathInfo[2]; WeblogManager wmgr = mRoller.getWeblogManager(); WeblogEntryData entry = wmgr.retrieveWeblogEntry(entryid); entries = wmgr.getPreviousEntries(entry, null, mMaxEntries + 1); } else throw new Exception("ERROR: bad URL"); // build collection AtomCollection col = new AtomCollection(); if (entries.size() > mMaxEntries) { // there are more entries, so include next link WeblogEntryData lastEntry = (WeblogEntryData)entries.get(entries.size() - 1); col.setNext(createNextLink(lastEntry, start, end, offset)); } // add up to max entries to collection int count = 0; Iterator iter = entries.iterator(); while (iter.hasNext() && count++ < mMaxEntries) { WeblogEntryData rollerEntry = (WeblogEntryData)iter.next(); AtomCollection.Member member = new AtomCollection.Member();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -