001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/search/tags/sakai_2-4-1/search-impl/impl/src/java/org/sakaiproject/search/component/adapter/contenthosting/DefaultContentDigester.java $
003: * $Id: DefaultContentDigester.java 22588 2007-03-14 09:53:30Z ian@caret.cam.ac.uk $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 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.search.component.adapter.contenthosting;
021:
022: import java.io.InputStream;
023: import java.io.Reader;
024: import java.io.StringReader;
025: import java.util.Properties;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.content.api.ContentResource;
030: import org.sakaiproject.entity.api.ResourceProperties;
031: import org.sakaiproject.search.api.SearchUtils;
032:
033: /**
034: * @author ieb
035: */
036: public class DefaultContentDigester implements ContentDigester {
037: private static final Log log = LogFactory
038: .getLog(DefaultContentDigester.class);
039: private int maxDigestSize = 1024 * 1024 * 20;
040: private Properties binaryTypes = null;
041:
042: public void init() {
043: try {
044: binaryTypes = new Properties();
045: InputStream pi = getClass()
046: .getResourceAsStream(
047: "/org/sakaiproject/search/component/bundle/binarytypes.config");
048: binaryTypes.load(pi);
049: pi.close();
050: } catch (Exception e) {
051: log
052: .error(
053: "Cant find binary types file /org/sakaiproject/search/component/bundle/binarytypes.config in class path",
054: e);
055: System.exit(-1);
056: }
057:
058: }
059:
060: public String getContent(ContentResource contentResource) {
061: try {
062: ResourceProperties rp = contentResource.getProperties();
063: StringBuilder sb = new StringBuilder();
064: sb
065: .append(
066: rp
067: .getProperty(ResourceProperties.PROP_DISPLAY_NAME))
068: .append(" ");
069: sb
070: .append(
071: rp
072: .getProperty(ResourceProperties.PROP_DESCRIPTION))
073: .append(" ");
074:
075: if (!isBinary(contentResource)
076: && contentResource.getContentLength() < maxDigestSize) {
077: try {
078: SearchUtils.appendCleanString(new String(
079: contentResource.getContent(), "UTF-8"), sb);
080: } catch (Exception e) {
081: }
082: }
083: return sb.toString();
084: } catch (Exception e) {
085: throw new RuntimeException("Failed to get content", e);
086: }
087: }
088:
089: /**
090: * @param contentResource
091: * @return
092: */
093: public boolean isBinary(ContentResource contentResource) {
094: String mimeType = contentResource.getContentType();
095: return "true".equals(binaryTypes.get(mimeType));
096: }
097:
098: public boolean accept(String mimeType) {
099: return true;
100: }
101:
102: /* (non-Javadoc)
103: * @see org.sakaiproject.search.component.adapter.contenthosting.ContentDigester#getContentReader(org.sakaiproject.content.api.ContentResource)
104: */
105: public Reader getContentReader(ContentResource contentResource) {
106: return new StringReader(getContent(contentResource));
107: }
108:
109: /**
110: * @return the maxDigestSize
111: */
112: public int getMaxDigestSize() {
113: return maxDigestSize;
114: }
115:
116: /**
117: * @param maxDigestSize the maxDigestSize to set
118: */
119: public void setMaxDigestSize(int maxDigestSize) {
120: this.maxDigestSize = maxDigestSize;
121: }
122:
123: }
|