001: /*
002: * Copyright ? 2006 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Sun Microsystems, Inc. has intellectual property rights relating to
005: * technology embodied in the product that is described in this document.
006: * In particular, and without limitation, these intellectual property
007: * rights may include one or more of the U.S. patents listed at
008: * http://www.sun.com/patents and one or more additional patents or
009: * pending patent applications in the U.S. and in other countries.
010: *
011: * U.S. Government Rights - Commercial software. Government users are subject
012: * to the Sun Microsystems, Inc. standard license agreement and applicable
013: * provisions of the FAR and its supplements. Use is subject to license terms.
014: * This distribution may include materials developed by third parties.
015: * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
016: * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
017: */
018: package com.sun.portal.app.blog.env;
019:
020: import com.iplanet.sso.SSOException;
021: import com.iplanet.sso.SSOToken;
022: import com.iplanet.sso.SSOTokenManager;
023: import com.sun.identity.security.AdminTokenAction;
024: import com.sun.portal.app.blog.BlogPortletException;
025: import com.sun.portal.app.blog.HTMLCleaner;
026: import com.sun.portal.search.demo.SearchDatabase;
027: import com.sun.portal.search.soif.SOIF;
028: import java.io.IOException;
029: import java.net.MalformedURLException;
030: import java.net.URL;
031: import java.security.AccessController;
032: import javax.portlet.PortletRequest;
033: import javax.servlet.http.HttpServletRequest;
034:
035: /**
036: * Abstract base class for search integration.
037: *
038: * Implementations of this class define the implementation of search
039: * for different runtime application environments.
040: */
041: public class PortalSearch extends AbstractSearch {
042: private static final HTMLCleaner CLEANER = new HTMLCleaner();
043:
044: public void insert(String id, String title, String content)
045: throws BlogPortletException {
046: if (id == null) {
047: throw new NullPointerException("id argument cannot be null");
048: }
049: if (id.length() == 0) {
050: throw new IllegalArgumentException(
051: "id argument cannot be length 0");
052: }
053: if (title == null) {
054: throw new NullPointerException(
055: "title argument cannot be null");
056: }
057: if (title.length() == 0) {
058: throw new IllegalArgumentException(
059: "title argument cannot be length 0");
060: }
061:
062: // content may be null
063:
064: URL surl = getPrefs().getSearchUrl();
065: if (surl == null) {
066: // no search server: fail silently
067: return;
068: }
069: String db = getPrefs().getSearchDatabase();
070: if (db == null) {
071: // no search db: fail silently
072: return;
073: }
074:
075: content = CLEANER.clean(content);
076:
077: SOIF rd = new SOIF("DOCUMENT", id);
078: rd.insert("title", title);
079: if (content != null && content.length() > 0) {
080: rd.insert("partial-text", content);
081: }
082:
083: try {
084: SearchDatabase sdb = getSearchDatabase(surl, db);
085: sdb.insert(rd);
086: // TODO: should we fail silently here?
087: } catch (MalformedURLException mue) {
088: throw new BlogPortletException(mue);
089: } catch (IOException ioe) {
090: throw new BlogPortletException(ioe);
091: }
092: }
093:
094: public void update(String id, String title, String content)
095: throws BlogPortletException {
096: if (id == null) {
097: throw new NullPointerException("id argument cannot be null");
098: }
099: if (id.length() == 0) {
100: throw new IllegalArgumentException(
101: "id argument cannot be length 0");
102: }
103: if (title == null) {
104: throw new NullPointerException(
105: "title argument cannot be null");
106: }
107: if (title.length() == 0) {
108: throw new IllegalArgumentException(
109: "title argument cannot be length 0");
110: }
111:
112: // content may be null
113:
114: URL surl = getPrefs().getSearchUrl();
115: if (surl == null) {
116: // no search server: fail silently
117: return;
118: }
119: String db = getPrefs().getSearchDatabase();
120: if (db == null) {
121: // no search db: fail silently
122: return;
123: }
124:
125: content = CLEANER.clean(content);
126:
127: SOIF rd = new SOIF("DOCUMENT", id);
128: rd.insert("title", title);
129: if (content != null && content.length() > 0) {
130: rd.insert("partial-text", content);
131: }
132:
133: try {
134: SearchDatabase sdb = getSearchDatabase(surl, db);
135: sdb.update(rd);
136: // TODO: should we fail silently here?
137: } catch (MalformedURLException mue) {
138: throw new BlogPortletException(mue);
139: } catch (IOException ioe) {
140: throw new BlogPortletException(ioe);
141: }
142: }
143:
144: public void delete(String id) throws BlogPortletException {
145: if (id == null) {
146: throw new NullPointerException("id argument cannot be null");
147: }
148: if (id.length() == 0) {
149: throw new IllegalArgumentException(
150: "id argument cannot be length 0");
151: }
152:
153: URL surl = getPrefs().getSearchUrl();
154: if (surl == null) {
155: // no search server: fail silently
156: return;
157: }
158: String db = getPrefs().getSearchDatabase();
159: if (db == null) {
160: // no search db: fail silently
161: return;
162: }
163:
164: try {
165: SearchDatabase sdb = getSearchDatabase(surl, db);
166: sdb.delete(id);
167: // TODO: should we fail silently here?
168: } catch (MalformedURLException mue) {
169: throw new BlogPortletException(mue);
170: } catch (IOException ioe) {
171: throw new BlogPortletException(ioe);
172: }
173: }
174:
175: private SearchDatabase getSearchDatabase(URL url, String db)
176: throws MalformedURLException {
177: SearchDatabase sdb = new SearchDatabase(url.toString(), db);
178: SSOToken ssoToken = (SSOToken) AccessController
179: .doPrivileged(AdminTokenAction.getInstance());
180: // if the token is available, pass it to the search db
181: if (ssoToken != null) {
182: String sessionId = ssoToken.getTokenID().toString();
183: sdb.setSessionID(sessionId);
184: }
185: return sdb;
186: }
187:
188: private SSOToken getSSOToken() {
189: try {
190: SSOToken ssoToken = null;
191:
192: PortletRequest pr = getPortletRequest();
193: if (pr != null) {
194: ssoToken = (SSOToken) pr
195: .getAttribute("javax.portlet.portletc.ssotoken");
196: }
197: if (ssoToken == null) {
198: HttpServletRequest hr = getHttpRequest();
199: if (hr != null) {
200: ssoToken = SSOTokenManager.getInstance()
201: .createSSOToken(hr);
202: }
203: }
204:
205: return ssoToken;
206: } catch (SSOException ssoe) {
207: return null;
208: }
209: }
210: }
|