001: package com.sun.portal.wsrp.producer.impl;
002:
003: import com.sun.portal.desktop.dp.DPChannel;
004: import com.sun.portal.desktop.dp.DPContainerChannel;
005: import com.sun.portal.desktop.dp.DPNode;
006: import com.sun.portal.desktop.dp.DPRoot;
007: import com.sun.portal.log.common.PortalLogger;
008: import com.sun.portal.wsrp.producer.DPConnection;
009: import com.sun.portal.wsrp.producer.ProducerException;
010: import java.util.HashSet;
011: import java.util.Iterator;
012: import java.util.Set;
013: import java.util.TreeSet;
014: import java.util.logging.Level;
015: import java.util.logging.Logger;
016: import javax.servlet.http.HttpServletRequest;
017:
018: public class DPFilter {
019:
020: private static Logger logger = PortalLogger
021: .getLogger(ProducerImpl.class);
022: private String portalId = null;
023: private String organization = null;
024: private HttpServletRequest req = null;
025:
026: private DPRoot dpRoot = null;
027: private static final Object dpLock = new Object();
028:
029: private static String CHANNEL_NAME_SEPARATOR = String
030: .valueOf(DPNode.CHANNEL_NAME_SEPARATOR);
031:
032: public DPFilter(String portalId, String organization) {
033: this .portalId = portalId;
034: this .organization = organization;
035: }
036:
037: public DPFilter(HttpServletRequest req, String organization) {
038: this .req = req;
039: this .organization = organization;
040: }
041:
042: public Set getExistingChannels() throws ProducerException {
043: DPConnection dpc = null;
044: if (req != null) {
045: dpc = new DPConnection(req);
046: } else {
047: dpc = new DPConnection(portalId);
048: }
049: dpRoot = dpc.getDPRoot(organization);
050:
051: DPNode currentNode = dpRoot;
052: Set result = new TreeSet();
053: Set channelNames = getChannelNames(currentNode);
054: Iterator iterator = channelNames.iterator();
055:
056: while (iterator.hasNext()) {
057: String channelName = (String) iterator.next();
058: logger.log(Level.FINEST, "PSDT_CSPT0019", channelName);
059: if (!isDisplayable(channelName)) {
060: continue;
061: }
062: DPChannel channel = getDPChannel(channelName);
063: //check if this channel is a container and if it is get
064: // the list of channels defined in the container
065: if (channel instanceof DPContainerChannel) {
066: // get the list for this container baseDN.
067: result.addAll(getChannelsInContainer(channel, false));
068: } else {
069: result.add(channelName);
070: }
071: }
072:
073: return filterResults(result);
074:
075: }
076:
077: private Set filterResults(Set channels) {
078: Set portletChannels = new TreeSet();
079:
080: for (Iterator i = channels.iterator(); i.hasNext();) {
081: String name = (String) i.next();
082: DPChannel dpch = dpRoot.getChannel(name);
083: String providerName = dpch.getProviderName();
084:
085: if (providerName.indexOf("__Portlet__") != -1
086: && (!providerName.equals("_WSRPWindowProvider"))) {
087: portletChannels.add(name);
088: }
089: }
090: return portletChannels;
091: }
092:
093: private String toFullyQualifiedName(DPNode container,
094: String shortName) {
095: return ((container instanceof DPRoot) ? "" : container
096: .getName()
097: + DPNode.CHANNEL_NAME_SEPARATOR)
098: + shortName;
099: }
100:
101: private Set getChannelsInContainer(DPChannel container,
102: boolean onlyContainers) throws ProducerException {
103: DPNode currentNode = container;
104: Set result = new TreeSet();
105: Set channelNames = getChannelNames(currentNode);
106: Iterator iterator = channelNames.iterator();
107:
108: while (iterator.hasNext()) {
109: String channelName = (String) iterator.next();
110: logger.log(Level.FINEST, "PSDT_CSPT0023", channelName);
111: String fQCN = toFullyQualifiedName(currentNode, channelName);
112:
113: if (!isDisplayable(fQCN)) {
114: continue;
115: }
116: DPChannel channel = null;
117: synchronized (dpLock) {
118: channel = currentNode.getChannel(channelName);
119: }
120: //check if this channel is a container and if it is get
121: // the list of channels defined in the container
122: if (channel instanceof DPContainerChannel) {
123: if (onlyContainers) {
124: result.add(fQCN);
125: }
126: // get the list for this container baseDN.
127: result.addAll(getChannelsInContainer(channel,
128: onlyContainers));
129: } else if (!onlyContainers) {
130: result.add(fQCN);
131: }
132: }
133: return result;
134:
135: }
136:
137: private Set getChannelNames(DPNode node) {
138: Set channelNames = null;
139: synchronized (dpLock) {
140: channelNames = node.getChannelNames();
141: }
142:
143: Set portletChannelNames = new HashSet();
144:
145: for (Iterator i = channelNames.iterator(); i.hasNext();) {
146: String name = (String) i.next();
147: DPChannel dpch = node.getChannel(name);
148: String providerName = dpch.getProviderName();
149: portletChannelNames.add(name);
150: }
151: return portletChannelNames;
152: }
153:
154: private DPChannel getDPChannel(String channelName)
155: throws ProducerException {
156: DPChannel channel = null;
157: synchronized (dpLock) {
158: if (!existsChannel(channelName)) {
159: // error key non-existent channel.
160: logger.log(Level.FINE, "PSDT_CSPT0035", new Object[] {
161: channelName, dpRoot.getName() });
162: throw new ProducerException("Channel does not exists: "
163: + channelName);
164: }
165: channel = dpRoot.getChannel(channelName);
166: }
167: return channel;
168: }
169:
170: private boolean existsChannel(String channel) {
171: boolean exists = dpRoot.channelExists(channel);
172: return exists;
173: }
174:
175: private boolean isDisplayable(String fqcn) {
176:
177: boolean isDisplayable = true;
178: if (fqcn == null || fqcn.length() == 0) {
179: isDisplayable = false;
180: return isDisplayable;
181: }
182: DPChannel channel = null;
183: try {
184: channel = getDPChannel(fqcn);
185: } catch (ProducerException tae) {
186: isDisplayable = false;
187: return isDisplayable;
188: }
189: String shortName = getTail(fqcn, CHANNEL_NAME_SEPARATOR);
190:
191: // Filter out all channels/containers that are defined advanced or
192: // if the name starts with "_"
193: if (channel == null || channel.isAdvanced()
194: || shortName.startsWith("_")) {
195:
196: isDisplayable = false;
197: }
198:
199: return isDisplayable;
200: }
201:
202: private String getTail(String rpn, String nameSeparator) {
203: String tail = null;
204: if (rpn != null && rpn.length() != 0) {
205: int index = rpn.lastIndexOf(nameSeparator);
206: if (index == -1) {
207: tail = rpn;
208: } else {
209: tail = rpn.substring(index + 1, rpn.length());
210: }
211: }
212: return tail;
213: }
214:
215: }
|