001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/help/tags/sakai_2-4-1/help-component/src/java/org/sakaiproject/component/app/help/RestConfigurationImpl.java $
003: * $Id: RestConfigurationImpl.java 7653 2006-04-12 12:10:02Z marquard@ched.uct.ac.za $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.component.app.help;
021:
022: import java.io.BufferedReader;
023: import java.io.IOException;
024: import java.io.InputStreamReader;
025: import java.io.StringReader;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.net.URLConnection;
029:
030: import javax.xml.parsers.DocumentBuilder;
031: import javax.xml.parsers.DocumentBuilderFactory;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.sakaiproject.api.app.help.RestConfiguration;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038: import org.xml.sax.InputSource;
039:
040: import sun.misc.BASE64Encoder;
041:
042: /**
043: * @author <a href="mailto:jlannan.iupui.edu">Jarrod Lannan</a>
044: * @version $Id: RestConfigurationImpl.java 7653 2006-04-12 12:10:02Z marquard@ched.uct.ac.za $
045: *
046: */
047: public class RestConfigurationImpl implements RestConfiguration {
048:
049: /** user:pass as string ... will be converted to Base64 **/
050: private String restCredentials;
051:
052: private String organization;
053: private String restDomain;
054: private String restUrl;
055: private long cacheInterval;
056:
057: private static String REST_DOMAIN_URL;
058: private static String REST_CORPUS_URL;
059:
060: private final static Log LOG = LogFactory
061: .getLog(RestConfigurationImpl.class);
062:
063: /**
064: * @see org.sakaiproject.api.app.help.RestConfiguration#getOrganization()
065: */
066: public String getOrganization() {
067: return organization;
068: }
069:
070: /**
071: * @see org.sakaiproject.api.app.help.RestConfiguration#setOrganization(java.lang.String)
072: */
073: public void setOrganization(String organization) {
074: this .organization = organization;
075: }
076:
077: /**
078: * @see org.sakaiproject.api.app.help.RestConfiguration#getRestCredentials()
079: */
080: public String getRestCredentials() {
081: return restCredentials;
082: }
083:
084: /**
085: * @see org.sakaiproject.api.app.help.RestConfiguration#setRestCredentials(java.lang.String)
086: */
087: public void setRestCredentials(String restCredentials) {
088: this .restCredentials = restCredentials;
089: }
090:
091: /**
092: * @see org.sakaiproject.api.app.help.RestConfiguration#getRestDomain()
093: */
094: public String getRestDomain() {
095: return restDomain;
096: }
097:
098: /**
099: * @see org.sakaiproject.api.app.help.RestConfiguration#setRestDomain(java.lang.String)
100: */
101: public void setRestDomain(String restDomain) {
102: this .restDomain = restDomain;
103: }
104:
105: /**
106: * @see org.sakaiproject.api.app.help.RestConfiguration#getRestUrl()
107: */
108: public String getRestUrl() {
109: return restUrl;
110: }
111:
112: /**
113: * @see org.sakaiproject.api.app.help.RestConfiguration#setRestUrl(java.lang.String)
114: */
115: public void setRestUrl(String restUrl) {
116: this .restUrl = restUrl;
117: }
118:
119: /**
120: * @see org.sakaiproject.api.app.help.RestConfiguration#getCacheInterval()
121: */
122: public long getCacheInterval() {
123: return cacheInterval;
124: }
125:
126: /**
127: * @see org.sakaiproject.api.app.help.RestConfiguration#setCacheInterval(long)
128: */
129: public void setCacheInterval(long cacheInterval) {
130: this .cacheInterval = cacheInterval;
131: }
132:
133: /**
134: * @see org.sakaiproject.api.app.help.RestConfiguration#getRestUrlInDomain()
135: */
136: public String getRestUrlInDomain() {
137: if (REST_DOMAIN_URL != null) {
138: return REST_DOMAIN_URL;
139: } else {
140: return REST_DOMAIN_URL = getRestUrl() + "/"
141: + getRestDomain() + "/" + "document/"
142: + getRestDomain() + "/";
143: }
144: }
145:
146: /**
147: * @see org.sakaiproject.api.app.help.RestConfiguration#getRestCorpusUrl()
148: */
149: public String getRestCorpusUrl() {
150: if (REST_DOMAIN_URL != null) {
151: return REST_CORPUS_URL;
152: } else {
153: return REST_CORPUS_URL = getRestUrl() + "/"
154: + getRestDomain() + "/" + "documents";
155: }
156: }
157:
158: /**
159: * @see org.sakaiproject.api.app.help.RestConfiguration#getCorpusDocument()
160: */
161: public String getCorpusDocument() {
162:
163: if (LOG.isDebugEnabled()) {
164: LOG.debug("getCorpusDocument()");
165: }
166:
167: URL url = null;
168: StringBuffer sBuffer = new StringBuffer();
169: BufferedReader br = null;
170: try {
171: url = new URL(getRestCorpusUrl());
172: URLConnection urlConnection = url.openConnection();
173:
174: String basicAuthUserPass = getRestCredentials();
175: String encoding = new BASE64Encoder()
176: .encode(basicAuthUserPass.getBytes());
177:
178: urlConnection.setRequestProperty("Authorization", "Basic "
179: + encoding);
180:
181: br = new BufferedReader(new InputStreamReader(urlConnection
182: .getInputStream()), 512);
183: int readReturn = 0;
184: char[] cbuf = new char[512];
185: while ((readReturn = br.read(cbuf, 0, 512)) != -1) {
186: sBuffer.append(cbuf, 0, readReturn);
187: }
188:
189: } catch (MalformedURLException e) {
190: LOG.error("Malformed URL in REST document: "
191: + url.getPath(), e);
192: } catch (IOException e) {
193: LOG.error("Could not open connection to REST document: "
194: + url.getPath(), e);
195: } finally {
196: try {
197: if (br != null) {
198: br.close();
199: }
200: } catch (IOException e) {
201: LOG.error("error closing corpus doc", e);
202: }
203: }
204:
205: return sBuffer.toString();
206: }
207:
208: /**
209: * @see org.sakaiproject.api.app.help.RestConfiguration#getResourceNameFromCorpusDoc(java.lang.String)
210: */
211: public String getResourceNameFromCorpusDoc(String xml) {
212: try {
213: DocumentBuilderFactory dbf = DocumentBuilderFactory
214: .newInstance();
215: dbf.setNamespaceAware(true);
216: DocumentBuilder builder = dbf.newDocumentBuilder();
217: StringReader sReader = new StringReader(xml);
218: InputSource inputSource = new org.xml.sax.InputSource(
219: sReader);
220: org.w3c.dom.Document xmlDocument = builder
221: .parse(inputSource);
222: sReader.close();
223:
224: NodeList nodeList = xmlDocument.getElementsByTagName("kbq");
225:
226: int nodeListLength = nodeList.getLength();
227: for (int i = 0; i < nodeListLength; i++) {
228: Node currentNode = nodeList.item(i);
229:
230: NodeList nlChildren = currentNode.getChildNodes();
231:
232: for (int j = 0; j < nlChildren.getLength(); j++) {
233: if (nlChildren.item(j).getNodeType() == Node.TEXT_NODE) {
234: return nlChildren.item(j).getNodeValue();
235: }
236: }
237: }
238: return null;
239: } catch (Exception e) {
240: LOG.error(e.getMessage(), e);
241: }
242:
243: return null;
244: }
245:
246: }
|