001
014
015 package com.liferay.portal.atom;
016
017 import com.liferay.portal.kernel.atom.AtomRequestContext;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.util.CharPool;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.model.Company;
025 import com.liferay.portal.model.User;
026 import com.liferay.portal.security.auth.CompanyThreadLocal;
027 import com.liferay.portal.service.CompanyLocalServiceUtil;
028 import com.liferay.portal.util.PortalUtil;
029
030 import javax.servlet.http.HttpServletRequest;
031
032 import org.apache.abdera.protocol.server.RequestContext;
033
034
037 public class AtomUtil {
038
039 public static String createCollectionLink(
040 AtomRequestContext atomRequestContext, String collectionName) {
041
042 return createEntryLink(atomRequestContext, collectionName, null);
043 }
044
045 public static String createEntryLink(
046 AtomRequestContext atomRequestContext, String collectionName,
047 String entryName) {
048
049 StringBundler sb = new StringBundler(5);
050
051 String targetBasePath = atomRequestContext.getTargetBasePath();
052
053 sb.append(targetBasePath);
054
055 sb.append(CharPool.SLASH);
056 sb.append(collectionName);
057
058 if (entryName != null) {
059 sb.append(CharPool.SLASH);
060 sb.append(entryName);
061 }
062
063 String entryLink = sb.toString();
064
065 String resolvedUri = atomRequestContext.getResolvedUri();
066
067 int pos = resolvedUri.indexOf(targetBasePath);
068
069 if (pos != -1) {
070 entryLink = resolvedUri.substring(0, pos) + entryLink;
071 }
072
073 return entryLink;
074 }
075
076 public static String createFeedTitleFromPortletName(
077 AtomRequestContext atomRequestContext, String portletId) {
078
079 String portletTitle = null;
080
081 try {
082 Company company = getCompany();
083
084 portletTitle = company.getName();
085 }
086 catch (Exception e) {
087 return null;
088 }
089
090 User user = getUser(atomRequestContext);
091
092 portletTitle = portletTitle.concat(StringPool.SPACE);
093
094 portletTitle = portletTitle.concat(
095 PortalUtil.getPortletTitle(portletId, user));
096
097 portletTitle = portletTitle.trim();
098
099 return portletTitle;
100 }
101
102 public static String createIdTagPrefix(String title) {
103 Company company = null;
104
105 try {
106 company = getCompany();
107 }
108 catch (Exception e) {
109 return StringPool.BLANK;
110 }
111
112 StringBundler sb = new StringBundler(5);
113
114 sb.append("tag:");
115 sb.append(company.getWebId());
116 sb.append(StringPool.COLON);
117 sb.append(title);
118 sb.append(StringPool.COLON);
119
120 String idTagPrefix = sb.toString();
121
122 return StringUtil.toLowerCase(idTagPrefix);
123 }
124
125 public static Company getCompany() throws PortalException, SystemException {
126 long companyId = CompanyThreadLocal.getCompanyId();
127
128 return CompanyLocalServiceUtil.getCompanyById(companyId);
129 }
130
131 public static AtomPager getPager(RequestContext requestContext) {
132 return (AtomPager)requestContext.getAttribute(
133 RequestContext.Scope.REQUEST, _PAGER);
134 }
135
136 public static User getUser(AtomRequestContext atomRequestContext) {
137 return (User)atomRequestContext.getRequestAttribute(_USER);
138 }
139
140 public static String resolveCollectionUrl(
141 String url, String collectionName) {
142
143 String collection = CharPool.SLASH + collectionName + CharPool.SLASH;
144
145 int collectionIndex = url.indexOf(collection);
146
147 if (collectionIndex == -1) {
148 return url;
149 }
150
151 collectionIndex += collectionName.length() + 1;
152
153 int questionIndex = url.indexOf(CharPool.QUESTION, collectionIndex);
154
155 if (questionIndex != -1) {
156 url =
157 url.substring(0, collectionIndex) +
158 url.substring(questionIndex);
159 }
160 else {
161 url = url.substring(0, collectionIndex);
162 }
163
164 return url;
165 }
166
167 public static void saveAtomPagerInRequest(
168 AtomRequestContext atomRequestContext, AtomPager atomPager) {
169
170 atomRequestContext.setRequestAttribute(_PAGER, atomPager);
171 }
172
173 public static void saveUserInRequest(
174 HttpServletRequest request, User user) {
175
176 request.setAttribute(_USER, user);
177 }
178
179 public static String setPageInUrl(String url, int page) {
180 int pageIndex = url.indexOf("page=");
181
182 if (pageIndex == -1) {
183 int questionIndex = url.indexOf(CharPool.QUESTION);
184
185 if (questionIndex == -1) {
186 url += CharPool.QUESTION;
187 }
188 else {
189 url += CharPool.AMPERSAND;
190 }
191
192 return url + "page=" + page;
193 }
194
195 int endIndex = url.indexOf(CharPool.AMPERSAND, pageIndex);
196
197 if (endIndex == -1) {
198 url = url.substring(0, pageIndex);
199 }
200 else {
201 url = url.substring(0, pageIndex) + url.substring(endIndex + 1);
202
203 url += CharPool.AMPERSAND;
204 }
205
206 url += "page=" + page;
207
208 return url;
209 }
210
211 private static final String _PAGER = AtomUtil.class.getName() + ".pager";
212
213 private static final String _USER = AtomUtil.class.getName() + ".user";
214
215 }