001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * any nuclear facility.
035: */
036:
037: package com.sun.portal.search.demo;
038:
039: import com.sun.portal.search.soif.SOIF;
040: import com.sun.portal.search.soif.SOIFInputStream;
041: import com.sun.portal.search.soif.SOIFOutputStream;
042: import java.io.IOException;
043: import java.io.PrintWriter;
044: import java.net.MalformedURLException;
045: import java.net.URL;
046: import java.net.URLConnection;
047: import java.util.ArrayList;
048: import java.util.List;
049:
050: /**
051: * This is a client side search database object.
052: * It maps to the database on server side by given server url and database name.
053: * If the database name is not specified, server uses the predefined default database name.
054: * Application can use this object to exchange its data with search server.
055: * Example:
056: * <PRE>
057: * public void submitSurveyToSearch(SurveyModel survey) throws Exception {
058: * SOIF soif = new SOIF("DOCUMENT", "survey::" + survey.getSurveyId());
059: * soif.insert("title", survey.getSurveyName());
060: * soif.insert("description", survey.getSurveyDescription());
061: * soif.insert("author", survey.getSurveyOwner());
062: * SearchDatabase db = new SearchDatabase(getSearchUrl() , getSearchDatabase());
063: * db.insert(soif);
064: * }
065: * public void deleteSurveyFromSearch(String surveyId) throws Exception {
066: * SearchDatabase db = new SearchDatabase(getSearchUrl() , getSearchDatabase());
067: * db.delete("survey::" + surveyId);
068: * }
069: * </PRE>
070: */
071: public class SearchDatabase {
072:
073: // submit operations
074: /**
075: * fetch RDs filtered by submit-view
076: */
077: static final String SUBMIT_RETRIEVE = "retrieve";
078: /**
079: * insert/replace entire RDs
080: */
081: static final String SUBMIT_INSERT = "insert";
082: /**
083: * delete entire RDs
084: */
085: static final String SUBMIT_DELETE = "delete";
086: /**
087: * update the given RD attributes only, can also be filtered by submit-view
088: */
089: static final String SUBMIT_UPDATE = "update";
090:
091: private URL searchUrl;
092: private String database;
093: private PrintWriter debugOut = null;
094: private String submitType = "nonpersistent";
095: private String submitView = null;
096: private String SSOToken = null;
097:
098: /**
099: * Creates a new instance of SearchDatabase represent default database
100: * @param searchUrl URL of the search server
101: * @throws java.net.MalformedURLException
102: */
103: public SearchDatabase(String searchUrl)
104: throws MalformedURLException {
105: this .searchUrl = new URL(searchUrl);
106: }
107:
108: /**
109: * Creates a new instance of SearchDatabase
110: * @param searchUrl URL of the search server
111: * @param database The name of the database.
112: * @throws java.net.MalformedURLException
113: */
114: public SearchDatabase(String searchUrl, String database)
115: throws MalformedURLException {
116: this .searchUrl = new URL(searchUrl);
117: this .database = database;
118: }
119:
120: /**
121: * @param The access token from the portal server
122: */
123: public void setSessionID(String sessID) {
124: SSOToken = sessID;
125: }
126:
127: private SOIF doSubmit(String operation, SOIF rd) throws IOException {
128: SOIF hdr = new SOIF("RDMHEADER", "-");
129: hdr.insert("rdm-type", "rd-submit-request");
130: if (database != null)
131: hdr.insert("submit-database", database);
132: if (SSOToken != null)
133: hdr.insert("rdm-access-token", SSOToken);
134: SOIF req = new SOIF("Request", "-");
135: req.insert("submit-type", submitType);
136: req.insert("submit-operation", operation);
137: if (this .submitView != null) {
138: req.insert("submit-view", submitView);
139: }
140: URLConnection pc = searchUrl.openConnection();
141: pc.setAllowUserInteraction(true);
142: pc.setUseCaches(false);
143: pc.setDoOutput(true);
144: pc.setDoInput(true);
145: SOIFOutputStream sos = new SOIFOutputStream(pc
146: .getOutputStream());
147: pc.connect();
148: if (debugOut != null)
149: debugOut.println(">>> Request\n");
150: sos.write(hdr);
151: if (debugOut != null)
152: debugOut.print(hdr);
153: sos.write(req);
154: if (debugOut != null)
155: debugOut.print(req);
156: sos.write(rd);
157: if (debugOut != null)
158: debugOut.print(rd);
159: sos.close();
160: if (debugOut != null)
161: debugOut.println(">>> Response\n");
162: SOIFInputStream sis = new SOIFInputStream(pc.getInputStream());
163: SOIF s;
164: while ((s = sis.readSOIF()) != null) {
165: if (debugOut != null)
166: debugOut.println(s);
167: if (s.getSchemaName().equalsIgnoreCase("DOCUMENT")) {
168: return s;
169: }
170: }
171: return null;
172: }
173:
174: /**
175: * Retrieve a Resource Description in SOIF format.
176: * @return Resource Description
177: * @param documentId ID of the Resource Description to retrieve.
178: * @throws java.io.IOException
179: */
180: public SOIF fetch(String documentId) throws IOException {
181: SOIF soif = new SOIF("DOCUMENT", documentId);
182: return doSubmit(SUBMIT_RETRIEVE, soif);
183: }
184:
185: /**
186: * Delete a Resource Description
187: * @param documentId The ID of the RD to delete.
188: * @throws java.io.IOException
189: */
190: public void delete(String documentId) throws IOException {
191: SOIF soif = new SOIF("DOCUMENT", documentId);
192: doSubmit(SUBMIT_DELETE, soif);
193: }
194:
195: /**
196: * Adding a new Resource Description
197: * @param rd The new RD to insert.
198: * @throws java.io.IOException
199: */
200: public void insert(SOIF rd) throws IOException {
201: doSubmit(SUBMIT_INSERT, rd);
202: }
203:
204: /**
205: * Modify a Resource Description.
206: * @param rd A RD contains only the attributes needed to be update.
207: * @throws java.io.IOException
208: */
209: public void update(SOIF rd) throws IOException {
210: doSubmit(SUBMIT_UPDATE, rd);
211: }
212:
213: public static List getDatabaseList(String searchUrl)
214: throws MalformedURLException, IOException {
215: URL url = new URL(searchUrl);
216: SOIF hdr = new SOIF("RDMHEADER", "-");
217: hdr.insert("rdm-type", "server-request");
218: hdr.insert("dblist-request", "true");
219:
220: URLConnection pc = url.openConnection();
221: pc.setAllowUserInteraction(true);
222: pc.setUseCaches(false);
223: pc.setDoOutput(true);
224: pc.setDoInput(true);
225: SOIFOutputStream sos = new SOIFOutputStream(pc
226: .getOutputStream());
227: pc.connect();
228: sos.write(hdr);
229:
230: SOIFInputStream sis = new SOIFInputStream(pc.getInputStream());
231: SOIF s;
232: ArrayList dbs = new ArrayList();
233: while ((s = sis.readSOIF()) != null) {
234: if (s.getSchemaName().equalsIgnoreCase("Database")) {
235: dbs.add(s);
236: }
237: }
238: return dbs;
239: }
240:
241: }
|