|
From: Michael K. <ko...@us...> - 2006-02-10 11:36:25
|
Update of /cvsroot/cobricks/cobricks2/src/org/cobricks/portal In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11203 Modified Files: MultipartFilter.java PortalManager.java PortalManagerImpl.java PortalPage.java PortalPresenter.java PortalServlet.java PortalServletAdaptor.java PortalStats.java itemontology.xml Added Files: PortalCache.java PortalObject.java PortalObjectFile.java Removed Files: PageCache.java Log Message: changed web object support in Portal Component to make binary objects possible Index: PortalManager.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/PortalManager.java,v retrieving revision 1.19 retrieving revision 1.20 diff -u -d -r1.19 -r1.20 --- PortalManager.java 2 Feb 2006 11:39:26 -0000 1.19 +++ PortalManager.java 10 Feb 2006 11:36:15 -0000 1.20 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -45,23 +45,31 @@ throws PortalCompletedException; public void printPage(PortalRequest portalRequest, PrintWriter out); - public PortalPage getPage(String pagePath, String pageName); - public PortalPage getPage(int itemid); + public PortalObject getObject(String pagePath, String pageName); + public PortalObject getObject(int itemid); - public int getPageItemId(String pagePath, String pageName); + public void saveFileToFilesystem(String pagepath, String pagename, + byte barr[]); + + public int getObjectItemId(String pagePath, String pageName); - public int createPage(String pagePath, String pageName, - Map attrs, User creator) + public int createObject(String pagePath, String pageName, + Map attrs, User creator) throws CobricksException; - public void updatePage(int itemid, Map attrs, User updater) + public void updateObject(int itemid, Map attrs, User updater) throws CobricksException; - public void deletePage(int itemid, String lang, User user) + public void deleteObject(int itemid, String lang, User user) throws CobricksException; public PortalStats getPortalStats(); + public void storeBinary(int itemid, String filename, String lang, + byte barr[]); + public String[] getBinaryLangs(int itemid); + public byte[] getBinary(int itemid, String lang, StringBuffer fname); + public List getPageUpdateHistory(int itemid, Date fromdate); public void addPageUpdateHistory(int itemid, int userid, String hostname, Date accdate); @@ -79,7 +87,7 @@ public void deleteFolder(int cid, User user) throws CobricksException; - public List searchPages(String searchExp, boolean searchInTitle); + public List searchObjects(String searchExp, boolean searchInTitle); public void writeLogEntry(String userlogin, String msg); Index: PortalStats.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/PortalStats.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- PortalStats.java 14 Oct 2004 05:52:12 -0000 1.6 +++ PortalStats.java 10 Feb 2006 11:36:15 -0000 1.7 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -16,7 +16,7 @@ import java.net.URL; import java.util.*; -import org.apache.log4j.*; +import org.apache.log4j.Logger; import org.cobricks.core.CoreManager; import org.cobricks.core.ComponentDirectory; Index: itemontology.xml =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/itemontology.xml,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- itemontology.xml 1 Feb 2006 20:36:21 -0000 1.18 +++ itemontology.xml 10 Feb 2006 11:36:15 -0000 1.19 @@ -1,6 +1,6 @@ <itemontology> -<class name="portalpage" parent="item" javaclassname="org.cobricks.portal.PortalPage"> +<class name="portalobject" parent="item" javaclassname="org.cobricks.portal.PortalObject"> <description lang="de">Portalobjekt</description> <attr name="name" type="string(30)"/> <attr name="mimetype" type="string(20)"/> @@ -10,15 +10,19 @@ <attr name="readroles" type="string[]" /> <attr name="writeroles" type="string[]" /> </class> - -<class name="portalpagehtml" parent="portalpage"> + +<class name="portalfile" parent="portalobject" javaclassname="org.cobricks.portal.PortalObjectFile"> +<description lang="de">Portalobjekt</description> +</class> + +<class name="portalpage" parent="portalobject" javaclassname="org.cobricks.portal.PortalPage"> <description lang="de">Portalseite</description> <attr name="fullcontext" type="boolean"> <default>true</default> </attr> </class> -<class name="wikipage" parent="portalpagehtml"> +<class name="wikipage" parent="portalpage"> <description lang="de">Wikiseite</description> <attr name="content" type="text" multilanguage="true" versioned="true" /> </class> Index: PortalServlet.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/PortalServlet.java,v retrieving revision 1.45 retrieving revision 1.46 diff -u -d -r1.45 -r1.46 --- PortalServlet.java 2 Feb 2006 17:18:00 -0000 1.45 +++ PortalServlet.java 10 Feb 2006 11:36:15 -0000 1.46 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -15,6 +15,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.net.URL; +import java.net.URLConnection; import java.util.Date; import java.util.HashMap; import java.util.Iterator; @@ -109,6 +110,8 @@ this.addTarget("create", "performCreatePage", "", false); this.addTarget("update", "performUpdatePage", "", false); this.addTarget("delete", "performDeletePage", "", false); + this.addTarget("createbin", "performCreateObj", "", false); + this.addTarget("updatebin", "performUpdateObj", "", false); this.addTarget("createfolder", "performCreateFolder", "", false); this.addTarget("updatefolder", "performUpdateFolder", "", false); this.addTarget("deletefolder", "performDeleteFolder", "", false); @@ -329,17 +332,17 @@ attrs.put("fullcontext", fullcontext); } - //Set the itemclass + // Set the itemclass String itemclass = prequest.getRequestParameter("itemclass"); if(itemclass == null) { - itemclass = "portalpagehtml"; + itemclass = "portalpage"; } attrs.put("itemclass", itemclass); try { int itemid = - portalManager.createPage(pagepath, pagename, attrs, - portalUser.getUser()); + portalManager.createObject(pagepath, pagename, attrs, + portalUser.getUser()); String hostname = request.getRemoteHost(); if (hostname == null) @@ -378,7 +381,7 @@ } try { - portalManager.deletePage(itemid, lang, portalUser.getUser()); + portalManager.deleteObject(itemid, lang, portalUser.getUser()); } catch (Exception e) { logger.error(LogUtil.ex("Failed deleting page.", e)); prequest.setReturnCode(2102); @@ -413,7 +416,7 @@ HttpServletRequest request = prequest.getHttpServletRequest(); - PortalPage page = portalManager.getPage(itemid); + PortalObject page = portalManager.getObject(itemid); Map attrs = new HashMap(); String lang[] = prequest.getRequestParameterValues("plang"); @@ -487,7 +490,7 @@ } else { attrs.put("newContentVersion", "false"); } - portalManager.updatePage(itemid, attrs, portalUser.getUser()); + portalManager.updateObject(itemid, attrs, portalUser.getUser()); String hostname = request.getRemoteHost(); if (hostname == null) @@ -508,6 +511,188 @@ /** * */ + public String performCreateObj(PortalRequest prequest, PrintWriter out) + throws PortalCompletedException + { + logger.debug("performCreateBin()"); + + // check access rights + PortalUser portalUser = prequest.getPortalUser(); + if (!userManager.getAccessControl().checkPermission(portalUser + .getUserId(), "page", "create", null)) { + prequest.setReturnCode(2000); + return "noaccess"; + } + + // get the uploaded file + HttpServletRequest request = prequest.getHttpServletRequest(); + Object upload = request.getAttribute("_file"); + if (upload == null) { + logger.warn("no file uploaded"); + prequest.setReturnCode(2101); + return "error"; + } + if (!(upload instanceof FileItem)) { + logger.warn("no file uploaded - "+upload.getClass().getName()); + prequest.setReturnCode(2101); + return "error"; + } + + FileItem uploadFile = (FileItem)upload; + String contentType = uploadFile.getContentType(); + int size = (int)uploadFile.getSize(); + byte[] data = uploadFile.get(); + + // get mandatory parameters from HTTP request + String pagepath = prequest.getRequestParameter("ppath"); + String pagename = prequest.getRequestParameter("pname"); + String uploadFileName = uploadFile.getName(); + String mimetype = + URLConnection.guessContentTypeFromName(uploadFileName); + logger.info("!!! filename="+uploadFileName+", mime="+mimetype); + if (pagename == null || pagename.trim().length()<1) { + pagename = uploadFileName; + } + else if (pagename.indexOf(".")<1) { + String tmps = uploadFile.getName(); + int pos = tmps.lastIndexOf("."); + if (pos >= 0) { + pagename += tmps.substring(pos); + } + } + String lang = prequest.getRequestParameter("plang"); + + // get optional parameters from HTTP request + String storedb = prequest.getRequestParameter("storedb"); + + if (storedb != null) { + // store the binary object in the database + // and create an item for it + Map attrs = new HashMap(); + attrs.put("mimetype", mimetype); + attrs.put("title", pagename); + // set the itemclass + String itemclass = prequest.getRequestParameter("itemclass"); + if(itemclass == null) { + itemclass = "portalfile"; + } + attrs.put("itemclass", itemclass); + try { + int itemid = + portalManager.createObject(pagepath, pagename, attrs, + portalUser.getUser()); + // store object + if (itemid > 0) { + logger.info("!!! storing "+data.length+" bytes"); + portalManager.storeBinary(itemid, pagename, lang, data); + } + + String hostname = request.getRemoteHost(); + if (hostname == null) + hostname = request.getRemoteAddr(); + portalManager. + addPageUpdateHistory(itemid, portalUser.getUserId(), + hostname, new Date()); + } catch (Exception e) { + logger.error(LogUtil.ex("Failed creating object.", e)); + prequest.setReturnCode(2101); + return "error"; + } + } else { + // store the binary object as file in the webspace + portalManager.saveFileToFilesystem(pagepath, pagename, data); + } + + prequest.setReturnCode(1101); + return "success"; + } + + + /** + * + */ + public String performUpdateObj(PortalRequest prequest, PrintWriter out) + throws PortalCompletedException + { + logger.debug("performUpdateBin()"); + + // check access rights + PortalUser portalUser = prequest.getPortalUser(); + if (!userManager.getAccessControl().checkPermission(portalUser + .getUserId(), "page", "update", null)) { + prequest.setReturnCode(2000); + return "noaccess"; + } + + // get the uploaded file + HttpServletRequest request = prequest.getHttpServletRequest(); + Object upload = request.getAttribute("_file"); + if (upload == null) { + logger.warn("no file uploaded"); + prequest.setReturnCode(2101); + return "error"; + } + if (!(upload instanceof FileItem)) { + logger.warn("no file uploaded - "+upload.getClass().getName()); + prequest.setReturnCode(2101); + return "error"; + } + + FileItem uploadFile = (FileItem)upload; + String contentType = uploadFile.getContentType(); + int size = (int)uploadFile.getSize(); + byte[] data = uploadFile.get(); + + // get mandatory parameters from HTTP request + String itemidstring = prequest.getRequestParameter("itemid"); + int itemid = 0; + try { + itemid = Integer.parseInt(itemidstring); + } catch (Exception e) { } + String pagename = prequest.getRequestParameter("pname"); + if (pagename == null || pagename.trim().length()<1) { + pagename = uploadFile.getName(); + } + else if (pagename.indexOf(".")<1) { + String tmps = uploadFile.getName(); + int pos = tmps.lastIndexOf("."); + if (pos >= 0) { + pagename += tmps.substring(pos+1); + } + } + String lang = prequest.getRequestParameter("plang"); + + // store the binary object in the database + // and update the item for it + Map attrs = new HashMap(); + try { + // TBD + + // store object + if (itemid > 0) { + portalManager.storeBinary(itemid, pagename, lang, data); + } + + String hostname = request.getRemoteHost(); + if (hostname == null) + hostname = request.getRemoteAddr(); + portalManager. + addPageUpdateHistory(itemid, portalUser.getUserId(), + hostname, new Date()); + } catch (Exception e) { + logger.error(LogUtil.ex("Failed updating object.", e)); + prequest.setReturnCode(2101); + return "error"; + } + + prequest.setReturnCode(1101); + return "success"; + } + + + /** + * + */ public String performCreateFolder(PortalRequest prequest, PrintWriter out) throws PortalCompletedException { Index: MultipartFilter.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/MultipartFilter.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- MultipartFilter.java 2 Feb 2006 11:39:26 -0000 1.3 +++ MultipartFilter.java 10 Feb 2006 11:36:15 -0000 1.4 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -30,7 +30,7 @@ import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUpload; import org.apache.commons.fileupload.FileUploadException; -import org.apache.log4j.*; +import org.apache.log4j.Logger; import org.cobricks.core.util.LogUtil; --- NEW FILE: PortalCache.java --- /* * Copyright (c) 2004-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software * License, either version 1.0 of the License, or (at your option) any * later version (see www.cobricks.org). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.portal; import java.util.HashMap; import java.util.Map; import java.util.Iterator; import org.apache.log4j.Logger; /** * Class for caching PortalObject objects. * * @author mic...@ac... * @version $Date: 2006/02/10 11:36:15 $ */ public class PortalCache { static Logger logger = Logger.getLogger(PortalCache.class); Map pathCache = null; Map itemidCache = null; boolean active = true; /** * */ public PortalCache() { pathCache = new HashMap(); itemidCache = new HashMap(); } public void inactivate() { active = false; } public PortalObject getObject(String path, String name) { if (!active) return null; String key = path+"/"+name; return (PortalObject)pathCache.get(key); } public PortalObject getObject(int itemid) { if (!active) return null; // tbd: when this works with the itemmanager, then rely on // itemmanager cache ... return (PortalObject)itemidCache.get(new Integer(itemid)); } public void setObject(PortalObject p) { if (!active) return; String key = p.getPagePath()+"/"+p.getPageName(); pathCache.put(key, p); if (p.getId()>0) { itemidCache.put(new Integer(p.getId()), p); } // page objects cache garbage collection int cacheSize = pathCache.size(); if (cacheSize % 10 == 0) logger.info("Portal objects cache size is "+cacheSize); if (cacheSize > 500) { logger.info("Portal objects cache size is bigger than limit (" +cacheSize+")"); int toDelete = cacheSize - 300; Iterator iter = pathCache.keySet().iterator(); for (int i=0; i<toDelete; i++) { Object key2 = iter.next(); pathCache.remove(key2); } logger.info("Portal objects cache reduced to "+pathCache.size() +" entries"); } } public void removeObject(int itemid) { if (!active) return; itemidCache.remove(new Integer(itemid)); } /** * Clear the cache */ public void clear() { pathCache = new HashMap(); itemidCache = new HashMap(); } } Index: PortalPresenter.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/PortalPresenter.java,v retrieving revision 1.35 retrieving revision 1.36 diff -u -d -r1.35 -r1.36 --- PortalPresenter.java 2 Feb 2006 11:39:26 -0000 1.35 +++ PortalPresenter.java 10 Feb 2006 11:36:15 -0000 1.36 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -193,7 +193,13 @@ pageName = filename; } } - PortalPage page = portalManager.getPage(pagePath, pageName); + PortalObject o = portalManager.getObject(pagePath, pageName); + if (!(o instanceof PortalPage)) { + logger.warn("Wrong type of portal object for " + +pagePath+pageName); + return ""; + } + PortalPage page = (PortalPage)o; Map pc = page.getStringPageContent(lang); if (pc == null || pc.size()<1) return ""; @@ -370,17 +376,22 @@ /** * Return a PortalPage object by the itemid which is given as String */ - public PortalPage getPage(String itemid) + public PortalObject getObject(String itemid) { int id = 0; try { id = Integer.parseInt(itemid); - return portalManager.getPage(id); + return portalManager.getObject(id); } catch (Exception e) { } return null; } + public PortalObject getPage(String itemid) + { + return getObject(itemid); + } + /** * Print some header information for html pages. This function * will be called in the Velocity header files. @@ -415,10 +426,11 @@ { StringBuffer sb = new StringBuffer(""); String lang = portalRequest.getPageLang(); - PortalPage page = portalManager.getPage(portalRequest.getPagePath(), - portalRequest.getPageName()); - if (page!=null) { - List languages = page.getLanguages(); + PortalObject page = portalManager. + getObject(portalRequest.getPagePath(), + portalRequest.getPageName()); + if (page!=null && (page instanceof PortalPage)) { + List languages = ((PortalPage)page).getLanguages(); ListIterator i = languages.listIterator(); while (i.hasNext()) { String tmps = (String)i.next(); @@ -662,7 +674,7 @@ public final String printOption(String label, String labelval, String val) { StringBuffer result = - new StringBuffer("<option value=\"" + labelval + "\">"); + new StringBuffer("<option value=\"" + labelval + "\""); if (labelval.equals(val)) { result.append(" selected=\"true\""); } --- NEW FILE: PortalObject.java --- /* * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software * License, either version 1.0 of the License, or (at your option) any * later version (see www.cobricks.org). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.portal; import java.io.File; import java.net.URLConnection; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; import org.cobricks.core.CoreManager; import org.cobricks.core.util.LogUtil; import org.cobricks.user.User; /** * Class for storing portal objects. The class is derived from Item, * since web objects are a special class of item (if they are stored in * the database). If the web object is not available in the database * the itemid in the object will be 0. * * @author mic...@ac... * @version $Date: 2006/02/10 11:36:15 $ */ public class PortalObject extends org.cobricks.item.Item { static Logger logger = Logger.getLogger(PortalObject.class); String pageName = null; String pagePath = null; Set pagePaths = null; String mimetype = null; Map pageTitles = new HashMap(); // lang - pagetitle String possibleLangs[] = {"de","en","fr","en","es"}; /** * */ public PortalObject() { super(); } public PortalObject(String pagePath, String pageName, String defaultLang) { super(); if (defaultLang!=null) this.defaultLanguage = defaultLang; this.pagePath = pagePath; if (pagePath == null) pagePath = "/"; this.pageName = pageName; setMimeTypeFromPageName(pageName); this.initAttrs(); } /** * */ public void postInit() { String mtype = (String)getAttribute("mimetype"); if (mtype != null) { mimetype = mtype; } String name = (String)getAttribute("name"); if (name != null) { pageName = name; if (mimetype == null) setMimeTypeFromPageName(name); } } public void postInit(CoreManager coreManager) { String mtype = (String)getAttribute("mimetype"); if (mtype != null) { mimetype = mtype; } String name = (String)getAttribute("name"); if (name != null) { pageName = name; if (mimetype == null) setMimeTypeFromPageName(name); } pagePaths = new HashSet(); Set folders = (Set)getAttribute("folders"); Iterator i = folders.iterator(); while (i.hasNext()) { Object o = i.next(); if (o instanceof Integer) { PortalManager portalManager = (PortalManager) coreManager. getComponentDirectory().getManager("portalManager"); o = portalManager.getPortalFolder(((Integer)o).intValue()); } PortalFolder pf = (PortalFolder)o; String path = pf.getPath(); pagePaths.add(path); pagePath = path; } } /** * */ public String[] getPagePaths() { String[] result = new String[pagePaths.size()]; Iterator it = pagePaths.iterator(); for (int i=0; i<pagePaths.size(); i++) { result[i] = (String)it.next(); } return result; } public String getPagePath() { if (pagePath == null) { if (pagePaths != null) pagePath = (String)(pagePaths.iterator().next()); } return pagePath; } public void addPagePath(String pagePath) { this.pagePaths.add(pagePath); if (this.pagePath == null) this.pagePath = pagePath; } public void removePagePath(String pagePath) { this.pagePaths.remove(pagePath); if (this.pagePath != null) { if (this.pagePath.equals(pagePath)) { this.pagePath = null; } } } public String getPageName() { if (pageName == null) { pageName = getAttributeAsString("name"); } return pageName; } public void setPageName(String pageName) { this.pageName = pageName; setAttribute("name", pageName); if (this.mimetype == null) setMimeTypeFromPageName(pageName); } public String getMimeType() { return mimetype; } public void setMimeTypeFromPageName(String pageName) { if (mimetype!=null) return; this.mimetype = getMimeTypeFromPageName(pageName); } static public String getMimeTypeFromPageName(String pageName) { String mimetype = URLConnection.guessContentTypeFromName(pageName); // guessContentTypeFromName() did not return a result, we use // simple suffix heuristics if (mimetype == null) { mimetype = "text/html"; String suffix = ""; int pos = pageName.lastIndexOf("."); if (pos > -1) { suffix = pageName.substring(pos+1); suffix = suffix.toLowerCase(); suffix = suffix.trim(); if (suffix.equals("html")) { mimetype = "text/html"; } if (suffix.equals("htm")) { mimetype = "text/html"; } if (suffix.equals("txt")) { mimetype = "text/plain"; } if (suffix.equals("css")) { mimetype = "text/css"; } if (suffix.equals("xml")) { mimetype = "text/xml"; } if (suffix.equals("rdf")) { mimetype = "text/rdf"; } if (suffix.equals("csv")) { mimetype = "text/csv"; } } } return mimetype; } public void setMimeType(String mimetype) { this.mimetype = mimetype; } public String getTitle(String lang) { String title = getAttributeAsString("title_" + lang); // if the page wasn't found in the requested language // check whether there exists a title in another language if (title == null || title.equals("")) { Iterator i = pageTitles.keySet().iterator(); if (i.hasNext()) { String la = (String) i.next(); title = (String) pageTitles.get(la); } } return title; } public void setTitle(String lang, String title) { lang = lang.toLowerCase(); this.setAttribute("title_"+lang,title); pageTitles.put(lang, title); } public Object getPageContent(String lang) { return null; } public void updateFromMap(Map map) { super.updateFromMap(map); initAttrs(); } /** * Initialize the pageTitles attribute from the attrs table */ public void initAttrs() { Iterator i = attrs.keySet().iterator(); while (i.hasNext()) { String aname = (String)i.next(); if (aname.startsWith("title_")) { pageTitles.put(aname.substring(6), (String)attrs.get(aname)); } } } /** * */ public void loadFromFiles(String[] filenames, File dir) { } /** * Check if the content of a given language has been loaded from * the database or from the file system. */ public boolean isFromDatabase(String lang) { return false; } /** * */ public boolean exists() { return true; } /** * */ public String toString() { StringBuffer sb = new StringBuffer("portalobject("); sb.append(pagePath); sb.append(pageName); sb.append(")"); return sb.toString(); } } Index: PortalPage.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/PortalPage.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -d -r1.23 -r1.24 --- PortalPage.java 22 Aug 2005 13:52:07 -0000 1.23 +++ PortalPage.java 10 Feb 2006 11:36:15 -0000 1.24 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -12,6 +12,9 @@ package org.cobricks.portal; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -27,31 +30,19 @@ import org.cobricks.user.User; /** - * Class for storing (web) pages. The class is derived from Item, since - * web pages are a special class of item (if they are stored in the - * database). If the page is not available in the database the itemid - * in the object will be 0. + * Class for storing web pages. * * @author mic...@ac... * @version $Date$ */ -public class PortalPage extends org.cobricks.item.Item +public class PortalPage extends PortalObject { static Logger logger = Logger.getLogger(PortalPage.class); - String pageName = null; - String pagePath = null; - Set pagePaths = null; - Map pageContents = new HashMap(); // lang - String content Map pageContentSource = new HashMap(); // lang - "file:*"|"item:*" - Map pageTitles = new HashMap(); // lang - pagetitle - - PortalManager portalManager; - - String possibleLangs[] = {"de","en","fr","en","es"}; /** @@ -64,13 +55,7 @@ public PortalPage(String pagePath, String pageName, String defaultLang) { - super(); - if (defaultLang!=null) - this.defaultLanguage = defaultLang; - this.initPageContent(); - this.pagePath = pagePath; - if (pagePath == null) pagePath = "/"; - this.pageName = pageName; + super(pagePath, pageName, defaultLang); } @@ -79,109 +64,18 @@ */ public void postInit() { - String name = (String)getAttribute("name"); - if (name != null) pageName = name; + super.postInit(); } public void postInit(CoreManager coreManager) { - pagePaths = new HashSet(); - Set folders = (Set)getAttribute("folders"); - Iterator i = folders.iterator(); - while (i.hasNext()) { - Object o = i.next(); - if (o instanceof Integer) { - PortalManager portalManager = (PortalManager) coreManager. - getComponentDirectory().getManager("portalManager"); - o = portalManager.getPortalFolder(((Integer)o).intValue()); - } - PortalFolder pf = (PortalFolder)o; - String path = pf.getPath(); - pagePaths.add(path); - pagePath = path; - } + super.postInit(coreManager); } /** * */ - public String[] getPagePaths() - { - String[] result = new String[pagePaths.size()]; - Iterator it = pagePaths.iterator(); - for (int i=0; i<pagePaths.size(); i++) { - result[i] = (String)it.next(); - } - return result; - } - - public String getPagePath() - { - if (pagePath == null) { - if (pagePaths != null) - pagePath = (String)(pagePaths.iterator().next()); - } - return pagePath; - } - - public void addPagePath(String pagePath) - { - this.pagePaths.add(pagePath); - if (this.pagePath == null) - this.pagePath = pagePath; - } - - public void removePagePath(String pagePath) - { - this.pagePaths.remove(pagePath); - if (this.pagePath != null) { - if (this.pagePath.equals(pagePath)) { - this.pagePath = null; - } - } - } - - public String getPageName() - { - if (pageName == null) { - pageName = getAttributeAsString("name"); - } - return pageName; - } - - public void setPageName(String pageName) - { - this.pageName = pageName; - setAttribute("name", pageName); - } - - - public String getTitle(String lang) - { - String title = getAttributeAsString("title_" + lang); - - // if the page wasn't found in the requested language - // check whether there exists a title in another language - if (title == null || title.equals("")) { - Iterator i = pageTitles.keySet().iterator(); - if (i.hasNext()) { - String la = (String) i.next(); - title = (String) pageTitles.get(la); - } - } - - return title; - } - - public void setTitle(String lang, String title) - { - lang = lang.toLowerCase(); - this.setAttribute("title_"+lang,title); - pageTitles.put(lang, title); - } - - public Object getPageContent(String lang) { return pageContents.get(lang); @@ -246,17 +140,11 @@ } - public void updateFromMap(Map map) - { - super.updateFromMap(map); - initPageContent(); - } - - /** - * Initialize the pageContents attribute from the attrs table + * Initialize the pageTitles and pageContents attributes from + * the attrs table */ - public void initPageContent() + public void initAttrs() { Iterator i = attrs.keySet().iterator(); while (i.hasNext()) { @@ -273,7 +161,7 @@ } } - + /** * */ @@ -339,6 +227,51 @@ /** + * + */ + public void loadFromFiles(String[] filenames, File dir) + { + for (int i=0; i<filenames.length; i++) { + // first load the files contents + StringBuffer content = new StringBuffer(""); + try { + File file = new File(dir, filenames[i]); + BufferedReader in = + new BufferedReader(new FileReader(file)); + while (true) { + String tmps = in.readLine(); + if (tmps == null) break; + content.append(tmps); + content.append("\n"); + } + } catch (Exception e) { + logger.warn("failed loading file " + +filenames[i]+": "+e.getMessage()); + } + // store the contents + String lang = "default"; + if (filenames[i].length()>pageName.length()) + lang = filenames[i].substring(pageName.length()+1); + // store contents if there is not already something + // in the pageContents map (e.g. from loadFromDatabase()) + String contents = (String)this.getPageContent(lang); + if (contents==null || contents.trim().length()<1) { + // if the contents from the database are empty, then + // also get the contents from the file - but keep + // the reference to the database (for updates) + if (this.getPageContent(lang)==null) { + this.setPageContent(lang, content.toString(), + "file:"+dir.toString()+"/" + +filenames[i]); + } else { + this.setPageContent(lang, content.toString(), null); + } + } + } // for (int i=0; ... + } + + + /** * Check if the content of a given language has been loaded from * the database or from the file system. */ --- PageCache.java DELETED --- --- NEW FILE: PortalObjectFile.java --- /* * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software * License, either version 1.0 of the License, or (at your option) any * later version (see www.cobricks.org). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.portal; import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; import org.cobricks.core.CoreManager; import org.cobricks.core.util.IOUtil; import org.cobricks.core.util.LogUtil; import org.cobricks.user.User; /** * * @author mic...@ac... * @version $Date: 2006/02/10 11:36:15 $ */ public class PortalObjectFile extends PortalObject { static Logger logger = Logger.getLogger(PortalObjectFile.class); Map objContents = new HashMap(); // lang - byte[] content Map objContentSource = new HashMap(); // lang - "file:*"|"item:*" /** * */ public PortalObjectFile() { super(); } public PortalObjectFile(String pagePath, String pageName, String defaultLang) { super(pagePath, pageName, defaultLang); } /** * */ public void postInit() { super.postInit(); } public void postInit(CoreManager coreManager) { super.postInit(coreManager); } /** * */ public Object getPageContent(String lang) { Object o = objContents.get(lang); if (o == null && objContents.size()>0) o = ((objContents.values()).iterator()).next(); return o; } public void setPageContent(String lang, Object content) { if (content instanceof byte[]) { objContents.put(lang, content); objContentSource.put(lang, "item"); } } public void setPageContent(String lang, Object content, Object source) { if (source!=null && source.equals("item")) setPageContent(lang, content); else { // just update the local caches - for pages from file system objContents.put(lang, content); if (source != null) objContentSource.put(lang, source); } } /** * */ public void loadFromFiles(String[] filenames, File dir) { for (int i=0; i<filenames.length; i++) { try { File file = new File(dir, filenames[i]); String lang = "default"; if (filenames[i].length()>pageName.length()) lang = filenames[i].substring(pageName.length()+1); try { byte[] content = IOUtil.readFully(new FileInputStream(file)); this.setPageContent(lang, content, "file:"+dir.toString()+"/" +filenames[i]); } catch(Exception e) { logger.warn(LogUtil.ex("Failed loading file "+file, e)); } } catch (Exception e) { logger.warn("failed loading file " +filenames[i]+": "+e.getMessage()); } } // for (int i=0; ... } /** * Check if the content of a given language has been loaded from * the database or from the file system. */ public boolean isFromDatabase(String lang) { if (lang==null) lang = "default"; Object source = objContentSource.get(lang); if (source == null) return false; if (source instanceof String) { if (source.equals("item")) return true; else return false; } else { return true; } } /** * */ public boolean exists() { if (objContents==null || objContents.size()<1) return false; return true; } /** * */ public String toString() { StringBuffer sb = new StringBuffer("portalobject("); sb.append(pagePath); sb.append(pageName); sb.append(","); Iterator i = objContentSource.keySet().iterator(); while (i.hasNext()) { String lang = (String)i.next(); sb.append(lang); if (i.hasNext()) sb.append(","); } sb.append(")"); return sb.toString(); } } Index: PortalServletAdaptor.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/PortalServletAdaptor.java,v retrieving revision 1.17 retrieving revision 1.18 diff -u -d -r1.17 -r1.18 --- PortalServletAdaptor.java 11 Aug 2005 11:20:17 -0000 1.17 +++ PortalServletAdaptor.java 10 Feb 2006 11:36:15 -0000 1.18 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -18,7 +18,7 @@ import javax.servlet.*; import javax.servlet.http.*; -import org.apache.log4j.*; +import org.apache.log4j.Logger; import org.cobricks.core.CoreManager; import org.cobricks.core.ComponentDirectory; Index: PortalManagerImpl.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/PortalManagerImpl.java,v retrieving revision 1.76 retrieving revision 1.77 diff -u -d -r1.76 -r1.77 --- PortalManagerImpl.java 3 Feb 2006 15:37:17 -0000 1.76 +++ PortalManagerImpl.java 10 Feb 2006 11:36:15 -0000 1.77 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -15,8 +15,10 @@ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FilenameFilter; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.InputStream; @@ -95,7 +97,7 @@ static Logger logger = Logger.getLogger(PortalManagerImpl.class); static String COMP_NAME = "Cobricks-2 Portal Component"; - static String COMP_VERSION = "V0.3 (Aug. 2005)"; + static String COMP_VERSION = "V0.4 (Feb. 2006)"; static String ROOTPORTALFOLDER = "ROOT"; @@ -113,7 +115,7 @@ protected VelocityContext velocityRootContext = null; - protected PageCache pageCache = null; + protected PortalCache portalCache = null; protected AccessControl accessControl; protected SessionHandler sessionHandler; @@ -136,6 +138,9 @@ protected Map portalFoldersByPath; protected String logfilename; + + // names of all itemclasses that are children of wikipage + Set wikiOntologyClasses = new HashSet(); /** @@ -144,7 +149,7 @@ public PortalManagerImpl() { super(); - pageCache = new PageCache(); + portalCache = new PortalCache(); portalFolders = new HashMap(); portalFoldersByPath = new HashMap(); } @@ -165,7 +170,7 @@ */ public void resetCache() { - pageCache.clear(); + portalCache.clear(); portalFolders = new HashMap(); portalFoldersByPath = new HashMap(); pageItemIdCache = new HashMap(); @@ -210,87 +215,115 @@ /** - * Get a PortalPage object for the given pagePath / pageName. + * Get a portal object for the given pagePath / pageName. * There are two possibilities for successfully loading pages: * - the page exists in the database (there is an item for the page) * - the page exists in the webspace only (no item) * The difference shows in the itemid (=0 or >0) */ - public PortalPage getPage(String pagePath, String pageName) + public PortalObject getObject(String pagePath, String pageName) { if (pagePath == null) pagePath = "/"; if (pageName == null) pageName = "index.html"; - PortalPage p = pageCache.getPage(pagePath, pageName); + PortalObject p = portalCache.getObject(pagePath, pageName); if (p == null) { - int itemid = getPageItemId(pagePath, pageName); + int itemid = getObjectItemId(pagePath, pageName); if (itemid > 0) { // load data from database - p = loadPageFromDatabase(itemid); - if (p == null) p = new PortalPage(); - // add file system content (webspace directory) - loadPageFromFilesystem(p); - } else { - p = new PortalPage(pagePath, pageName, defaultLanguage); - // add file system content (webspace directory) - loadPageFromFilesystem(p); + p = loadObjectFromDatabase(itemid); + if (p!=null) + loadObjectFromFilesystem(p); + } + if (p == null) { + String mimetype = + PortalObject.getMimeTypeFromPageName(pageName); + if (mimetype.startsWith("text")) + p = new PortalPage(pagePath, pageName, defaultLanguage); + else + p = new PortalObjectFile(pagePath, pageName, + defaultLanguage); + loadObjectFromFilesystem(p); } if (p != null) - pageCache.setPage(p); + portalCache.setObject(p); } return p; } - + public PortalObject getPage(String pagePath, String pageName) + { + return getObject(pagePath, pageName); + } + + /** - * Get a PortalPage object for the given itemid. In this case the page + * Get a portal object for the given itemid. In this case the page * must exist in the database (there is an item for the page). * Otherwise, no result (NULL) is returned. */ - public PortalPage getPage(int itemid) + public PortalObject getObject(int itemid) { if (itemid < 1) return null; - PortalPage p = pageCache.getPage(itemid); + PortalObject p = portalCache.getObject(itemid); if (p == null) { // load data from database - p = loadPageFromDatabase(itemid); + p = loadObjectFromDatabase(itemid); if (p!=null) - pageCache.setPage(p); + portalCache.setObject(p); } return p; } + public PortalObject getPage(int itemid) + { + return getObject(itemid); + } + /** * Try to load the contents for the given itemid from the database. */ - public PortalPage loadPageFromDatabase(int itemid) + public PortalObject loadObjectFromDatabase(int itemid) { if (itemid < 1) return null; - logger.info("Getting page item "+itemid+" from database"); + logger.info("Getting object item "+itemid+" from database"); try { Object o = itemManager.getItem(itemid); - if (!(o instanceof PortalPage)) { - logger.warn("object class of portal page object is " + if (!(o instanceof PortalObject)) { + logger.warn("object class of portal object is " +o.getClass().getName()); return null; } - PortalPage result = (PortalPage)o; - // explicitly set content - result.initPageContent(); + PortalObject result = (PortalObject)o; + result.initAttrs(); + + // if it is a binary object, then set the content + String itemclass = result.getItemClass(); + // TBD: also true for itemclass derived ffrom portalfile ... + if (itemclass.equals("portalfile")) { + List res = getBinary(itemid); + Iterator i = res.iterator(); + while (i.hasNext()) { + Map m = (Map)i.next(); + String lang = (String)m.get("lang"); + Object content = m.get("content"); + ((PortalObjectFile)result).setPageContent(lang, content); + } + } return result; } catch (Exception e) { - logger.warn(LogUtil.ex("Failed loading PortalPage object", e)); + logger.warn(LogUtil.ex("Failed loading portal object", e)); } return null; } /** - * Try to load the contents for the given pagePath, pageName combination - * from the filesystem .. and store the results in the PortalPage object + * Try to load the object for the given pagePath, pageName combination + * from the filesystem .. and store the results in the portal object * (if there are not already entries in the object) */ - public void loadPageFromFilesystem(PortalPage p) + public void loadObjectFromFilesystem(PortalObject p) { String dirname = webspaceDir; String pagePath = p.getPagePath(); @@ -302,50 +335,16 @@ dirname += pagePath; } try { - FilenameFilter filenameFilter = new PortalFilenameFilter(pageName); + // check for all language versions ... + FilenameFilter filenameFilter = + new PortalFilenameFilter(pageName); File dir = new File(dirname); if (dir.isDirectory()) { String[] filenames = dir.list(filenameFilter); if (filenames != null) { - for (int i=0; i<filenames.length; i++) { - // first load the files contents - StringBuffer content = new StringBuffer(""); - try { - File file = new File(dir, filenames[i]); - BufferedReader in = - new BufferedReader(new FileReader(file)); - while (true) { - String tmps = in.readLine(); - if (tmps == null) break; - content.append(tmps); - content.append("\n"); - } - } catch (Exception e) { - logger.warn("failed loading file " - +filenames[i]+": "+e.getMessage()); - } - // store the contents - String lang = "default"; - if (filenames[i].length()>pageName.length()) - lang = filenames[i].substring(pageName.length()+1); - // store contents if there is not already something - // in the pageContents map (e.g. from loadFromDatabase()) - String contents = (String)p.getPageContent(lang); - if (contents==null || contents.trim().length()<1) { - // if the contents from the database are empty, then - // also get the contents from the file - but keep - // the reference to the database (for updates) - if (p.getPageContent(lang)==null) { - p.setPageContent(lang, content.toString(), - "file:"+dir.toString()+"/" - +filenames[i]); - } else { - p.setPageContent(lang, content.toString(), null); - } - } - } // for (int i=0; ... - } // if (filenames != null) - } // if (dir.isDirectory()) + p.loadFromFiles(filenames, dir); + } + } } catch (Exception e) { logger.error(LogUtil. exception("Failed loading page template from " @@ -355,10 +354,33 @@ /** + * + */ + public void saveFileToFilesystem(String pagepath, String pagename, + byte barr[]) + { + String tmps = webspaceDir; + if (tmps.endsWith(File.separator)) + tmps = tmps.substring(0, tmps.length()-1); + File file = new File(tmps+pagepath+pagename); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(file); + fos.write(barr); + } catch (Exception e) { + logger.error(LogUtil.ex("Failed writing file", e)); + } finally { + if (fos != null) + try { fos.close(); } catch (Exception ex) { } + } + } + + + /** * Check the access permission of the file. * The possible actions are "read" or "write". */ - protected boolean checkAccessPermission(PortalPage page, + protected boolean checkAccessPermission(PortalObject page, PortalUser portalUser, String action) { @@ -392,67 +414,23 @@ String pagePath = portalRequest.getPagePath(); String pageName = portalRequest.getPageName(); String lang = portalRequest.getLang(); - PortalPage page = getPage(pagePath, pageName); + PortalObject page = getObject(pagePath, pageName); // check if it is a text file or a binary one ... - String mimetype = URLConnection.guessContentTypeFromName(pageName); + String mimetype = page.getMimeType(); logger.info("mimetype of request = "+mimetype); - if (mimetype == null) { - mimetype = "text/html"; - // guessContentTypeFromName() did not return a result, we use - // simple suffix heuristics - String suffix = ""; - int pos = pageName.lastIndexOf("."); - if (pos > -1) { - suffix = pageName.substring(pos+1); - suffix = suffix.toLowerCase(); - suffix = suffix.trim(); - if (suffix.equals("html")) { - mimetype = "text/html"; - } - if (suffix.equals("htm")) { - mimetype = "text/html"; - } - if (suffix.equals("txt")) { - mimetype = "text/plain"; - } - if (suffix.equals("css")) { - mimetype = "text/css"; - } - if (suffix.equals("xml")) { - mimetype = "text/xml"; - } - if (suffix.equals("rdf")) { - mimetype = "text/rdf"; - } - if (suffix.equals("csv")) { - mimetype = "text/csv"; - } - } - } // if it is a text page, then user printPage() if (mimetype.startsWith("text")) { - printPage(portalRequest, out); + printPage(portalRequest, page, out); return; } - // ... otherwise send file directly + // ... otherwise send file content directly logger.info("identified non text file ... "+mimetype); - // check access rights - boolean access = true; - // TBD - if (!access) { - pagePath = "/"; - pageName = "noaccess.html"; - page = getPage(pagePath, pageName); - printPage(portalRequest, page, out); - return; - } - - Object o = page.getPageContent(lang); - if (o == null) { + Object content = page.getPageContent(lang); + if (content == null) { logger.warn("Failed getting content for page "+pagePath+pageName +","+lang+" - loading "+notFoundPath); pagePath = notFoundPath; @@ -462,33 +440,28 @@ pageName = pagePath.substring(pos+1); pagePath = pagePath.substring(0, pos+1); } - page = getPage(pagePath, pageName); + page = getObject(pagePath, pageName); printPage(portalRequest, page, out); return; } - // TBD: return content ... + ... [truncated message content] |