⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rolleratomhandler.java

📁 这个weblogging 设计得比较精巧
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                member.setTitle(rollerEntry.getDisplayTitle());                member.setUpdated(rollerEntry.getUpdateTime());                member.setHref(absUrl                     + "/atom/" + username + "/entry/" + rollerEntry.getId());                col.addMember(member);            }            return col;        }        throw new Exception("ERROR: not authorized");    }        /**     * Helper method that returns collection of resources, called by getCollection().     */    public AtomCollection getCollectionOfResources(            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);        FileManager fmgr = mRoller.getFileManager();        File[] files = fmgr.getFiles(website);        if (canView(website))         {            AtomCollection col = new AtomCollection();            for (int i=0; i<files.length; i++)            {                AtomCollection.Member member = new AtomCollection.Member();                member.setTitle(files[i].getName());                member.setUpdated(new Date(files[i].lastModified()));                member.setHref(absUrl                     + "/atom/" + username + "/resource/" + files[i].getName() );                col.addMember(member);            }            return col;        }        throw new Exception("ERROR: not authorized");           }    /**     * Helper method that returns collection of categories, called by getCollection().     */    public AtomCollection getCollectionOfCategories(            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);        WeblogManager wmgr = mRoller.getWeblogManager();        List items = wmgr.getWeblogCategories(website);        if (canView(website))         {            AtomCollection col = new AtomCollection();            Iterator iter = items.iterator();            Date now = new Date();            while (iter.hasNext())            {                WeblogCategoryData item = (WeblogCategoryData)iter.next();                AtomCollection.Member member = new AtomCollection.Member();                member.setTitle(item.getPath());                member.setUpdated(now);                member.setHref(                    absUrl + "/atom/" + username + "/category/" + item.getId());                col.addMember(member);            }            return col;        }        throw new Exception("ERROR: not authorized");           }    //--------------------------------------------------------------------- entries        /**     * Create entry in the entry collection (a Roller blog has only one).     */    public Entry postEntry(String[] pathInfo, Entry entry) throws Exception    {        // authenticated client posted a weblog entry        String username = pathInfo[0];        WebsiteData website = mRoller.getUserManager().getWebsite(username);        if (canEdit(website))        {                        // Save it and commit it            WeblogEntryData rollerEntry = createRollerEntry(website, entry);            rollerEntry.save();               mRoller.commit();                                    // Throttle one entry per second             // (MySQL timestamp has 1 sec resolution, damnit)            Thread.sleep(1000);                                    // TODO: ping the appropriate ping            // TODO: flush the cache on Atom post            //flushPageCache(mRequest);                        return createAtomEntry(rollerEntry);        }        throw new Exception("ERROR not authorized to edit website");    }        /**     * Retrieve entry, URI like this /blog-name/entry/id     */    public Entry getEntry(String[] pathInfo) throws Exception    {        if (pathInfo.length == 3) // URI is /blogname/entries/entryid        {            WeblogEntryData entry =                 mRoller.getWeblogManager().retrieveWeblogEntry(pathInfo[2]);            if (!canView(entry))            {                throw new Exception("ERROR not authorized to view entry");            }            else if (entry != null)             {                return createAtomEntry(entry);            }            throw new Exception("ERROR: entry not found");        }        throw new Exception("ERROR: bad URI");    }    /**     * Update entry, URI like this /blog-name/entry/id     */    public Entry putEntry(String[] pathInfo, Entry entry) throws Exception    {        if (pathInfo.length == 3) // URI is /blogname/entries/entryid        {             WeblogEntryData rollerEntry =                 mRoller.getWeblogManager().retrieveWeblogEntry(pathInfo[2]);            if (canEdit(rollerEntry))            {                    rollerEntry.setTitle(entry.getTitle());                rollerEntry.setText(((Content)entry.getContents().get(0)).getValue());                rollerEntry.setUpdateTime(new Timestamp(new Date().getTime()));                rollerEntry.save();                mRoller.commit();                return createAtomEntry(rollerEntry);            }            throw new Exception("ERROR not authorized to put entry");        }        throw new Exception("ERROR: bad URI");    }    /**     * Delete entry, URI like this /blog-name/entry/id     */    public void deleteEntry(String[] pathInfo) throws Exception    {        if (pathInfo.length == 3) // URI is /blogname/entries/entryid        {             WeblogEntryData rollerEntry =                 mRoller.getWeblogManager().retrieveWeblogEntry(pathInfo[2]);            if (canEdit(rollerEntry))            {                    rollerEntry.remove();                mRoller.commit();                return;            }            throw new Exception("ERROR not authorized to delete entry");        }        throw new Exception("ERROR: bad URI");    }    //-------------------------------------------------------------------- resources    /**     * Create new resource in generic collection (a Roller blog has only one).     * TODO: can we avoid saving temporary file?     * TODO: do we need to handle mutli-part MIME uploads?     * TODO: use Jakarta Commons File-upload?     */    public String postResource(String[] pathInfo,             String name, String contentType, InputStream is)            throws Exception    {        // authenticated client posted a weblog entry        String username = pathInfo[0];        WebsiteData website = mRoller.getUserManager().getWebsite(username);        if (canEdit(website) && pathInfo.length > 1)        {            try            {                FileManager fmgr = mRoller.getFileManager();                RollerMessages msgs = new RollerMessages();                                // save to temp file                if (name == null)                 {                    throw new Exception(                        "ERROR[postResource]: No 'name' present in HTTP headers");                }                File tempFile = File.createTempFile(name,"tmp");                FileOutputStream fos = new FileOutputStream(tempFile);                Utilities.copyInputToOutput(is, fos);                fos.close();                                // If save is allowed by Roller system-wide policies                   if (fmgr.canSave(website, name, tempFile.length(), msgs))                 {                    // Then save the file                    FileInputStream fis = new FileInputStream(tempFile);                    fmgr.saveFile(website, name, tempFile.length(), fis);                    fis.close();                                        // TODO: build URL to uploaded file should be done in FileManager                    String uploadPath = RollerContext.getUploadPath(                        mRequest.getSession(true).getServletContext());                    uploadPath += "/" + website.getUser().getUserName() + "/" + name;                    return RequestUtils.printableURL(                        RequestUtils.absoluteURL(mRequest, uploadPath));                                    }                tempFile.delete();                throw new Exception("File upload denied because:" + msgs.toString());            }            catch (Exception e)            {                String msg = "ERROR in atom.postResource";                mLogger.error(msg,e);                throw new Exception(msg);            }        }        throw new Exception("ERROR not authorized to edit website");    }            /**      * Get absolute path to resource specified by path info.     */    public String getResourceFilePath(String[] pathInfo) throws Exception    {        // ==> /<blogname>/resources/<filename>        String uploadPath = RollerContext.getUploadPath(                mRequest.getSession(true).getServletContext());        return uploadPath + File.separator + pathInfo[2];    }            /**     * Update resource specified by pathInfo using data from input stream.     * Expects pathInfo of form /blog-name/resources/name     */    public void putResource(String[] pathInfo,             String contentType, InputStream is) throws Exception    {        if (pathInfo.length > 2)        {           String name = pathInfo[2];             postResource(pathInfo, name, contentType, is);        }        throw new Exception("ERROR: bad pathInfo");    }    /**     * Delete resource specified by pathInfo.     * Expects pathInfo of form /blog-name/resources/name     */    public void deleteResource(String[] pathInfo) throws Exception    {        // authenticated client posted a weblog entry        String username = pathInfo[0];        WebsiteData website = mRoller.getUserManager().getWebsite(username);        if (canEdit(website) && pathInfo.length > 1)        {            try            {                FileManager fmgr = mRoller.getFileManager();                fmgr.deleteFile(website, pathInfo[2]);            }            catch (Exception e)            {                String msg = "ERROR in atom.deleteResource";                mLogger.error(msg,e);                throw new Exception(msg);            }        }        throw new Exception("ERROR not authorized to edit website");    }    //------------------------------------------------------------------ URI testers        /**     * True if URL is the introspection URI.     */    public boolean isIntrospectionURI(String[] pathInfo)    {        if (pathInfo.length == 1 && pathInfo[0].equals(mUsername)) return true;        return false;    }        /**     * True if URL is a entry URI.     */    public boolean isEntryURI(String[] pathInfo)         {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -