Source Code Cross Referenced for XsltPortal.java in  » ERP-CRM-Financial » sakai » org » theospi » portfolio » portal » web » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » sakai » org.theospi.portfolio.portal.web 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**********************************************************************************
0002:         * $URL:https://source.sakaiproject.org/svn/osp/trunk/portal/webapp/src/java/org/theospi/portfolio/portal/web/XsltPortal.java $
0003:         * $Id:XsltPortal.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
0004:         ***********************************************************************************
0005:         *
0006:         * Copyright (c) 2006, 2007 The Sakai Foundation.
0007:         *
0008:         * Licensed under the Educational Community License, Version 1.0 (the "License");
0009:         * you may not use this file except in compliance with the License.
0010:         * You may obtain a copy of the License at
0011:         *
0012:         *      http://www.opensource.org/licenses/ecl1.php
0013:         *
0014:         * Unless required by applicable law or agreed to in writing, software
0015:         * distributed under the License is distributed on an "AS IS" BASIS,
0016:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017:         * See the License for the specific language governing permissions and
0018:         * limitations under the License.
0019:         *
0020:         **********************************************************************************/package org.theospi.portfolio.portal.web;
0021:
0022:        import javax.xml.transform.TransformerFactory;
0023:        import javax.xml.transform.OutputKeys;
0024:        import org.sakaiproject.authz.api.Role;
0025:        import org.sakaiproject.component.cover.ComponentManager;
0026:        import org.sakaiproject.component.cover.ServerConfigurationService;
0027:        import org.sakaiproject.entity.api.ResourceProperties;
0028:        import org.sakaiproject.exception.IdUnusedException;
0029:        import org.sakaiproject.exception.PermissionException;
0030:        import org.sakaiproject.portal.charon.CharonPortal;
0031:        import org.sakaiproject.site.api.Site;
0032:        import org.sakaiproject.site.api.SitePage;
0033:        import org.sakaiproject.site.api.ToolConfiguration;
0034:        import org.sakaiproject.site.cover.SiteService;
0035:        import org.sakaiproject.tool.api.*;
0036:        import org.sakaiproject.tool.cover.ActiveToolManager;
0037:        import org.sakaiproject.tool.cover.SessionManager;
0038:        import org.sakaiproject.user.api.Preferences;
0039:        import org.sakaiproject.user.api.User;
0040:        import org.sakaiproject.user.cover.PreferencesService;
0041:        import org.sakaiproject.util.ResourceLoader;
0042:        import org.sakaiproject.util.StringUtil;
0043:        import org.sakaiproject.util.Web;
0044:        import org.theospi.portfolio.portal.intf.PortalManager;
0045:        import org.theospi.portfolio.portal.model.SitePageWrapper;
0046:        import org.theospi.portfolio.portal.model.SiteType;
0047:        import org.theospi.portfolio.portal.model.ToolCategory;
0048:        import org.w3c.dom.Document;
0049:        import org.w3c.dom.Element;
0050:        import org.w3c.dom.Node;
0051:        import org.xml.sax.InputSource;
0052:        import org.xml.sax.SAXException;
0053:
0054:        import javax.servlet.ServletConfig;
0055:        import javax.servlet.ServletException;
0056:        import javax.servlet.ServletContext;
0057:        import javax.servlet.http.HttpServletRequest;
0058:        import javax.servlet.http.HttpServletResponse;
0059:        import javax.xml.parsers.DocumentBuilder;
0060:        import javax.xml.parsers.DocumentBuilderFactory;
0061:        import javax.xml.parsers.ParserConfigurationException;
0062:        import javax.xml.transform.*;
0063:        import javax.xml.transform.dom.DOMSource;
0064:        import javax.xml.transform.stream.StreamResult;
0065:        import javax.xml.transform.stream.StreamSource;
0066:        import java.io.*;
0067:        import java.net.MalformedURLException;
0068:        import java.net.URL;
0069:        import java.util.*;
0070:
0071:        /**
0072:         * Created by IntelliJ IDEA.
0073:         * User: John Ellis
0074:         * Date: Feb 9, 2006
0075:         * Time: 2:21:33 PM
0076:         * To change this template use File | Settings | File Templates.
0077:         */
0078:        public class XsltPortal extends CharonPortal {
0079:
0080:            private PortalManager portalManager;
0081:            private DocumentBuilder documentBuilder;
0082:            private Templates templates;
0083:            private Templates toolCategoryTemplates;
0084:            private URIResolver servletResolver;
0085:            private String categoryBasePath;
0086:
0087:            /** messages. */
0088:            private static ResourceLoader rb = new ResourceLoader(
0089:                    "org/theospi/portfolio/portal/messages");
0090:
0091:            /** base messages. */
0092:            private static ResourceLoader rbsitenav = new ResourceLoader(
0093:                    "sitenav");
0094:
0095:            private static final String TOOL_CATEGORY = "category";
0096:            private static final String TOOL_CATEGORY_HELPER = "category_helper";
0097:            private static final String SITE_TYPE = "site_type";
0098:            private static final String SITE_TYPE_HELPER = "site_type_helper";
0099:
0100:            protected void doPost(HttpServletRequest req,
0101:                    HttpServletResponse res) throws ServletException,
0102:                    IOException {
0103:                // get the Sakai session
0104:                Session session = SessionManager.getCurrentSession();
0105:
0106:                // recognize what to do from the path
0107:                String option = req.getPathInfo();
0108:
0109:                // get the parts (the first will be "")
0110:                String[] parts = option.split("/");
0111:
0112:                if (parts.length < 2) {
0113:                    super .doPost(req, res);
0114:                    return;
0115:                }
0116:
0117:                if (parts[1].equals(SITE_TYPE_HELPER)) {
0118:                    // Resolve the site_type of the form /portal/site_type_helper/project
0119:                    String siteTypeKey = parts[2];
0120:                    doSiteTypeHelper(req, res, session, siteTypeKey,
0121:                            parts.length == 4);
0122:                } else {
0123:                    super .doPost(req, res);
0124:                }
0125:            }
0126:
0127:            protected void doGet(HttpServletRequest req, HttpServletResponse res)
0128:                    throws ServletException, IOException {
0129:                // get the Sakai session
0130:                Session session = SessionManager.getCurrentSession();
0131:
0132:                // recognize what to do from the path
0133:                String option = req.getPathInfo();
0134:
0135:                // get the parts (the first will be "")
0136:                String[] parts = option.split("/");
0137:
0138:                if (parts.length < 2) {
0139:                    super .doGet(req, res);
0140:                    return;
0141:                }
0142:
0143:                if (parts[1].equals(SITE_TYPE)) {
0144:                    // Resolve the site_type of the form /portal/site_type/project
0145:                    String siteTypeKey = parts[2];
0146:                    doSiteType(req, res, session, siteTypeKey);
0147:                } else if (parts[1].equals(SITE_TYPE_HELPER)) {
0148:                    // Resolve the site_type of the form /portal/site_type_helper/project
0149:                    String siteTypeKey = parts[2];
0150:                    doSiteTypeHelper(req, res, session, siteTypeKey,
0151:                            parts.length == 4);
0152:                } else if (parts[1].equals(TOOL_CATEGORY)) {
0153:                    // Resolve the site_type of the form /portal/category/<siteId>/<categoryKey>/<optionalToolId>
0154:                    String siteId = parts[2];
0155:                    String categoryKey = parts[3];
0156:                    String pageId = null;
0157:                    if (parts.length > 4) {
0158:                        pageId = parts[4];
0159:                    }
0160:                    doCategory(req, res, session, siteId, categoryKey, pageId);
0161:                } else if (parts[1].equals(TOOL_CATEGORY_HELPER)) {
0162:                    // Resolve the site_type of the form /portal/category_helper/<siteId>/<categoryKey>
0163:                    String siteId = parts[2];
0164:                    String categoryKey = parts[3];
0165:                    doCategoryHelper(req, res, session, siteId, categoryKey,
0166:                            parts.length > 4);
0167:                } else {
0168:                    super .doGet(req, res);
0169:                }
0170:            }
0171:
0172:            protected void forwardPortal(ActiveTool tool,
0173:                    HttpServletRequest req, HttpServletResponse res,
0174:                    ToolConfiguration p, String skin, String toolContextPath,
0175:                    String toolPathInfo) throws ToolException, IOException {
0176:
0177:                // if there is a stored request state, and path, extract that from the
0178:                // session and
0179:                // reinstance it
0180:
0181:                // generate the forward to the tool page placement
0182:                String portalPlacementUrl = getPortalString()
0183:                        + super .getPortalPageUrl(p);
0184:                res.sendRedirect(portalPlacementUrl);
0185:                return;
0186:
0187:            }
0188:
0189:            protected String getPortalString() {
0190:                return "/osp-portal";
0191:            }
0192:
0193:            protected void doCategoryHelper(HttpServletRequest req,
0194:                    HttpServletResponse res, Session session, String siteId,
0195:                    String categoryKey, boolean returning)
0196:                    throws ToolException, IOException {
0197:                if (session.getUserId() == null) {
0198:                    doLogin(req, res, session, req.getPathInfo(), false);
0199:                    return;
0200:                }
0201:
0202:                Site site = getPortalManager().getSite(siteId);
0203:                String siteTypeKey = getPortalManager().decorateSiteType(site);
0204:                SiteType siteType = getPortalManager().getSiteType(siteTypeKey);
0205:
0206:                Document doc = getDocumentBuilder().newDocument();
0207:                Element root = doc.createElement("toolCategory");
0208:                ToolCategory category = getPortalManager().getToolCategory(
0209:                        siteType.getKey(), categoryKey);
0210:
0211:                Map categoryPageMap = getPortalManager().getPagesByCategory(
0212:                        siteId);
0213:                List categoryPages = (List) categoryPageMap.get(category);
0214:                root.appendChild(createCategoryXml(doc, category,
0215:                        categoryPages, siteId, categoryKey, null));
0216:
0217:                Element rolesElement = createRolesXml(doc, siteId);
0218:                root.appendChild(rolesElement);
0219:
0220:                doc.appendChild(root);
0221:                outputDocument(req, res, session, doc,
0222:                        getToolCategoryTransformer());
0223:
0224:                /*
0225:                ToolSession toolSession = session.getToolSession(Web.escapeJavascript(categoryKey));
0226:
0227:                if (!returning) {
0228:                   toolSession.setAttribute(PortalManager.SITE_TYPE, siteTypeKey);
0229:                   toolSession.setAttribute(PortalManager.SITE_ID, siteId);
0230:                   toolSession.setAttribute(PortalManager.TOOL_CATEGORY, categoryKey);
0231:                   toolSession.setAttribute(PortalManager.CONTEXT, getContext());
0232:                }
0233:
0234:                // put the session in the request attribute
0235:                req.setAttribute(Tool.TOOL_SESSION, toolSession);
0236:
0237:                // set as the current tool session
0238:                SessionManager.setCurrentToolSession(toolSession);
0239:
0240:                // put the placement id in the request attribute
0241:                String placementId = Web.escapeJavascript(categoryKey);
0242:                req.setAttribute(Tool.PLACEMENT_ID, placementId);
0243:
0244:                ActiveTool helperTool = ActiveToolManager.getActiveTool("osp.tool.category");
0245:                Placement placement = new org.sakaiproject.util.Placement(placementId, helperTool, null, null, null);
0246:
0247:                String context = req.getPathInfo();
0248:                forwardTool(helperTool, req, res, placement, siteType.getSkin(), getContext() + "/" + context, "/toolCategory");
0249:                 */
0250:            }
0251:
0252:            protected Element createRolesXml(Document doc, String siteId) {
0253:                Element rolesElement = doc.createElement("roles");
0254:                List roles = getPortalManager().getRoles(siteId);
0255:                for (Iterator i = roles.iterator(); i.hasNext();) {
0256:                    Role role = (Role) i.next();
0257:                    if (role != null) {
0258:                        Element roleElement = doc.createElement("role");
0259:                        roleElement.setAttribute("id", role.getId());
0260:                        rolesElement.appendChild(roleElement);
0261:                    }
0262:                }
0263:                return rolesElement;
0264:            }
0265:
0266:            protected void doSiteTypeHelper(HttpServletRequest req,
0267:                    HttpServletResponse res, Session session,
0268:                    String siteTypeKey, boolean returning) throws ToolException {
0269:                if (session.getUserId() == null) {
0270:                    doLogin(req, res, session, req.getPathInfo(), false);
0271:                    return;
0272:                }
0273:
0274:                SiteType siteType = getPortalManager().getSiteType(siteTypeKey);
0275:
0276:                ToolSession toolSession = session.getToolSession(Web
0277:                        .escapeJavascript(siteTypeKey));
0278:
0279:                toolSession.setAttribute(PortalManager.RELOAD_SITES, "true");
0280:
0281:                if (!returning) {
0282:                    toolSession.setAttribute(PortalManager.SITE_TYPE,
0283:                            siteTypeKey);
0284:                }
0285:
0286:                // put the session in the request attribute
0287:                req.setAttribute(Tool.TOOL_SESSION, toolSession);
0288:
0289:                // set as the current tool session
0290:                SessionManager.setCurrentToolSession(toolSession);
0291:
0292:                // put the placement id in the request attribute
0293:                String placementId = Web.escapeJavascript(siteTypeKey);
0294:                req.setAttribute(Tool.PLACEMENT_ID, placementId);
0295:
0296:                ActiveTool helperTool = ActiveToolManager
0297:                        .getActiveTool("osp.site.type");
0298:                Placement placement = new org.sakaiproject.util.Placement(
0299:                        placementId, "osp.site.type", helperTool, null, null,
0300:                        null);
0301:
0302:                String context = req.getPathInfo();
0303:                forwardTool(helperTool, req, res, placement,
0304:                        siteType.getSkin(), getContext() + "/" + context,
0305:                        "/siteType");
0306:            }
0307:
0308:            protected void doCategory(HttpServletRequest req,
0309:                    HttpServletResponse res, Session session, String siteId,
0310:                    String categoryKey, String pageId) throws IOException,
0311:                    ToolException {
0312:                siteId = checkVisitSite(siteId, session, req, res);
0313:                if (siteId == null) {
0314:                    return;
0315:                }
0316:                Document doc = createPortalDocument(session, null, siteId,
0317:                        categoryKey, pageId, req);
0318:                outputDocument(req, res, session, doc);
0319:            }
0320:
0321:            protected void doSiteType(HttpServletRequest req,
0322:                    HttpServletResponse res, Session session, String siteTypeKey)
0323:                    throws IOException, ToolException {
0324:                if (session.getUserId() == null) {
0325:                    doLogin(req, res, session, req.getPathInfo(), false);
0326:                    return;
0327:                }
0328:                Document doc = createPortalDocument(session, siteTypeKey, null,
0329:                        null, null, req);
0330:                outputDocument(req, res, session, doc);
0331:            }
0332:
0333:            protected void outputDocument(HttpServletRequest req,
0334:                    HttpServletResponse res, Session session, Document doc)
0335:                    throws IOException {
0336:                outputDocument(req, res, session, doc, getTransformer());
0337:            }
0338:
0339:            protected void outputDocument(HttpServletRequest req,
0340:                    HttpServletResponse res, Session session, Document doc,
0341:                    Transformer transformer) throws IOException {
0342:
0343:                res.setContentType("text/html; charset=UTF-8");
0344:                res.addDateHeader("Expires", System.currentTimeMillis()
0345:                        - (1000L * 60L * 60L * 24L * 365L));
0346:                res.addDateHeader("Last-Modified", System.currentTimeMillis());
0347:                res
0348:                        .addHeader("Cache-Control",
0349:                                "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
0350:                res.addHeader("Pragma", "no-cache");
0351:
0352:                PrintWriter out = res.getWriter();
0353:
0354:                try {
0355:                    StreamResult outputTarget = new StreamResult(out);
0356:                    transformer.transform(new DOMSource(doc), outputTarget);
0357:                } catch (TransformerException e) {
0358:                    throw new RuntimeException(e);
0359:                }
0360:            }
0361:
0362:            protected void doSite(HttpServletRequest req,
0363:                    HttpServletResponse res, Session session, String siteId,
0364:                    String pageId, String toolContextPath) throws IOException,
0365:                    ToolException {
0366:                siteId = checkVisitSite(siteId, session, req, res);
0367:
0368:                if (siteId == null) {
0369:                    return;
0370:                }
0371:
0372:                Document doc = createPortalDocument(session, null, siteId,
0373:                        null, pageId, req);
0374:                outputDocument(req, res, session, doc);
0375:            }
0376:
0377:            protected void doWorksite(HttpServletRequest req,
0378:                    HttpServletResponse res, Session session, String siteId,
0379:                    String pageId, String toolContextPath) throws IOException,
0380:                    ToolException {
0381:                siteId = checkVisitSite(siteId, session, req, res);
0382:                if (siteId == null) {
0383:                    return;
0384:                }
0385:                Document doc = createPortalDocument(session, null, siteId,
0386:                        null, pageId, req);
0387:                outputDocument(req, res, session, doc);
0388:            }
0389:
0390:            protected void doPage(HttpServletRequest req,
0391:                    HttpServletResponse res, Session session, String pageId,
0392:                    String toolContextPath) throws IOException, ToolException {
0393:                SitePage page = getPortalManager().getSitePage(pageId);
0394:                if (page == null) {
0395:                    doError(req, res, session, ERROR_WORKSITE);
0396:                    return;
0397:                }
0398:                String siteId = page.getSiteId();
0399:                siteId = checkVisitSite(siteId, session, req, res);
0400:                if (siteId == null) {
0401:                    return;
0402:                }
0403:                Document doc = createPortalDocument(session, null, siteId,
0404:                        getPortalManager().getPageCategory(siteId, pageId),
0405:                        pageId, req);
0406:                outputDocument(req, res, session, doc);
0407:            }
0408:
0409:            protected void postLogin(HttpServletRequest req,
0410:                    HttpServletResponse res, Session session, String loginPath)
0411:                    throws ToolException {
0412:                if (session.getAttribute(Tool.HELPER_DONE_URL) == null) {
0413:                    session.setAttribute(Tool.HELPER_DONE_URL, Web.returnUrl(
0414:                            req, null));
0415:                }
0416:
0417:                super .postLogin(req, res, session, loginPath);
0418:            }
0419:
0420:            protected Document createPortalDocument(Session session,
0421:                    String siteTypeKey, String siteId, String toolCategoryKey,
0422:                    String pageId, HttpServletRequest req) throws IOException,
0423:                    ToolException {
0424:                Document doc = getDocumentBuilder().newDocument();
0425:
0426:                Element root = doc.createElement("portal");
0427:                doc.appendChild(root);
0428:
0429:                User currentUser = getPortalManager().getCurrentUser();
0430:                boolean loggedIn = false;
0431:                if (currentUser != null && currentUser.getId().length() > 0) {
0432:                    root.appendChild(createUserXml(doc, currentUser));
0433:                    loggedIn = true;
0434:                }
0435:
0436:                root.appendChild(createLoginXml(doc, req));
0437:
0438:                Map siteTypesMap = getPortalManager().getSitesByType(siteId);
0439:                Site site = null;
0440:                SitePage page = null;
0441:
0442:                if (siteId != null) {
0443:                    site = getPortalManager().getSite(siteId);
0444:                }
0445:
0446:                if (pageId != null) {
0447:                    page = getPortalManager().getSitePage(pageId);
0448:                }
0449:
0450:                if (siteTypeKey == null) {
0451:                    siteTypeKey = getPortalManager().decorateSiteType(site);
0452:                }
0453:
0454:                SiteType siteType = findSiteType(siteTypesMap, siteTypeKey);
0455:
0456:                root.appendChild(createTitleXml(doc, site, page));
0457:
0458:                List skins = getSkins(siteType, site);
0459:                root.appendChild(createSkinsXml(doc, skins));
0460:                root.appendChild(createConfixXml(doc, skins, site, loggedIn));
0461:                root.appendChild(createSiteTypesXml(doc, siteTypesMap,
0462:                        siteTypeKey, siteId));
0463:
0464:                if (siteId != null) {
0465:                    Map pageCateogries = getPortalManager().getPagesByCategory(
0466:                            siteId);
0467:
0468:                    checkToolPermissions(pageCateogries);
0469:
0470:                    if (pageId == null && toolCategoryKey == null) {
0471:                        // need to pick first page or category
0472:                        ToolCategory category = findFirstCategory(pageCateogries);
0473:                        if (category.getKey().equals(
0474:                                ToolCategory.UNCATEGORIZED_KEY)) {
0475:                            List pages = (List) pageCateogries.get(category);
0476:                            pageId = findFirstPage(pages);
0477:                        } else {
0478:                            toolCategoryKey = category.getKey();
0479:                        }
0480:                    }
0481:
0482:                    root.appendChild(createPageCategoriesXml(doc,
0483:                            pageCateogries, siteId, toolCategoryKey, pageId));
0484:                }
0485:
0486:                if (siteId != null) {
0487:                    Element rolesElement = createRolesXml(doc, siteId);
0488:                    root.appendChild(rolesElement);
0489:                }
0490:
0491:                if (!getPortalManager().isDisplaySiteTypes()) {
0492:                    try {
0493:                        root.appendChild(createSitesTabArea(session, siteId,
0494:                                doc, req));
0495:                    } catch (SAXException e) {
0496:                        throw new ToolException(e);
0497:                    }
0498:                }
0499:
0500:                root.appendChild(createExternalizedXml(doc));
0501:
0502:                return doc;
0503:            }
0504:
0505:            protected void checkToolPermissions(Map pages) {
0506:                List emptyCategories = new ArrayList();
0507:                for (Iterator<ToolCategory> i = pages.keySet().iterator(); i
0508:                        .hasNext();) {
0509:                    ToolCategory category = i.next();
0510:                    List categoryPages = (List) pages.get(category);
0511:                    checkPagesAccess(categoryPages);
0512:                    if (categoryPages.isEmpty()) {
0513:                        emptyCategories.add(category);
0514:                    }
0515:                }
0516:
0517:                for (Iterator<ToolCategory> i = emptyCategories.iterator(); i
0518:                        .hasNext();) {
0519:                    ToolCategory category = i.next();
0520:                    pages.remove(category);
0521:                }
0522:            }
0523:
0524:            protected void checkPagesAccess(List categoryPages) {
0525:                for (Iterator<SitePageWrapper> i = categoryPages.iterator(); i
0526:                        .hasNext();) {
0527:                    SitePageWrapper page = i.next();
0528:                    List pageTools = page.getPage().getTools();
0529:                    if (!checkToolsAccess(page.getPage().getContainingSite(),
0530:                            pageTools)) {
0531:                        i.remove();
0532:                    }
0533:                }
0534:            }
0535:
0536:            protected boolean checkToolsAccess(Site site, List pageTools) {
0537:                boolean retVal = false;
0538:
0539:                for (Iterator<Placement> i = pageTools.iterator(); i.hasNext();) {
0540:                    Placement tool = i.next();
0541:                    if (allowTool(site, tool)) {
0542:                        retVal = true;
0543:                    }
0544:                }
0545:
0546:                return retVal;
0547:            }
0548:
0549:            protected String findFirstPage(List pages) {
0550:                SitePageWrapper first = null;
0551:                for (Iterator i = pages.iterator(); i.hasNext();) {
0552:                    SitePageWrapper wrapper = (SitePageWrapper) i.next();
0553:                    if (first == null || first.getOrder() > wrapper.getOrder()) {
0554:                        first = wrapper;
0555:                    }
0556:                }
0557:                return first.getPage().getId();
0558:            }
0559:
0560:            protected ToolCategory findFirstCategory(Map pageCateogries) {
0561:                ToolCategory first = null;
0562:
0563:                for (Iterator i = pageCateogries.keySet().iterator(); i
0564:                        .hasNext();) {
0565:                    ToolCategory category = (ToolCategory) i.next();
0566:                    if (first == null || first.getOrder() > category.getOrder()) {
0567:                        first = category;
0568:                    }
0569:                }
0570:
0571:                return first;
0572:            }
0573:
0574:            protected SiteType findSiteType(Map siteTypesMap, String siteTypeKey) {
0575:                for (Iterator i = siteTypesMap.keySet().iterator(); i.hasNext();) {
0576:                    SiteType siteType = (SiteType) i.next();
0577:                    if (siteType.getKey().equals(siteTypeKey)) {
0578:                        return siteType;
0579:                    }
0580:                }
0581:
0582:                return null;
0583:            }
0584:
0585:            private Element createSitesTabArea(Session session, String siteId,
0586:                    Document doc, HttpServletRequest req) throws IOException,
0587:                    SAXException {
0588:                Element siteTabs = doc.createElement("siteTabs");
0589:                CharArrayWriter writer = new CharArrayWriter();
0590:                PrintWriter printWriter = new PrintWriter(writer);
0591:                printWriter.write("<div id=\"blank\">");
0592:                includeTabs(printWriter, req, session, siteId, "site", false);
0593:                Document tabs = getDocumentBuilder().parse(
0594:                        new InputSource(new CharArrayReader(writer
0595:                                .toCharArray())));
0596:                siteTabs.appendChild(doc.importNode(tabs.getDocumentElement(),
0597:                        true));
0598:                return siteTabs;
0599:            }
0600:
0601:            protected Element createExternalizedXml(Document doc) {
0602:                Element externalized = doc.createElement("externalized");
0603:
0604:                for (Iterator i = rb.entrySet().iterator(); i.hasNext();) {
0605:                    Map.Entry entry = (Map.Entry) i.next();
0606:                    externalized.appendChild(createExternalizedEntryXml(doc,
0607:                            entry.getKey(), entry.getValue()));
0608:                }
0609:
0610:                for (Iterator i = rbsitenav.entrySet().iterator(); i.hasNext();) {
0611:                    Map.Entry entry = (Map.Entry) i.next();
0612:                    externalized.appendChild(createExternalizedEntryXml(doc,
0613:                            entry.getKey(), entry.getValue()));
0614:                }
0615:
0616:                return externalized;
0617:            }
0618:
0619:            protected Element createExternalizedEntryXml(Document doc,
0620:                    Object key, Object value) {
0621:                Element entry = doc.createElement("entry");
0622:                entry.setAttribute("key", (String) key);
0623:
0624:                appendTextElementNode(doc, "value", (String) value, entry);
0625:
0626:                return entry;
0627:            }
0628:
0629:            protected Element createTitleXml(Document doc, Site site,
0630:                    SitePage page) {
0631:                Element titleElement = doc.createElement("pageTitle");
0632:                String title = ServerConfigurationService
0633:                        .getString("ui.service");
0634:
0635:                if (site != null) {
0636:                    title += ":" + site.getTitle();
0637:                }
0638:
0639:                if (page != null) {
0640:                    title += ":" + page.getTitle();
0641:                }
0642:                safeAppendTextNode(doc, titleElement, title, true);
0643:
0644:                return titleElement;
0645:            }
0646:
0647:            protected Element createConfixXml(Document doc, List skins,
0648:                    Site site, boolean loggedIn) {
0649:                Element config = doc.createElement("config");
0650:
0651:                String skinRepo = ServerConfigurationService
0652:                        .getString("skin.repo");
0653:                if (skins.size() > 0) {
0654:                    skinRepo += "/" + skins.get(skins.size() - 1);
0655:                } else {
0656:                    skinRepo += "/"
0657:                            + ServerConfigurationService
0658:                                    .getString("skin.default");
0659:                }
0660:
0661:                if (site != null) {
0662:                    String presenceUrl = getContext() + "/presence/"
0663:                            + site.getId();
0664:                    boolean showPresence = ServerConfigurationService
0665:                            .getBoolean("display.users.present", true);
0666:                    Element presence = doc.createElement("presence");
0667:                    safeAppendTextNode(doc, presence, presenceUrl, true);
0668:                    presence.setAttribute("include", new Boolean(showPresence
0669:                            && loggedIn).toString());
0670:                    config.appendChild(presence);
0671:                }
0672:
0673:                Element logo = doc.createElement("logo");
0674:                safeAppendTextNode(doc, logo, skinRepo
0675:                        + "/images/logo_inst.gif", true);
0676:                Element banner = doc.createElement("banner");
0677:                safeAppendTextNode(doc, banner, skinRepo
0678:                        + "/images/banner_inst.gif", true);
0679:                Element logout = doc.createElement("logout");
0680:                safeAppendTextNode(doc, logout, getContext() + "/logout", true);
0681:
0682:                String copyright = ServerConfigurationService
0683:                        .getString("bottom.copyrighttext");
0684:                String service = ServerConfigurationService.getString(
0685:                        "ui.service", "Sakai");
0686:                String serviceVersion = ServerConfigurationService.getString(
0687:                        "version.service", "?");
0688:                String sakaiVersion = ServerConfigurationService.getString(
0689:                        "version.sakai", "?");
0690:                String server = ServerConfigurationService.getServerId();
0691:                String helpUrl = ServerConfigurationService.getHelpUrl(null);
0692:                String[] bottomNav = ServerConfigurationService
0693:                        .getStrings("bottomnav");
0694:                String[] poweredByUrl = ServerConfigurationService
0695:                        .getStrings("powered.url");
0696:                String[] poweredByImage = ServerConfigurationService
0697:                        .getStrings("powered.img");
0698:                String[] poweredByAltText = ServerConfigurationService
0699:                        .getStrings("powered.alt");
0700:
0701:                config.appendChild(logo);
0702:                config.appendChild(banner);
0703:                config.appendChild(logout);
0704:
0705:                appendTextElementNode(doc, "copyright", copyright, config);
0706:                appendTextElementNode(doc, "service", service, config);
0707:                appendTextElementNode(doc, "serviceVersion", serviceVersion,
0708:                        config);
0709:                appendTextElementNode(doc, "sakaiVersion", sakaiVersion, config);
0710:                appendTextElementNode(doc, "server", server, config);
0711:                appendTextElementNode(doc, "helpUrl", helpUrl, config);
0712:                appendTextElementNodes(doc, bottomNav, config, "bottomNavs",
0713:                        "bottomNav");
0714:
0715:                if ((poweredByUrl != null) && (poweredByImage != null)
0716:                        && (poweredByAltText != null)
0717:                        && (poweredByUrl.length == poweredByImage.length)
0718:                        && (poweredByUrl.length == poweredByAltText.length)) {
0719:                    for (int i = 0; i < poweredByUrl.length; i++) {
0720:                        config.appendChild(createPoweredByXml(doc,
0721:                                poweredByAltText[i], poweredByImage[i],
0722:                                poweredByUrl[i]));
0723:                    }
0724:                } else {
0725:                    config.appendChild(createPoweredByXml(doc,
0726:                            "Powered by Sakai",
0727:                            "/library/image/sakai_powered.gif",
0728:                            "http://sakaiproject.org"));
0729:                }
0730:
0731:                appendTextElementNodes(doc, poweredByUrl, config,
0732:                        "poweredByUrls", "poweredByUrl");
0733:                appendTextElementNodes(doc, poweredByImage, config,
0734:                        "poweredByImages", "poweredByImage");
0735:                appendTextElementNodes(doc, poweredByAltText, config,
0736:                        "poweredByAltTexts", "poweredByAltText");
0737:
0738:                return config;
0739:            }
0740:
0741:            protected Element createPoweredByXml(Document doc, String text,
0742:                    String image, String url) {
0743:                Element poweredBy = doc.createElement("poweredBy");
0744:
0745:                appendTextElementNode(doc, "text", text, poweredBy);
0746:                appendTextElementNode(doc, "image", image, poweredBy);
0747:                appendTextElementNode(doc, "url", url, poweredBy);
0748:
0749:                return poweredBy;
0750:            }
0751:
0752:            protected void appendTextElementNodes(Document doc,
0753:                    String[] strings, Element parent, String topNodeName,
0754:                    String nodeName) {
0755:                Element topNode = doc.createElement(topNodeName);
0756:
0757:                if (strings == null) {
0758:                    return;
0759:                }
0760:
0761:                for (int i = 0; i < strings.length; i++) {
0762:                    appendTextElementNode(doc, nodeName, strings[i], topNode);
0763:                }
0764:
0765:                parent.appendChild(topNode);
0766:            }
0767:
0768:            protected void appendTextElementNode(Document doc, String name,
0769:                    String text, Element parent) {
0770:                Element element = doc.createElement(name);
0771:                safeAppendTextNode(doc, element, text, true);
0772:                parent.appendChild(element);
0773:            }
0774:
0775:            protected Element createPageCategoriesXml(Document doc, Map pages,
0776:                    String siteId, String toolCategoryKey, String pageId) {
0777:                Element pagesElement = doc.createElement("categories");
0778:
0779:                for (Iterator i = pages.keySet().iterator(); i.hasNext();) {
0780:                    ToolCategory category = (ToolCategory) i.next();
0781:                    List categoryPageList = (List) pages.get(category);
0782:                    pagesElement.appendChild(createCategoryXml(doc, category,
0783:                            categoryPageList, siteId, toolCategoryKey, pageId));
0784:                }
0785:                return pagesElement;
0786:            }
0787:
0788:            protected Element createCategoryXml(Document doc,
0789:                    ToolCategory category, List categoryPageList,
0790:                    String siteId, String categoryKey, String pageId) {
0791:                Element categoryElement = doc.createElement("category");
0792:                boolean selected = category.getKey().equals(categoryKey);
0793:                categoryElement.setAttribute("selected", new Boolean(selected)
0794:                        .toString());
0795:                categoryElement.setAttribute("order", new Integer(category
0796:                        .getOrder()).toString());
0797:                Element categoryKeyElement = doc.createElement("key");
0798:                safeAppendTextNode(doc, categoryKeyElement, category.getKey(),
0799:                        false);
0800:                Element categoryEscapedKeyElement = doc
0801:                        .createElement("escapedKey");
0802:                safeAppendTextNode(doc, categoryEscapedKeyElement, Web
0803:                        .escapeJavascript(category.getKey()), false);
0804:                Element categoryUrlElement = doc.createElement("url");
0805:                // Resolve the site_type of the form /portal/category/<siteId>/<categoryKey>/<optionalToolId>
0806:                safeAppendTextNode(doc, categoryUrlElement, getContext() + "/"
0807:                        + TOOL_CATEGORY + "/" + siteId + "/"
0808:                        + category.getKey(), true);
0809:                Element categoryHelperUrlElement = doc
0810:                        .createElement("helperUrl");
0811:                // Resolve the site_type of the form /portal/category_helper/<siteId>/<categoryKey>/<optionalToolId>
0812:                safeAppendTextNode(doc, categoryHelperUrlElement, getContext()
0813:                        + "/" + TOOL_CATEGORY_HELPER + "/" + siteId + "/"
0814:                        + category.getKey(), true);
0815:
0816:                categoryElement.appendChild(categoryKeyElement);
0817:                categoryElement.appendChild(categoryEscapedKeyElement);
0818:                categoryElement.appendChild(categoryUrlElement);
0819:                categoryElement.appendChild(categoryHelperUrlElement);
0820:
0821:                if (portalManager.isUseDb()) {
0822:                    appendTextElementNode(doc, "layoutFile", category.getKey(),
0823:                            categoryElement);
0824:                } else {
0825:                    appendTextElementNode(
0826:                            doc,
0827:                            "layoutFile",
0828:                            getCategoryHomePagePath(category.getHomePagePath()),
0829:                            categoryElement);
0830:                }
0831:
0832:                Element pagesElement = doc.createElement("pages");
0833:
0834:                for (Iterator i = categoryPageList.iterator(); i.hasNext();) {
0835:                    SitePageWrapper page = (SitePageWrapper) i.next();
0836:                    pagesElement.appendChild(createPageXml(doc,
0837:                            page.getOrder(), siteId, page.getPage(), category
0838:                                    .getKey(), pageId));
0839:                }
0840:
0841:                categoryElement.appendChild(pagesElement);
0842:                return categoryElement;
0843:            }
0844:
0845:            /**
0846:             * concatenates categoryBasePath, locale, and homePagePath.  If can't resolve this path the locale
0847:             * portion is removed.
0848:             * @param homePagePath
0849:             * @return localized path
0850:             */
0851:            protected String getCategoryHomePagePath(String homePagePath) {
0852:                ResourceLoader rl = new ResourceLoader();
0853:                String localeStr = rl.getLocale().toString();
0854:                String localizedPath = categoryBasePath + "_" + localeStr + "/"
0855:                        + homePagePath;
0856:                try {
0857:                    URL url = getServletContext().getResource(localizedPath);
0858:                    if (url != null) {
0859:                        return localizedPath;
0860:                    }
0861:                    // try without the country part
0862:                    localizedPath = categoryBasePath + "_"
0863:                            + localeStr.replaceFirst("_.*", "") + "/"
0864:                            + homePagePath;
0865:                    url = getServletContext().getResource(localizedPath);
0866:                    if (url != null) {
0867:                        return localizedPath;
0868:                    }
0869:                } catch (MalformedURLException e) {
0870:                    e.printStackTrace();
0871:                }
0872:                return categoryBasePath + "/" + homePagePath;
0873:            }
0874:
0875:            protected Element createPageXml(Document doc, int index,
0876:                    String siteId, SitePage page, String parentCategoryKey,
0877:                    String pageId) {
0878:                Element pageElement = doc.createElement("page");
0879:                pageElement
0880:                        .setAttribute("order", new Integer(index).toString());
0881:                boolean pageSelected = page.getId().equals(pageId);
0882:                pageElement.setAttribute("selected", new Boolean(pageSelected)
0883:                        .toString());
0884:                pageElement.setAttribute("layout",
0885:                        new Integer(page.getLayout()).toString());
0886:                pageElement.setAttribute("popUp", new Boolean(page.isPopUp())
0887:                        .toString());
0888:                pageElement.setAttribute("toolId", getFirstToolId(page));
0889:                Element pageName = doc.createElement("title");
0890:                safeAppendTextNode(doc, pageName, page.getTitle(), true);
0891:                // portal/site/9607661f-f3aa-4938-8005-c3ffaa228c6c/page/0307f10c-225b-4db8-803e-b12f24e38544
0892:
0893:                Element pageUrl = doc.createElement("url");
0894:                if (parentCategoryKey != null
0895:                        && !parentCategoryKey
0896:                                .equals(ToolCategory.UNCATEGORIZED_KEY)) {
0897:                    safeAppendTextNode(doc, pageUrl, getContext()
0898:                            + "/category/" + siteId + "/" + parentCategoryKey
0899:                            + "/" + page.getId(), true);
0900:                } else {
0901:                    safeAppendTextNode(doc, pageUrl, getContext() + "/site/"
0902:                            + siteId + "/page/" + page.getId(), true);
0903:                }
0904:
0905:                Element popPageUrl = doc.createElement("popUrl");
0906:                safeAppendTextNode(doc, popPageUrl, getContext() + "/page/"
0907:                        + page.getId(), true);
0908:
0909:                Element columns = doc.createElement("columns");
0910:
0911:                for (int i = 0; i < 2; i++) {
0912:                    Element column = doc.createElement("column");
0913:                    column.setAttribute("index", new Integer(i).toString());
0914:                    column.appendChild(createColumnToolsXml(doc, page
0915:                            .getTools(i), page));
0916:                    columns.appendChild(column);
0917:                }
0918:
0919:                pageElement.appendChild(pageName);
0920:                pageElement.appendChild(pageUrl);
0921:                pageElement.appendChild(popPageUrl);
0922:                pageElement.appendChild(columns);
0923:
0924:                return pageElement;
0925:            }
0926:
0927:            protected String getFirstToolId(SitePage page) {
0928:                List tools = page.getTools();
0929:
0930:                if (tools.size() > 0) {
0931:                    Placement placement = (Placement) tools.get(0);
0932:                    if (placement.getTool() != null)
0933:                        return placement.getTool().getId();
0934:                }
0935:
0936:                return "";
0937:            }
0938:
0939:            protected Element createColumnToolsXml(Document doc, List tools,
0940:                    SitePage page) {
0941:                Element toolsElement = doc.createElement("tools");
0942:
0943:                for (Iterator i = tools.iterator(); i.hasNext();) {
0944:                    Placement placement = (Placement) i.next();
0945:                    if (placement.getTool() != null)
0946:                        toolsElement.appendChild(createToolXml(doc, placement,
0947:                                page));
0948:                }
0949:
0950:                return toolsElement;
0951:            }
0952:
0953:            protected Element createToolXml(Document doc, Placement placement,
0954:                    SitePage page) {
0955:                Element toolElement = doc.createElement("tool");
0956:
0957:                Element title = doc.createElement("title");
0958:                safeAppendTextNode(doc, title, page.getTitle(), true);
0959:
0960:                Element escapedId = doc.createElement("escapedId");
0961:                String id = Web.escapeJavascript("i" + placement.getId());
0962:
0963:                id = id.substring(1);
0964:
0965:                safeAppendTextNode(doc, escapedId, id, true);
0966:
0967:                //portal/tool/ad222467-e186-4cca-80e9-d12a9d6db392?panel=Main
0968:                Element toolUrl = doc.createElement("url");
0969:                safeAppendTextNode(doc, toolUrl, getContext() + "/tool/"
0970:                        + placement.getId() + "?panel=Main", true);
0971:
0972:                //portal/title/ad222467-e186-4cca-80e9-d12a9d6db392
0973:                Element toolTitleUrl = doc.createElement("titleUrl");
0974:                safeAppendTextNode(doc, toolTitleUrl, getContext() + "/title/"
0975:                        + placement.getId(), true);
0976:
0977:                toolElement.appendChild(title);
0978:                toolElement.appendChild(escapedId);
0979:                toolElement.appendChild(toolUrl);
0980:                toolElement.appendChild(toolTitleUrl);
0981:
0982:                return toolElement;
0983:            }
0984:
0985:            protected Element createSiteTypesXml(Document doc,
0986:                    Map siteTypesMap, String siteTypeKey, String siteId) {
0987:                Element siteTypes = doc.createElement("siteTypes");
0988:                List types = new ArrayList(siteTypesMap.keySet());
0989:                Collections.sort(types);
0990:
0991:                for (Iterator i = types.iterator(); i.hasNext();) {
0992:                    SiteType type = (SiteType) i.next();
0993:                    boolean selected = type.getKey().equals(siteTypeKey);
0994:                    siteTypes.appendChild(createSiteTypeXml(doc, type,
0995:                            (List) siteTypesMap.get(type), selected, siteId));
0996:                }
0997:
0998:                return siteTypes;
0999:            }
1000:
1001:            protected Element createSiteTypeXml(Document doc, SiteType type,
1002:                    List sites, boolean selected, String siteId) {
1003:                Element siteTypeElement = doc.createElement("siteType");
1004:                siteTypeElement.setAttribute("selected", new Boolean(selected)
1005:                        .toString());
1006:                siteTypeElement.setAttribute("order", new Integer(type
1007:                        .getOrder()).toString());
1008:                siteTypeElement.setAttribute("userSite", new Boolean(
1009:                        type == SiteType.MY_WORKSPACE).toString());
1010:                Element siteTypeKey = doc.createElement("key");
1011:                safeAppendTextNode(doc, siteTypeKey, type.getKey(), false);
1012:                siteTypeElement.appendChild(siteTypeKey);
1013:
1014:                Element siteTypeName = doc.createElement("name");
1015:                safeAppendTextNode(doc, siteTypeName, type.getName(), false);
1016:                siteTypeElement.appendChild(siteTypeName);
1017:
1018:                Element siteTypeEscapedKey = doc.createElement("escapedKey");
1019:                safeAppendTextNode(doc, siteTypeEscapedKey, Web
1020:                        .escapeJavascript(type.getKey()), false);
1021:                siteTypeElement.appendChild(siteTypeEscapedKey);
1022:
1023:                Element siteTypeUrl = doc.createElement("url");
1024:                // /portal/site_type/<key>
1025:
1026:                try {
1027:                    Site site = getPortalManager().getSite(type.getKey());
1028:                    safeAppendTextNode(doc, siteTypeUrl, getContext()
1029:                            + "/site/" + type.getKey(), true);
1030:                } catch (RuntimeException e) {
1031:                    safeAppendTextNode(doc, siteTypeUrl, getContext() + "/"
1032:                            + SITE_TYPE + "/" + type.getKey(), true);
1033:                }
1034:                siteTypeElement.appendChild(siteTypeUrl);
1035:
1036:                Element siteTypeHelperUrl = doc.createElement("helperUrl");
1037:                // /portal/site_type_helper/<key>
1038:                safeAppendTextNode(doc, siteTypeHelperUrl, getContext() + "/"
1039:                        + SITE_TYPE_HELPER + "/" + type.getKey(), true);
1040:                siteTypeElement.appendChild(siteTypeHelperUrl);
1041:
1042:                siteTypeElement.appendChild(createSitesListXml(doc, sites,
1043:                        siteId));
1044:                return siteTypeElement;
1045:            }
1046:
1047:            protected void safeAppendTextNode(Document doc, Element element,
1048:                    String text, boolean cdata) {
1049:                if (text != null) {
1050:                    element.appendChild(cdata ? doc.createCDATASection(text)
1051:                            : doc.createTextNode(text));
1052:                }
1053:            }
1054:
1055:            protected Element createSitesListXml(Document doc, List mySites,
1056:                    String siteId) {
1057:                Session session = SessionManager.getCurrentSession();
1058:
1059:                int prefTabs = 4;
1060:                List prefExclude = new Vector();
1061:                List prefOrder = new Vector();
1062:                if (session.getUserId() != null) {
1063:                    Preferences prefs = PreferencesService
1064:                            .getPreferences(session.getUserId());
1065:                    ResourceProperties props = prefs
1066:                            .getProperties("sakai.portal.sitenav");
1067:                    try {
1068:                        prefTabs = (int) props.getLongProperty("tabs");
1069:                    } catch (Exception any) {
1070:                    }
1071:
1072:                    List l = props.getPropertyList("exclude");
1073:                    if (l != null) {
1074:                        prefExclude = l;
1075:                    }
1076:
1077:                    l = props.getPropertyList("order");
1078:                    if (l != null) {
1079:                        prefOrder = l;
1080:                    }
1081:                }
1082:
1083:                int tabsToDisplay = prefTabs;
1084:
1085:                mySites.removeAll(prefExclude);
1086:                // re-order mySites to have order first, the rest later
1087:                List ordered = new Vector();
1088:                for (Iterator i = prefOrder.iterator(); i.hasNext();) {
1089:                    String id = (String) i.next();
1090:
1091:                    // find this site in the mySites list
1092:                    int pos = indexOf(id, mySites);
1093:                    if (pos != -1) {
1094:                        // move it from mySites to order
1095:                        Site s = (Site) mySites.get(pos);
1096:                        ordered.add(s);
1097:                        mySites.remove(pos);
1098:                    }
1099:                }
1100:
1101:                // pick up the rest of the mySites
1102:                ordered.addAll(mySites);
1103:                mySites = ordered;
1104:
1105:                Element sitesElement = doc.createElement("sites");
1106:                int order = 0;
1107:                for (Iterator i = mySites.iterator(); i.hasNext();) {
1108:                    Site site = (Site) i.next();
1109:                    boolean selected = siteId != null ? site.getId().equals(
1110:                            siteId) : false;
1111:                    sitesElement.appendChild(createSiteXml(doc, site, selected,
1112:                            order, false));
1113:                    order++;
1114:                }
1115:                return sitesElement;
1116:            }
1117:
1118:            protected Element createSiteXml(Document doc, Site site,
1119:                    boolean selected, int order, boolean extra) {
1120:                Element siteElement = doc.createElement("site");
1121:                siteElement.setAttribute("selected", new Boolean(selected)
1122:                        .toString());
1123:                siteElement
1124:                        .setAttribute("extra", new Boolean(extra).toString());
1125:                siteElement.setAttribute("published", new Boolean(site
1126:                        .isPublished()).toString());
1127:                siteElement
1128:                        .setAttribute("order", new Integer(order).toString());
1129:
1130:                Element siteUrl = doc.createElement("url");
1131:                // http://localhost:8080/portal/site/bc5b1aa2-c53b-4fd1-0017-f91eef511e65<key>
1132:                safeAppendTextNode(doc, siteUrl, getContext() + "/site/"
1133:                        + site.getId(), true);
1134:                siteElement.appendChild(siteUrl);
1135:
1136:                Element siteTitle = doc.createElement("title");
1137:                safeAppendTextNode(doc, siteTitle, site.getTitle(), true);
1138:                siteElement.appendChild(siteTitle);
1139:
1140:                Element siteDescription = doc.createElement("description");
1141:                safeAppendTextNode(doc, siteDescription, site.getDescription(),
1142:                        true);
1143:                siteElement.appendChild(siteDescription);
1144:
1145:                return siteElement;
1146:            }
1147:
1148:            protected Element createSkinsXml(Document doc, List skins) {
1149:                Element skinsElement = doc.createElement("skins");
1150:                int index = 0;
1151:
1152:                String skinRepo = ServerConfigurationService
1153:                        .getString("skin.repo");
1154:
1155:                Element skin = doc.createElement("skin");
1156:                skin.setAttribute("order", index + "");
1157:                safeAppendTextNode(doc, skin, getContext()
1158:                        + "/library/css/osp-portal.css", true);
1159:                skinsElement.appendChild(skin);
1160:                index++;
1161:
1162:                for (Iterator i = skins.iterator(); i.hasNext();) {
1163:                    String skinUrl = (String) i.next();
1164:                    index = appendSkin(skinRepo, skinUrl, doc, index,
1165:                            skinsElement, "/portal.css");
1166:                }
1167:
1168:                if (index == 1) {
1169:                    String skinUrl = ServerConfigurationService
1170:                            .getString("skin.default");
1171:                    index = appendSkin(skinRepo, skinUrl, doc, index,
1172:                            skinsElement, "/portal.css");
1173:                }
1174:
1175:                return skinsElement;
1176:            }
1177:
1178:            protected Element createToolSkinsXml(Document doc, List skins) {
1179:                Element skinsElement = doc.createElement("toolSkins");
1180:                int index = 0;
1181:
1182:                String skinRepo = ServerConfigurationService
1183:                        .getString("skin.repo");
1184:
1185:                Element skin = doc.createElement("skin");
1186:                skin = doc.createElement("skin");
1187:                skin.setAttribute("order", index + "");
1188:                safeAppendTextNode(doc, skin, getContext()
1189:                        + "/library/skin/tool_base.css", true);
1190:                skinsElement.appendChild(skin);
1191:                index++;
1192:
1193:                for (Iterator i = skins.iterator(); i.hasNext();) {
1194:                    String skinUrl = (String) i.next();
1195:                    index = appendSkin(skinRepo, skinUrl, doc, index,
1196:                            skinsElement, "/tool.css");
1197:                }
1198:
1199:                if (index == 2) {
1200:                    String skinUrl = ServerConfigurationService
1201:                            .getString("skin.default");
1202:                    index = appendSkin(skinRepo, skinUrl, doc, index,
1203:                            skinsElement, "/tool.css");
1204:                }
1205:
1206:                return skinsElement;
1207:            }
1208:
1209:            protected int appendSkin(String skinRepo, String skinUrl,
1210:                    Document doc, int index, Element skinsElement,
1211:                    String cssFile) {
1212:                Element skin;
1213:                String skinPortalUrl = skinRepo + "/" + skinUrl + cssFile;
1214:                skin = doc.createElement("skin");
1215:                skin.setAttribute("order", index + "");
1216:                safeAppendTextNode(doc, skin, skinPortalUrl, true);
1217:                skinsElement.appendChild(skin);
1218:                index++;
1219:                return index;
1220:            }
1221:
1222:            protected List getSkins(SiteType siteType, Site site) {
1223:                List skins = new ArrayList();
1224:
1225:                if (siteType != null && siteType.getSkin() != null) {
1226:                    skins.add(siteType.getSkin());
1227:                }
1228:
1229:                if (site != null && site.getSkin() != null) {
1230:                    skins.add(site.getSkin());
1231:                }
1232:
1233:                return skins;
1234:            }
1235:
1236:            protected Element createUserXml(Document doc, User current) {
1237:                Element user = doc.createElement("currentUser");
1238:
1239:                appendTextElementNode(doc, "id", current.getId(), user);
1240:                appendTextElementNode(doc, "first", current.getFirstName(),
1241:                        user);
1242:                appendTextElementNode(doc, "last", current.getLastName(), user);
1243:                appendTextElementNode(doc, "email", current.getEmail(), user);
1244:
1245:                return user;
1246:            }
1247:
1248:            protected Element createLoginXml(Document doc,
1249:                    HttpServletRequest req) {
1250:                Element login = doc.createElement("loginInfo");
1251:
1252:                String logInOutUrl = Web.serverUrl(req);
1253:                String loginText = null;
1254:                String logoutText = null;
1255:                String image1 = null;
1256:
1257:                // for a possible second link
1258:                String logInOutUrl2 = null;
1259:                String loginText2 = null;
1260:                String image2 = null;
1261:
1262:                boolean topLogin = Boolean.TRUE.toString().equalsIgnoreCase(
1263:                        ServerConfigurationService.getString("top.login"));
1264:                boolean containerLogin = Boolean.TRUE.toString()
1265:                        .equalsIgnoreCase(
1266:                                ServerConfigurationService
1267:                                        .getString("container.login"));
1268:                if (containerLogin)
1269:                    topLogin = false;
1270:
1271:                if (!topLogin) {
1272:                    logInOutUrl += getPortalString() + "/login";
1273:
1274:                    // let the login url be overridden by configuration
1275:                    String overrideLoginUrl = StringUtil
1276:                            .trimToNull(ServerConfigurationService
1277:                                    .getString("login.url"));
1278:                    if (overrideLoginUrl != null)
1279:                        logInOutUrl = overrideLoginUrl;
1280:
1281:                    // check for a login text override
1282:                    loginText = StringUtil
1283:                            .trimToNull(ServerConfigurationService
1284:                                    .getString("login.text"));
1285:                    if (loginText == null)
1286:                        loginText = rbsitenav.getString("log.login");
1287:
1288:                    // check for an image for the login
1289:                    image1 = StringUtil.trimToNull(ServerConfigurationService
1290:                            .getString("login.icon"));
1291:
1292:                    //    check for a possible second, xlogin link
1293:                    if (Boolean.TRUE.toString().equalsIgnoreCase(
1294:                            ServerConfigurationService
1295:                                    .getString("xlogin.enabled"))) {
1296:                        // get the text and image as configured
1297:                        loginText2 = StringUtil
1298:                                .trimToNull(ServerConfigurationService
1299:                                        .getString("xlogin.text"));
1300:                        image2 = StringUtil
1301:                                .trimToNull(ServerConfigurationService
1302:                                        .getString("xlogin.icon"));
1303:                        logInOutUrl2 = ServerConfigurationService
1304:                                .getString("portalPath")
1305:                                + "/xlogin";
1306:
1307:                        appendTextElementNode(doc, "logInOutUrl2",
1308:                                logInOutUrl2, login);
1309:                        appendTextElementNode(doc, "loginText2", loginText2,
1310:                                login);
1311:                        appendTextElementNode(doc, "image2", image2, login);
1312:                    }
1313:                }
1314:
1315:                // check for a logout text override
1316:                logoutText = StringUtil.trimToNull(ServerConfigurationService
1317:                        .getString("logout.text"));
1318:                if (logoutText == null)
1319:                    logoutText = rbsitenav.getString("sit_log");
1320:
1321:                appendTextElementNode(doc, "topLogin", Boolean
1322:                        .toString(topLogin), login);
1323:                appendTextElementNode(doc, "logInOutUrl", logInOutUrl, login);
1324:                appendTextElementNode(doc, "loginText", loginText, login);
1325:                appendTextElementNode(doc, "logoutText", logoutText, login);
1326:                appendTextElementNode(doc, "image1", image1, login);
1327:
1328:                return login;
1329:            }
1330:
1331:            protected String checkVisitSite(String siteId, Session session,
1332:                    HttpServletRequest req, HttpServletResponse res)
1333:                    throws IOException, ToolException {
1334:                // default site if not set
1335:                if (siteId == null) {
1336:                    if (session.getUserId() == null) {
1337:                        siteId = ServerConfigurationService.getGatewaySiteId();
1338:                    } else {
1339:                        siteId = SiteService.getUserSiteId(session.getUserId());
1340:                    }
1341:                }
1342:
1343:                // find the site, for visiting
1344:                Site site = null;
1345:                try {
1346:                    site = getSiteVisit(siteId);
1347:                } catch (IdUnusedException e) {
1348:                    doError(req, res, session, ERROR_SITE);
1349:                    return null;
1350:                } catch (PermissionException e) {
1351:                    // if not logged in, give them a chance
1352:                    if (session.getUserId() == null) {
1353:                        doLogin(req, res, session, req.getPathInfo(), false);
1354:                    } else {
1355:                        doError(req, res, session, ERROR_SITE);
1356:                    }
1357:                    return null;
1358:                }
1359:
1360:                return site.getId();
1361:            }
1362:
1363:            public void init(ServletConfig config) throws ServletException {
1364:                super .init(config);
1365:                setPortalManager((PortalManager) ComponentManager
1366:                        .get(PortalManager.class.getName() + ".tx"));
1367:                try {
1368:                    setDocumentBuilder(DocumentBuilderFactory.newInstance()
1369:                            .newDocumentBuilder());
1370:                    String transformPath = config.getInitParameter("transform");
1371:                    Templates templates = createTemplate(config, transformPath);
1372:                    setTemplates(templates);
1373:
1374:                    String transformToolCategoryPath = config
1375:                            .getInitParameter("transformToolCategory");
1376:                    Templates templatesToolCategory = createTemplate(config,
1377:                            transformToolCategoryPath);
1378:                    setServletResolver(new PortalResourceUriResolver(
1379:                            getPortalManager(), config.getServletContext()));
1380:                    setToolCategoryTemplates(templatesToolCategory);
1381:
1382:                    categoryBasePath = config
1383:                            .getInitParameter("categoryBasePath");
1384:                    if (categoryBasePath == null
1385:                            || categoryBasePath.length() == 0) {
1386:                        categoryBasePath = "/WEB-INF/category";
1387:                    }
1388:
1389:                    Collection categoriesNeedingLoading = getPortalManager()
1390:                            .getCategoriesInNeedOfFiles();
1391:
1392:                    if (categoriesNeedingLoading != null) {
1393:                        for (Iterator<ToolCategory> i = categoriesNeedingLoading
1394:                                .iterator(); i.hasNext();) {
1395:                            processCategoryFiles(i.next(), config
1396:                                    .getServletContext());
1397:                        }
1398:
1399:                        getPortalManager().saveToolCategories(
1400:                                categoriesNeedingLoading);
1401:                    }
1402:                } catch (ParserConfigurationException e) {
1403:                    throw new ServletException(e);
1404:                } catch (TransformerConfigurationException e) {
1405:                    throw new ServletException(e);
1406:                } catch (MalformedURLException e) {
1407:                    throw new ServletException(e);
1408:                } catch (IOException e) {
1409:                    throw new ServletException(e);
1410:                }
1411:            }
1412:
1413:            protected void processCategoryFiles(ToolCategory toolCategory,
1414:                    ServletContext context) throws IOException {
1415:                File base = new File(categoryBasePath);
1416:                String parentPath = base.getParent();
1417:
1418:                Set paths = context.getResourcePaths(parentPath);
1419:
1420:                for (Iterator<String> i = paths.iterator(); i.hasNext();) {
1421:                    String path = i.next();
1422:                    if (path.startsWith(categoryBasePath)) {
1423:                        path += toolCategory.getHomePagePath();
1424:                        if (context.getResource(path) != null) {
1425:                            processCategoryFile(toolCategory, path, context
1426:                                    .getResourceAsStream(path));
1427:                        }
1428:                    }
1429:                }
1430:            }
1431:
1432:            protected void processCategoryFile(ToolCategory toolCategory,
1433:                    String path, InputStream is) throws IOException {
1434:                String locale = path.substring(categoryBasePath.length());
1435:                locale = locale.substring(0, locale.length()
1436:                        - toolCategory.getHomePagePath().length() - 1);
1437:
1438:                if (locale.startsWith("_")) {
1439:                    locale = locale.substring(1);
1440:                }
1441:
1442:                ByteArrayOutputStream bos = new ByteArrayOutputStream();
1443:                int c = is.read();
1444:                while (c != -1) {
1445:                    bos.write(c);
1446:                    c = is.read();
1447:                }
1448:                bos.flush();
1449:                toolCategory.getPages().put(locale, bos.toByteArray());
1450:            }
1451:
1452:            private Templates createTemplate(ServletConfig config,
1453:                    String transformPath) throws MalformedURLException,
1454:                    TransformerConfigurationException {
1455:                InputStream stream = config.getServletContext()
1456:                        .getResourceAsStream(transformPath);
1457:                URL url = config.getServletContext().getResource(transformPath);
1458:                String urlPath = url.toString();
1459:                String systemId = urlPath.substring(0,
1460:                        urlPath.lastIndexOf('/') + 1);
1461:                Templates templates = TransformerFactory.newInstance()
1462:                        .newTemplates(new StreamSource(stream, systemId));
1463:                return templates;
1464:            }
1465:
1466:            public PortalManager getPortalManager() {
1467:                return portalManager;
1468:            }
1469:
1470:            public void setPortalManager(PortalManager portalManager) {
1471:                this .portalManager = portalManager;
1472:            }
1473:
1474:            protected String getContext() {
1475:                return "/" + this .getServletContext().getServletContextName();
1476:            }
1477:
1478:            public DocumentBuilder getDocumentBuilder() {
1479:                return documentBuilder;
1480:            }
1481:
1482:            public void setDocumentBuilder(DocumentBuilder documentBuilder) {
1483:                this .documentBuilder = documentBuilder;
1484:            }
1485:
1486:            protected Transformer getToolCategoryTransformer() {
1487:                try {
1488:                    Transformer trans = getToolCategoryTemplates()
1489:                            .newTransformer();
1490:                    trans.setURIResolver(getServletResolver());
1491:                    return trans;
1492:                } catch (TransformerConfigurationException e) {
1493:                    throw new RuntimeException(e);
1494:                }
1495:            }
1496:
1497:            public Transformer getTransformer() {
1498:                try {
1499:                    Transformer trans = getTemplates().newTransformer();
1500:                    trans.setURIResolver(getServletResolver());
1501:                    return trans;
1502:                } catch (TransformerConfigurationException e) {
1503:                    throw new RuntimeException(e);
1504:                }
1505:            }
1506:
1507:            public Templates getTemplates() {
1508:                return templates;
1509:            }
1510:
1511:            public void setTemplates(Templates templates) {
1512:                this .templates = templates;
1513:            }
1514:
1515:            public Templates getToolCategoryTemplates() {
1516:                return toolCategoryTemplates;
1517:            }
1518:
1519:            public void setToolCategoryTemplates(Templates toolCategoryTemplates) {
1520:                this .toolCategoryTemplates = toolCategoryTemplates;
1521:            }
1522:
1523:            public URIResolver getServletResolver() {
1524:                return servletResolver;
1525:            }
1526:
1527:            public void setServletResolver(URIResolver servletResolver) {
1528:                this .servletResolver = servletResolver;
1529:            }
1530:
1531:            protected void dumpDocument(Node node) {
1532:                try {
1533:                    Transformer transformer = TransformerFactory.newInstance()
1534:                            .newTransformer();
1535:                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
1536:                    transformer.transform(new DOMSource(node),
1537:                            new StreamResult(System.out));
1538:                } catch (Exception e) {
1539:                    e.printStackTrace();
1540:                }
1541:            }
1542:
1543:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.