001: /**
002: * $Id: TemplateNativeContainerProvider.java,v 1.11 2006/09/05 19:56:20 dpolla Exp $
003: * Copyright 2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.providers.containers.template;
014:
015: import com.sun.portal.providers.containers.ProviderWindowStates;
016: import com.sun.portal.providers.context.ContainerProviderContext;
017: import com.sun.portal.providers.context.ProviderContextException;
018: import com.sun.portal.providers.ProviderException;
019: import com.sun.portal.providers.Provider;
020: import com.sun.portal.log.common.PortalLogger;
021:
022: import javax.servlet.http.HttpServletResponse;
023: import javax.servlet.http.HttpServletRequest;
024: import java.util.ResourceBundle;
025: import java.util.List;
026: import java.util.Hashtable;
027: import java.util.ListIterator;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.HashMap;
031: import java.util.logging.Level;
032: import java.util.logging.LogRecord;
033: import java.util.logging.Logger;
034: import java.net.URL;
035:
036: public class TemplateNativeContainerProvider extends
037: WirelessContainerProviderAdapter {
038:
039: private List minimizedChannelsList = null;
040: private static int[] supportedStates = new int[2];
041: private static Logger logger = PortalLogger
042: .getLogger(TemplateNativeContainerProvider.class);
043:
044: static {
045: supportedStates[0] = ProviderWindowStates.MINIMIZE;
046: supportedStates[1] = ProviderWindowStates.NORMAL;
047: }
048:
049: ContainerProviderContext context = null;
050: ResourceBundle bundle = null;
051: Map serviceMap = null;
052:
053: public StringBuffer getContent(HttpServletRequest request,
054: HttpServletResponse response) throws ProviderException {
055:
056: StringBuffer content = null;
057: context = getContainerProviderContext();
058: String maximizedChannel = getMaximizedChannel(request);
059:
060: if (maximizedChannel != null) {
061:
062: if (logger.isLoggable(Level.FINEST)) {
063: String[] param = { "maximized channel",
064: maximizedChannel };
065: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
066: }
067:
068: content = buildMaximizedChannel(request, response);
069:
070: } else {
071: if (logger.isLoggable(Level.FINEST))
072: logger.log(Level.FINEST, "PSMA_CSPWPCT0002");
073: content = buildPage(request, response);
074: }
075:
076: return content;
077:
078: }
079:
080: private StringBuffer buildMaximizedChannel(
081: HttpServletRequest request, HttpServletResponse response)
082: throws ProviderException {
083:
084: context = getContainerProviderContext();
085: String maximizedChannel = getMaximizedChannel(request);
086: Provider provider = context.getProvider(request, getName(),
087: maximizedChannel);
088:
089: Hashtable channelTags = new Hashtable();
090:
091: channelTags.put(PROVIDER_TAG, maximizedChannel);
092: channelTags.put(TITLE_TAG, provider.getTitle());
093: channelTags.put(CONTENT_TAG, provider.getContent(request,
094: response));
095: URL helpURL = null;
096: try {
097: helpURL = provider.getHelp(request);
098: } catch (ProviderException e) {
099: if (logger.isLoggable(Level.INFO))
100: logger.log(Level.INFO, "PSMA_CSPWPCT0003", e);
101: }
102: if (helpURL != null) {
103: channelTags.put(PROVIDERHELP_TAG,
104: getTemplate(PROVHELP_TEMPLATE));
105: channelTags.put(HELPLINK_TAG, helpURL.toString());
106: }
107:
108: return getTemplate(MAXIMIZEDCHANNEL_TEMPLATE, channelTags);
109:
110: }
111:
112: private StringBuffer buildPage(HttpServletRequest request,
113: HttpServletResponse response) throws ProviderException {
114:
115: context = getContainerProviderContext();
116: bundle = getResourceBundle();
117: serviceMap = new HashMap();
118:
119: List userChannels = getOrderedSelectedChannels(request);
120: minimizedChannelsList = userChannels;
121:
122: boolean channelsAreMinimized = getBooleanProperty(DEFAULTISMINIMIZED);
123:
124: // channel content and links
125: StringBuffer links = new StringBuffer();
126: StringBuffer content = new StringBuffer();
127: StringBuffer channelContent = null;
128: StringBuffer linkContent = null;
129: StringBuffer pagelinkContent = null;
130:
131: // tag tables
132: Hashtable channelTags = new Hashtable();
133: Hashtable mainTags = new Hashtable();
134:
135: String name = null;
136: String title = null;
137: URL helpURL = null;
138: String providerContent = null;
139: Provider provider = null;
140:
141: mainTags.put(PRODUCTNAME, getStringProperty(PRODUCTNAME));
142: URL mainHelpURL = null;
143: try {
144: mainHelpURL = getHelp(request);
145: } catch (ProviderException e) {
146: if (logger.isLoggable(Level.INFO))
147: logger.log(Level.INFO, "PSMA_CSPWPCT0003", e);
148: }
149: if (mainHelpURL != null) {
150: mainTags.put(MAINHELP_TAG, getTemplate(MAINHELP_TEMPLATE));
151: mainTags.put(HELPLINK_TAG, mainHelpURL.toString());
152: }
153:
154: // build links for template tag tables
155: Map editPages = getMapProperty(EDITPAGES);
156: StringBuffer contentQuerystring = new StringBuffer();
157: StringBuffer layoutQuerystring = new StringBuffer();
158: contentQuerystring = contentQuerystring.append(ACTION).append(
159: '=').append(EDIT).append('&').append(PROVIDER).append(
160: '=').append(editPages.get(CONTENT)).append('&').append(
161: CLIENT).append('=').append(context.getClientType());
162: layoutQuerystring = layoutQuerystring.append(ACTION)
163: .append('=').append(EDIT).append('&').append(PROVIDER)
164: .append('=').append(editPages.get(LAYOUT)).append('&')
165: .append(CLIENT).append('=').append(
166: context.getClientType());
167: mainTags.put(CONTENTLINK_TAG, context.getDesktopURL(request,
168: contentQuerystring.toString(), true));
169: mainTags.put(LAYOUTLINK_TAG, context.getDesktopURL(request,
170: layoutQuerystring.toString(), true));
171: mainTags.put(LOGOUTLINK_TAG, context.getLogoutURL());
172:
173: // Get max size for device
174: int maxSize = MAXPAGESIZE;
175: String maxSizeStr = context.getClientTypeProperty(MAXSIZE);
176: try {
177: if (maxSizeStr != null) {
178: maxSize = Integer.parseInt(maxSizeStr);
179: if (maxSize < 0) {
180: maxSize = MAXPAGESIZE;
181: }
182: }
183: } catch (NumberFormatException nfe) {
184: if (logger.isLoggable(Level.INFO)) {
185: LogRecord rec = new LogRecord(Level.INFO,
186: "PSMA_CSPWPCT0004");
187: String[] param = { maxSizeStr };
188: rec.setParameters(param);
189: rec.setThrown(nfe);
190: rec.setLoggerName(logger.getName());
191: logger.log(rec);
192: }
193:
194: }
195:
196: if (logger.isLoggable(Level.FINEST)) {
197: String[] param = { "maxSize", "" + maxSize };
198: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
199: }
200:
201: // set current size counter
202: // size here can never be zero because if main template is missing,
203: // desktop is not presentable
204: int currentSize = getTemplate(MAIN_TEMPLATE, mainTags).length();
205:
206: if (logger.isLoggable(Level.FINEST)) {
207: String[] param = { "currentSize", "" + currentSize };
208: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
209: }
210:
211: // read page and startChannel params
212: int startChannel = 1;
213: String page = request.getParameter(PAGE);
214: try {
215: startChannel = Integer.parseInt(request
216: .getParameter(STARTCHANNEL));
217: if ((startChannel < 1)
218: || (startChannel > userChannels.size())) {
219: startChannel = 1;
220: }
221: } catch (NumberFormatException nfe) {
222: startChannel = 1;
223: }
224:
225: if (logger.isLoggable(Level.FINEST)) {
226: String[] param = { "page", page };
227: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
228: param[0] = "startChannel";
229: param[1] = "" + startChannel;
230: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
231: }
232:
233: // See if this device is starting at a higher channel. If so,
234: // Add the "previous channels" to the content and increment the size.
235: if ((page != null) && (page.equalsIgnoreCase(NEXT))
236: && (startChannel > 1)) {
237:
238: if (logger.isLoggable(Level.FINEST))
239: logger.log(Level.FINEST, "PSMA_CSPWPCT0005");
240:
241: Hashtable pageTags = new Hashtable();
242: pageTags.put(PAGEMSG_TAG, bundle.getString(PREVIOUSMSG));
243: StringBuffer prevQuerystring = new StringBuffer();
244: prevQuerystring.append(PAGE).append('=').append(PREVIOUS)
245: .append('&').append(STARTCHANNEL).append('=')
246: .append(startChannel - 1);
247: pageTags.put(PAGELINK_TAG, context.getDesktopURL(request,
248: prevQuerystring.toString(), true));
249:
250: StringBuffer previousTemplate = getTemplate(
251: PAGELINK_TEMPLATE, pageTags);
252:
253: links.append(previousTemplate.toString());
254: currentSize += previousTemplate.length();
255: if (logger.isLoggable(Level.FINEST)) {
256: String[] param = { "" + currentSize,
257: previousTemplate.toString() };
258: logger.log(Level.FINEST, "PSMA_CSPWPCT0006", param);
259: }
260: }
261:
262: // I assume startChannel is position of channel starting from one.
263: // The Iterator assumes the list starts at zero.
264: int currentChannel = startChannel + 1;
265:
266: // handle a previous page
267: if ((page != null) && (page.equalsIgnoreCase(PREVIOUS))
268: && (startChannel > 1)) {
269:
270: if (logger.isLoggable(Level.FINEST))
271: logger.log(Level.FINEST, "PSMA_CSPWPCT0007");
272:
273: int forecastSize = currentSize;
274:
275: // see how many providers will fit on this previous page and
276: // calculate according appropriate start channel
277: for (ListIterator li = userChannels
278: .listIterator(startChannel); li.hasPrevious();) {
279:
280: currentChannel--;
281: name = (String) li.previous();
282:
283: if (logger.isLoggable(Level.FINEST)) {
284: String[] param = { "currentChannel",
285: "" + currentChannel };
286: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
287: param[0] = "name";
288: param[1] = name;
289: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
290: }
291:
292: provider = context
293: .getProvider(request, getName(), name);
294: title = provider.getTitle();
295: channelTags.put(TITLE_TAG, title);
296:
297: if (channelsAreMinimized) {
298: StringBuffer maximizeQuerystring = new StringBuffer();
299: maximizeQuerystring = maximizeQuerystring.append(
300: PROVIDER).append('=').append(getName())
301: .append('&').append(MAXIMIZEDCHANNEL)
302: .append('=').append(
303: userChannels.indexOf(name));
304: channelTags.put(MAXIMIZELINK_TAG, context
305: .getDesktopURL(request, maximizeQuerystring
306: .toString(), true));
307:
308: linkContent = getTemplate(MAXIMIZELINK_TEMPLATE,
309: channelTags);
310: } else {
311: providerContent = provider.getContent(request,
312: response).toString();
313:
314: channelTags.put(PROVIDER_TAG, name);
315: channelTags.put(CONTENT_TAG, providerContent);
316: try {
317: helpURL = provider.getHelp(request);
318: } catch (ProviderException e) {
319: if (logger.isLoggable(Level.INFO))
320: logger.log(Level.INFO, "PSMA_CSPWPCT0003",
321: e);
322: }
323: if (helpURL != null) {
324: channelTags.put(PROVIDERHELP_TAG,
325: getTemplate(PROVHELP_TEMPLATE));
326: channelTags.put(HELPLINK_TAG, helpURL
327: .toString());
328:
329: }
330:
331: // Check if this content provider will go over the total
332: // size.
333: // If not, add the provider size to the forecast size.
334: // If so, set the startChannel to the currentChannel
335: channelContent = getTemplate(CHANNEL_TEMPLATE,
336: channelTags);
337: forecastSize += channelContent.length();
338:
339: linkContent = getTemplate(LINK_TEMPLATE,
340: channelTags);
341: }
342:
343: forecastSize += linkContent.length();
344:
345: pagelinkContent = getTemplate(PAGELINK_TEMPLATE);
346: forecastSize += pagelinkContent.length();
347:
348: if (logger.isLoggable(Level.FINEST)) {
349: String[] param = { "forecastSize",
350: "" + forecastSize };
351: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
352: param[0] = "maxSize";
353: param[1] = "" + maxSize;
354: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
355: }
356:
357: // still have more previous channels that won't fit,
358: // insert previous another link and increment size
359: if (forecastSize > maxSize) {
360: // make sure the currentChannel +1 is not greater than
361: // the orginial start channel.
362: if (currentChannel < startChannel) {
363: startChannel = currentChannel + 1;
364: }
365:
366: Hashtable pageTags = new Hashtable();
367: pageTags.put(PAGEMSG_TAG, bundle
368: .getString(PREVIOUSMSG));
369: StringBuffer prevQuerystring = new StringBuffer();
370: prevQuerystring.append(PAGE).append('=').append(
371: PREVIOUS).append('&').append(STARTCHANNEL)
372: .append('=').append(startChannel - 1);
373: pageTags.put(PAGELINK_TAG, context.getDesktopURL(
374: request, prevQuerystring.toString(), true));
375:
376: StringBuffer previousTemplate = getTemplate(
377: PAGELINK_TEMPLATE, pageTags);
378:
379: links.append(previousTemplate.toString());
380: currentSize += previousTemplate.length();
381:
382: if (logger.isLoggable(Level.FINEST)) {
383: String[] param = { "" + currentSize,
384: previousTemplate.toString() };
385: logger.log(Level.FINEST, "PSMA_CSPWPCT0006",
386: param);
387: }
388: break;
389: }
390:
391: // start channel is where ever we stopped incrementing backwards
392: // based on what channels will fit
393: startChannel = currentChannel;
394: }
395: }
396:
397: currentChannel = 0;
398: int additionalSize;
399:
400: // increment through channels
401: for (Iterator i = userChannels.iterator(); i.hasNext();) {
402:
403: currentChannel++;
404: additionalSize = 0;
405: name = (String) i.next();
406:
407: // See if this device is starting at a higher channel. if so,
408: // skip lesser channels
409: if (currentChannel < startChannel) {
410: if (logger.isLoggable(Level.FINEST)) {
411: String[] param = { "" + currentChannel, name };
412: logger.log(Level.FINEST, "PSMA_CSPWPCT0008");
413: }
414:
415: continue;
416: }
417:
418: if (logger.isLoggable(Level.FINEST)) {
419: String[] param = { "currentChannel",
420: "" + currentChannel };
421: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
422: param[0] = "name";
423: param[1] = name;
424: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
425: }
426:
427: provider = context.getProvider(request, getName(), name);
428: title = provider.getTitle();
429: channelTags.put(TITLE_TAG, title);
430:
431: if (channelsAreMinimized) {
432: StringBuffer maximizeQuerystring = new StringBuffer();
433: maximizeQuerystring = maximizeQuerystring.append(
434: PROVIDER).append('=').append(getName()).append(
435: '&').append(MAXIMIZEDCHANNEL).append('=')
436: .append(userChannels.indexOf(name));
437: channelTags.put(MAXIMIZELINK_TAG, context
438: .getDesktopURL(request, maximizeQuerystring
439: .toString(), true));
440:
441: linkContent = getTemplate(MAXIMIZELINK_TEMPLATE,
442: channelTags);
443: } else {
444: providerContent = provider
445: .getContent(request, response).toString();
446:
447: channelTags.put(PROVIDER_TAG, name);
448: channelTags.put(CONTENT_TAG, providerContent);
449: try {
450: helpURL = provider.getHelp(request);
451:
452: } catch (ProviderException e) {
453: if (logger.isLoggable(Level.INFO))
454: logger.log(Level.INFO, "PSMA_CSPWPCT0003", e);
455: }
456: if (helpURL != null) {
457: channelTags.put(PROVIDERHELP_TAG,
458: getTemplate(PROVHELP_TEMPLATE));
459: channelTags.put(HELPLINK_TAG, helpURL.toString());
460: }
461:
462: channelContent = getTemplate(CHANNEL_TEMPLATE,
463: channelTags);
464: additionalSize = channelContent.length();
465:
466: linkContent = getTemplate(LINK_TEMPLATE, channelTags);
467: }
468:
469: additionalSize += linkContent.length();
470:
471: if (logger.isLoggable(Level.FINEST)) {
472: String[] param = { "currentSize", "" + currentSize };
473: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
474: param[0] = "additionalSize";
475: param[1] = "" + additionalSize;
476: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
477: param[0] = "maxSize";
478: param[1] = "" + maxSize;
479: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
480: }
481:
482: // Check if this channel will put us over the total size
483: // If not, add the channel to the content and increment
484: // the channel number
485: // If so, add a link to the next channels and break
486: if (((currentSize + additionalSize) > maxSize)) {
487:
488: if (currentChannel <= startChannel
489: && !channelsAreMinimized) {
490: // Put in a "content too large" message in place of
491: // generated content.
492:
493: Hashtable errorTags = new Hashtable();
494: errorTags.put(NOCONTENT_TAG, bundle
495: .getString(TOOLARGE));
496: channelTags.put(CONTENT_TAG, getTemplate(
497: NOCONTENT_TEMPLATE, errorTags).toString());
498:
499: channelContent = getTemplate(CHANNEL_TEMPLATE,
500: channelTags);
501: content.append(channelContent.toString());
502:
503: links.append(linkContent.toString());
504: if (logger.isLoggable(Level.FINEST)) {
505: String[] param = { linkContent.toString(),
506: channelContent.toString() };
507: logger.log(Level.FINEST, "PSMA_CSPWPCT0009",
508: param);
509: }
510:
511: }
512:
513: currentChannel++;
514:
515: if (currentChannel <= userChannels.size()) {
516: Hashtable pageTags = new Hashtable();
517: pageTags
518: .put(PAGEMSG_TAG, bundle.getString(NEXTMSG));
519: StringBuffer nextQuerystring = new StringBuffer();
520: nextQuerystring.append(PAGE).append('=').append(
521: NEXT).append('&').append(STARTCHANNEL)
522: .append('=').append(currentChannel);
523: pageTags.put(PAGELINK_TAG, context.getDesktopURL(
524: request, nextQuerystring.toString(), true));
525:
526: StringBuffer nextTemplate = getTemplate(
527: PAGELINK_TEMPLATE, pageTags);
528:
529: links.append(nextTemplate.toString());
530: if (logger.isLoggable(Level.FINEST)) {
531: logger.log(Level.FINEST, "PSMA_CSPWPCT0019",
532: nextTemplate.toString());
533: }
534:
535: }
536:
537: break;
538: }
539: // If channel content fits, append the
540: // value to the content stringbuffer.
541: // channel content will be null if channels are minimized
542: if (channelContent != null) {
543: content.append(channelContent.toString());
544: }
545: links.append(linkContent.toString());
546: currentSize += additionalSize;
547: }
548: // Put the content stringbuffer into the main tagtable.
549: // Content will never be null as it is a new StringBuffer
550: mainTags.put(CONTENT_TAG, content.toString());
551:
552: // Put the links stringbuffer into the main tagtable.
553: if (links.length() == 0) {
554: String noContent = bundle.getString(NOCONTENT);
555: channelTags.put(TITLE_TAG, noContent);
556: linkContent = getTemplate(LINK_TEMPLATE, channelTags);
557: links.append(linkContent.toString());
558: }
559: mainTags.put(LINKS_TAG, links.toString());
560:
561: // Load the page template, swapping in the main tagtable.
562: return getTemplate(MAIN_TEMPLATE, mainTags);
563:
564: }
565:
566: public boolean isPresentable(HttpServletRequest request) {
567:
568: try {
569: if (logger.isLoggable(Level.FINEST)) {
570: Object[] param = { "clientType",
571: getProviderContext().getClientType() };
572: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
573: param[0] = "filePath";
574: param[1] = getProviderContext().getClientPath();
575: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
576: param[0] = "templatePath";
577: param[1] = getProviderContext().getTemplatePath(
578: getName(), MAIN_TEMPLATE);
579: logger.log(Level.FINEST, "PSMA_CSPWPCT0001", param);
580: }
581:
582: if (getProviderContext().getTemplatePath(getName(),
583: MAIN_TEMPLATE) != null) {
584: return getTemplate(MAIN_TEMPLATE) != null;
585: } else {
586: return false;
587: }
588: } catch (ProviderContextException e) {
589: return false;
590: } catch (ProviderException e) {
591: return false;
592: }
593: }
594:
595: public int getWindowState(String channelName)
596: throws ProviderException {
597:
598: boolean channelsAreMinimized = getBooleanProperty(DEFAULTISMINIMIZED);
599:
600: if (channelsAreMinimized) {
601: return ProviderWindowStates.MINIMIZE;
602: } else {
603: return ProviderWindowStates.NORMAL;
604: }
605:
606: }
607:
608: public int[] getSupportedWindowStates() throws ProviderException {
609: return supportedStates;
610: }
611:
612: /**
613: * Returns the maximized channel specified by this request, or
614: * <code>null</code> if none exists.
615: * @param request the current request
616: * @return the maximized channel
617: * @throws ProviderException
618: */
619: public String getMaximizedChannel(HttpServletRequest request)
620: throws ProviderException {
621:
622: String maximizedChannel = null;
623: String maximizedChannelParam = request
624: .getParameter(MAXIMIZEDCHANNEL);
625:
626: if (maximizedChannelParam != null) {
627: List userChannels;
628: if (!getProviderContext().isAuthless(request)
629: && minimizedChannelsList != null) {
630: userChannels = minimizedChannelsList;
631: } else {
632: userChannels = getOrderedSelectedChannels(request);
633: }
634: try {
635: maximizedChannel = (String) userChannels.get(Integer
636: .parseInt(maximizedChannelParam));
637: } catch (Exception e) {
638: throw new ProviderException(
639: "WirelessTemplateDesktop.getMaximizedChannel(): ",
640: e);
641: }
642: }
643:
644: return maximizedChannel;
645: }
646:
647: }
|