Source Code Cross Referenced for IFrameAction.java in  » ERP-CRM-Financial » sakai » org » sakaiproject » web » tool » 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.sakaiproject.web.tool 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**********************************************************************************
002:         * $URL: https://source.sakaiproject.org/svn/web/tags/sakai_2-4-1/web-tool/tool/src/java/org/sakaiproject/web/tool/IFrameAction.java $
003:         * $Id: IFrameAction.java 18589 2006-12-01 07:14:55Z joshua.ryan@asu.edu $
004:         ***********************************************************************************
005:         *
006:         * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007:         * 
008:         * Licensed under the Educational Community License, Version 1.0 (the "License"); 
009:         * you may not use this file except in compliance with the License. 
010:         * You may obtain a copy of the License at
011:         * 
012:         *      http://www.opensource.org/licenses/ecl1.php
013:         * 
014:         * Unless required by applicable law or agreed to in writing, software 
015:         * distributed under the License is distributed on an "AS IS" BASIS, 
016:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
017:         * See the License for the specific language governing permissions and 
018:         * limitations under the License.
019:         *
020:         **********************************************************************************/package org.sakaiproject.web.tool;
021:
022:        import java.util.ArrayList;
023:        import java.util.Iterator;
024:        import java.util.Properties;
025:        import java.util.Collections;
026:        import java.net.URLEncoder;
027:
028:        import org.sakaiproject.authz.api.AuthzGroup;
029:        import org.sakaiproject.authz.api.GroupNotDefinedException;
030:        import org.sakaiproject.authz.api.Role;
031:        import org.sakaiproject.authz.cover.AuthzGroupService;
032:        import org.sakaiproject.cheftool.Context;
033:        import org.sakaiproject.cheftool.JetspeedRunData;
034:        import org.sakaiproject.cheftool.RunData;
035:        import org.sakaiproject.cheftool.VelocityPortlet;
036:        import org.sakaiproject.cheftool.VelocityPortletPaneledAction;
037:        import org.sakaiproject.component.cover.ServerConfigurationService;
038:        import org.sakaiproject.entity.api.Reference;
039:        import org.sakaiproject.entity.cover.EntityManager;
040:        import org.sakaiproject.event.api.SessionState;
041:        import org.sakaiproject.exception.IdUnusedException;
042:        import org.sakaiproject.site.api.Site;
043:        import org.sakaiproject.site.api.SitePage;
044:        import org.sakaiproject.site.api.ToolConfiguration;
045:        import org.sakaiproject.site.cover.SiteService;
046:        import org.sakaiproject.tool.api.Placement;
047:        import org.sakaiproject.tool.api.Session;
048:        import org.sakaiproject.tool.api.ToolSession;
049:        import org.sakaiproject.tool.cover.SessionManager;
050:        import org.sakaiproject.tool.cover.ToolManager;
051:        import org.sakaiproject.util.ResourceLoader;
052:        import org.sakaiproject.util.StringUtil;
053:        import org.sakaiproject.user.api.User;
054:        import org.sakaiproject.user.api.UserNotDefinedException;
055:        import org.sakaiproject.user.cover.UserDirectoryService;
056:
057:        /**
058:         * <p>
059:         * IFrameAction is the Sakai tool to place any web content in an IFrame on the page.
060:         * </p>
061:         * <p>
062:         * Three special modes are supported - these pick the URL content from special places:
063:         * </p>
064:         * <ul>
065:         * <li>"site" - to show the services "server.info.url" configuration URL setting</li>
066:         * <li>"workspace" - to show the configured "myworkspace.info.url" URL, introducing a my workspace to users</li>
067:         * <li>"worksite" - to show the current site's "getInfoUrlFull()" setting</li>
068:         * </ul>
069:         */
070:        public class IFrameAction extends VelocityPortletPaneledAction {
071:
072:            /** Resource bundle using current language locale */
073:            protected static ResourceLoader rb = new ResourceLoader("iframe");
074:
075:            /** The source URL, in state, config and context. */
076:            protected final static String SOURCE = "source";
077:
078:            /** The value in state and context for the source URL to actually used, as computed from special and URL. */
079:            protected final static String URL = "url";
080:
081:            /** The height, in state, config and context. */
082:            protected final static String HEIGHT = "height";
083:
084:            /** The custom height from user input * */
085:            protected final static String CUSTOM_HEIGHT = "customNumberField";
086:
087:            /** The special attribute, in state, config and context. */
088:            protected final static String SPECIAL = "special";
089:
090:            /** Support an external url defined in sakai.properties, in state, config and context. */
091:            protected final static String SAKAI_PROPERTIES_URL_KEY = "sakai.properties.url.key";
092:
093:            /** If set, always hide the OPTIONS button */
094:            protected final static String HIDE_OPTIONS = "hide.options";
095:
096:            /** Special value for site. */
097:            protected final static String SPECIAL_SITE = "site";
098:
099:            /** Special value for myworkspace. */
100:            protected final static String SPECIAL_WORKSPACE = "workspace";
101:
102:            /** Special value for worksite. */
103:            protected final static String SPECIAL_WORKSITE = "worksite";
104:
105:            /** The title, in state and context. */
106:            protected final static String TITLE = "title";
107:
108:            /**
109:             * Whether to pass through the PID to the URL displayed in the IFRAME. This enables integration in that the application in the IFRAME will know what site and tool it is part of.
110:             */
111:            private final static String PASS_PID = "passthroughPID";
112:
113:            /** Valid digits for custom height from user input **/
114:            protected static final String VALID_DIGITS = "0123456789";
115:
116:            /** Choices of pixels displayed in the customization page */
117:            public String[] ourPixels = { "300px", "450px", "600px", "750px",
118:                    "900px", "1200px", "1800px", "2400px" };
119:
120:            /** Attributes for web content tool page title **/
121:            private static final String STATE_PAGE_TITLE = "pageTitle";
122:
123:            private static final String FORM_PAGE_TITLE = "title-of-page";
124:
125:            private static final String FORM_TOOL_TITLE = "title-of-tool";
126:
127:            /**
128:             * Expand macros to insert session information into the URL?
129:             */
130:            private final static String MACRO_EXPANSION = "expandMacros";
131:
132:            /** Macro name: Site id (GUID) */
133:            protected static final String MACRO_SITE_ID = "${SITE_ID}";
134:            /** Macro name: User id */
135:            protected static final String MACRO_USER_ID = "${USER_ID}";
136:            /** Macro name: User enterprise id */
137:            protected static final String MACRO_USER_EID = "${USER_EID}";
138:            /** Macro name: First name */
139:            protected static final String MACRO_USER_FIRST_NAME = "${USER_FIRST_NAME}";
140:            /** Macro name: Last name */
141:            protected static final String MACRO_USER_LAST_NAME = "${USER_LAST_NAME}";
142:            /** Macro name: Role */
143:            protected static final String MACRO_USER_ROLE = "${USER_ROLE}";
144:            /** Macro name: Session */
145:            protected static final String MACRO_SESSION_ID = "${SESSION_ID}";
146:
147:            private static final String MACRO_CLASS_SITE_PROP = "SITE_PROP:";
148:
149:            private static final String IFRAME_ALLOWED_MACROS_PROPERTY = "iframe.allowed.macros";
150:
151:            private static final String MACRO_DEFAULT_ALLOWED = "${USER_ID},${USER_EID},${USER_FIRST_NAME},${USER_LAST_NAME},${SITE_ID},${USER_ROLE},${SESSION_ID}";
152:
153:            private static ArrayList allowedMacrosList;
154:
155:            /**
156:             * Populate the state with configuration settings
157:             */
158:            protected void initState(SessionState state,
159:                    VelocityPortlet portlet, JetspeedRunData rundata) {
160:                // TODO: we might want to keep this from running for each request - but by letting it we get fresh info each time... -ggolden
161:                super .initState(state, portlet, rundata);
162:
163:                Placement placement = ToolManager.getCurrentPlacement();
164:                Properties config = placement.getConfig();
165:
166:                // set the pass_pid parameter
167:                boolean passPid = false;
168:                String passPidStr = config.getProperty(PASS_PID, "false");
169:                state.removeAttribute(PASS_PID);
170:                if ("true".equalsIgnoreCase(passPidStr)) {
171:                    state.setAttribute(PASS_PID, Boolean.TRUE);
172:                    passPid = true;
173:                }
174:
175:                // Assume macro expansion (disable on request)
176:                boolean macroExpansion = true;
177:                String macroExpansionStr = config.getProperty(MACRO_EXPANSION,
178:                        "true");
179:
180:                state.removeAttribute(MACRO_EXPANSION);
181:                if ("false".equalsIgnoreCase(macroExpansionStr)) {
182:                    state.setAttribute(MACRO_EXPANSION, Boolean.FALSE);
183:                    macroExpansion = false;
184:                }
185:
186:                // set the special setting
187:                String special = config.getProperty(SPECIAL);
188:
189:                // initialize list of approved macros for replacement within URL
190:                if (allowedMacrosList == null) {
191:
192:                    allowedMacrosList = new ArrayList();
193:
194:                    final String allowedMacros = ServerConfigurationService
195:                            .getString(IFRAME_ALLOWED_MACROS_PROPERTY,
196:                                    MACRO_DEFAULT_ALLOWED);
197:
198:                    String parts[] = allowedMacros.split(",");
199:
200:                    if (parts != null) {
201:
202:                        for (int i = 0; i < parts.length; i++) {
203:
204:                            allowedMacrosList.add(parts[i]);
205:
206:                        }
207:
208:                    }
209:
210:                }
211:
212:                final String sakaiPropertiesUrlKey = config
213:                        .getProperty(SAKAI_PROPERTIES_URL_KEY);
214:
215:                final String hideOptions = config.getProperty(HIDE_OPTIONS);
216:
217:                // check for an older way the ChefWebPagePortlet took parameters, converting to our "special" values
218:                if (special == null) {
219:                    if ("true".equals(config.getProperty("site"))) {
220:                        special = SPECIAL_SITE;
221:                    } else if ("true".equals(config.getProperty("workspace"))) {
222:                        special = SPECIAL_WORKSPACE;
223:                    } else if ("true".equals(config.getProperty("worksite"))) {
224:                        special = SPECIAL_WORKSITE;
225:                    }
226:                }
227:
228:                state.removeAttribute(SPECIAL);
229:                if ((special != null) && (special.trim().length() > 0)) {
230:                    state.setAttribute(SPECIAL, special);
231:                }
232:
233:                state.removeAttribute(HIDE_OPTIONS);
234:                if ((hideOptions != null) && (hideOptions.trim().length() > 0)) {
235:                    state.setAttribute(HIDE_OPTIONS, hideOptions);
236:                }
237:
238:                // set the source url setting
239:                String source = StringUtil.trimToNull(config
240:                        .getProperty(SOURCE));
241:
242:                // check for an older way the ChefWebPagePortlet took parameters, converting to our "source" value
243:                if (source == null) {
244:                    source = StringUtil.trimToNull(config.getProperty("url"));
245:                }
246:
247:                // store the raw as-configured source url
248:                state.removeAttribute(SOURCE);
249:                if (source != null) {
250:                    state.setAttribute(SOURCE, source);
251:                }
252:
253:                // compute working URL, modified from the configuration URL if special
254:                String url = sourceUrl(special, source, placement.getContext(),
255:                        macroExpansion, passPid, placement.getId(),
256:                        sakaiPropertiesUrlKey);
257:                state.setAttribute(URL, url);
258:
259:                // set the height
260:                state.setAttribute(HEIGHT, config.getProperty(HEIGHT, "600px"));
261:
262:                // set the title
263:                state.setAttribute(TITLE, placement.getTitle());
264:
265:                if (state.getAttribute(STATE_PAGE_TITLE) == null) {
266:                    SitePage p = SiteService.findPage(getCurrentSitePageId());
267:                    state.setAttribute(STATE_PAGE_TITLE, p.getTitle());
268:                }
269:
270:            }
271:
272:            /**
273:             * Get the current site page our current tool is placed on.
274:             * 
275:             * @return The site page id on which our tool is placed.
276:             */
277:            protected String getCurrentSitePageId() {
278:                ToolSession ts = SessionManager.getCurrentToolSession();
279:                if (ts != null) {
280:                    ToolConfiguration tool = SiteService.findTool(ts
281:                            .getPlacementId());
282:                    if (tool != null) {
283:                        return tool.getPageId();
284:                    }
285:                }
286:
287:                return null;
288:            }
289:
290:            /**
291:             * Compute the actual URL we will used, based on the configuration special and source URLs
292:             */
293:            protected String sourceUrl(String special, String source,
294:                    String context, boolean macroExpansion, boolean passPid,
295:                    String pid, String sakaiPropertiesUrlKey) {
296:                String rv = StringUtil.trimToNull(source);
297:
298:                // if marked for "site", use the site intro from the properties
299:                if (SPECIAL_SITE.equals(special)) {
300:                    // set the url to the site config'ed url
301:                    rv = StringUtil.trimToNull(ServerConfigurationService
302:                            .getString("server.info.url"));
303:                }
304:
305:                // if marked for "workspace", use the "user" site info from the properties
306:                else if (SPECIAL_WORKSPACE.equals(special)) {
307:                    // set the url to the site config'ed url
308:                    rv = StringUtil.trimToNull(ServerConfigurationService
309:                            .getString("myworkspace.info.url"));
310:                }
311:
312:                // if marked for "worksite", use the setting from the site's definition
313:                else if (SPECIAL_WORKSITE.equals(special)) {
314:                    // set the url to the site of this request's config'ed url
315:                    try {
316:                        // get the site's info URL, if defined
317:                        Site s = SiteService.getSite(context);
318:                        rv = StringUtil.trimToNull(s.getInfoUrlFull());
319:
320:                        // compute the info url for the site if it has no specific InfoUrl
321:                        if (rv == null) {
322:                            // access will show the site description or title...
323:                            rv = ServerConfigurationService.getAccessUrl()
324:                                    + s.getReference();
325:                        }
326:                    } catch (Exception any) {
327:                    }
328:                }
329:
330:                else if (sakaiPropertiesUrlKey != null
331:                        && sakaiPropertiesUrlKey.length() > 1) {
332:                    // set the url to a string defined in sakai.properties
333:                    rv = StringUtil.trimToNull(ServerConfigurationService
334:                            .getString(sakaiPropertiesUrlKey));
335:                }
336:
337:                // if it's not special, and we have no value yet, set it to the webcontent instruction page, as configured
338:                if (rv == null) {
339:                    rv = StringUtil.trimToNull(ServerConfigurationService
340:                            .getString("webcontent.instructions.url"));
341:                }
342:
343:                if (rv != null) {
344:                    // accept a partial reference url (i.e. "/content/group/sakai/test.gif"), convert to full url
345:                    rv = convertReferenceUrl(rv);
346:
347:                    // pass the PID through on the URL, IF configured to do so
348:                    if (passPid) {
349:                        if (rv.indexOf("?") < 0) {
350:                            rv = rv + "?";
351:                        } else {
352:                            rv = rv + "&";
353:                        }
354:
355:                        rv = rv + "pid=" + pid;
356:                    }
357:
358:                    if (macroExpansion) {
359:                        rv = doMacroExpansion(rv);
360:                    }
361:                }
362:
363:                return rv;
364:            }
365:
366:            /**
367:             * If the url is a valid reference, convert it to a URL, else return it unchanged.
368:             */
369:            protected String convertReferenceUrl(String url) {
370:                // make a reference
371:                Reference ref = EntityManager.newReference(url);
372:
373:                // if it didn't recognize this, return it unchanged
374:                if (!ref.isKnownType())
375:                    return url;
376:
377:                // return the reference's url
378:                return ref.getUrl();
379:            }
380:
381:            /**
382:             * Get the current user id
383:             * @throws SessionDataException
384:             * @return User id
385:             */
386:            private String getUserId() throws SessionDataException {
387:                Session session = SessionManager.getCurrentSession();
388:
389:                if (session == null) {
390:                    throw new SessionDataException("No current user session");
391:                }
392:                return session.getUserId();
393:            }
394:
395:            /**
396:             * Get the current session id
397:             * @throws SessionDataException
398:             * @return Session id
399:             */
400:            private String getSessionId() throws SessionDataException {
401:                Session session = SessionManager.getCurrentSession();
402:
403:                if (session == null) {
404:                    throw new SessionDataException("No current user session");
405:                }
406:                return session.getId();
407:            }
408:
409:            /**
410:             * Get the current user eid
411:             * @throws SessionDataException
412:             * @return User eid
413:             */
414:            private String getUserEid() throws SessionDataException {
415:                Session session = SessionManager.getCurrentSession();
416:
417:                if (session == null) {
418:                    throw new SessionDataException("No current user session");
419:                }
420:                return session.getUserEid();
421:            }
422:
423:            /**
424:             * Get current User information
425:             * @throws IdUnusedException, SessionDataException
426:             * @return {@link User} data
427:             * @throws UserNotDefinedException 
428:             */
429:            private User getUser() throws IdUnusedException,
430:                    SessionDataException, UserNotDefinedException {
431:
432:                return UserDirectoryService.getUser(this .getUserId());
433:            }
434:
435:            /**
436:             * Get the current site id
437:             * @throws SessionDataException
438:             * @return Site id (GUID)
439:             */
440:            private String getSiteId() throws SessionDataException {
441:                Placement placement = ToolManager.getCurrentPlacement();
442:
443:                if (placement == null) {
444:                    throw new SessionDataException("No current tool placement");
445:                }
446:                return placement.getContext();
447:            }
448:
449:            /**
450:             * Fetch the user role in the current site
451:             * @throws IdUnusedException, SessionDataException
452:             * @return Role
453:             * @throws GroupNotDefinedException 
454:             */
455:            private String getUserRole() throws IdUnusedException,
456:                    SessionDataException, GroupNotDefinedException {
457:                AuthzGroup group;
458:                Role role;
459:
460:                group = AuthzGroupService.getAuthzGroup("/site/" + getSiteId());
461:                if (group == null) {
462:                    throw new SessionDataException("No current group");
463:                }
464:
465:                role = group.getUserRole(this .getUserId());
466:                if (role == null) {
467:                    throw new SessionDataException("No current role");
468:                }
469:                return role.getId();
470:            }
471:
472:            /**
473:             * Get a site property by name
474:             *
475:             * @param name Property name
476:             * @throws IdUnusedException, SessionDataException
477:             * @return The property value (null if none)
478:             */
479:            private String getSiteProperty(String name)
480:                    throws IdUnusedException, SessionDataException {
481:                Site site;
482:
483:                site = SiteService.getSite(getSiteId());
484:                return site.getProperties().getProperty(name);
485:            }
486:
487:            /**
488:             * Lookup value for requested macro name
489:             */
490:            private String getMacroValue(String macroName) {
491:                try {
492:                    if (macroName.equals(MACRO_USER_ID)) {
493:                        return this .getUserId();
494:                    }
495:                    if (macroName.equals(MACRO_USER_EID)) {
496:                        return this .getUserEid();
497:                    }
498:                    if (macroName.equals(MACRO_USER_FIRST_NAME)) {
499:                        return this .getUser().getFirstName();
500:                    }
501:                    if (macroName.equals(MACRO_USER_LAST_NAME)) {
502:                        return this .getUser().getLastName();
503:                    }
504:
505:                    if (macroName.equals(MACRO_SITE_ID)) {
506:                        return getSiteId();
507:                    }
508:                    if (macroName.equals(MACRO_USER_ROLE)) {
509:                        return this .getUserRole();
510:                    }
511:                    if (macroName.equals(MACRO_SESSION_ID)) {
512:                        return this .getSessionId();
513:                    }
514:
515:                    if (macroName.startsWith("${" + MACRO_CLASS_SITE_PROP)) {
516:                        macroName = macroName.substring(2); // Remove leading "${"
517:                        macroName = macroName.substring(0,
518:                                macroName.length() - 1); // Remove trailing "}" 
519:
520:                        // at this point we have "SITE_PROP:some-property-name"
521:                        // separate the property name from the prefix then return the property value
522:                        String[] sitePropertyKey = macroName.split(":");
523:
524:                        if (sitePropertyKey != null
525:                                && sitePropertyKey.length > 1) {
526:
527:                            String sitePropertyValue = getSiteProperty(sitePropertyKey[1]);
528:
529:                            return (sitePropertyValue == null) ? ""
530:                                    : sitePropertyValue;
531:
532:                        }
533:                    }
534:                } catch (Throwable throwable) {
535:                    return "";
536:                }
537:                /*
538:                 * An unsupported macro: use the original text "as is"
539:                 */
540:                return macroName;
541:            }
542:
543:            /**
544:             * Expand one macro reference
545:             * @param text Expand macros found in this text
546:             * @param macroName Macro name
547:             */
548:            private void expand(StringBuffer sb, String macroName) {
549:                int index;
550:
551:                /*
552:                 * Replace every occurance of the macro in the parameter list
553:                 */
554:                index = sb.indexOf(macroName);
555:                while (index != -1) {
556:                    String macroValue = URLEncoder
557:                            .encode(getMacroValue(macroName));
558:
559:                    sb.replace(index, (index + macroName.length()), macroValue);
560:                    index = sb
561:                            .indexOf(macroName, (index + macroValue.length()));
562:                }
563:            }
564:
565:            /**
566:             * Expand macros, inserting session and site information
567:             * @param originalText Expand macros found in this text
568:             * @return [possibly] Updated text
569:             */
570:            private String doMacroExpansion(String originalText) {
571:                StringBuffer sb;
572:
573:                /*
574:                 * Quit now if no macros are embedded in the text
575:                 */
576:                if (originalText.indexOf("${") == -1) {
577:                    return originalText;
578:                }
579:                /*
580:                 * Expand each macro
581:                 */
582:                sb = new StringBuffer(originalText);
583:
584:                Iterator i = allowedMacrosList.iterator();
585:
586:                while (i.hasNext()) {
587:
588:                    String macro = (String) i.next();
589:
590:                    expand(sb, macro);
591:
592:                }
593:
594:                return sb.toString();
595:            }
596:
597:            /**
598:             * Setup the velocity context and choose the template for the response.
599:             */
600:            public String buildMainPanelContext(VelocityPortlet portlet,
601:                    Context context, RunData rundata, SessionState state) {
602:                // do options if we are in options mode
603:                if (MODE_OPTIONS.equals(state.getAttribute(STATE_MODE))) {
604:                    return buildOptionsPanelContext(portlet, context, rundata,
605:                            state);
606:                }
607:
608:                // if we rely on state (like all the other tools), we won't pick up any changes others make to the configuration till we are refreshed... -ggolden
609:
610:                // set our configuration into the context for the vm
611:                context.put(URL, (String) state.getAttribute(URL));
612:                context.put(HEIGHT, state.getAttribute(HEIGHT));
613:
614:                // set the resource bundle with our strings
615:                context.put("tlang", rb);
616:
617:                // setup for the options menu if needed
618:
619:                String hideOptions = (String) state.getAttribute(HIDE_OPTIONS);
620:
621:                if (hideOptions != null && "true".equalsIgnoreCase(hideOptions)) {
622:                    // always hide Options menu if hide.options is specified
623:                } else if (SiteService.allowUpdateSite(ToolManager
624:                        .getCurrentPlacement().getContext()))
625:
626:                {
627:                    context.put("options_title", ToolManager
628:                            .getCurrentPlacement().getTitle()
629:                            + " " + rb.getString("gen.options"));
630:                }
631:
632:                return (String) getContext(rundata).get("template");
633:            }
634:
635:            /**
636:             * Setup the velocity context and choose the template for options.
637:             */
638:            public String buildOptionsPanelContext(VelocityPortlet portlet,
639:                    Context context, RunData data, SessionState state) {
640:                // provide the source, and let the user edit, if not special
641:                String special = (String) state.getAttribute(SPECIAL);
642:                if (special == null) {
643:                    String source = (String) state.getAttribute(SOURCE);
644:                    if (source == null)
645:                        source = "";
646:                    context.put(SOURCE, source);
647:                    context.put("heading", rb.getString("gen.custom"));
648:                }
649:
650:                // set the heading based on special
651:                else {
652:                    if (SPECIAL_SITE.equals(special)) {
653:                        context.put("heading", rb.getString("gen.custom.site"));
654:                    }
655:
656:                    else if (SPECIAL_WORKSPACE.equals(special)) {
657:                        context.put("heading", rb
658:                                .getString("gen.custom.workspace"));
659:                    }
660:
661:                    else if (SPECIAL_WORKSITE.equals(special)) {
662:                        context.put("heading", rb
663:                                .getString("gen.custom.worksite"));
664:
665:                        // for worksite, also include the Site's infourl and description
666:                        try {
667:                            Site s = SiteService.getSite(ToolManager
668:                                    .getCurrentPlacement().getContext());
669:
670:                            String infoUrl = StringUtil.trimToNull(s
671:                                    .getInfoUrl());
672:                            if (infoUrl != null) {
673:                                context.put("info_url", infoUrl);
674:                            }
675:
676:                            String description = StringUtil.trimToNull(s
677:                                    .getDescription());
678:                            if (description != null) {
679:                                context.put("description", description);
680:                            }
681:                        } catch (Throwable e) {
682:                        }
683:                    }
684:
685:                    else {
686:                        context.put("heading", rb.getString("gen.custom"));
687:                    }
688:                }
689:
690:                boolean selected = false;
691:                String height = state.getAttribute(HEIGHT).toString();
692:                for (int i = 0; i < ourPixels.length; i++) {
693:                    if (height.equals(ourPixels[i])) {
694:                        selected = true;
695:                        continue;
696:                    }
697:                }
698:                if (!selected) {
699:                    String[] strings = height.trim().split("px");
700:                    context.put("custom_height", strings[0]);
701:                    height = rb.getString("gen.heisomelse");
702:                }
703:                context.put(HEIGHT, height);
704:
705:                context.put(TITLE, state.getAttribute(TITLE));
706:                context.put("tlang", rb);
707:
708:                context.put("doUpdate", BUTTON + "doConfigure_update");
709:                context.put("doCancel", BUTTON + "doCancel");
710:
711:                context.put("form_tool_title", FORM_TOOL_TITLE);
712:                context.put("form_page_title", FORM_PAGE_TITLE);
713:
714:                // if we are part of a site, and the only tool on the page, offer the popup to edit
715:                Placement placement = ToolManager.getCurrentPlacement();
716:                ToolConfiguration toolConfig = SiteService.findTool(placement
717:                        .getId());
718:                if ((state.getAttribute(SPECIAL) == null)
719:                        && (toolConfig != null)) {
720:                    try {
721:                        Site site = SiteService.getSite(toolConfig.getSiteId());
722:                        SitePage page = site.getPage(toolConfig.getPageId());
723:
724:                        // if this is the only tool on that page, update the page's title also
725:                        if ((page.getTools() != null)
726:                                && (page.getTools().size() == 1)) {
727:                            context.put("showPopup", Boolean.TRUE);
728:                            context.put("popup", Boolean
729:                                    .valueOf(page.isPopUp()));
730:
731:                            context.put("pageTitleEditable", Boolean.TRUE);
732:                            context.put("page_title", (String) state
733:                                    .getAttribute(STATE_PAGE_TITLE));
734:                        }
735:                    } catch (Throwable e) {
736:                    }
737:                }
738:
739:                // pick the "-customize" template based on the standard template name
740:                String template = (String) getContext(data).get("template");
741:
742:                // pick the site customize template if we are in that mode
743:                if (SPECIAL_WORKSITE.equals(special)) {
744:                    template = template + "-site-customize";
745:                } else {
746:                    template = template + "-customize";
747:                }
748:
749:                return template;
750:            }
751:
752:            /**
753:             * Handle the configure context's update button
754:             */
755:            public void doConfigure_update(RunData data, Context context) {
756:                // TODO: if we do limit the initState() calls, we need to make sure we get a new one after this call -ggolden
757:
758:                String peid = ((JetspeedRunData) data).getJs_peid();
759:                SessionState state = ((JetspeedRunData) data)
760:                        .getPortletSessionState(peid);
761:
762:                Placement placement = ToolManager.getCurrentPlacement();
763:
764:                // get the site toolConfiguration, if this is part of a site.
765:                ToolConfiguration toolConfig = SiteService.findTool(placement
766:                        .getId());
767:
768:                // read source if we are not special
769:                if (state.getAttribute(SPECIAL) == null) {
770:                    String source = StringUtil.trimToZero(data.getParameters()
771:                            .getString(SOURCE));
772:                    if ((source != null) && (source.length() > 0)
773:                            && (!source.startsWith("/"))
774:                            && (source.indexOf("://") == -1)) {
775:                        source = "http://" + source;
776:                    }
777:
778:                    // update state
779:                    // state.setAttribute(SOURCE, source);
780:                    placement.getPlacementConfig().setProperty(SOURCE, source);
781:                }
782:
783:                else if (SPECIAL_WORKSITE.equals(state.getAttribute(SPECIAL))) {
784:                    String infoUrl = StringUtil.trimToNull(data.getParameters()
785:                            .getString("infourl"));
786:                    if ((infoUrl != null) && (infoUrl.length() > 0)
787:                            && (!infoUrl.startsWith("/"))
788:                            && (infoUrl.indexOf("://") == -1)) {
789:                        infoUrl = "http://" + infoUrl;
790:                    }
791:                    String description = StringUtil.trimToNull(data
792:                            .getParameters().getString("description"));
793:
794:                    // update the site info
795:                    try {
796:                        SiteService.saveSiteInfo(ToolManager
797:                                .getCurrentPlacement().getContext(),
798:                                description, infoUrl);
799:                    } catch (Throwable e) {
800:                    }
801:                }
802:
803:                // height
804:                String height = data.getParameters().getString(HEIGHT);
805:                if (height.equals(rb.getString("gen.heisomelse"))) {
806:                    String customHeight = data.getParameters().getString(
807:                            CUSTOM_HEIGHT);
808:                    if ((customHeight != null) && (!customHeight.equals(""))) {
809:                        if (!checkDigits(customHeight)) {
810:                            addAlert(state, rb
811:                                    .getString("java.alert.pleentval"));
812:                            return;
813:                        }
814:                        state.setAttribute(HEIGHT, customHeight);
815:                        height = customHeight + "px";
816:                        state.setAttribute(HEIGHT, height);
817:                        placement.getPlacementConfig().setProperty(HEIGHT,
818:                                height);
819:                    } else {
820:                        addAlert(state, rb.getString("java.alert.pleentval"));
821:                        return;
822:                    }
823:                } else {
824:                    state.setAttribute(HEIGHT, height);
825:                    placement.getPlacementConfig().setProperty(HEIGHT, height);
826:                }
827:
828:                // title
829:                String title = data.getParameters().getString(TITLE);
830:                // state.setAttribute(TITLE, title);
831:                if (StringUtil.trimToNull(title) == null) {
832:                    addAlert(state, rb.getString("gen.tootit"));
833:                    return;
834:                }
835:                placement.setTitle(title);
836:
837:                // for web content tool, if it is a site page tool, and the only tool on the page, update the page title / popup.
838:                if ((state.getAttribute(SPECIAL) == null)
839:                        && (toolConfig != null)) {
840:                    try {
841:                        Site site = SiteService.getSite(toolConfig.getSiteId());
842:                        SitePage page = site.getPage(toolConfig.getPageId());
843:
844:                        // if this is the only tool on that page, update the page's title also
845:                        if ((page.getTools() != null)
846:                                && (page.getTools().size() == 1)) {
847:                            // TODO: save site page title? -ggolden
848:                            String newPageTitle = data.getParameters()
849:                                    .getString(FORM_PAGE_TITLE);
850:                            String currentPageTitle = (String) state
851:                                    .getAttribute(STATE_PAGE_TITLE);
852:
853:                            if (StringUtil.trimToNull(newPageTitle) == null) {
854:                                addAlert(state, rb.getString("gen.pagtit"));
855:                                return;
856:                            } else if (!newPageTitle.equals(currentPageTitle)) {
857:                                page.setTitle(newPageTitle);
858:                                state.setAttribute(STATE_PAGE_TITLE,
859:                                        newPageTitle);
860:                            }
861:
862:                            // popup
863:                            boolean popup = data.getParameters().getBoolean(
864:                                    "popup");
865:                            page.setPopup(popup);
866:
867:                            SiteService.save(site);
868:                        }
869:                    } catch (Exception ignore) {
870:                    }
871:                }
872:
873:                // save
874:                // TODO: we might have just saved the entire site, so this would not be needed -ggolden
875:                placement.save();
876:
877:                // we are done with customization... back to the main mode
878:                state.removeAttribute(STATE_MODE);
879:
880:                // deliver an update to the title panel (to show the new title)
881:                // String titleId = titlePanelUpdateId(peid);
882:                // schedulePeerFrameRefresh(titleId);
883:
884:                // refresh the whole page, since popup and title may have changed
885:                scheduleTopRefresh();
886:            }
887:
888:            /**
889:             * doCancel called for form input tags type="submit" named="eventSubmit_doCancel" cancel the options process
890:             */
891:            public void doCancel(RunData data, Context context) {
892:                // access the portlet element id to find our state
893:                String peid = ((JetspeedRunData) data).getJs_peid();
894:                SessionState state = ((JetspeedRunData) data)
895:                        .getPortletSessionState(peid);
896:
897:                // we are done with customization... back to the main mode
898:                state.removeAttribute(STATE_MODE);
899:                state.removeAttribute(STATE_MESSAGE);
900:            }
901:
902:            /**
903:             * Check if the string from user input contains any characters other than digits
904:             * 
905:             * @param height
906:             *        String from user input
907:             * @return True if all are digits. Or False if any is not digit.
908:             */
909:            private boolean checkDigits(String height) {
910:                for (int i = 0; i < height.length(); i++) {
911:                    if (VALID_DIGITS.indexOf(height.charAt(i)) == -1)
912:                        return false;
913:                }
914:                return true;
915:            }
916:
917:            /**
918:             * Note a "local" problem (we failed to get session or site data)
919:             */
920:            private static class SessionDataException extends Exception {
921:                public SessionDataException(String text) {
922:                    super(text);
923:                }
924:            }
925:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.