001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/content/tags/sakai_2-4-1/content-impl/impl/src/java/org/sakaiproject/content/impl/BaseExtensionResourceFilter.java $
003: * $Id: BaseExtensionResourceFilter.java 22558 2007-03-13 19:16:50Z jimeng@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.content.impl;
021:
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.sakaiproject.content.api.ContentResource;
026: import org.sakaiproject.content.api.ContentResourceFilter;
027: import org.sakaiproject.content.api.ResourceToolAction;
028: import org.sakaiproject.entity.api.ResourceProperties;
029:
030: /**
031: * This class implements the typical mime type and extension filter. This will be a registered bean with the component manager that application components can extend to control the list of mime types and the list of acceptable extentions.
032: */
033: public class BaseExtensionResourceFilter implements
034: ContentResourceFilter {
035: private boolean viewAll = true;
036:
037: private List mimeTypes;
038:
039: private List acceptedExtensions;
040:
041: public boolean allowSelect(ContentResource resource) {
042: // on a new resource, it seems that getContentType is more reliable than accessing that value through properties.
043: String mimeType = resource.getContentType(); // .getProperties().getProperty(ResourceProperties.PROP_CONTENT_TYPE);
044: String[] parts = mimeType.split("/");
045: String primaryType = parts[0];
046:
047: if (!getMimeTypes().isEmpty()
048: && !getMimeTypes().contains(primaryType)
049: && !getMimeTypes().contains(mimeType)) {
050: return false;
051: }
052:
053: String filePath = resource.getUrl();
054:
055: if (getAcceptedExtensions() != null) {
056: // check extension
057: for (Iterator i = getAcceptedExtensions().iterator(); i
058: .hasNext();) {
059: if (filePath.endsWith("."
060: + i.next().toString().toLowerCase())) {
061: return true;
062: }
063: }
064:
065: return false;
066: } else {
067: return true;
068: }
069: }
070:
071: public boolean allowView(ContentResource contentResource) {
072: if (isViewAll()) {
073: return true;
074: }
075:
076: return allowSelect(contentResource);
077: }
078:
079: public List getMimeTypes() {
080: return mimeTypes;
081: }
082:
083: /**
084: * The list of mime types to allow. The passed in content resource will be tested to see if the resouce's primary mime type is included in the list (ie "text" for "text/xml") and then the whole mime type will be tested for existence in the list.
085: *
086: * @param mimeTypes
087: */
088: public void setMimeTypes(List mimeTypes) {
089: this .mimeTypes = mimeTypes;
090: }
091:
092: public boolean isViewAll() {
093: return viewAll;
094: }
095:
096: /**
097: * boolean to indicate if all resources should be viewable. If this is false, then the viewable resources will be based on the mime types and extention set in the other properties.
098: *
099: * @param viewAll
100: */
101: public void setViewAll(boolean viewAll) {
102: this .viewAll = viewAll;
103: }
104:
105: public List getAcceptedExtensions() {
106: return acceptedExtensions;
107: }
108:
109: /**
110: * List of accepted file name extensions. If this list is null, all extensions are acceptable.
111: *
112: * @param acceptedExtensions
113: */
114: public void setAcceptedExtensions(List acceptedExtensions) {
115: this .acceptedExtensions = acceptedExtensions;
116: }
117:
118: /**
119: * This method controls which "create" actions are allowed for ContentCollections.
120: * Filtering "create" actions based on mimetype and extension is not yet
121: * supported very well, so this may require tweaking over time.
122: * @param actions A collection of actions to test
123: * @return A list of actions that should be shown in the filepicker
124: * for each collection.
125: */
126: public List<ResourceToolAction> filterAllowedActions(
127: List<ResourceToolAction> actions) {
128: // suggest checking for html, text, upload, etc and treating each separately
129: return actions;
130: }
131: }
|