001: /*
002: * CommunityURLManager.java
003: *
004: * Created on May 17, 2005, 12:53 PM
005: *
006: * To change this template, choose Tools | Options and locate the template under
007: * the Source Creation and Management node. Right-click the template and choose
008: * Open. You can then make changes to the template in the Source Editor.
009: */
010:
011: package com.sun.portal.community.urlmanager.impl;
012:
013: import javax.servlet.http.HttpServletRequest;
014: import com.sun.portal.desktop.context.DesktopAppContext;
015: import com.sun.portal.desktop.context.DesktopAppContextThreadLocalizer;
016: import com.sun.portal.desktop.context.DesktopContext;
017: import com.sun.portal.desktop.context.DesktopContextThreadLocalizer;
018: import com.sun.portal.desktop.context.DSAMEMultiPortalConstants;
019:
020: import com.sun.portal.desktop.DesktopParameters;
021:
022: import com.sun.portal.community.urlmanager.CommunityURLManager;
023: import com.sun.portal.community.CommunityContainerName;
024: import com.sun.portal.community.CommunityFactory;
025: import com.sun.portal.community.CommunityException;
026: import com.sun.portal.community.CommunityId;
027: import com.sun.portal.community.RoleId;
028: import com.sun.portal.community.impl.TypeConverter;
029:
030: import com.sun.portal.community.mc.ConfigTable.ConfigKey;
031: import com.sun.portal.community.mc.CMCPrincipal;
032: import com.sun.portal.community.mc.CMCRolePrincipal;
033:
034: import java.util.Set;
035: import java.util.HashSet;
036: import java.util.Iterator;
037: import java.util.Collections;
038:
039: import java.net.URL;
040: import java.net.MalformedURLException;
041: import java.net.URLEncoder;
042: import java.io.UnsupportedEncodingException;
043:
044: /**
045: * This class reads desktop service IS properties related to creating community URLs and creates those URLs.
046: *
047: */
048: public class DSAMECommunityURLManagerImpl implements
049: CommunityURLManager {
050:
051: /**
052: * For a community, returns the desktop URL to that community for the user
053: */
054: public String getCommunityURL(HttpServletRequest request,
055: String userId, CommunityId cid) throws CommunityException {
056:
057: return getCommunityURL(request, cid);
058: }
059:
060: /**
061: * For a community, returns the desktop URL to that community for the current user
062: */
063: public String getCommunityURL(HttpServletRequest request,
064: CommunityId cid) throws CommunityException {
065:
066: // get the community container
067: String communityContainer = new CommunityContainerName(cid)
068: .toString();
069: String url = getCommunityURLInternal(request,
070: communityContainer);
071:
072: url += "&" + getCurrentMembershipParameter(cid);
073: return url;
074:
075: }
076:
077: /**
078: * Returns the bookmarkable, readable, autologin, access URL to a community.
079: */
080: public String getCommunityAccessURL(HttpServletRequest request,
081: CommunityId cid) throws CommunityException {
082: // deprecated - public api should take name only
083: String idstring = cid.toString();
084: String name = idstring.substring(idstring.indexOf("__") + 2); // assume ps can map name back to id (default is jdo)
085: return getCommunityAccessURL(request, name);
086: }
087:
088: /**
089: * Returns the bookmarkable, readable, autologin, access URL to a community.
090: * Eg, http://server/portal/dt?dt.community=blah for a named community
091: * and, http://server/portal/dt?dt.community= for the community home page
092: */
093: public String getCommunityAccessURL(HttpServletRequest request,
094: String communityName) throws CommunityException {
095: boolean useold = false; // XXX config
096: boolean namedCommunity = communityName != null
097: || communityName.length() > 0;
098: if (useold) {
099: if (namedCommunity)
100: return getCommunityURL(request, new CommunityId("jdo",
101: communityName));
102: else {
103: return getCommunityHomeURL(request);
104: }
105: }
106: if (communityName == null)
107: communityName = "";
108: DesktopContext dc = DesktopContextThreadLocalizer.get();
109: String url = dc.getDesktopURL(request,
110: DesktopParameters.CURRENT_MEMBERSHIP + "="
111: + urlencode(communityName));
112: return url;
113: }
114:
115: private String getCurrentMembershipParameter() {
116: return getCurrentMembershipParameter(null);
117: }
118:
119: private String getCurrentMembershipParameter(CommunityId cid) {
120: String membershipString = null;
121:
122: if (cid == null) {
123: membershipString = DesktopParameters.CURRENT_MEMBERSHIP_VALUE_NONE;
124: } else {
125: membershipString = cid.toString();
126: }
127: return DesktopParameters.CURRENT_MEMBERSHIP + "="
128: + membershipString;
129: }
130:
131: /**
132: * returns the desktop URL to community home
133: */
134: public String getCommunityHomeURL(HttpServletRequest request) {
135: String communityHomeContainer = getCommunityHomeContainer();
136: return getCommunityURLInternal(request, communityHomeContainer)
137: + "&" + getCurrentMembershipParameter();
138: }
139:
140: /**
141: * returns the desktop URL to community create
142: */
143: public String getCommunityCreateURL(HttpServletRequest request) {
144:
145: String communityCreateContainer = getCommunityCreateContainer();
146: return getCommunityURLInternal(request,
147: communityCreateContainer);
148:
149: }
150:
151: /**
152: * Internal method to generate URLs
153: */
154: protected String getCommunityURLInternal(
155: HttpServletRequest request, String containerName) {
156:
157: // get desktop url
158: String desktopURL = getDesktopURL(request);
159:
160: // get the getCommunityParentContainerURLParameter
161: String communityParentURLParameter = getCommunityParentContainerURLParameter();
162:
163: // calculate the separator
164: String separator;
165: if ((desktopURL.indexOf("&") > 0)
166: || (desktopURL.indexOf("?") > 0)) {
167: separator = "&";
168: } else {
169: separator = "?";
170: }
171:
172: // concatenate it all
173: // TODO: somewhere I need to encode this stuff TODO ...
174: return desktopURL + separator + communityParentURLParameter
175: + "=" + containerName;
176:
177: }
178:
179: protected String getDesktopURL(HttpServletRequest request) {
180: DesktopAppContext dac = DesktopAppContextThreadLocalizer.get();
181: return dac.getDesktopURL(request);
182: }
183:
184: protected String getLoginURL() {
185: DesktopContext dc = DesktopContextThreadLocalizer.get();
186: return dc.getLoginURL();
187: }
188:
189: protected String getCommunityParentContainerURLParameter() {
190: DSAMEMultiPortalConstants dmpc = DSAMEMultiPortalConstants
191: .getInstance();
192: DesktopContext dc = DesktopContextThreadLocalizer.get();
193: return dc
194: .getStringAttribute(dmpc.MP_ATTR_COMMUNITY_PARENT_CONTAINER_URL_PARAMETER);
195: }
196:
197: protected String getCommunityHomeContainer() {
198: DSAMEMultiPortalConstants dmpc = DSAMEMultiPortalConstants
199: .getInstance();
200: DesktopContext dc = DesktopContextThreadLocalizer.get();
201: return dc
202: .getStringAttribute(dmpc.MP_ATTR_COMMUNITY_HOME_CONTAINER_NAME);
203: }
204:
205: protected String getCommunityCreateContainer() {
206: DSAMEMultiPortalConstants dmpc = DSAMEMultiPortalConstants
207: .getInstance();
208: DesktopContext dc = DesktopContextThreadLocalizer.get();
209: return dc
210: .getStringAttribute(dmpc.MP_ATTR_COMMUNITY_CREATE_CONTAINER_NAME);
211: }
212:
213: private String urlencode(String s) {
214: try {
215: return URLEncoder.encode(s, "UTF-8");
216: } catch (Exception wonthappen) {
217: }
218: return s;
219: }
220:
221: }
|